Question

In: Computer Science

PLEASE DO NOT OVERRIDE ANY EXCEPTIONS TASK: You want to develop a Java program that will...

PLEASE DO NOT OVERRIDE ANY EXCEPTIONS

TASK:

You want to develop a Java program that will allow you to keep track of a set of employees. In reviewing your employee list, you notice that your employees fall into two categories: Salaried and Hourly. The following table shows the information that you keep in your employee list for each type of employee.

Field

Type

Salaried

Hourly

id

int

Yes

Yes

name

String

Yes

Yes

title

String

Yes

No

position

String

No

Yes

salary

int

Yes

No

hourlyRate

double

No

Yes

Create a NetBeans project with the prefix Lab101.

Use the same naming convention used in the Lab100 assignment, i.e. Lab101-LastFM.

In this project create three classes named Employee, Salaried and Hourly such that:

  • The Employee class contains all of the fields common to both types of entries in your employee list.
  • The Salaried class is a subclass of Employee and contains only those fields that are specific to the Salaried entries in your employee book.
  • The Hourly class is a subclass of Employee and contains only those fields that are specific to the Hourly entries in your employee book.
  • Each of these classes contains all of the “normally expected” methods. The “normally expected” methods are:
    • At least one constructor that is an overload constructor that includes all of the necessary information to create an instance.
    • A getter and setter (accessor and mutator) method for each instance/class variable
    • A toString method
    • An equals method
  • Create your classes so that you can keep track of the
    • Total number of Employees
    • Total number of Salaried employees
    • Total number of Hourly employees
    • Note that you are tracking the number of times each class has been instantiated, i.e. a constructor has been called.

Create a fourth class name Client that will be used to test your other classes.

In the Client class:

  • This class must include the main method
  • In the main method
    • Create an array employeeList of type Employee of length 10
    • Add three Salaried contacts to the array
    • Add three Hourly employees contacts to the array
    • Data for each of the contacts must be entered from the keyboard.
    • The employee types must NOT be grouped, i.e. the salaried employees should be interleaved with the hourly employees.
    • Once you entered the data for the six employees:
      • print out the contents of the array using a loop.
      • This loop should print out the contents of every entry in the array including the blank (null) entries.
    • Now give everyone in the employeeList a 10% raise.
      • When applying the raises use a loop to step across the array.
    • After you have given everyone a 10% raise:
      • Print out the contents of the array using a loop.
      • This time do not print the blank (null) entries.
    • Explicitly test the equals methods for each of your classes.

Solutions

Expert Solution

//Java code

public abstract class Employee {
    /**
     * Attributes common to all
     */
    protected int id;
    protected String name;
    //Constructor

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    //Getters and setters

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    /**
     *
     * @return description of employee
     */
    @Override
    public String toString() {
        return "Employee Id: "+id+" Employee's Name: "+name;
    }

    @Override
    public boolean equals(Object obj) {
        return this== ((Employee)obj);
    }
}

//==========================================

public class Salaried extends Employee {
    private String title;
    private int salary;
    private static int countSalariedEmployee =0;
    //Constructor

    public Salaried(int id, String name, String title, int salary) {
        super(id, name);
        this.title = title;
        this.salary = salary;
        countSalariedEmployee++;
    }

    //getters and setters


    public static int getCountSalariedEmployee() {
        return countSalariedEmployee;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    /**
     *
     * @return description of salaried employee
     */
    @Override
    public String toString() {
        return super.toString()+" Title: "+title+" salary: $"+salary;
    }
}

//==================================================

public class Hourly extends Employee {
    private String position;
    private double hourlyRate;
    private static int countHourlyEmployee=0;

    //Constructor

    public Hourly(int id, String name, String position, double hourlyRate) {
        super(id, name);
        this.position = position;
        this.hourlyRate = hourlyRate;
        countHourlyEmployee++;
    }

