Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 . Identify the errors in each of the following pieces of C# program code. Writ

ID: 3762994 • Letter: 1

Question

1. Identify the errors in each of the following pieces of C# program code. Write into your MS Word document the corrected code. Note that there may be more than one error in each piece of code.
a)
if (age >= 65);
Console.WriteLine("Age greater than or equal to 65");
else
Console.WriteLine("Age is less than 65 )";
b)
int x = 1, total;
while ( x <= 10 )
{
total += x;
++x;
}
c)
int x=1;
while (x <= 100)
total += x;
++x;

d)
int y = 0;
while (y > 0) {
Console.WriteLine(y);
++y;
}

.................................................................................................................................................

2. A C# program is given below. Use MS Word to trace the values of each variable throughout the life of the program and to record the output. To do this:
Create a table in your MS Word document
Use variable names for the headings above each column and use the heading “Console Output” for the last column
Each new row should represent the next step in the program: that is, the line of code being processed. When complete you should have more than 11 rows in your table
In each step (row), only record changing values or new console outputs (that is, you shouldn’t write a value in every cell in the table)

................................................................................................................................................................

3. Write a C# application that uses a loop to display the following table of values.

N 10*N 100*N 1000*N

1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000

..........................................................................................................

4. Develop the following C# application and Pseudocode:
Your program should use Console’s ReadLine method to accept the number of hours worked by 3 employees last week and their hourly rate. Your program should then display all 3 employees gross pays, formatted as currency. Any hours they worked above 40 hours should be paid at time and a half. After processing 3 employee details the program should ask the user to press “y” to try again or any other key to exit.


a) In your Word doc, create an algorithm using pseudocode and 3 levels of top-down refinement to model this problem – that is top, 1st, and 2nd levels of pseudocode.
Use the reserved words provided to you in the “Pseudocode Reserved Words” document.
[This program is so simple that it hardly requires pseudocode at all, let alone 3 levels. Remember that the purpose of this task is to test your understanding of pseudocode, not so much to help you with writing the C# code]

b) Convert the 2nd level pseudocode into a C# application.

If you write all your code in a single class you can get a maximum of 10 marks. If you create 2 classes as described here you can get 20 marks.
Your Main method should contain the Console.ReadLine() code that asks for hours worked and rate of pay. It should also contain code that creates 3 Employee objects from a second class called Employee.
This Employee class should:
a. Contain private data members
b. Contain a Constructor that sets an employee’s rate and hours to 0
c. Contain Rate and Hours “Properties” that set and get the private data members. The set method should validate user input
d. Contain a setEmployee() method that sets both the rate and hours worked
e. Contain a displayEmployee() method that displays an employee’s details exactly as shown on the following page’s test data
Your program should not crash if invalid input (such as a String) is entered into the console and it shouldn’t treat negative values as acceptable. See the test data on the following page to see how invalid input should be treated.
Your code should make use of the each Property’s set methods for the first 2 employees and the setEmployee() method for the 3rd.

You will get a bonus of up to 5 marks if you implement an array of 3 employee objects and use for loops to process the inputs and displays for that array. If you do this, then use the default constructor and the Properties for each employee (not the setEmployee() method, which you still need to create).


c) When you run the program, enter the data for 3 employees using different data to the test data used below here, and include a screenshot(s) of the console in your Word doc.


You can test and debug your application with this employee data:
Employee 1
Enter hourly rate: 7.25
Enter hours worked: 50
Employee 2
Enter hourly rate: 8.50
Enter hours worked: 0
Employee 3
Enter hourly rate: 0
Enter hours worked: 30
Pay for employee 1 is $362.50
Pay for employee 2 is $0
Pay for employee 3 is $0
Press “y” to repeat or any other key to exit...y
Employee 1
Enter hourly rate: 6.00
Enter hours worked: 50
Employee 2
Enter hourly rate: 8.50
Enter hours worked: abcde
Employee 3
Enter hourly rate: -10.00
Enter hours worked: 30
Pay for employee 1 is $362.50
Pay for employee 2 is --error--
Pay for employee 3 is --error--
Press “y” to repeat or any other key to exit...n

Explanation / Answer

1.

a.The semicolon at the end of the if condition should be removed. The closing double quote of the second Console.WriteLine should be inside the closing parenthesis.

b.The variable total should be initialized to zero

c.The two statements should be enclosed in curly braces to properly group them into the body of the while; otherwise the loop will be an infinite loop

d.The ++ operator should be changed to --; otherwise the loop will be an infinite loop. The closing curly brace for the while loop is missing

2.1 4 9 16 25 36 49 64 81 100

Total is 385

3.

using System;
public class Table
{
public static void Main( string[] args )
{
int n = 1;

Console.WriteLine( "N 10*N 100*N 1000*N " );

while ( n <= 5 )
{
Console.WriteLine( "{0} {1} {2} {3}",
n, ( 10 * n ), ( 100 * n ), ( 1000 * n ) );
++n;
} // end while loop
} // end Main
} // end class Table

4.determine the gross pay for three employees

First refinement: initialize the variables input the hours worked for the current worker input the hourly rate of the current worker calculate and display worker’s gross pay process the next worker of the three

Second refinement: initialize count to 1 while the employee count is less than or equal to 3

prompt the user for the hours worked for the current employee

input the hours worked for the current employee

prompt the user for the employee’s hourly rate

input the employee’s hourly rate

if the hours input is less than or equal to 40

calculate gross pay by multiplying hours worked by hourly rate

else

calculate gross pay by multiplying 40 by the hourly rate,

then adding the product of the number of hours worked above 40

and 1.5 times the hourly rate

display the employee’s gross pay

increment the employee count

CODE


// Application calculates wages.
using System;

public class Wages
{
// calculates wages for 3 employees
public void CalculateWages()
{
decimal pay; // gross pay
int hours; // hours worked
decimal rate; // hourly rate
int count = 1; // number of employees

// repeat calculation for 3 employees
while ( count <= 3 )
{
// prompt user and read the hourly rate
Console.Write( "Enter hourly rate: " );
rate = Convert.ToDecimal( Console.ReadLine() );

// prompt user and read hours worked
Console.Write( "Enter hours worked: " );
hours = Convert.ToInt32( Console.ReadLine() );

// calculate wages
if ( hours <= 40 ) // straight time
pay = hours * rate;
else // with overtime
pay = ( 40 * rate ) + ( hours - 40 ) * ( rate * 1.5M );

// display the pay for the current employee
Console.WriteLine( "Pay for employee is {0:C} ", pay );

++count;
} // end while loop
} // end Main

}


// Test application for class Wages
public class WagesTest
{
public static void Main( string[] args )
{
Wages application = new Wages();
application.CalculateWages();
} // end Main
} // end class WagesTest