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   
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 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...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT