Question

By editing the code below to include composition, enums, toString; must do the following: Prompt the...

By editing the code below to include composition, enums, toString; must do the following:

  1. Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input
  2. Create a new class that validates the dates that are input (can copy date class from the book)
  3. Incorporate composition into your class with these dates
  4. Use enums to identify the employee status as fulltime (40 or more hours worked for the week) or part-time (worked less than 40 hours)
  5. Print the birthdate and hire date, name, status (fulltime or part-time), the number of hours worked and paycheck amount in the HourlyTest class using an overriding toString method that you create to format the object’s output.
  6. Create a static int variable in the Hourly class that keeps track of the number of weekly paychecks calculated. You will need to add one to this variable in one of the hourly methods or the constructor (see figure 8.12)

Requirements:

  • Ask the user to input the employee’s birth date and hire date (as month, day and year) in the HourlyTest Class.
  • Create another class that validates the dates and initializes the validated birth date and hire date instance variables (see Fig. 8.7).
  • Use the enum type to identify the status of employment as “fulltime” or parttime”
  • Create a toString method that will format and print the hire date and birth date of the employees with a complete description. The dates should be formatted in an acceptable date display format (i.e. 9/8/2015).
  • Print out the number of paychecks calculated (only print one time).
  • Include a UML of the Hourly class

Here is the grading rubric:

- Use enums to identify the employee as fulltime or parttime
- Use a static int variable that tracks the total number of paychecks calculated
- Create another class that sets dates and validate parameters
- Use composition correctly
- Edit the toString method to include status
- Correctly print out information required
- UML of Hourly class

* * * * * * ALL REFERENCED FIGURES CAN BE FOUND HERE* * * * * * *

https://www.chegg.com/homework-help/questions-and-answers/actual-question-figure-examples-another-assignment-fig-87-datejava-date-class-declaration--q39647975

*Due to the length this post became after adding these examples, Chegg would not allow that long of post. So, it had to be included separately.

* * * * * * HERE IS THE STARTING POINT / CODE TO BE MODIFIED * * * * * *

*** Hourly class ***

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;
       
    // 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)
    {
        if (hoursWorked < 1 || hoursWorked > 80)
        {
            throw new IllegalArgumentException("Data entered was out of range.");
        }       
            this.hoursWorked = hoursWorked;   
    } // end of setter
  
    public double getPayRate()
    {
        return payRate;
    } // end of getter
  
    public void setPayRate(double payRate)
    {
        if (payRate < 15 || payRate > 30)
        {
            throw new IllegalArgumentException("Data entered was out of range.");     
        }
            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
  
    @Override
    public String toString()
    {
        return String.format("\n" + firstName + " " + lastName +
                "'s weekly paycheck will be: $" + String.format("%.2f", weeklyPay));
    }

} //end of class Hourly

*** HourlyTest class ***

public class HourlyTest {

    public static void main(String[] args)
    {
        String firstName;
        String lastName;
        double hoursWorked;
        double payRate;

        Scanner input = new Scanner(System.in);

        // Create Hourly class object
        Hourly hourly = new Hourly();

        // Ask user to enter values
        System.out.print("Enter first name: ");
        firstName = input.nextLine();
        hourly.setFirstName(firstName);

        System.out.print("Enter last name: ");
        lastName = input.nextLine();
        hourly.setLastName(lastName);

        System.out.print("Enter the number of hours worked: ");
        hoursWorked = Double.parseDouble(input.nextLine());
      
        try
        {
         hourly.setHoursWorked(hoursWorked); // when values are out of range
        }
        catch (IllegalArgumentException e)
        {
            System.out.printf("Exception: %s%n%n", e.getMessage());
          
            System.out.print("Enter the number of hours worked: ");
            hoursWorked = Double.parseDouble(input.nextLine());
        }      
        hourly.setHoursWorked(hoursWorked);
      
      
        System.out.print("Enter hourly rate: $");
        payRate = Double.parseDouble(input.nextLine());
              
        try
        {
         hourly.setPayRate(payRate); // all values out of range
        }
        catch (IllegalArgumentException e)
        {
            System.out.printf("Exception: %s%n%n", e.getMessage());
          
            System.out.print("Enter hourly rate: $");
            payRate = Double.parseDouble(input.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 = input.nextInt();

            System.out.print("Enter hourly rate: $");
            payRate = input.nextDouble();
            input.nextLine();
      
            hourly.setHoursWorked(hoursWorked);
            hourly.setPayRate(payRate);
            weeklyPay = hourly.getPaycheck();
        }

        // Printing object by name only
        System.out.println(hourly);
        input.close();

    } // end of main method

} // end of class HourlyTest

Thank you in advance for your assistance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

***** THIS IS THE NEW MODIFIED CODE *****

Date.java

public class Date {
  
private int month;
private int day;
private int year;
  
private static final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  
public Date(int month, int day, int year)
{
// check if month in range
if (month <= 0 || month > 12)
{
throw new IllegalArgumentException("month (" + month + ") must be 1-12");
}
  
if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
{
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}
  
if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}
this.month = month;
this.day = day;
this.year = year;
}
  
@Override
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
}
}

Employee.java

public class Employee {

private static int count = 0; // number of Employees created
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;
private EmployeeStatus employeeStatus;
private double hoursWorked;
  
private enum EmployeeStatus
{
FullTime, PartTime;
}

public Employee(String firstName, String lastName, Date birthDate, Date hireDate, double hoursWorked) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
setHoursWorked(hoursWorked);
if(this.hoursWorked >= 40)
this.employeeStatus = EmployeeStatus.FullTime;
else if(this.hoursWorked < 40)
this.employeeStatus = EmployeeStatus.PartTime;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public static int getCount() {
return count;
}

public Date getBirthDate() {
return birthDate;
}

public Date getHireDate() {
return hireDate;
}

public String getEmployeeStatus() {
return employeeStatus.toString();
}

public double getHoursWorked() {
return hoursWorked;
}
  
public void setHoursWorked(double hoursWorked)
{
if (hoursWorked < 1 || hoursWorked > 80)
{
throw new IllegalArgumentException("Hours worked data entered was out of range.");
}   
this.hoursWorked = hoursWorked;
}

@Override
public String toString() {
return ("Name: " + getFirstName() + " " + getLastName() + ", Birth Date: " + getBirthDate()
+ ", Hire Date: " + getHireDate() + ", Hours Worked: " + getHoursWorked()
+ ", Employee Status: " + getEmployeeStatus());
}
}

Hourly.java

public class Hourly {
private Employee employee;
private double payRate;
private double weeklyPay;
private static int numWeeklyPayChecks = 0;
private int numPayChecks;
public static final int MAXREGULAR = 40;
  
public Hourly()
{
this.employee = null;
this.payRate = 0;
this.weeklyPay = 0;
this.numPayChecks = 0;
}

public Hourly(Employee employee, double payRate) {
this.employee = employee;
setPayRate(payRate);
numWeeklyPayChecks += 1;
this.numPayChecks = numWeeklyPayChecks;
}

public Employee getEmployee() {
return employee;
}

public double getPayRate() {
return payRate;
}
  
public void setPayRate(double payRate)
{
if (payRate < 15 || payRate > 30)
{
throw new IllegalArgumentException("Pay rate data entered was out of range.");   
}
this.payRate = payRate;   
}
  
public void calculatePaycheck()
{
this.weeklyPay = this.employee.getHoursWorked() * this.payRate;
  
if (this.employee.getHoursWorked() > MAXREGULAR) // if here was overtime. . .
{
double otHours = this.employee.getHoursWorked() - 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
}
}
  
@Override
public String toString()
{
calculatePaycheck();
return(this.employee.toString() + "\nWeekly PayCheck: $" + String.format("%.2f", this.weeklyPay)
+ "\nNumber of Weekly PayChecks: " + this.numPayChecks);
}
}

HourlyTest.java (Main class)

import java.util.ArrayList;
import java.util.Scanner;

public class HourlyTest {
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Hourly> hourlies = new ArrayList<>();
  
System.out.print("How many Employee details you want to enter: ");
int num = Integer.parseInt(sc.nextLine().trim());
  
for(int i = 0; i < num; i++)
{
System.out.println("EMPLOYEE " + (i + 1) + ":\n---------");
System.out.print("Enter first name: ");
String firstName = sc.nextLine().trim();
System.out.print("Enter last name: ");
String lastName = sc.nextLine().trim();
  
// Date of birth
int birthDay, birthMonth, birthYear;
Date birthDate;
while(true)
{
try
{
System.out.print("Date of birth:\n--------------\nEnter day: ");
birthDay = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter month: ");
birthMonth = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter year: ");
birthYear = Integer.parseInt(sc.nextLine().trim());

birthDate = new Date(birthMonth, birthDay, birthYear);
break;

}catch(IllegalArgumentException ile){
System.out.println(ile.getMessage() + "\n");
}
}
  
// Date of hire
System.out.println();
int hireDay, hireMonth, hireYear;
Date hireDate;
while(true)
{
try
{
System.out.print("Date of hire:\n--------------\nEnter day: ");
hireDay = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter month: ");
hireMonth = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter year: ");
hireYear = Integer.parseInt(sc.nextLine().trim());

hireDate = new Date(hireMonth, hireDay, hireYear);
break;

}catch(IllegalArgumentException ile){
System.out.println(ile.getMessage() + "\n");
}
}
  
double hoursWorked, payRate;
Hourly hourly;
while(true)
{
try
{
System.out.print("\nEnter the number of hours worked: ");
hoursWorked = Double.parseDouble(sc.nextLine().trim());
  
System.out.print("\nEnter the payrate for the employee: $");
payRate = Double.parseDouble(sc.nextLine());
  
hourly = new Hourly(new Employee(firstName, lastName, birthDate, hireDate, hoursWorked), payRate);
  
break;
}catch(IllegalArgumentException ile){
System.out.print(ile.getMessage() + "\n");
}
}
  
hourlies.add(hourly);
  
System.out.println();
}
  
  
System.out.println("\n*** ALL EMPLOYEE DETAILS ***\n----------------------------\n");
for(Hourly hourly : hourlies)
{
System.out.println(hourly + "\n");
}
sc.close();
}
}

***************************************************************** SCREENSHOT ******************************************************

Add a comment
Know the answer?
Add Answer to:
By editing the code below to include composition, enums, toString; must do the following: Prompt the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each...

    HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and...

  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • This is my code but my overtime calculation is wrong and I cant figure out how...

    This is my code but my overtime calculation is wrong and I cant figure out how to round to two decimal places import java.util.Scanner; public class HourlyWage { public static void main(String[] args) { //Variables double totalWage; double totalOvertimePay; double totalPay; String name; double overtimeHours = 0.0; double hoursWorked = 0.0; double hourlyWage = 0.0; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your name "); name = keyboard.next(); System.out.println("Please enter your hourly wage "); hourlyWage = keyboard.nextDouble(); System.out.println("How many hours...

  • Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is...

    Add an HourlyPlusCommissionEmployee class to the PayrollSystem app by subclassing an existing class. A HourlyPlusCommissionEmployee is a kind of CommissionEmployee with the following differences and specifications: HourlyPlusCommissionEmployee earns money based on 2 separate calculations: commissions are calculated by the CommissionEmployee base class hourly pay is calculated exactly the same as the HourlyEmployee class, but this is not inherited, it must be duplicated in the added class BasePlusCommissionEmployee inherits from CommissionEmployee and includes (duplicates) the details of SalariedEmployee. Use this as...

  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • In the processLineOfData method, write the code to handle case "H" of the switch statement such...

    In the processLineOfData method, write the code to handle case "H" of the switch statement such that: • An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows: Double.parseDouble(rate) Call the parsePaychecks method in this class passing the Hourly Employee object created in the previous step and the checks variable. Call the...

  • This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions...

    This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program. Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class. The regular expression you will need for a Phone number is: "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$" and for an SSN: "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$" This exercise is meant to be done with the video listed in the...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT