Question

Given attached you will find a file from Programming Challenge 5 of chapter 6 with required...

Given attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions:

An empty string is given for the employee’s name.

An invalid value is given to the employee’s ID number. If you implemented this field as a string, then an empty string could be invalid. If you implemented this field as a numeric variable, then a negative number or zero would be invalid.

An invalid number is given for the number of hours worked. This would be a negative number or a number greater than 84.

An invalid number is given for the hourly pay rate. This would be a negative number or a number greater than 25.

1.Write a program in Java using JOptionPane

2. Modify the Payroll class so that it throws the appropriate exception when any of these errors occur. Demonstrate the exception classes in a program.

3. Make sure the program should run both internally and externally.

/**
   Payroll class
   Chapter 6, Programming Challenge 5
*/

public class Payroll
{
   private String name; // Employee name
   private int idNumber; // ID number
   private double payRate; // Hourly pay rate
   private double hoursWorked; // Number of hours worked

   /**
The constructor initializes an object with the
employee's name and ID number.
@param n The employee's name.
@param i The employee's ID number.
   */

   public Payroll(String n, int i)
   {
name = n;
idNumber = i;
   }

   /**
The setName sets the employee's name.
@param n The employee's name.
   */

   public void setName(String n)
   {
name = n;
   }

   /**
The setIdNumber sets the employee's ID number.
@param i The employee's ID number.
   */
  
   public void setIdNumber(int i)
   {
idNumber = i;
   }

   /**
The setPayRate sets the employee's pay rate.
@param p The employee's pay rate.
   */
  
   public void setPayRate(double p)
   {
payRate = p;
   }

   /**
The setHoursWorked sets the number of hours worked.
@param h The number of hours worked.
   */

   public void setHoursWorked(double h)
   {
hoursWorked = h;
   }

   /**
The getName returns the employee's name.
@return The employee's name.
   */

   public String getName()
   {
return name;
   }

   /**
The getIdNumber returns the employee's ID number.
@return The employee's ID number.
   */
  
   public int getIdNumber()
   {
return idNumber;
   }

   /**
The getPayRate returns the employee's pay rate.
@return The employee's pay rate.
   */

   public double getPayRate()
   {
return payRate;
   }

   /**
The getHoursWorked returns the hours worked by the
employee.
@return The hours worked.
   */


   public double getHoursWorked()
   {
return hoursWorked;
   }

   /**
The getGrossPay returns the employee's gross pay.
@return The employee's gross pay.
   */

   public double getGrossPay()
   {
return hoursWorked * payRate;
   }
}

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

here is your files : -------->>>>>>>

StringException.java : -------->>>>>>

public class StringException extends Exception{
public StringException(String msg){
  super(msg);
}
}

NumberException: -------------->>>>>>>>>>>

public class NumberException extends Exception{
public NumberException(String msg){
  super(msg);
}
}

Payroll.java : -------->>>>>>>>>

public class Payroll
{
private String name;// Employee name
private int idNumber;// ID number
private double payRate; // Hourly pay rate
private double hoursWorked; // Number of hours worked
/**
The constructor initializes an object with the
employee's name and ID number.
@param n The employee's name.
@param i The employee's ID number.
*/
public Payroll(String n, int i)throws StringException,NumberException{
  setName(n);
  setIdNumber(i);
}
/**
The setName sets the employee's name.
@param n The employee's name.
*/

public void setName(String n)throws StringException{
  if(n.equals("")){
   throw new StringException("Not A valid Name");
  }
  name = n;
}

/**
The setIdNumber sets the employee's ID number.
@param i The employee's ID number.
*/

public void setIdNumber(int i)throws NumberException{
  if(i <= 0){
   throw new NumberException("The Value For ID is either Negative or Zero");
  }
  idNumber = i;
}

/**
The setPayRate sets the employee's pay rate.
@param p The employee's pay rate.
*/

public void setPayRate(double p)throws NumberException{
  if(p < 0 || p > 25){
   throw new NumberException("The value for PayRate is either Negative or larger than 25");
  }
  payRate = p;
}

/**
The setHoursWorked sets the number of hours worked.
@param h The number of hours worked.
*/

public void setHoursWorked(double h)throws NumberException{
  if(h < 0 || h > 84){
   throw new NumberException("The value for Hours Worked is either Negative or larger than 84");
  }
  hoursWorked = h;
}

/**
The getName returns the employee's name.
@return The employee's name.
*/

public String getName(){
  return name;
}

/**
The getIdNumber returns the employee's ID number.
@return The employee's ID number.
*/

public int getIdNumber(){
  return idNumber;
}

/**
The getPayRate returns the employee's pay rate.
@return The employee's pay rate.
*/

public double getPayRate(){
  return payRate;
}

/**
The getHoursWorked returns the hours worked by the
employee.
@return The hours worked.
*/

public double getHoursWorked(){
  return hoursWorked;
}

/**
The getGrossPay returns the employee's gross pay.
@return The employee's gross pay.
*/

public double getGrossPay(){
  return hoursWorked * payRate;
}
}

Add a comment
Know the answer?
Add Answer to:
Given attached you will find a file from Programming Challenge 5 of chapter 6 with required...
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
  • java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll...

    java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: • An empty string is given for the employee’s name. • An invalid value is given for the employee’s ID number. If you implemented this field as a string, then an empty string would be invalid. If you implemented this field as a numeric variable, then a negative number or...

  • HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private...

    HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private int empNumber;    private double hoursWorked;    private double payRate;       private static int hospitalEmployeeCount = 0;    //-----------------------------------------------------------------    // Sets up this hospital employee with default information.    //-----------------------------------------------------------------    public HospitalEmployee()    { empName = "Chris Smith"; empNumber = 9999; hoursWorked = 0; payRate =0;               hospitalEmployeeCount++;    }       //overloaded constructor.    public HospitalEmployee(String eName,...

  • 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...

  • 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: 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 Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • I would like help and there are three different classes in the coding.. 14.11 Prog11 Inheritance(PayrollOvertime)...

    I would like help and there are three different classes in the coding.. 14.11 Prog11 Inheritance(PayrollOvertime) Create three files to submit: • Payroll class.java -Base class definition • PayrollOvertime.jave - Derived class definition • ClientClass.java - contains main() method Implement the two user define classes with the following specifications: Payroll class (the base class) (4 pts) • 5 data fields(protected) o String name - Initialized in default constructor to "John Doe" o int ID - Initialized in default constructor to...

  • Design a Payroll class with the following fields:

    IN JAVADesign a Payroll class with the following fields:• name: a String containing the employee's name• idNumber: an int representing the employee's ID number• rate: a double containing the employee's hourly pay rate• hours: an int representing the number of hours this employee has workedThe class should also have the following methods:• Constructor: takes the employee's name and ID number as arguments• Accessors: allow access to all of the fields of the Payroll class• Mutators: let the user assign values...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • C++ Program The Ward Bus Manufacturing Company has recently hired you to help them convert their...

    C++ Program The Ward Bus Manufacturing Company has recently hired you to help them convert their manual payroll system to a computer-based system. Write a program to produce a 1-week payroll report for only one employee to serve as a prototype (model) for the administration to review. Input for the system will be the employee’s 4-digit ID number, the employee’s name, hours worked that week, and the employee’s hourly pay rate. Output should consist of the employee’s ID number, the...

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