Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Starting Out With Java: Early Objects 5th Ed. 11. Exception Project This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee

Starting Out With Java: Early Objects 5th Ed.

11. Exception Project This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee 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 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 throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. Write a test program that demonstrates how each of these exception conditions work.

I am attaching a solution to Programming Challenge 1 from Chapter 9 for modification according to the specifications for #11

/**

* Chapter 9

* Programming Challenge 1: Employee and ProductionWorker Classes

* The Employee class stores data about an employee.

*/

public class Employee

{

private String name; // Name

private String employeeNumber; // Employee number

private String hireDate; // Hire date

/**

* Constructor

* Assigns argument values to the fields

*/

public Employee(String n, String num, String date)

{

name = n;

setEmployeeNumber(num);

hireDate = date;

}

/**

* Constructor

* Initializes fields to empty strings

*/

public Employee()

{

name = "";

employeeNumber = "";

hireDate = "";

}

/**

* setName method

*/

public void setName(String n)

{

name = n;

}

/**

* setEmployeeNumber method

*/

public void setEmployeeNumber(String e)

{

if (isValidEmpNum(e))

employeeNumber = e;

else

employeeNumber = "";

}

/**

* setHireDate method

*/

public void setHireDate(String h)

{

hireDate = h;

}

/**

* getName method

*/

public String getName()

{

return name;

}

/**

* getEmployeeNumber method

*/

public String getEmployeeNumber()

{

return employeeNumber;

}

/**

* getHireDate method

*/

public String getHireDate()

{

return hireDate;

}

/**

* isValidEmpNum method

* This private method returns true if the arg

* e references a valid ID number. Othewise it

* returns false.

*/

private boolean isValidEmpNum(String e)

{

boolean status = true;

if (e.length() != 5)

status = false;

else

{

if ((!Character.isDigit(e.charAt(0))) ||

(!Character.isDigit(e.charAt(1))) ||

(!Character.isDigit(e.charAt(2))) ||

(e.charAt(3) != '-') ||

(!Character.isLetter(e.charAt(4))))

status = false;

}

return status;

}

/**

* toString method

*/

public String toString()

{

String str = "Name: " + name + " Employee Number: ";

if (employeeNumber == "")

str += "INVALID EMPLOYEE NUMBER";

else

str += employeeNumber;

str += (" Hire Date: " + hireDate);

return str;

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.text.DecimalFormat;

/**

* Chapter 9

* Programming Challenge 1: Employee and ProductionWorker Classes

* The ProductionWorker class stores data about an employee

* that is a production worker.

*/

public class ProductionWorker extends Employee

{

// Constants for day and night shifts

public static final int DAY_SHIFT = 1;

public static final int NIGHT_SHIFT = 2;

private int shift; // Worker's shift

private double payRate; // Worker's pay rate

/**

* Constructor

* Assigns argument values to the fields

*/

public ProductionWorker(String n, String num, String date,

int sh, double rate)

{

super(n, num, date);

shift = sh;

payRate = rate;

}

/**

* Constructor

* Initializes fields to default values

*/

public ProductionWorker()

{

super();

shift = DAY_SHIFT;

payRate = 0.0;

}

/**

* setShift method

*/

public void setShift(int s)

{

shift = s;

}

/**

* setPayRate method

*/

public void setPayRate(double p)

{

payRate = p;

}

/**

* getShift method

*/

public int getShift()

{

return shift;

}

/**

* getPayRate method

*/

public double getPayRate()

{

return payRate;

}

/**

* toString method

*/

public String toString()

{

DecimalFormat dollar = new DecimalFormat("#,##0.00");

String str = super.toString();

str += " Shift: ";

if (shift == DAY_SHIFT)

str += "Day";

else if (shift == NIGHT_SHIFT)

str += "Night";

else

str += "INVALID SHIFT NUMBER";

str += (" Hourly Pay Rate: $" +

dollar.format(payRate));

return str;

}

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* Chapter 9
* Programming Challenge 1: Employee and ProductionWorker Classes
* This program demonstrates the ProductionWorker class.
*/
public class WorkerDemo
{
public static void main(String[] args)
{
String shift;
// Create a ProductionWorker object and pass the initialization
// data to the constructor.
ProductionWorker pw =
new ProductionWorker("John Smith", "123-A", "11-15-2005",
ProductionWorker.DAY_SHIFT, 16.50);
// Display the data.
System.out.println("Here's the first production worker.");
System.out.println(pw);
// Create another ProductionWorker object and use the
// set methods.
ProductionWorker pw2 = new ProductionWorker();
pw2.setName("Joan Jones");
pw2.setEmployeeNumber("222-L");
pw2.setHireDate("12-12-2005");
pw2.setShift(ProductionWorker.NIGHT_SHIFT);
pw2.setPayRate(18.50);
// Display the data.
System.out.println(" Here's the second production worker.");
System.out.println(pw2);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions