CS 1063-001,   Exam 2, Fall 2005
Answers

Note: Answers are in red.

  1. Suppose a variable temperature is supposed to be in the range from 96.0 to 100.6 inclusive. Write a short piece of Java code (not an entire class or even a method) that will print the message "Temperature OK" in case the temperature is in range and will print "You are sick" in case the temperature is out of range.

  2. Write a short piece of Java code that will print the message "s1 and s2 are equal" in case the string s1 is equal (all characters the same) to the string s2. [You must not use the == operator directly on the two strings.]

  3. Write a short piece of Java code that will start with an integer variable minutes and will give values to two variables: hours, the number of hours in the given minutes, and mins, the number of minutes left over after the hours are left off. Thus mins must be between 0 and 59 inclusive. [For example, if minutes is 217, then hours should be 3 and mins should be 37.]

Example class: the Employee class.

  1. Complete the code for a Employee class, which is based on one of the laboratories and on a simple example in the review.

    The Employee class has private data members (called instance variables in the lectures) giving an employee's

    There are "get" methods to return each of these fields.

    There is one constructor, which takes a name, hours worked, and hourly rate as formal parameters.

    There is a method calculateWages, which uses the values of myHoursWorked and myHourlyRate to calculate a value for myWages. The amount of wages is just the hours times the rate, except that you must pay time and a half for all hours worked over 40. There are several different ways that this method can be written and used, so that there is no single correct answer (although of course there is a correct numerical answer).

    There should also be a toString method.

    You will be filling in the blanks below to complete the Java implementation of this class:

  2. Fill in code in the test program below to do the following:
    1. Instantiate an Employee object with variable name e1 and with employee name Susan Smith, hours worked 50.0, and with an hourly rate of 10.0.

    2. Give code to print the wages of the instance e1. (You will have to arrange it so that these wages are calculated by the calculateWages method. There are several correct ways to do this.) Instead of what I show below, you could have made calculateWages public and just call it directly from main.

    3. Similarly, create another instance of the Employee class with variable name e2 and with employee name Ted Bundy, hours worked 55.0, and with an hourly rate of 7.50.

    4. Give code that will use the toString method to print the information about this second instance.

    Other approaches to parts 4 and 5: There are many other ways to structure an answer to the above two parts, but a different approach has to fit together as a whole.

    For example, you might make the calculateWages function just give a value to myWages, and return nothing:

    You could make this public and call it from the main function. So to print the wages (part 5. ii.), you would do something like:


Points for each problem: 1-15, 2-15, 3-15, 4-30, 5-25.