The language is Java. Thank you. Design a Payroll class with the following field
ID: 3850209 • Letter: T
Question
The language is Java. Thank you.
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 class Mutators: let the user assign values to the fields of the Payroll class grossPay: returns the employee's gross pay, which is calculated as the number of hours worked times the hourly pay rate. Write another program that demonstrates the class by creating a Payroll object, then asking the user to enter the data for an employee in the order: name, ID number, rate, hours. The program should then print out a statement in the following format (for example, if you had an employee named Chris Jacobsen with ID number 11111, who works for 5 hours at exist10/hr): Chris Jacobsen, employee number 11111, made exist50.00 in gross pay. Using text forming so that the gross pay is rounded to two decimal places. Enter middot employee's middot name: Hermione middot Granger Enter middot employee's middot ID middot number: 107548 Enter middot hourly middot rate: 100.5 Enter middot number of hours middot worked: 45 Hermione middot Granger, middot employee middot number middot 107548, made middot exist4522.50 middot in middot gross middot pay.Explanation / Answer
public class Payroll {
private String name;
private int idNumber;
private double rate;
private int hours;
/**
* @param name
* @param idNumber
*/
public Payroll(String name, int idNumber) {
this.name = name;
this.idNumber = idNumber;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the idNumber
*/
public int getIdNumber() {
return idNumber;
}
/**
* @return the rate
*/
public double getRate() {
return rate;
}
/**
* @return the hours
*/
public int getHours() {
return hours;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param idNumber
* the idNumber to set
*/
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
/**
* @param rate
* the rate to set
*/
public void setRate(double rate) {
this.rate = rate;
}
/**
* @param hours
* the hours to set
*/
public void setHours(int hours) {
this.hours = hours;
}
public double grossPay() {
return getRate() * getHours();
}
}
import java.util.Scanner;
public class TestPayroll {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
// declaration
String name;
int idNumber;
double rate;
int hours;
scanner = new Scanner(System.in);
// prompt get input
System.out.print("Enter employeees name:");
name = scanner.next();
System.out.print("Enter employeees id:");
idNumber = scanner.nextInt();
System.out.print("Enter hourly rate:");
rate = scanner.nextDouble();
System.out.print("Enter number of hours worked:");
hours = scanner.nextInt();
// create payroll object
Payroll payroll = new Payroll(name, idNumber);
payroll.setHours(hours);
payroll.setRate(rate);
System.out.printf("%s, employee number %d,made %.2f in gross pay ",
payroll.getName(), payroll.getIdNumber(),
payroll.grossPay());
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter employeees name:Rajesh
Enter employeees id:107548
Enter hourly rate:100.5
Enter number of hours worked:45
Rajesh, employee number 107548,made 4522.50 in gross pay
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.