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

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 write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT