Preliminaries Create a project submission folder, in the form Lab#FirstNameLastN
ID: 3839951 • Letter: P
Question
Preliminaries
Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab7DavidLiu
Create an Eclipse Java Project.
Add a Java class named Payroll to your project
Add a Java class named ArrayOps to your project
Add a Java class called Main to your project
Exercises
Part 1) – The ArrayOps class – 5 points
ArrayOps is the first class we will make, as you make use of it in the others.
Note: In the event I don’t get to it in lecture before this is assigned, you can use arrays as return types an arguments. A method:
public char[] findLetter(int[] blah){//stuff…}
Would take an int array as an argument and return a char array.
ArrayOps has no constructor or fields, so rejoice! Instead, it has the following methods:
a public static method called copyIntArray. It takes an int array as an argument and returns a brand new one, made by copying the values from the first array
a public static method called copyDoubleArray. It does the same as copyIntArray, but takes and returns a double array
two public static methods both named sumArray. One should take an int array, the other should take a double array. The one that takes an int array should return an integer and the one that takes a double should return a double.
Both copyIntArray and copyDoubleArray should follow this same general algorithm. While I write it using ‘int’, replace it with double for the DoubleArray method:
Make a new int array -> the size of the array should match the size of the argument array
Loop through the values in the array passed as an argument
For each value, copy it into the new array
Return the new array
For the summing methods, you should sum the values of the array passed as an argument and pass the sum. This should be similar to other for loop summations we’ve done in the past.
Part 2) – The Payroll Class – 10 points
The second part is focused around making the Payroll Class itself.
Payroll should have a private final constant int field called SIZE (or something like it). Set it to 7.
The Payroll class needs the four following fields:
employeeID – an array of Strings, which will hold employee ID numbers. It should be private and have size SIZE
hours – an array of integers, which will hold the hours the employees worked. It should have size SIZE
payRates – an array of doubles holding the pay rates for employees
wages – an array of doubles holding the wages for employees
The Payroll constructor takes no arguments. It should initialize wages to an empty array of size SIZE. It should assign the 7 values to the employeeID array. You may use whatever IDs you wish. An example of 7 values is:
7623A, 1182B, 1182C, 1337G, 8910Y, 2323X, 6921F
Payroll should have getters for all fields
Payroll should have setters for hours and payRates.
Instead of just using field = argument, hours should use ArrayOps’ copyIntArray method (hours = copyIntArray(argumentarray)
payRates should use the copyDoubleArray
Payroll needs a method setWages. setWages is void and has the following algorithm:Using a regular for loop:
Calculate pay = hours * payRate for a given index
Set wage[i] = pay, where i is the current elements of the array being accessed
Payroll should have a toString method, that prints out:
“PAYROLL DATA”
“-----“
For each Employee:
EmployeeID : the ID (from the array)
Pay: the wage (from the array) . Use a DecimalFormat object to round to two decimal points
The sum of hours and wages, using ArrayOps’ sum methods
Sample output:
PAYROLL DATA
-----
Employee ID: 5658845
Gross pay: $626.00
Employee ID: 4520125
Gross pay: $666.00
Employee ID: 7895122
Gross pay: $720.00
Employee ID: 8777541
Gross pay: $897.75
Employee ID: 8451277
Gross pay: $300.00
Employee ID: 1302850
Gross pay: $300.00
Employee ID: 7580489
Gross pay: $400.00
Sum Hours: 272
Sum Pay: $3909.75
Part 3) – The Main Class – 5 points
The Main Class is responsible for using Payroll.
Main should make three arrays – an array of ints and one double array. They should be all be size 7.
Main should make an instance of Payroll.
Main should then loop through the employeeID array of the Payroll object. You can store it temporarily (String[] arr = employeeID’s accessor method] or just call the method directly in the for loop if you are using the enhanced for loop. The loop should:
Ask the user for the hours worked by the current employee and store it in the int array
Ask the user for the pay rate for the current employee and store it in one of the double arrays
It should look something like this:
Enter the hours worked by employee number 5658845: 40
Enter the hourly pay rate for employee number 5658845: 15.65
Enter the hours worked by employee number 4520125: 37
Enter the hourly pay rate for employee number 4520125: 18.00
Enter the hours worked by employee number 7895122: 40
Enter the hourly pay rate for employee number 7895122: 18.00
Enter the hours worked by employee number 8777541: 35
Enter the hourly pay rate for employee number 8777541: 25.65
Enter the hours worked by employee number 8451277: 40
Enter the hourly pay rate for employee number 8451277: 7.50
Enter the hours worked by employee number 1302850: 40
Enter the hourly pay rate for employee number 1302850: 7.50
Enter the hours worked by employee number 7580489: 40
Enter the hourly pay rate for employee number 7580489: 10.00
Once the for loop is done, use Payroll’s setter methods to set the int array as the hours and the double array as the rates.
Call Payroll.setWages()
Finally, print out Payroll. It should use the toString written for it above.
Explanation / Answer
ArrayOps.java
public class ArrayOps {
/* This method will copies the integer array
* passed as argument to new Integer array
*/
public static int[] copyIntArray(int arr[]) {
int arr1[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
return arr1;
}
/* This method will copies the Double array
* passed as argument to new Double array
*/
public static double[] copyDoubleArray(double arr[]) {
double arr1[] = new double[arr.length];
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
return arr1;
}
//This method calculates the sum of all integer array elements
public static int sumArray(int arr[]) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
//This method calculates the sum of all double array elements
public static double sumArray(double arr[]) {
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}
_____________________
Payroll.java
import java.text.DecimalFormat;
public class Payroll {
//Declaring constant
public final int SIZE=7;
//Declaring arrays
private String employeeID[]={"5658845", "4520125", "7895122", "8777541", "8451277", "1302850", "7580489"};;
private int hours[];
private double payRates[];
private double wages[];
//Zero argumented cosntructor
public Payroll() {
//employeeID=
hours=new int[SIZE];
payRates=new double[SIZE];
wages=new double[SIZE];
}
//Setters and getters
public String[] getEmployeeID() {
return employeeID;
}
public int[] getHours() {
return hours;
}
public void setHours(int[] hours) {
this.hours = ArrayOps.copyIntArray(hours);
}
public double[] getPayRates() {
return payRates;
}
public void setPayRates(double[] payRates) {
this.payRates =ArrayOps.copyDoubleArray(payRates);
}
public double[] getWages() {
return wages;
}
public void setWages() {
for(int i=0;i<wages.length;i++)
{
wages[i]=hours[i]*payRates[i];
}
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat("#.##");
System.out.println("PAYROLL DATA");
System.out.println("-----");
for(int i=0;i<wages.length;i++)
{
System.out.println("Employee ID:"+employeeID[i]);
System.out.println("Gross pay: $"+df.format(wages[i]));
System.out.println();
}
System.out.println(" Sum Hours:"+ArrayOps.sumArray(hours));
System.out.println(" Sum Pay: $"+ArrayOps.sumArray(wages));
return "";
}
}
______________________
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Declaring a variable
int size=7;
//Creating hours array and payrate array
int hours[]=new int[size];
double payrate[]=new double[size];
//Crweating an instance of Payroll class
Payroll p=new Payroll();
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//getting the inputs entered by the user
for(int i=0;i<size;i++)
{
System.out.print("Enter the hours worked by employee number "+p.getEmployeeID()[i]+":");
hours[i]=sc.nextInt();
System.out.print("Enter the hourly pay rate for employee number "+p.getEmployeeID()[i]+":");
payrate[i]=sc.nextDouble();
}
//calling the setters on the Payroll
p.setHours(hours);
p.setPayRates(payrate);
p.setWages();
//Displaying the output
p.toString();
}
}
____________________
Output:
Enter the hours worked by employee number 5658845:40
Enter the hourly pay rate for employee number 5658845:15.65
Enter the hours worked by employee number 4520125:37
Enter the hourly pay rate for employee number 4520125:18.00
Enter the hours worked by employee number 7895122:40
Enter the hourly pay rate for employee number 7895122:18.00
Enter the hours worked by employee number 8777541:35
Enter the hourly pay rate for employee number 8777541:25.65
Enter the hours worked by employee number 8451277:40
Enter the hourly pay rate for employee number 8451277:7.50
Enter the hours worked by employee number 1302850:40
Enter the hourly pay rate for employee number 1302850:7.50
Enter the hours worked by employee number 7580489:40
Enter the hourly pay rate for employee number 7580489:10.00
PAYROLL DATA
-----
Employee ID:5658845
Gross pay: $626
Employee ID:4520125
Gross pay: $666
Employee ID:7895122
Gross pay: $720
Employee ID:8777541
Gross pay: $897.75
Employee ID:8451277
Gross pay: $300
Employee ID:1302850
Gross pay: $300
Employee ID:7580489
Gross pay: $400
Sum Hours:272
Sum Pay: $3909.75
______________Thank YOu