Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA from control structures through objects 6th edition tony gaddis java progra

ID: 3836758 • Letter: J

Question

JAVA from control structures through objects 6th edition tony gaddis java programming Question exception project this assighment assumes you have completed programming challenges 1 of chapter 10 (emplyee and productionWorker Classes). Modify the employee and ProductionWorker classes so they throw exceptions when the following errors occur:

****The employee class should throw an exception named InvalidEmployyNumber when it recieves an invalid employee number

****the ProductionWorker class should throw an exception named InvalidShift when it recieves an invalid shift

****The productionWorker class should throw an exception named InvalidPayRate when it recieves a negative number for the hourly pay rate.

Part 2 Section D (this part is worth 60 points) 12/13/16 Page 1 Review the code below, which is a programming challenge from one of the chapters (Employee and Production Worker Classes). Modify the Employee and errors occur: ProductionWorker classes so they throw exceptions when the following The Employee class should throw an exception named InvalidEmployeeNumber when it receives an invalid employee number. The class should throw an exception named Invalid Shift when receives an invalid shift. The Production Worker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. You do not have to compile the code, but it should be syntactically correct. (Note: I do not want pseudo-code, but a Java implementation.) Please put your hand-est You (clean sheet(sh with and include the exam isroem your answer is. If there will need to label this so Lknow exactly code below direstlya on the exam, you can also include your answer in the import java text. Decimal Format ProductionWorker cians public class production Worker extends Employee Constant for the day and night shifts final int DAY SHIFT 2 public int NIGHT SHIFT static The employee' pay rate int shift Rate name private ith a constructor initializes an obs eet pay a employee The employee' s name s number date num The employee s The aram

Explanation / Answer

import java.io.*;
import java.text.DecimalFormat;
//class Employee definition
class Employee
{
   //Instance variables
private String employeeName;
private String employeeNumber;
private String hireDate;

   //Default Constructor
    Employee()
   {
       employeeName = "";
       employeeNumber = "";
       hireDate = "";
   }//End of constructor

    //Parameterized constructor
   public Employee(String eN, String nbr, String hd)
   {
       employeeName = eN;
       employeeNumber = nbr;
       hireDate = hd;
   }//End of constructor

   //Method to set name
   public void setName(String en)
   {
       employeeName = en;
   }//End of method

   //Method to set hire date
   public void setHireDate(String hd)
   {
       hireDate = hd;
   }//End of method

   //Method to return hire date
   public String getHireDate()
   {
       return hireDate;
   }//End of method

   //Method to return name
   public String getName()
   {
       return employeeName;
   }//End of method

   //Method to return employee number
   public String getEmployeeNumber()
   {
       return employeeNumber;
   }//End of method

   //Overrides to string method
   public String toString()
   {
       String res = "Name: " + employeeName + "nEmployee Number: ";

       if(employeeNumber == "")
       {
           res += "EMPLOYEE NUMBER IS INVALID";
       }
       else
       {
           res += employeeNumber;
       }
       return res += ("nHire Date: " + hireDate);
   }//End of method
}//End of class

//Define class InvalidShift derived from Exception
class InvalidShift extends Exception
{

   //Default constructor
   public InvalidShift()
   {
       //Calls base class constructor
   super("Error: Invalid Shift occured");
   }//End of constructor
}//End of class

//Define class InvalidPayRate derived from Exception
class InvalidPayRate extends Exception
{
   //Default constructor
   public InvalidPayRate()
   {
       //Calls base class constructor
       super("Error: Invalid pay rate discovered.");
   }//End of constructor
}//End of class

//Define class InvalidEmployeeNumber derived from Exception
class InvalidEmployeeNumber extends Exception
{
   //Default constructor
   public InvalidEmployeeNumber()
   {
       //Calls base class constructor
       super("Error: Invalid employee number occured");
   }//End of constructor
}//End of class

//Define class ProductionWorker derived from Employee
class ProductionWorker extends Employee
{
   //Instance variables
   public static final int dayShift = 1;
   public static final int nightShift = 2;
   private int shift;
   private double hourlyPayRate;
  
   //Default Constructor
   public ProductionWorker()
   {
       //Calls base class constructor
       super();
       shift = dayShift;
       hourlyPayRate = 0.0;
   }//End of constructor

   //Parameterized Constructor
   public ProductionWorker(String en, String num, String hd, int sft, double pr)
   {
       //Calls base class constructor
       super(en, num, hd);
       shift = sft;
       hourlyPayRate = pr;
   }//End of constructor

   //Method to returns shift
   public int getShift()
   {
       return shift;
   }//End of method

   //Method to returns pay rate
   public double getPayRate()
   {
       return hourlyPayRate;
   }//End of method

   //Overrides toString() method
   public String toString()
   {
       //Decimal format
       DecimalFormat currency = new DecimalFormat("#,###.00");
       //Calls base class toString() method
       String res = super.toString();
       res += "nShift: ";
       //checks shift
       if(shift == dayShift)
       {
           res += " The Day Shift";
       }
       else if(shift == nightShift)
       {
           res += " The Night Shift";
       }
       else
       {
           res += " SHIFT NUMBER IS INVALID";
       }
       return res += (" The Hourly Pay Rate is $" + currency.format(hourlyPayRate));
   }//End of method
}//End of class

//Driver class
public class ExceptionDemo
{
   //Main method
   public static void main(String[] args)
   {
       //Creates an object
       ProductionWorker pw = new ProductionWorker();
       System.out.println(" Production Worker - 1");
       pw = createWorker("Pyari", "1234", "12/2/2014", 2, 32.50);      
       System.out.println(pw);
      
       System.out.println(" Production Worker - 2");
       pw = createWorker("Mohan", "12", "12/3/2013", 2, 32.50);
       System.out.println(pw);
      
       System.out.println(" Production Worker - 3");
       pw = createWorker("Sahu", "1111", "04/2/2004", 4, 32.50);
       System.out.println(pw);
      
       System.out.println(" Production Worker - 4");
       pw = createWorker("Ram", "2222", "10/6/2017", 2, -32.50);
       System.out.println(pw);
   }//End of method

   //Assigns member to the object and returns the object after validating data
   public static ProductionWorker createWorker(String name, String empNum, String date, int shiftNum, double rate)
   {
       //check for invalid employee number
       try
       {
           //Checks for exactly 4 character employee code
           if(empNum.length() < 4 || empNum.length() > 4)
               //Throw exception for invalid employee number  
               throw new InvalidEmployeeNumber();
       }//End of try
       catch(InvalidEmployeeNumber e)
       {
           //Display Error Message
           System.out.println(e.getMessage());
       }//End of catch

       //check for invalid shift number
       try
       {
           //Checks either one or two
           if(shiftNum != 1 && shiftNum != 2)
               //Throw exception for invalid shift number
               throw new InvalidShift();
       }//End of try
       catch(InvalidShift e)
       {
           //Display Error Message
           System.out.println(e.getMessage());
       }//End of catch
         
       //check for invalid pay rate
       try
       {
           //If rate is negative
           if(rate < 0)
               //Throw exception for invalid pay rate
       throw new InvalidPayRate();
       }//End of try
       catch(InvalidPayRate e)
       {
           //Display Error Message
           System.out.println(e.getMessage());
       }//End of catch
       ProductionWorker pW = new ProductionWorker(name, empNum, date, shiftNum, rate);
       return pW; //return production worker
   }//End of method
}//End of class

Outout:


Production Worker - 1
Name: PyarinEmployee Number: 1234nHire Date: 12/2/2014nShift: The Night Shift
The Hourly Pay Rate is $32.50

Production Worker - 2
Error: Invalid employee number occured

Name: MohannEmployee Number: 12nHire Date: 12/3/2013nShift: The Night Shift
The Hourly Pay Rate is $32.50

Production Worker - 3
Error: Invalid Shift occured

Name: SahunEmployee Number: 1111nHire Date: 04/2/2004nShift: SHIFT NUMBER IS INVALID
The Hourly Pay Rate is $32.50

Production Worker - 4
Error: Invalid pay rate discovered.

Name: RamnEmployee Number: 2222nHire Date: 10/6/2017nShift: The Night Shift
The Hourly Pay Rate is $-32.50