Note: Answers are in red.
double temperature = 98.6;
// put the rest of the code below
if (temperature >= 96.0 && temperature <= 100.6)
System.out.println("Temperature OK");
else
System.out.println("You are sick");
// another solution:
if (temperature < 96.0 || temperature > 100.6)
System.out.println("You are sick");
else
System.out.println("Temperature OK");
// another solution:
if (temperature < 96.0)
System.out.println("You are sick");
else if (temperature > 100.6)
System.out.println("You are sick");
else
System.out.println("Temperature OK");
String s1 = "Joe";
String s2 = "Moe";
// put the rest of the code below
if (s1.equals(s2) == true) System.out.println("s1 and s2 are equal");
// another solution:
if (s1.equals(s2)) System.out.println("s1 and s2 are equal");
int minutes = 217; // put the rest of the code below int hours = minutes / 60; int mins = minutes % 60; // another solution: int hours = minutes / 60; mins = minutes - hours*60; System.out.println(minutes + "minutes is same as" + hours + " hours and " + mins + " minutes");
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:
public class Employee {
// fill in the data members (also known as instance variables) below
private String myName;
private double myHoursWorked;
private double myHourlyRate;
private double myWages;
// fill in the constructor below
public Employee(String name, double hoursWorked, double hourlyRate) {
myName = name;
myHoursWorked = hoursWorked;
myHourlyRate = hourlyRate;
myWages = calculateWages(); // don't have to call this in the constructor
}
// fill in the accessor methods below
public String getName() { return myName; }
public double getHoursWorked() { return myHoursWorked; }
public double getHourlyRate() { return myHourlyRate; }
public double getWages() { return myWages; }
// fill in the calculateWages method below
private double calculateWages() { // don't have to make this private
double wages;
wages = myHoursWorked*myHourlyRate;
// now add in extra half rate for hours > 40
if (myHoursWorked > 40.0)
wages = wages + (myHoursWorked - 40.0)*myHourlyRate*0.5;
return wages;
}
// fill in the toString method below
public String toString() { // details of toString can vary a lot
return "Name: " + myName +
"\n Hours Worked: " + myHoursWorked +
"\n Hourly Rate: " + myHourlyRate +
"\n Wages: " + myWages;
}
}
public class EmployeeTest {
public static void main (String[] args) {
Employee e1 = new Employee("Susan Smith", 50.0, 10.0);
System.out.println("Susan Smith wages: " + e1.getWages());
Employee e2 = new Employee("Ted Bundy", 55.0, 7.50);
System.out.println(e2);
}
}
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:
public void calculateWages() { // make this public
myWages = myHoursWorked*myHourlyRate;
// now add in extra half rate for hours > 40
if (myHoursWorked > 40.0)
myWages = myWages + (myHoursWorked - 40.0)*myHourlyRate*0.5;
}
Employee e1 = new Employee("Susan Smith", 50.0, 10.0);
e1.calculateWages(); // so that myWages gets a value
System.out.println("Susan Smith wages: " + e1.getWages());