November 6, 2020
You can download the exams for Section A and for Section B. Note that they were different, but of equivalent difficulty.
Below, find possible solutions and general comments.
// The next 4 lines were not needed in your answer.
char iniLName;
decimal pricePaid;
iniLName = 'K'; // Dummy value, change it to test.
pricePaid = 420M; // Dummy value, change it to test.
if(pricePaid > 600M){Console.WriteLine("You are in first class");}
else if (iniLName < 'H'){Console.WriteLine("You are in second class, left-side aisle.");}
else {Console.WriteLine("You are in second class, right-side aisle.");}You may (rightfully) think “Hey, but I didn’t know characters have an order”, and would be right. But you could also work out a solution using switch or if, like:
if(pricePaid > 600M){Console.WriteLine("You are in first class");}
else if (iniLName == 'A' || iniLName == 'B' ||
iniLName == 'C' || iniLName == 'D' ||
iniLName == 'E' || iniLName == 'F' ||
iniLName == 'G' || iniLName == 'H'){
Console.WriteLine("You are in second class, left-side aisle.");
}
else {Console.WriteLine("You are in second class, right-side aisle.");}Using format specifiers and getting the conditions right were more important that getting the loop correct for that exercise.
You can use the code below to test your answers:
// The three values below are dummy.
// Change them to test your answers.
string citizenship = "US";
decimal income = 200M;
int age = 20;
switch (citizenship) {
case("US"):
case ("CA"):
if (income > 100)
if (age < 21) Console.WriteLine("Go to office A.");
else if (age < 60) Console.WriteLine("Go to office B.");
else Console.WriteLine("Go to office C.");
else
Console.WriteLine("Go to office D.");
break;
case ("DE"):
if (income > 200 && age > 18) Console.WriteLine("Go to office E.");
else Console.WriteLine("Go to office F.");
break;
case ("FR"):
if (age <= 18 || income <= 10) Console.WriteLine("Go to office G.");
else if (income > 200) Console.WriteLine("Go to office H.");
break;
default:
if (age > 21) Console.WriteLine("Go to office I.");
else Console.WriteLine("Go to office J.");
break;
}The two solutions are fairly similar. Short code was priviledged below, also because they show an original use of do while loops: the multiplication actually takes place before asking for a value, so that the multiplication will be performed only if the user actually gave a value to multiply. Of course, for the first run of the loop, we multiply by the “dummy” value 1.
int product = 1;
int answer = 1;
do{
product *= answer;
Console.WriteLine("Enter a number, or 0 (zero) to quit.");
answer = int.Parse(Console.ReadLine());
}while(answer != 0);
Console.WriteLine("The product of the numbers you entered is " + product + ".");For both sections, refer to the solution given in lab 12. The version given to Section B was actually a simplification of that exercise.
The questions were identical for both sections:
Scale, int.public Square(){} is a constructor for that class that takes no argument.ToString() method is implicitely called when we try to display an object using Console.WriteLine.| Square |
|---|
| - dimension : int |
| + GetDimension() : int |
| + SetDimension(dimensionP : int) : void |