Debug Exercises

Each of the four files in the CH8 Debug zipped folder has syntax and/or logical errors. In each case, determine the problem, and fix the program.

DebugEight1

[code language=”csharp”] // GetData() method accepts order number and quantity
// that are used in the Main() method
// Price is $3.99 each
using System;
using static System.Console;
class DebugEight1
{
static void Main()
{
int orderNum, quantity;
double total;
const double PRICE_EACH = 3.99;
GetData(orderNum; quantity);
total = quantity * PRICEEACH;
WriteLine("Order #{0}. Quantity ordered = {1}",
orderNum, quantity;
WriteLine("Total is {0}", total.ToString("C"));
}
private static void GetData(out order, out amount)
{
String s1, s2;
Write("Enter order number ");
s1 = ReadLine;
Write("Enter quantity ");
s2 = ReadLine;
order = ConvertToInt32(s1);
amount = ConvertToInt32(s2);
}
}

[/code]

DebugEight2

[code language=”csharp”] // Overloaded method gives bonus points
// whether grade is a number or letter
using static System.Console;
class DebugEight2
{
static void Main()
{
int numericScore = 82;
string letterScore = B;
Write("Score was {0}. ", numericScore);
GiveBonus(ref numericScore);
WriteLine("Now it is {0}.", numericScore);
Write("Grade was ", letterScore);
GiveBonus(ref letterScore);
WriteLine("Now it is ", letterScore);
}
private static void giveBonus(int ref testScore)
{
const int BONUS = 5;
testScore = BONUS;
}
private static void GiveBonus(string ref letterScore)
{
const string BONUS = "+";
letterScore = BONUS;
}
}

[/code]

DebugEight3

[code language=”csharp”] // Program demonstrates method that can be called
// with one, two, or three arguments
// Tuition is $80 per credit
// Default district code is I.
// If student is in-district (code I), then there is no
// out-of-district fee, which is $300
// Default scholarship status is false.
// If student is on scholarship, tuition is free
using static System.Console;
class DebugEight3
{
static void Main()
{
WriteLine("Tuition is {0}", CalculateTuition(15));
WriteLine("Tuition is {0}",CalculateTuition(15, ‘O’));
WriteLine("Tuition is {0}",CalculateTuition(15, ‘O’, true));
}
double private static CalculateTuition(double credits, char code = ‘I’, bool scholarship)
{
double tuition
const double RATE = 80.00;
const double OUT_DISTRICT_FEE = 300.00;
tuition = credits * RATE;
if(code != ‘I’)
tuition += OUTDISTRICTFEE;
if(scholarship)
tuition = 0;
}
}

[/code]

DebugEight4

[code language=”csharp”] // Program demonstrates overloaded methods
// that display an int, an amount of money, or a string
// decorated with an argument character
// or a default character ‘X’
using static System.Console;
class DebugEight4
{
static void Main()
{
FancyDisplay(33);
FancyDisplay(44, "@");
FancyDisplay(55.55);
FancyDisplay(77.77, ‘*’);
FancyDisplay("hello");
FancyDisplay("goodbye", ‘#’);
}
private static void FancyDisplay(int num, char decoration = ‘X’)
{
WriteLine("{0}{1}{2} {1} {0}{1}{2}\n",
decoration, num);
}
private static void FancyDisply(double num, char decoration = ‘X’)
{
WriteLine("{0}{0}{0} {0}{0}{0}\n",
decoration, num.ToString("C"));
}
private static void FancyDisplay(word, char decoration = ‘X’)
{
WriteLine("{0}{0}{0} {1} {1}{0}{0}\n",
decoration, word);
}

}

[/code]