    //getters and setters


    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    /**
     *
     * @return description of the hourly Employee
     */
    @Override
    public String toString() {
        return super.toString()+ " Position: "+position+" Hourly Rate: $"+String.format("%.2f",hourlyRate);
    }
}

//=======================================

import java.util.Scanner;

public class Client {
    public static void main(String[] args)
    {
        /**
         * Create an array employeeList of type Employee of length 10
         */
        int length = 10;
        Employee[] employeeList = new Employee[length];
        /**
         * Add three Salaried contacts to the array
         */
        String name,position, title ;
        int id, salary;
        double hourlyRate;
        //Scanner for keyboard entry
        Scanner keyboard = new Scanner(System.in);
        for (int i = 0; i <6 ; i++) {
            if(i%2==0) {
                System.out.print("Enter Employee's Id: ");
                id = keyboard.nextInt();
                System.out.print("Enter Employee's name: ");
                name = keyboard.nextLine();
                keyboard.nextLine();
                System.out.print("Enter Employee's title: ");
                title = keyboard.nextLine();
                System.out.print("Enter Employee's salary: ");
                salary = keyboard.nextInt();

                employeeList[i] = new Salaried(id, name, title, salary);
            }
            /**
             * Add three Hourly employees contacts to the array
             */
            else
            {
                System.out.print("Enter Employee's Id: ");
                id = keyboard.nextInt();
                keyboard.nextLine();
                System.out.print("Enter Employee's name: ");
                name = keyboard.nextLine();
                System.out.print("Enter Employee's position: ");
                position = keyboard.nextLine();

                System.out.print("Enter Employee's hourly rate: ");
                hourlyRate = keyboard.nextDouble();
                employeeList[i] = new Hourly(id,name,position,hourlyRate);
            }
        }

        /**
         * print out the contents of the array using a loop.
         */
        for(Employee e: employeeList)
        {
            System.out.println(e);
        }
        /**
         * Now give everyone in the employeeList a 10% raise
         */
        for (int i = 0; i <employeeList.length ; i++) {
            if(employeeList[i]!=null)
            {
                if(employeeList[i] instanceof Salaried)
                {
                    salary = ((Salaried)employeeList[i]).getSalary();
                    salary +=salary*.1;
                    ((Salaried)employeeList[i]).setSalary(salary);
                }
                else
                {
                    hourlyRate = ((Hourly)employeeList[i]).getHourlyRate();
                    hourlyRate+=hourlyRate*.1;
                    ((Hourly)employeeList[i]).setHourlyRate(hourlyRate);
                }
            }
        }
        /**
         * print without null
         */
        for(Employee e: employeeList)
        {
            if(e==null)
                break;
            System.out.println(e);
        }
        //Check equality
        System.out.println(employeeList[0].equals(employeeList[2]));
        System.out.println(employeeList[1].equals(employeeList[4]));
    }
}

//Output

//If you need any help regarding this solution ............ please leave a comment ........ thanks


Related Solutions

Develop a rudimentary Java program that will allow anyone to enter any number of grades and...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and then calculate grade point average. For reference, a grade point average is a weighted average of a set of grades based on the relative number of credits. For example, a 1 credit “A” (4.0) should count twice as much as a .5 credit “C” (3.0), and a 1.5 credit “A+”(4.33) should count three times as much as the “C” and 1.5 times as much...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
In the second task, you will write a Java program that validates an input to see...
In the second task, you will write a Java program that validates an input to see whether it matches a specified pattern. You will have to read a bit about regular expressions for this Lab. 1. Prompt the user to enter the email address. 2. Read the user input as a String. 3. A regular expression that specifies the pattern for an email address is given by “[a-zA-Z0- 9.]++@[a-zA-Z]++.(com|ca|biz)”. Use this as the input to the matches method in the...
I am using Java and what do you mean samples? Creation of Program Application (Development Task...
I am using Java and what do you mean samples? Creation of Program Application (Development Task 2) This program should use a class file you create called: SavingsAccount. This class file should store the following information: Annual Interest Rate (this can be hardcoded) Starting Balance (use a constructor to accept this information) Create method to add all Deposits from Deposits.txt file. Create method to subtract all Withdrawals from Withdrawals.txt file. Create method to calculate the interest rate which is the...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or run error.) Q. 1 Calculate the expression a)         x= 7.0 + (12 %3)*5 – 3;                    b)         x= 7 + (11 / 2)*5 + 3;            Q 2: Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.) Q 3. Write a program that prompts the capacity, in gallons,...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. QUESTION Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT