Question

In: Computer Science

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 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.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 


If you are satisfied with the solution, please rate the answer. 

Thanks

===========================================================================


public class Payroll {

    private String name;
    private int idNumber;
    private double rate;
    private int hours;

    //• Constructor: takes the employee's name and ID number as arguments

    public Payroll(String name, int idNumber) {
        this.name = name;
        this.idNumber = idNumber;
        rate = 0.0;
        hours = 0;
    }

    //• Accessors: allow access to all of the fields of the Payroll class


    public String getName() {
        return name;
    }

    public int getIdNumber() {
        return idNumber;
    }

    public double getRate() {
        return rate;
    }

    public int getHours() {
        return hours;
    }

    //• Mutators: let the user assign values to the fields of the Payroll class

    public void setName(String name) {
        this.name = name;
    }

    public void setIdNumber(int idNumber) {
        this.idNumber = idNumber;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    //• grossPay: returns the employee's gross pay, which is calculated as the number of
    //hours worked times the hourly pay rate.
    public double grossPay() {
        return getRate() * getHours();
    }


    // when we print object this line will get executed
    @Override
    public String toString() {
        //
        return getName() + ", employee number " + getIdNumber() + ", made $" +
                //Using text forming so that the gross
                // pay is rounded to two decimal places.
                String.format("%.2f", grossPay()) +
                " in gross pay.";
    }
}

===========================================================================

import java.util.Scanner;

public class PayrollProgram {

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter employee name: ");
        String name = scanner.nextLine();

        System.out.print("Enter employee ID number: ");
        int id = scanner.nextInt();

        System.out.print("Enter employees hourly rate: ");
        double rate = scanner.nextDouble();

        System.out.print("Enter the number of hours employee worked: ");
        int hours = scanner.nextInt();

        // create an object of Payroll class
        Payroll payroll = new Payroll(name,id);

        // set the values of the rate and hours using mutator methods
        payroll.setRate(rate);
        payroll.setHours(hours);

        // print the object, toString() method will be inbvoked.
        System.out.println(payroll);
    }
}

===========================================================================

Please appreciate our effort with an upvote : )

thank you so much.


Related Solutions

(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
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,...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT