Can someone help me with this java challenge Design a Payroll class with the fol
ID: 3919031 • Letter: C
Question
Can someone help me with this java challenge
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
$10/hr):
Chris Jacobsen, employee number 11111, made $50.00 in gross pay.
Using text forming so that the gross pay is rounded to two decimal places.
Explanation / Answer
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
class Payroll
{
//data members
String name;
int idNumber;
double rate;
int hours;
//methods
//constructor
//takes employe name and id number
Payroll(String Name,int id)
{
this.name = Name;
this.idNumber=id;
}
//accessors//
String getName()
{
return this.name;
}
int getID()
{
return this.idNumber;
}
double getRate()
{
return this.rate;
}
int getHours()
{
return this.hours;
}
//mutators//
void setName(String s)
{
this.name=s;
}
void setID(int i)
{
this.idNumber=i;
}
void setRate(double r)
{
this.rate=r;
}
void setHours(int h)
{
this.hours=h;
}
//method to find grosspay
double GrossPay()
{
return this.hours*this.rate;
}
};
public class Employe {
public static void main(String argv[])
{
String s;
double r;
int id,h;
//to read input
Scanner sc = new Scanner(System.in);
System.out.print("Enter name:");
s = sc.nextLine();
System.out.print("Enter id:");
id =sc.nextInt();
System.out.print("Enter rate:");
r = sc.nextDouble();
System.out.print("Enter hours:");
h = sc.nextInt();
//creating payroll object...
Payroll p = new Payroll(s,id);
p.setHours(h);
p.setRate(r);
System.out.print("You had an employe named "+p.getName());
System.out.print("with ID number "+p.getID());
System.out.print(", who works for "+p.getHours());
System.out.println(" at $"+p.getRate()+"/hr:");
System.out.println(p.getName()+", employee number "+p.getID()+", made $"+p.GrossPay()+" in gross pay.");
}
}
output:
run:
Enter name:Chris Jacobsen
Enter id:11111
Enter rate:10
Enter hours:5
You had an employe named Chris Jacobsenwith ID number 11111, who works for 5 at $10.0/hr:
Chris Jacobsen, employee number 11111, made $50.0 in gross pay.
BUILD SUCCESSFUL (total time: 32 seconds)