Question

In: Computer Science

JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...

JAVA

(1) Create two files to submit:

  • Payroll.java - Class definition
  • PayrollClient.java - Contains main() method

Build the Payroll class with the following specifications:

  • 4 private fields
    • String name - Initialized in default constructor to "John Doe"
    • int ID - Initialized in default constructor to 9999
    • doulbe payRate - Initialized in default constructor to 15.0
    • doulbe hrWorked - Initialized in default constructor to 40
  • 2 constructors (public)
    • Default constructor
    • A constructor that accepts the employee’s name, ID, and pay rate as arguments. These values should be assigned to the object’s name, ID, and payRate fields.
  • 7 public member methods (mutators, accessors, and other) (8 pts)
    • getName()
    • getID()
    • setPayRate() & getPayRate()
    • setHrWorked() & getHrWorked()
    • A method named grossPay that returns employee’s gross pay (=hrWorked * payRate)

(2) In main(), create two Payroll objects: one use the info inputted by the user, and the other use the default constructor, and print the info for each employee (2 pts)

Ex:

Enter the name, ID, pay rate, and hours worked of an employee:
Linda Smith
1234
20.5
35


Employee Records: 
Name            ID           Pay Rate      Hrs Worked     Gross Pay 
Linda Smith     1234         $20.50/hr     35.0          $717.50
John Doe        9999         $15.00/hr     40.0          $600.00

Solutions

Expert Solution

Payroll.java and PayrollClient.java are created in Payroll Package inside the Payroll Package. I have attached the Structure of this project below:

JAVA CODE:

Payroll.java

package Payroll;
// Payroll Class
public class Payroll {
        // private field name of type String
        private String name;
        // private field ID of type int
        private int ID;
        // private field payRate of type double
        private double payRate;
        // private field hrWorked of type double
        private double hrWorked;
        // Default constructor
        public Payroll() {
                // String name - Initialized in default constructor to "John Doe"
                this.name = "John Doe";
                // int ID - Initialized in default constructor to 9999
                this.ID = 9999;
                // double payRate - Initialized in default constructor to 15.0
                this.payRate = 15.0;
                // double hrWorked - Initialized in default constructor to 40
                this.hrWorked = 40;
        }
        // A constructor that accepts the employee’s name, ID, and pay rate as arguments.
        public Payroll(String name, int ID, double payRate, double hrWorked) {
                // The values passed in the argument are assigned to the fields
                this.name = name;
                this.ID = ID;
                this.payRate = payRate;
                this.hrWorked = hrWorked;
        }
        // getName accessor method
        public String getName() {
                return name;
        }
        // getID accessor method
        public int getID() {
                return ID;
        }
        // getPayRate accessor method
        public double getPayRate() {
                return payRate;
        }
        // setPayRate mutator method
        public void setPayRate(double payRate) {
                this.payRate = payRate;
        }
        // getHrWorked accessor method
        public double getHrWorked() {
                return hrWorked;
        }
        // setHrWorked mutator method
        public void setHrWorked(double hrWorked) {
                this.hrWorked = hrWorked;
        }
        // grossPay returns employee’s gross pay
        public double grossPay() {
                return this.hrWorked * this.payRate;
        }
}

PayrollClient.java

package Payroll;
import java.text.DecimalFormat;
import java.util.Scanner;
// PayrollClient Class
public class PayrollClient {
        // Has main method
        public static void main(String[] args) {
                // The decimal number in the print output of gross pay should have two numbers after the decimal point
                DecimalFormat df = new DecimalFormat("#.00");
                // A Scanner object is created
                Scanner sc = new Scanner(System.in);
                // Displays prompt
                System.out.println("Enter the name, ID, pay rate, and hours worked of an employee:");
                // Gets name, ID, pay rate and number of hours worked from the user
                String name = sc.nextLine();
                int ID = Integer.parseInt(sc.nextLine());
                double payRate = Double.parseDouble((sc.nextLine()));
                double hrWorked = Double.parseDouble((sc.nextLine()));
                // Two objects of Payroll class are created
                // p1 is created using user input
                Payroll p1 = new Payroll(name, ID, payRate, hrWorked);
                // p2 object uses default constructor
                Payroll p2 = new Payroll();
                // Output values are printed
                System.out.println("Employee Records:");
                System.out.println("Name         ID       Pay Rate    Hrs Worked    Gross Pay");
                System.out.println(p1.getName() + "  " + p1.getID() + "     $" + p1.getPayRate() + "/hr    " + p1.getHrWorked() 
                + "          $" + df.format(p1.grossPay()));
                System.out.println(p2.getName() + "     " + p2.getID() + "     $" + p2.getPayRate() + "/hr    " + p2.getHrWorked() 
                + "          $" + df.format(p1.grossPay()));
        }
}

OUTPUT:


Related Solutions

In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
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...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT