Question

In: Computer Science

JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...

JAVA PROGRAMMING.

In this assignment, you are to create a class named Payroll.

In the class, you are to have the following data members:

name: String (5 pts)

id: String   (5 pts)

hours: int   (5 pts)

rate: double (5 pts)

private members (5 pts)

You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts)

The class definition should also handle the following exceptions:

An employee name should not be empty, otherwise an exception should be thrown. (10 pts)

An employee id should have the form LLNNNN. If that form is not received, an exception should be thrown. (10 pts)

An employee's hours should neither be negative nor greater than 84. An exception should be thrown otherwise. (10 pts)

An employee's pay rate should neither be negative nor greater than 25.00. An exception should be thrown otherwise. (10 pts)

Demonstrate this class in a program (separate class or in the same class). (5 pts)

The exception messages should be appropriate to the specific exception that has occurred. (5 pts)

Create a package payroll for the project. (5 pts)

Solutions

Expert Solution

Sample Output:

CODE TO COPY:

File: Payroll.java

// Payroll class implementation
package payroll;
public class Payroll
{
   // private members
   private String name;
   private String id;
   private int hours;
   private double rate;
  
   // no-arg constructor implementation
   public Payroll()
   {
       name = "";
       id = "";
       hours = 0;
       rate = 0.0;
   }
  
   // parameterized constructor implementation
   public Payroll(String aname, String aid, int ahours, double arate)
   {
       setName(aname);
       setId(aid);
       setHours(ahours);
       setRate(arate);
   }

   // setters
  
   // setName method implementation
   public void setName(String aname)
   {
       if(aname.equals(""))
       {
           throw new IllegalArgumentException("Employee's name should not be empty!");
       }
          
       name = aname;
   }

   // setId method implementation
   public void setId(String aid)
   {      
       if(aid.length() != 6 || !Character.isLetter(aid.charAt(0)) || !Character.isLetter(aid.charAt(1))
               || !Character.isDigit(aid.charAt(2)) || !Character.isDigit(aid.charAt(3))
               || !Character.isDigit(aid.charAt(4)) || !Character.isDigit(aid.charAt(5)))
       {
           throw new IllegalArgumentException("Employee's id should have the form LLNNNN!");
       }
      
       id = aid;
   }
  
   // setHours method implementation
   public void setHours(int ahours)
   {
       if(ahours < 0 || ahours > 84)
       {
           throw new IllegalArgumentException("Employee's hours should neither be negative nor greater than 84!");
       }
      
       hours = ahours;
   }

   // setRate method implementation
   public void setRate(double arate)
   {
       if(arate < 0 || arate > 25.00)
       {
           throw new IllegalArgumentException("Employee's pay rate should neither be negative nor greater than 25.00!");
       }
      
       rate = arate;
   }

   // getters
  
   // getName method implementation
   public String getName()
   {
       return name;
   }

   // getId method implementation
   public String getId()
   {
       return id;
   }

   // getHours method implementation
   public int getHours()
   {
       return hours;
   }

   // getRate method implementation
   public double getRate()
   {
       return rate;
   }
  
} // end of Payroll class

File: PayrollDemo.java

// PayrollDemo class implementation
package payroll;
public class PayrollDemo
{
   // start main method
   public static void main(String[] args)
   {
       // create an object for the Payroll class
       Payroll pr = new Payroll();
      
       // test the setter method
       pr.setName("Joseph Li");
       pr.setId("JL3699");
       pr.setHours(36);
       pr.setRate(18.36);
      
       // test the getter method
       System.out.println("Detailes of Payroll...");
       System.out.println("Name: " + pr.getName());
       System.out.println("ID: " + pr.getId());
       System.out.println("Hours: " + pr.getHours());
       System.out.println("Pay rate: " + pr.getRate());      
   } // end of main method
} // end of PayrollDemo class


Related Solutions

THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
In java: -Create a class named Animal
In java: -Create a class named Animal
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT