Question

In: Computer Science

Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...

Java

Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in.

*NOTE*- I have the code for the rest of the program all I need is what the instructions indicate. Thank you.

HourlyEmployee11 source code:

public class HourlyEmployee11 extends Employee11 implements PartTimeEmployee11 {
  
//creating scanner object to read input
Scanner sc = new Scanner(System.in);
  
//declaring instance variables
private int hrsWorked, otHrs, empStatus;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
  
//constructor to set instance variables
public HourlyEmployee11(String firstName, String lastName, int hrsWorked,
int empStatus, int otHrs, double hrlyRate, double regularPay, double otPay) {
  
//calling super class constructor
super (firstName, lastName);
  
if (empStatus == 1) {
  
System.out.println("Enter the employee's hourly rate of pay:");
hrlyRate = sc.nextDouble();
  
System.out.println("Enter the number of hours worked by the employee:");
hrsWorked = sc.nextInt();
  
if (hrsWorked > 40) {
otHrs = hrsWorked - 40;
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
}
else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
}
else if (empStatus == 2) {
otPay = 0;
wklyPaycheck = HRLY_WKLYPAY;
}
  
this.hrsWorked = hrsWorked;
this.otHrs = otHrs;
this.empStatus = empStatus;
this.hrlyRate = hrlyRate;
this.regularPay = regularPay;
this.otPay = otPay;
WklyPaycheck();
counter++;
}

//method that calculates the weekly paycheck
public void WklyPaycheck() {
  
if (empStatus == 1) {
wklyPaycheck = regularPay + otPay;
}
}
  
//getter(method) that retrieves the "wklyPaycheck"
public double getWklyPaycheck() {
return wklyPaycheck;
}
  
public void payPrint() {
System.out.printf("%s The employee is an Hourly Employee and its weekly "
+ "paycheck amount is $%.2f.\n", super.toString(), wklyPaycheck);
}
}

HourlyExc source code:

package employeetest11;
import java.util.InputMismatchException;

public class HourlyExc extends Exception {
//modify
}

Solutions

Expert Solution

// HourlyExc.java

import java.util.InputMismatchException;

public class HourlyExc extends Exception {
  
   public HourlyExc()
   {
       super();
   }
  
   public HourlyExc(String message)
   {
       super(message);
   }
}

// end of HourlyExc.java

//HourlyEmployee11.java

public class HourlyEmployee11 extends Employee11 implements PartTimeEmployee11 {
  
   //creating scanner object to read input
   Scanner sc = new Scanner(System.in);
  
   //declaring instance variables
   private int hrsWorked, otHrs, empStatus;
   private double hrlyRate, regularPay, otPay, wklyPaycheck;
  
   //constructor to set instance variables
   public HourlyEmployee11(String firstName, String lastName, int hrsWorked,
   int empStatus, int otHrs, double hrlyRate, double regularPay, double otPay) {
  
       //calling super class constructor
       super (firstName, lastName);
      
       if (empStatus == 1) {
      
           // loop that continues until user enters a valid hourly wage
           while(true)
           {
               System.out.println("Enter the employee's hourly rate of pay:");
               try{
                   // input the hourly wage
                   hrlyRate = sc.nextDouble();
                  
                   // if hourly wage is invalid, throw HourlyExc exception
                   if(hrlyRate < 15 || hrlyRate > 25)
                       throw new HourlyExc("Invalid hourly wage");
                   else // valid hourly wage exit the loop
                       break;
               }catch(HourlyExc e)
               {
                   sc.nextLine(); // discard the entire line
                   System.out.println("Hourly wage for empStatus 1 must be between $15.00/hr. and $25.00/hr.");
               }
           }
          
           System.out.println("Enter the number of hours worked by the employee:");
           hrsWorked = sc.nextInt();
          
           if (hrsWorked > 40) {
               otHrs = hrsWorked - 40;
               otPay = ((hrlyRate * 1.5) * otHrs);
               regularPay = hrlyRate * 40;
           }
           else {
               otPay = 0;
               regularPay = hrlyRate * hrsWorked;
           }
       }
       else if (empStatus == 2) {
           otPay = 0;
           wklyPaycheck = HRLY_WKLYPAY;
       }
      
       this.hrsWorked = hrsWorked;
       this.otHrs = otHrs;
       this.empStatus = empStatus;
       this.hrlyRate = hrlyRate;
       this.regularPay = regularPay;
       this.otPay = otPay;
       WklyPaycheck();
       counter++;
   }

   //method that calculates the weekly paycheck
   public void WklyPaycheck() {
  
       if (empStatus == 1) {
           wklyPaycheck = regularPay + otPay;
       }
   }
  
   //getter(method) that retrieves the "wklyPaycheck"
   public double getWklyPaycheck() {
       return wklyPaycheck;
   }
  
   public void payPrint() {
       System.out.printf("%s The employee is an Hourly Employee and its weekly "
       + "paycheck amount is $%.2f.\n", super.toString(), wklyPaycheck);
   }
}

// end of HourlyEmployee11.java


Related Solutions

Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
1 .RuntimeException class is a subclass of the Exception class. Fill in the blanks to complete...
1 .RuntimeException class is a subclass of the Exception class. Fill in the blanks to complete the declaration of the RuntimeException class. _____________   ________________    _________________ . . . 3. RuntimeException is a subclass of Exception. The constructor RuntimeException(String message) calls the parent class's constructor with the message argument. Which of the following makes the call correctly? this(message); super(message); super.Exception(message); Exception(message); 4. RuntimeException has overloaded constructors: the 1-argument constructor RuntimeException(String message) and the 2-argument constructor RuntimeException(String message, Throwable cause). The 2-argument...
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT