Question

In: Computer Science

make a Pay class that had fields for a single employee's name, ID number, hourly pay,...

make a Pay class that had fields for a single employee's name, ID number, hourly pay, number of hours worked. use appropriate getter and setter methods, and a constructor accepting the employees name, ID number, hourly pay, and number of hours worked as arguments. The Pay class should also have a method that returns the employee's gross pay (gross pay = hourly pay* number of hours worked).
In addition include a toString method that prints to screen the employee name, ID number, hourly pay, number of hours worked, and gross pay.

Then write a program that demos the Pay class by creating a Pay object, and having user enter the data for employee.
use input valididation for what user enters. the demo program should display the name, ID number, hourly pay, number of hours worked and gross pay of the Pay object.

use java programming language

Solutions

Expert Solution

Explanation:I have written the classes Pay and PayDemo class which also has the main method to showsthe output of the program, please find the images attached with the answer.I have checked input validation using scanner please comment if you need any other kind of validaton.Please upvote if you liked my answer and comment if you need any modification or explanation.

//Pay class

public class Pay {
   private String employeeName;
   private int idNumber;
   private float hourlyPay;
   private float hoursWorked;

   public Pay(String employeeName, int idNumber, float hourlyPay,
           float hoursWorked) {
       super();
       this.employeeName = employeeName;
       this.idNumber = idNumber;
       this.hourlyPay = hourlyPay;
       this.hoursWorked = hoursWorked;
   }
   public String getEmployeeName() {
       return employeeName;
   }
   public void setEmployeeName(String employeeName) {
       this.employeeName = employeeName;
   }
   public int getIdNumber() {
       return idNumber;
   }
   public void setIdNumber(int idNumber) {
       this.idNumber = idNumber;
   }
   public float getHourlyPay() {
       return hourlyPay;
   }
   public void setHourlyPay(float hourlyPay) {
       this.hourlyPay = hourlyPay;
   }
   public float getHoursWorked() {
       return hoursWorked;
   }
   public void setHoursWorked(float hoursWorked) {
       this.hoursWorked = hoursWorked;
   }

   public float employeeGrossPay() {
       return hourlyPay * hoursWorked;
   }

   @Override
   public String toString() {
       return "employeeName=" + employeeName + ", idNumber=" + idNumber
               + ", hourlyPay=" + hourlyPay + ", hoursWorked=" + hoursWorked;
   }

}

//PayDemo class

import java.util.Scanner;

public class PayDemo {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);
       String employeeName = "";
       int idNumber = -1;
       float hourlyPay = 0;
       float hoursWorked = 0;
       System.out.print("Enter name of the employee:");
       if (input.hasNext())
           employeeName = input.nextLine();
       else
           System.out.println("Invalid input for employee name");
       System.out.print("Enter ID number of the employee:");
       if (input.hasNextInt())
           idNumber = input.nextInt();
       else
           System.out.println("Invalid input for id number");
       System.out.print("Enter hourly pay of the employee:");
       if (input.hasNextFloat())
           hourlyPay = input.nextFloat();
       else
           System.out.println("Invalid input for hourly Pay");
       System.out.print("Enter the number of hours worked of employee:");
       if (input.hasNextFloat())
           hoursWorked = input.nextFloat();
       else
           System.out.println("Invalid input for hours Worked");
       Pay employeePay = new Pay(employeeName, idNumber, hourlyPay,
               hoursWorked);
       System.out.println(employeePay.toString());
       System.out.println("Gross pay: " + employeePay.employeeGrossPay());
       input.close();

   }

}

Output:


Related Solutions

Create an Automobile class for a dealership. Include fields for an ID number, make, model, color,...
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design 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 worked The 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...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either (I)D number or (S)ales value. ----------------------- Salesperson.java public class Salesperson {    private int id;    private double sales;    Salesperson(int idNum, double amt)    {       id = idNum;       sales = amt;    }    public int getId()...
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Write a class named ContactEntry that has fields for a person’s name, phone number and email...
Write a class named ContactEntry that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five ContactEntry objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. I repeat, NO-ARG constructors....
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT