Question

JAVA PROGRAMMING Create an Employee class which implements Comparable<Employee> The constructor consists of an employee’s first...

JAVA PROGRAMMING

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

import java.util.PriorityQueue;

public class PriorityQueueUserDefinedObjectExample {

    public static void main(String[] args) {
        PriorityQueue<Employee> employeePriorityQueue = new PriorityQueue<>();

        employeePriorityQueue.add(new Employee("Bob", 200000.00));
        employeePriorityQueue.add(new Employee("John",135000.00));
        employeePriorityQueue.add(new Employee("Jessica",315000.00));
        employeePriorityQueue.add(new Employee("Bebo",267000.00));

        while(!employeePriorityQueue.isEmpty()) {
            System.out.println(employeePriorityQueue.remove());
        }
    }

}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Objects;

public class Employee implements Comparable<Employee> {
    private String name;
    private double salary;

    public Employee(String name,double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

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

    @Override
    public boolean equals(Object o) {
        if(this == o) return true;
        if(o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Double.compare(employee.salary, salary) == 0 && Objects.equals(name,employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name,salary);
    }

    @Override

    public String toString() {
        return "Employee{" + "name='" + name + '\'' + ",salary=" + salary + '}';
    }

    @Override
    public int compareTo(Employee employee) {
        if(this.getSalary() > employee.getSalary()) {
            return 1;
        } else if(this.getSalary() < employee.getSalary()) {
            return -1;
        } else {
            return 0;
        }
    }

}

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING Create an Employee class which implements Comparable<Employee> The constructor consists of an employee’s first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create java application with the following specifications: -create an employee class with the following attibutes: name...

    create java application with the following specifications: -create an employee class with the following attibutes: name and salary -add constuctor with arguments to initialize the attributes to valid values -write corresponding get and set methods for the attributes -add a method in Employee called verify() that returns true/false and validates that the salary falls in range1,000.00-99,999.99 Add a test application class to allow the user to enter employee information and display output: -using input(either scanner or jOption), allow user to...

  • C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to...

    C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to the project. Copy and paste the code is listed below: Design a class named Employee. The class should keep the following information in member variables: Employee name Employee number Hire date // Specification file for the Employee class #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { private:        // Declare the Employee name string variable here. // Declare the Employee...

  • Create a Java application, that support the following: Create an Employee class, which holds following information:...

    Create a Java application, that support the following: Create an Employee class, which holds following information: Employee First Name Employee Last Name Employee Phone Number Employee Address Employee id Employee Title Employee salary Create an application that uses the Employee class Constructors –Getters and setters A minimum of 3 constructors including default constructor equals() method Helper methods toString() method Create an array of 5 Employee objects Use a Scanner to read in 5 employee information Change 2 employees information (3-items...

  • in java Define a class named ComparableNames thats extends Person and implements comparable Define a class...

    in java Define a class named ComparableNames thats extends Person and implements comparable Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To method to compare the person on the basis of name. Write a test class to find the order of two ComparableNames. class Person String name; Person00 Person(String n) name=n: Hint: in class ComparableName, you only need a constructor with arg to initialize the data filed defined in the super class...

  • In one file create an Employee class as per the following specifications: three private instance variables:...

    In one file create an Employee class as per the following specifications: three private instance variables: name (String), startingSalary (int), yearlyIncrement (int) a single constructor with three arguments: the name, the starting salary, and the yearly increment. In the constructor, initialize the instance variables with the provided values. get and set methods for each of the instance variables. A computation method, computeCurrentSalary, that takes the number of years in service as its argument. The method returns the current salary using...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEm...

    Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT