In: Computer Science
in JAVA PLEASE SHOW OUTPUT!
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
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!!!!!!!!----------