Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer all three in Java Practice Questions You want to sort the Employee

ID: 3706957 • Letter: P

Question

Please answer all three in Java Practice Questions You want to sort the Employee class shown using Arrays.sortO public class Employee implements Comparable private String 1Name; private double salary; public int compareTo( Object p) / write the code that goes here a Write the compareTo() method that will sort by salary in descending order (largest to smallest). b. Wrie bhe comparetol) methoud that will of ty 1Name in acending order (alphabetically) 2. Write the Java class declarations for the Employee class and 2 subclasses, Salaried and Hourly. Only provide implementations for the constructors and methods shown in the UML diagram. For the Salaried class, the getweeklyPay() method returns salary 52. For the Hourly class, the getweeklyPay() method returns payRate 4e. Employee -fName: String IName: String +Employeel fName: String, IName : String ) Salaried Hourly annualSalary: double payRate : double +Hourly( fName : String, IName: String, +getWeeklyPay(): double + Salaried( fName: String, IName: String salary : double ) payRate: double) + getWeeklyPay) : double 3. Write the code to read a CSV file named test.txt and place the following entries in variables (name, department, pay rate, hours worked): George williams, Sales, 12.95, 4 Rcad esv file once Do split once then read

Explanation / Answer

1.

Employee .java
public class Employee implements Comparable<Employee> {

   private String lName;
   private double salary;

   // constructor
   public Employee(String lName, double salary) {
       super();
       this.lName = lName;
       this.salary = salary;
   }

   // getters and setters

   public String getlName() {
       return lName;
   }

   public void setlName(String lName) {
       this.lName = lName;
   }

   public double getSalary() {
       return salary;
   }

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

   // To Sort based on the salary , Descending Order
   @Override
   public int compareTo(Employee o) {

       return (int) (o.getSalary() - this.getSalary());
   }

   @Override
   public String toString() {
       return "Employee [LastName=" + lName + ", salary=" + salary + "]";
   }

   // Same method can be rewritten as
   // To Sort based on the lName , Ascending Order (Please uncomment to test )
  
   /*@Override
   public int compareTo(Employee o) {

       return this.getlName().compareTo(o.getlName());
   }*/

}

TestEmployee.java

import java.util.Arrays;

public class TestEmployee {

   public static void main(String[] args) {
       // Declare Array of Employee
       Employee[] employees = new Employee[5];
       employees[0] = new Employee("James", 35000);
       employees[1] = new Employee("Clara", 50000);
       employees[2] = new Employee("Hales", 60000);
       employees[3] = new Employee("Mathews", 15000);
       employees[4] = new Employee("Andrew", 55000);

       // printing the values before sorting
       System.out.println("Before Sorting");
       for (Employee employee : employees) {
           System.out.println(employee);
       }

       // Sort based on salary

       Arrays.sort(employees);

       // printing the values After sorting
       System.out.println("After Sorting");
       for (Employee employee : employees) {
           System.out.println(employee);
       }
   }

}

Output:

Before Sorting
Employee [LastName=James, salary=35000.0]
Employee [LastName=Clara, salary=50000.0]
Employee [LastName=Hales, salary=60000.0]
Employee [LastName=Mathews, salary=15000.0]
Employee [LastName=Andrew, salary=55000.0]
After Sorting
Employee [LastName=Hales, salary=60000.0]
Employee [LastName=Andrew, salary=55000.0]
Employee [LastName=Clara, salary=50000.0]
Employee [LastName=James, salary=35000.0]
Employee [LastName=Mathews, salary=15000.0]

Question 2.


public class Employee
{
   private String fName;
   private String lName;
  
   //Constructor
   public Employee(String fName, String lName) {
       super();
       this.fName = fName;
       this.lName = lName;
   }
  
   //getters and setters
   public String getfName() {
       return fName;
   }
   public void setfName(String fName) {
       this.fName = fName;
   }
   public String getlName() {
       return lName;
   }
   public void setlName(String lName) {
       this.lName = lName;
   }
   @Override
   public String toString() {
       return "Employee [fisrtName=" + fName + ", LastName=" + lName + "]";
   }
  
  
}

SalariedEmployee.java


public class SalariedEmployee extends Employee {
   private double annualSalary;

   public SalariedEmployee(String fName, String lName, double annualSalary) {
       super(fName, lName);
       this.annualSalary = annualSalary;
   }

  
   public double getAnnualSalary() {
       return annualSalary;
   }


   public void setAnnualSalary(double annualSalary) {
       this.annualSalary = annualSalary;
   }


   public double getWeeklyPay() {
       return annualSalary / 52;
   }
}

HourlyEmployee.java


public class HourlyEmployee extends Employee {
private double payRate;

public HourlyEmployee(String fName, String lName, double payRate) {
   super(fName, lName);
   this.payRate = payRate;
}

public double getPayRate() {
   return payRate;
}

public void setPayRate(double payRate) {
   this.payRate = payRate;
}
public double getWeeklyPay() {
   return getPayRate()*42;
}
}

3.

// Read the file
   private static ArrayList<String> readFile() {
       List<String> list = null;
       try {
           list = Files.readAllLines(new File("./test.txt").toPath(), Charset.defaultCharset());
       } catch (IOException e) {
           e.printStackTrace();
       }
       return (ArrayList<String>) list;
   }

//Call above method to Read the File .

//Then parse the String tokewise

As follows

    ArrayList<String> lines=readFile();
       for (String string : lines) {
           // get tokens
           String[] tokens = string.split(",");
           //assign tokens to variables
           String name=tokens[0];
           String department=tokens[1];
           double payRate=Double.parseDouble(tokens[2]);
           int hourWorked=Integer.parseInt(tokens[3]);
       }
      
       //Create Objects and Assign above Values