4.7. Lab 04
Note that the course policy is that you should not use generative AI without authorization. If you are suspected to have used generative AI and not able to explain/reproduce your work when requested, all your related assignments throughout the semester will be regraded as 0.
Create a dotnet console app project (Create a C# Project) in your introcscs directory called Chapter04.
Use the file Program.cs to code.
Prepare your code in VS Code.
The namespace of this project is not important at this point of time but let us call it IntroCSCS.
The class name of this project is Chapter04.
When executing code, you will only use the Main() method.
You will prepare methods in the same class to be called from the Main() method.
Use a Word document to prepare your assignment.
Paste your screenshots of your code in VS Code (including namespace and class name) and the results of execution (command prompt and username is part of the execution).
Number the questions and annotate your answers, when appropriate, to show your understanding.
4.7.1. Boolean Expressions
Use
csharprepl
to test the expressions below and write the resulted output after the single-line comments at the right.You may copy and paste the code below to VS Code or a Word document to prepare the answers.
Prepare a screenshot.
int x = 5; x; // x == 5; // x == 6; // x; // x != 6; // x = 6; 6 == x; // 6 != x; // "hi" == "h" + "i"; // "HI" != "hi"; // string s = "Hello"; string t = "HELLO"; s == t; // s.ToUpper() == t; //
4.7.2. if-else
Exercise
Think of two different inputs you could give that would make the execution of the code fragment proceed differently. What would happen in each case? (Assume we have access to the class UIF that prints the prompt line, intake the value input, and turn the input value into proper data type.) (Google if you do not know what v.Length means.)
Write your answer to your Word document directly under the question number (e.g., 4.7.2.b).
Consider:
string v = UIF.PromptLine("Enter a word: "); if (v.Length > 3) { v = v + v; Console.WriteLine("Now we have " + v); } else { Console.WriteLine("We still have " + v); }
Consider:
int x = UIF.PromptInt("Enter a integer: "); Console.Write("The magnitude of " + x + " is "); if (x < 0) { Console.WriteLine(-x); } else { Console.WriteLine(x); }
4.7.3. Graduation
Prepare a method in class Chapter04 called Graduation.
Prepare the code in VS Code.
Call your method Graduation.
Make sure the code runs (
dotnet run
).Screenshot the code and paste it in your Word document.
Write a method, Graduation(), that prompts students for how many credits they have. Print whether of not they have enough credits for graduation. (At M S&T, a minimum of 120 credit hours are required for graduation.)
4.7.4. Calculate Weekly Wages
prepare a method in class Chapter04 called CalcWeeklyWages
Prepare the code in VS Code.
Call your method Graduation.
Make sure the code runs (
dotnet run
).Screenshot the code and paste it in your Word document.
Given a person’s work hours for the week and regular hourly wage, calculate the total pay for the week, taking into account overtime. Hours worked over 40 are overtime, paid at 1.5 times the normal rate. This is a natural place for a method enclosing the calculation.
The problem clearly indicates two cases: when no more than 40 hours are worked or when more than 40 hours are worked. In case more than 40 hours are worked, it is convenient to introduce a variable overtimeHours.
Your code would execute successfully and correctly when the following execution is performed in the Main() method of class Chapter04:
// Calculate Wages
Console.Write("Enter hours worked: ");
double hours = double.Parse(Console.ReadLine());
Console.Write("Enter dollars paid per hour: ");
double wage = double.Parse(Console.ReadLine());
double total = CalcWeeklyWages(hours, wage);
Console.WriteLine(
"Wages for {0} hours at ${1:F2} per hour are ${2:F2}.",
hours, wage, total);
Note that:
Two complete sample code can be found here:
When calling from Main(), do not forget to return from the method.
Suffix
F
means data typefloat
and the number followed means decimal places.
4.7.5. Congress Exercise
A person is eligible to be a US Senator who is at least 30 years old and has been a US citizen for at least 9 years. Write a program Congress() to obtain age and length of citizenship from the user and print out if a person is eligible to be a Senator or not. A person is eligible to be a US Representative who is at least 25 years old and has been a US citizen for at least 7 years. Elaborate your program Congress() so it obtains age and length of citizenship and prints whether a person is eligible to be a US Representative only, or is eligible for both offices, or is eligible for neither.
This exercise could be done by making an exhaustive treatment of all possible combinations of age and citizenship. Try to avoid that.
Caution: be sure to do exhaustive testing. It is easy to write code that is correct for some inputs, but not all.