C++ Lab Exercises – Week 3



Computer Science 221

Lab Exercises

Week 8

Objective: Learn to implement two-way and multi-way decision-making using Java if-else statements.

Preparation: Read sections 5.1-5.5 of Wu. For each lab exercise,

• write out a flow chart showing the decision(s) to be made

• from your flow chart, write pseudo code if-else statements that make the decision(s). Do not write complete Java programs, just focus on the if-else statements.

Make two copies of your flow charts and pseudo code. Turn in one copy at the beginning of lab and keep the other copy to use during lab. This written preparatory work counts for half of your lab grade. Late work is not accepted.

Exercises: Demonstrate your solution to exercise 3 to the instructor, lab assistant, or tutor by Thursday.

1. The ACME corporation pays its employees hourly wages. Workers are paid time-and-a-half for any work in excess of 40 hours per week. Complete the following Java program so that it correctly prints the user's earnings for the week. You have to implement the method getEarning() for the Wage class, which is passed the hours and the wage and returns earnings. Use one if-else statement.

public static void main(String[] args)

{

double wage;

double hours;

double earnings;

Scanner sc = new Scanner(System.in) ;

System.out.print("Enter your hours for the week: ");

hours = sc.nextDouble();

writer.print("Enter your hourly wage: ");

wage = sc.nextDouble() ;

//put your message here

earnings = Wage.getEarning(hours, wage) ;

System.out.print("Your paycheck is: $");

System.out.println(earnings);

}//main

2. Normal human body temperature is between 97.0 and 99.5 degrees. Write a Java program that reads the user's temperature and prints either “healthy” or “sick”. For that problem you have to create a Health class, which has a class method isHealth(). It is passed the user’s temperature and returns true if the user is in the norm, or false otherwise. Use a single if-else statement. Use a Boolean expression with a logical operator (either && or ||).

3. Write a Java program which reads a year (integer) from the user and decides whether that year is a leap year. It invokes a class method isLeap() of the class Leap, which is passed the integer year and returns true if the year is leap. A year is a leap year (and so contains a February 29) if it is divisible by 4. But if the year is also divisible by 100 then it is not a leap year, unless it is divisible by 400. This means that years such as 1992, 1996 are leap years because they are divisible by 4 and are not affected by the rest of the rule which applies to century years such as 1900 and 2000. Century years are not leap years except where they are a multiple of 400. Hence, the years 1700, 1800 and 1900 were not leap years and did not contain a February 29. But the year 2000 was a leap year, the first such century leap year since 1600.

Challenge: If you finish the exercises early, try to solve this problem. Write a Java program that reads a date from the user in numeric form. For example, February 17, 2003 would be entered as the three integers 2, 17, and 2003. Your program must then determine if the date is a “valid” date. Use the following information to determine if the date is valid: January, March, May, July, August, October, and December all have 31 days. April, June, September, and November all have 30 days. February has 28 days in a non-leap year and 29 days in a leap year. Echo the input and print either “valid date” or “invalid date” as output.

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

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download