Question

In: Computer Science

in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor...

in JAVA PLEASE SHOW OUTPUT!

  1. PriorityQueueUserDefinedObjectExample (20)

Create an Employee class which implements Comparable<Employee>

The constructor consists of an employee’s first name and an employee’s salary, both of which are instance variables.

Create accessor and mutator methods for both of these variables

Write an equals method that returns true if the salaries are equal with one cent and the names are exactly equal

Write a compareTo method that returns 1 if the salary of this employee is greater than the salary of the comparable, -1 if less than and 0 if equal.

Create a class called PriorityQueueUserDefinedObjectExample

Create a Scanner to read from the user input.

Create a Scanner and a PrintWriter to read to a file and output to a different file. (note: You should prompt for the names of these files).

The input file has a single name followed by a 5 or 6 figure salary on each line. (You can use my empsalaries.txt in the homework for Lesson 7)

Create a PriorityQueue

Read in and add each employee to the queue

Use the remove method as you write the employee and salary to the output file from lowest salary to highest.

See sample input file empsalaries.txt and output file priorityemp.txt

//empsalaries

James 1000000.42
Oscar 7654321.89
Jose 352109.00
Daniel 98476.22
Juan 452198.70
Sean 221133.55
Ryan 1854123.77

//priorityemp

    NAME           SALARY
  Daniel        $ 98476.22
    Sean        $221133.55
    Jose        $352109.00
    Juan        $452198.70
   James        $1000000.42
    Ryan        $1854123.77
   Oscar        $7654321.89

Solutions

Expert Solution

SOLUTION-
I have solve the problem in Java code with comments and screenshot for easy understanding :)

CODE-

// Employee.java

public class Employee implements Comparable<Employee> {

   // Instance variables
   private String firstName;
   private double salary;

   /**
   * Constructor
   *
   * @param firstName
   * - employee's first name
   * @param salary
   * - employee's salary
   */
   public Employee(String firstName, double salary) {
       this.firstName = firstName;
       this.salary = salary;
   }

   /**
   * This method returns the employee's firstName
   */
   public String getFirstName() {
       return firstName;
   }

   /**
   * This method returns the employee's salary
   */
   public double getSalary() {
       return salary;
   }

   /**
   * This method sets the employee's first name
   *
   * @param firstName
   * - the firstName to set
   */
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   /**
   * This method sets the employee's salary
   *
   * @param salary
   * - the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }

   /**
   * This method returns true if salaries of this and the given Employee are
   * equal with one cent and the names are exactly equal.
   */
   @Override
   public boolean equals(Object obj) {
       // Check if obj and this refer to the same object
       if (this == obj)
           return true;

       // Check whether obj is null
       if (obj == null)
           return false;

       // Cast obj to Employee
       Employee e = (Employee) obj;
       if ((Math.abs(this.salary - e.salary) <= 0.01) && (this.firstName.equalsIgnoreCase(e.firstName)))
           return true;

       // Default return
       return false;
   }

   @Override
   public int compareTo(Employee e) {
       // Check if salary of this Employee is greater than Employee e
       if (this.salary > e.salary)
           return 1;
       else if (this.salary < e.salary)
           return -1;
       else
           return 0;
   }
}

// PriorityQueueUserDefinedObjectExample.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.Scanner;

public class PriorityQueueUserDefinedObjectExample {

   public static void main(String[] args) {
       // Scanner to user input
       Scanner input = new Scanner(System.in);

       // Get input file name
       System.out.print("Enter input file name: ");
       String inputFile = input.nextLine();

       // Get output file name
       System.out.print("Enter output file name: ");
       String outputFile = input.nextLine();

       // Create PriorityQueue
       PriorityQueue<Employee> queue = new PriorityQueue<Employee>();

       // Scanner to read from a file
       try {
           Scanner inFile = new Scanner(new File(inputFile));

           // Read file
           while(inFile.hasNextLine()){
               // Read a line of data
               String line = inFile.nextLine().trim();

               // Tokenize line
               String[] data = line.split("\\s+");

               // Create Employee object
               Employee e = new Employee(data[0], Double.parseDouble(data[1]));

               // Add e to queue
               queue.add(e);
           }

           // Close file
           inFile.close();

           // PrintWriter object for writing to the file
           PrintWriter pw = new PrintWriter(new File(outputFile));

           // Write each Employee object from queue to file
           while (!queue.isEmpty()) {
               // Get Employee at the front of the queue
               Employee e = queue.remove();
               pw.println(String.format("%s $%.2f", e.getFirstName(), e.getSalary()));
           }

           // Close PrintWriter
           pw.close();

           // Display message
           System.out.print("\nOutput written to file " + outputFile);
       } catch (FileNotFoundException fnfe) {
           fnfe.printStackTrace();
       }
   }
}

CODE SCREENSHOT:

SAMPLE OUTPUT:

console output

EmpInput.txt

EmpOutput.txt

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable interfaces The class should have the following private member variables: • the x value of a point: int • the y value of a point: int and the class should have the following public member functions: • default constructor which initializes the point to the origin of the Cartesian coordinate system • explicit constructor which initializes the point to a pair of given value...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor:...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor: Point(double x, double y, double z) and, the following API: double distanceto(Point q) it returns the Euclidean distance between this and q. The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2). String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0). Write a main method in the...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT