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
finish the java programming Create a new class named MyCharacterListTools that provides the API shown below....
finish the java programming Create a new class named MyCharacterListTools that provides the API shown below. This class does not need a constructor nor will any user-defined constructor be called by the provided code in MainClassQ1.java. ◦ MyLinkedList createCharacterList(String str) – This method is to return a MyLinkedList whose data items are the characters of str. All of the characters must appear in the list and in the same order as they are given in str. ◦ void removeNonLetters(MyLinkedList list)...
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...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
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...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference. …………………………...…….. …………………………...……. Instructions LAB5 Instructions Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT