Debug Exercises – Fixed

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 = 0, quantity = 0;
double total;
const double PRICE_EACH = 3.99;
GetData(ref orderNum, ref quantity);
total = quantity * PRICE_EACH;
WriteLine("Order #{0}. Quantity ordered = {1}",
orderNum, quantity);
WriteLine("Total is {0}", total.ToString("c"));
}
private static void GetData(ref int order, ref int amount)
{
String s1, s2;
Write("Enter order number: ");
s1 = ReadLine();
Write("Enter quantity: ");
s2 = ReadLine();
order = Convert.ToInt32(s1);
amount = Convert.ToInt32(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 {0}, ", letterScore);
GiveBonus(ref letterScore);
WriteLine("Now it is {0}", letterScore);
}
private static void GiveBonus(ref int testScore)
{
const int BONUS = 5;
testScore = testScore + BONUS;
}
private static void GiveBonus(ref string letterScore)
{
const string BONUS = "+";
letterScore = 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:C}", CalculateTuition(15));
WriteLine("Tuition is {0:C}", CalculateTuition(15, ‘O’));
WriteLine("Tuition is {0:C}", CalculateTuition(15, ‘O’, true));
}
private static double CalculateTuition(double credits, char code = ‘I’, bool scholarship = false)
{
double tuition;
const double RATE = 80.00;
const double OUT_DISTRICT_FEE = 300.00;
tuition = credits * RATE;
if (code != ‘I’)
tuition += OUT_DISTRICT_FEE;
if (scholarship)
tuition = 0;
return tuition;
}
}
[/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} {1} {0}{1}\n",
decoration, num);
}
private static void FancyDisplay(double num, char decoration = ‘X’)
{
WriteLine("{0}{0}{0} {1} {0}{0}{0}\n",
decoration, num.ToString("C"));
}
private static void FancyDisplay(string word, char decoration = ‘X’)
{
WriteLine("{0}{0}{0} {1} {0}{0}{0}\n",
decoration, word);
}

}
[/code]