In: Computer Science
Lesson is about Input validation, throwing exceptions, overriding toString
Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method).
Here is the Case Study Code to Model After:
public class Time1 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// set a new time value using universal time; throw
an
// exception if the hour, minute or second is
invalid
public void setTime(int hour, int minute, int second)
{
// validate hour, minute and
second
if (hour < 0 || hour >= 24 ||
minute < 0 || minute >= 60 ||
second < 0 ||
second >= 60) {
throw new
IllegalArgumentException(
"hour, minute and/or second was out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}
// convert to String in universal-time format
(HH:MM:SS)
public String toUniversalString() {
return
String.format("%02d:%02d:%02d", hour, minute, second);
}
// convert to String in standard-time format
(H:MM:SS AM or PM)
public String toString() {
return String.format("%d:%02d:%02d
%s",
((hour == 0 ||
hour == 12) ? 12 : hour % 12),
minute, second,
(hour < 12 ? "AM" : "PM"));
}
}
* * * * Here are is the code I'm supposed to modify from our last assignment * * * *
*** HOURLY.java***
public class Hourly
{
// Setting private Instance variables
private String firstName;
private String lastName;
private double hoursWorked;
private double payRate;
private double weeklyPay;
private static final int MAXREGULAR = 40;
Scanner scan = new Scanner(System.in);
// constructor
public Hourly()
{
// set data field to
zero
this.firstName =
"";
this.lastName =
"";
this.hoursWorked =
0.0;
this.payRate =
0.0;
} // end of constructor
// Set and get methods
public String getFirstName()
{
return firstName;
} // end of getter
public void setFirstName(String firstName)
{
this.firstName =
firstName;
} // end of setter
public String getLastName()
{
return lastName;
} // end of getter
public void setLastName(String lastName)
{
this.lastName =
lastName;
} // end of setter
public double getHoursWorked()
{
return
hoursWorked;
} // end of getter
public void setHoursWorked(double
hoursWorked)
{
while(hoursWorked < 1
|| hoursWorked > 80)
{
System.out.println("\nYou must enter a number between 1 and 80.
"
+ "Please try again!\n");
System.out.print("Enter the number of hours worked: ");
hoursWorked = Double.parseDouble(scan.nextLine());
}
this.hoursWorked =
hoursWorked;
} // end of setter
public double getPayRate()
{
return payRate;
} // end of getter
public void setPayRate(double payRate)
{
while(payRate < 15 ||
payRate > 30)
{
System.out.println("\nYou must enter an hourly rate between "
+ "$15 - $30. Please try again!\n");
System.out.print("Enter hourly rate: $");
payRate =
Double.parseDouble(scan.nextLine());
}
this.payRate =
payRate;
} // end of setter
// Method to return weeklyPay
public double getPaycheck()
{
this.weeklyPay =
this.hoursWorked * this.payRate;
if (hoursWorked >
MAXREGULAR) // if here was overtime. . .
{
double otHours = hoursWorked - MAXREGULAR; // calculate overtime
hours
double otPay = otHours * (payRate * 0.5); // pay "half" the rate
for overtime hours worked
weeklyPay = weeklyPay + otPay; // add overtime pay to regular
pay
}
return weeklyPay;
} // end of method getPaycheck
} //end of class Hourly
***HOURLYTEST.java***
public class HourlyTest
{
public static void main(String[] args)
{
String
firstName;
String lastName;
double
hoursWorked;
double payRate;
Scanner scan = new Scanner(System.in);
// Create Hourly
class object
Hourly hourly = new
Hourly();
// Ask user to enter
values
System.out.print("Enter
first name: ");
firstName =
scan.nextLine();
hourly.setFirstName(firstName);
System.out.print("Enter last name: ");
lastName =
scan.nextLine();
hourly.setLastName(lastName);
System.out.print("Enter the number of hours worked: ");
hoursWorked =
Double.parseDouble(scan.nextLine());
hourly.setHoursWorked(hoursWorked);
System.out.print("Enter hourly rate: $");
payRate =
Double.parseDouble(scan.nextLine());
hourly.setPayRate(payRate);
// Call method to get
weeklyPay
double weeklyPay =
hourly.getPaycheck();
// if enter uses
invalid data
if (weeklyPay ==
0.0)
{
System.out.print("Enter the number of hours worked: ");
hoursWorked = scan.nextInt();
System.out.print("Enter hourly rate: $");
payRate = scan.nextDouble();
scan.nextLine();
hourly.setHoursWorked(hoursWorked);
hourly.setPayRate(payRate);
weeklyPay = hourly.getPaycheck();
}
// Print weekly
weeklyPay value
System.out.println("\nThank you! \n" + firstName + " " + lastName
+
"'s weekly paycheck will be: $" + String.format("%.2f",
weeklyPay));
scan.close();
} // end of main method
} // end of class HourlyTest
***Grading Rubric***
- Hourly Class validates hourly pay and hours worked correctly
in a set method
- Hourly Class correctly throws an IllegalArgumentException if
either is not valid
- Exception is caught (try/catch) and message is output to the
user
- toString method is overridden in Hourly Class
- Main method prints object by its name only
- UML of the Hourly class
Hourly.java (modified)
public class Hourly
{
// Setting private Instance variables
private String firstName;
private String lastName;
private double hoursWorked;
private double payRate;
private double weeklyPay;
private static final int MAXREGULAR = 40;
Scanner scan = new Scanner(System.in);
// constructor
public Hourly()
{
// set data field to zero
this.firstName = "";
this.lastName = "";
this.hoursWorked = 0.0;
this.payRate = 0.0;
} // end of constructor
// Set and get methods
public String getFirstName()
{
return firstName;
} // end of getter
public void setFirstName(String firstName)
{
this.firstName = firstName;
} // end of setter
public String getLastName()
{
return lastName;
} // end of getter
public void setLastName(String lastName)
{
this.lastName = lastName;
} // end of setter
public double getHoursWorked()
{
return hoursWorked;
} // end of getter
public void setHoursWorked(double hoursWorked)
{
try{
if(hoursWorked < 1 || hoursWorked > 80)
{
System.out.println("\nYou must enter a number between 1 and 80. "
+ "Please try again!\n");
System.out.print("Enter the number of hours worked: ");
hoursWorked = Double.parseDouble(scan.nextLine());
}
else{
throw new IllegalArgumentException("Hourse worked must be positive and/or less than 80");
}
this.hoursWorked = hoursWorked;
}
catch(Exception e){
System.out.println(e.getMessage());
}
} // end of setter
public double getPayRate()
{
return payRate;
} // end of getter
public void setPayRate(double payRate)
{
try{
if(payRate < 15 || payRate > 30)
{
System.out.println("\nYou must enter an hourly rate between "
+ "$15 - $30. Please try again!\n");
System.out.print("Enter hourly rate: $");
payRate = Double.parseDouble(scan.nextLine());
}
else{
throw new IllegalArgumentException("Pay rate must be between 15 to 30 dollars a week");
}
this.payRate = payRate;
} // end of setter
catch(Exception e){
System.out.println(e.getMessage());
}
}
// Method to return weeklyPay
public double getPaycheck()
{
try{
this.weeklyPay = this.hoursWorked * this.payRate;
if (hoursWorked > MAXREGULAR) // if here was overtime. . .
{
double otHours = hoursWorked - MAXREGULAR; // calculate overtime hours
double otPay = otHours * (payRate * 0.5); // pay "half" the rate for overtime hours worked
weeklyPay = weeklyPay + otPay; // add overtime pay to regular pay
}
return weeklyPay;
}
catch(Exception e){
System.out.println(e.getMessage());
}
} // end of method getPaycheck
} //end of class Hourly
Time1.java
public class Time1 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// set a new time value using universal time; throw an
// exception if the hour, minute or second is invalid
public void setTime(int hour, int minute, int second) {
// validate hour, minute and second
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 ||
second < 0 || second >= 60) {
throw new IllegalArgumentException(
"hour, minute and/or second was out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
// convert to String in standard-time format (H:MM:SS AM or PM)
public String toString() {
String str = String.format("Hour %d:%02d \n Minute: %02d \n Second: %s",
((hour == 0 || hour == 12) ? 12 : hour % 12),minute, second, (hour < 12 ? "AM" : "PM"));
return str;
}
}