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 (:ref:`create-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. 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. .. code-block:: 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; // .. Simple ``if`` 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.) .. - Write your answer to your Word document directly under the question .. number (e.g., 4.7.3.a). .. a. Consider:: .. string v = UIF.PromptLine("Enter a word: "); .. if (v.Length > 3) { .. v = v + v; .. } .. Console.WriteLine("Now we have " + v); .. #. Consider:: .. int x = UIF.PromptInt("Enter a integer: "); .. Console.Write("The magnitude of " + x + " is "); .. if (x < 0) { .. x = -x; .. } .. Console.WriteLine(x); ``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). a. 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); } 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.) 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: - https://github.com/mstbit/introcs-csharp-examples/blob/master/wages1/wages1.cs - https://github.com/mstbit/introcs-csharp-examples/blob/master/wages2/wages2.cs #. When calling from Main(), do not forget to return from the method. #. Suffix ``F`` means data type ``float`` and the number followed means decimal places. 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. .. Implication Exercise .. ---------------------- .. We have introduced C# Boolean operators for AND, OR, and NOT. .. There are other Boolean operators important in logic, .. that are not directly given as a C# operator. .. One example is "implies", also expressed .. in a logical if-then statement: If I am expecting rain, then I am carrying an .. umbrella. Otherwise put: "I am expecting rain" *implies* .. "I am carrying an umbrella". The first part is a Boolean expression called the .. *hypothesis*, and the second part is called the *conclusion*. In general, when .. A and B are Boolean expressions, "A implies B" is also a Boolean expression. .. Just as the truth of a compound Boolean expression like "A and B" depends on the .. truth value of the two parts, so with *implies*: .. If you are using good logic, and you start with a true assertion, .. you should only be able to conclude something else true, so it is true that .. "true implies true". If you start with garbage you can use that false statement .. in a logical argument and end up with something either false or true: .. "false implies false" and "false implies true" are both true. The only thing .. that should not work is to start with something true and conclude .. something false. If that were the case, logical arguments would be useless, .. so "true implies false" is false. There is no C# operator for "implies", but .. you can check all four cases of Boolean values for A and B to see that .. "A implies B" is true exactly when "not A or B" is true. We can .. express this in C# as ``!A || B``. .. So here is a silly little exercise illustrating both implication and using .. the C# Boolean operators: Ask the user whether "I am expecting rain" is true. .. (We have the UI function Agree.) Then check with the user whether .. "I am carrying an umbrella." Then conclude and print out .. whether the implication "If I am expecting rain, then I am carrying an .. umbrella." is true or not in this situation.