Question

In: Computer Science

Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...

Java

Write a Payroll class as demonstrated in the following UML diagram.

Member variables of the class are:

  • NUM_EMPLOYEES: a constant integer variable to hold the number of employees.

  • employeeId. An array of integers to hold employee identification numbers.

  • hours. An array of integers to hold the number of hours worked by each employee

  • payRate. An array of doubles to hold each employee’s hourly pay rate

  • wages. An array of seven doubles to hold each employee’s gross wages

    The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose identification number is stored in element 0 of the employeeId array. That same employee’s pay rate should be stored in element 0 of the payRate array.

    In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee’s identification number as an argument and returns the gross pay for that employee.

    Demonstrate the class in a complete program that asks for the number of employees first. Then for each employee, it should ask for the employee ID. Then, displaying each employee number, it should ask the user to enter that employee’s hours and pay rate. It should then display each employee’s identification number and gross wages.

    Input Validation: Do not accept negative values for hours or numbers less than 6.00 for pay rate. A sample interaction is shown below:

Enter number of employee: 3
Enter the ID of Employee # 1: 123456
Enter the hours worked by employee number 123456: 20 Enter the hourly pay rate for employee number 123456: 4 ERROR: Enter 6.00 or greater for pay rate: 7
Enter the ID of Employee # 2: 654321
Enter the hours worked by employee number 654321: Enter the hourly pay rate for employee number 654321: Enter the ID of Employee # 3: 102030
Enter the hours worked by employee number 102030: Enter the hourly pay rate for employee number 102030:

PAYROLL DATA ============ Employee ID: 123456 Gross pay: $140.00 Employee ID: 654321 Gross pay: $100.00 Employee ID: 102030 Gross pay: $300.00

Solutions

Expert Solution

code for your program is provided below. code is thoroughly explained in code comments. Output screenshot as well as screenshot of code in IDE is provided in the last for your better uderstanding. If you need any further clarification please feel free to ask in comments. I would really appreciate if you would let me know if the explanation provided is upto your satisfaction.

#####################################################################

CODE

import java.util.Scanner;   //for taking input from user

public class Payroll 
{
        //private data members
        private final int NUM_EMPLOYEES;   //to store maximum employees
        private int[] employeeId;       //to store ids for employeees
        private int[] hours;    //to store number of hours
        private double[] payRate;       //to store pay rate for each employee
        private double[] wages; //to store total wages
        
        //constructor
        Payroll(int numberOfEmployees)
        {
                NUM_EMPLOYEES=numberOfEmployees;        //set number of employees
                employeeId=new int[NUM_EMPLOYEES];  //create array with given number
                hours=new int[NUM_EMPLOYEES];
                payRate=new double[NUM_EMPLOYEES]; //create payrate array with given size
                wages=new double[NUM_EMPLOYEES];  //create  wages array with given size
                
        }
        
        //geter to get employee id array
        public int[] getEmployeeId() {
                return employeeId;
        }

        //setter to set employeeid array
        public void setEmployeeId(int[] employeeId) {
                this.employeeId = employeeId;
        }

        //get to get hours array
        public int[] getHours() {
                return hours;
        }

        //setter tos et number of hours
        public void setHours(int[] hours) {
                this.hours = hours;
        }

        //getter to get payrate array
        public double[] getPayRate() {
                return payRate;
        }

        //setter to set payrate array
        public void setPayRate(double[] payRate) {
                this.payRate = payRate;
        }

        //getter to get wages array
        public double[] getWages() {
                return wages;
        }

        //seter to calculate the wages using helper method grossPayCalculator
        public void setWages() {
                for(int i=0;i<NUM_EMPLOYEES;i++)   //for each employee
                {
                        //call method to calculate pay and enter in the wages array
                        wages[i]=grossPayCalculator(employeeId[i]);     
                }
                
        }
        
        //method to return pay of an employyee
        public double grossPayCalculator(int id)
        {
                
                for(int i=0;i<NUM_EMPLOYEES;i++) //loop through employee 
                {
                        if(employeeId[i]==id)   //if id matches
                        {
                                return hours[i]*payRate[i];     //return pay
                        }
                }
                
                return -1;      //if there is no employye by that id then return -1
        }
        
        //overrided toString() to display all the ids and wages
        public String toString()
        {
                String str;
                
                str="PAYROLL DATA================";
                for(int i=0;i<NUM_EMPLOYEES;i++)   //for each employee
                {
                        str+="\nEmployee ID: "+employeeId[i];   //add id of employee in string
                        str+="  Gross pay: $"+String.format("%.2f",wages[i]);   //add gross pay in string with 2 decimal format
                }
                
                return str;             //rerurn the resultant string
        }

        //main method
        public static void main(String[] args) 
        {
                Scanner scan=new Scanner(System.in);   //for taking input
                int number=0;  //to store number of employees
                
                System.out.print("Enter number of employees: ");
                number=scan.nextInt();  //read number of employees
                
                Payroll roll=new Payroll(number);   //make object of Payroll with given number of max employees
                
                double payRate[]=new double[number];    //array to store pay rate
                int hours[]=new int[number];    //array to store number of hours
                int id[]=new int[number];       //array to store ids pof employees
                
                for(int i=0;i<number;i++)   //loop  to enter details for each employee
                {
                        System.out.print("\nEnter the ID of Employee # "+(i+1)+": ");
                        id[i]=scan.nextInt();   //read id
                        
                        System.out.print("Enter the hours worked by employee number "+id[i]+": ");
                        hours[i]=scan.nextInt();  //read hours
                        while(hours[i]<0)   //if hours is negative then loop again to enter data again
                        {
                                System.out.print("ERROR: Enter positive Value for Number of hours: ");
                                hours[i]=scan.nextInt();   //read hours again
                        }
                        
                        System.out.print("Enter the hourly pay rate for employee number "+id[i]+": ");
                        payRate[i]=scan.nextDouble();   //read payrate of employee
                        while(payRate[i]<6)   //if payrate is less than 6 loop again
                        {
                                System.out.print("ERROR: Enter 6.00 or greate for pay rate: ");
                                payRate[i]=scan.nextDouble();   //read payrate again
                        }
                }
                
                roll.setEmployeeId(id);    //set the employee ids in object
                roll.setHours(hours);    //set the employee hours in object
                roll.setPayRate(payRate);     //set the employee  payrates in object
                
                roll.setWages();   //set the wages of each employee in class
                
                System.out.println(roll);  //print payroll data
                
                scan.close();
        }
}

OUTPUT

#######################################################################

CODE IN IDE


Related Solutions

A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String Which of the following is a correct declaration of the member in Java? 1 public String getHeaderField(String name) 2 public String getHeaderField; 3 public String getHeaderField(String) 4 public static String getHeaderField( name) ------------------------------------------------------------------------------------------------------------------------------------- Note: all answers are case sensitive. Inside a class, the declaration of a constructor looks like the following: public JButton(String text) 1. From the constructor, you infer that the name of...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed:...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object with the current time as its seed. Constructs a Random object with a specified seed. Returns a random int value. Returns a random int value between 0 and n (exclusive). Returns a random long value. Returns a random double value between 0.0 and 1.0 (exclusive). Returns...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Please perform reverse engineering to compose a UML class diagram for the provided java code /*...
Please perform reverse engineering to compose a UML class diagram for the provided java code /* Encapsulated family of Algorithms * Interface and its implementations */ public interface IBrakeBehavior { public void brake(); } public class BrakeWithABS implements IBrakeBehavior { public void brake() { System.out.println("Brake with ABS applied"); } } public class Brake implements IBrakeBehavior { public void brake() { System.out.println("Simple Brake applied"); } } /* Client that can use the algorithms above interchangeably */ public abstract class Car {...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT