Question

In Java: Executable Class create an array of Employee objects. You can copy the array you...

In Java:

Executable Class

  • create an array of Employee objects. You can copy the array you made for Chapter 20.
  • create an ArrayList of Employee objects from that array.
  • use an enhanced for loop to print all employees as shown in the sample output.
  • create a TreeMap that uses Strings for keys and Employees as values.
  • this TreeMap should map Employee ID numbers to their associated Employees.
  • process the ArrayList to add elements to this map.
  • print all employees in ID # order as shown, but do so using the TreeMap's forEach method and a lambda expression

So far I have created the array of the 8 employee objects and the ArrayList. I have also created the enhanced for loop. I just can't figure out how to get the TreeMap to work with the ArrayList to add the elements to the map and print out the list of employees with their associated id numbers from smallest id number to largest id number.

Here is my code so far.


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;


class Employee {
   private String id;

   private String lastName;

   private String firstName;

   private int salary;


public Employee(String _id, String ln, String fn, int sal){

   id = _id;

   firstName = fn;

   lastName = ln;

   salary = sal;

   }
   //get the id and return the id
   public String getId() {

   return id;

   }
   //get the first name and return the first name
   public String getFirstName() {

   return firstName;

   }
   //get the last name and return the last name
public String getLastName() {

   return lastName;

   }
//get the salary and return the salary
   public int getSalary() {

   return salary;

   }
   //display the employees attributes
   public String toString(){
   //format the salary
   NumberFormat formatter = NumberFormat.getInstance(Locale.US);

   return "ID "+id+":"+lastName+", "+firstName+", salary $"+formatter.format(salary);

   }


public static void main(String[] args) {
  
   //array of 8 employee objects
   Employee []employees = new Employee[8];

   employees[0] = new Employee("12345", "Baker", "Tom", 200000);

   employees[1] = new Employee("56789", "Jones", "Dan", 130000);                                              

   employees[2] = new Employee("24680", "Scott", "Ann", 90000);

   employees[3] = new Employee("13579", "Jones", "Pat", 80000);

   employees[4] = new Employee("11111", "Scott", "Bob", 65000);

   employees[5] = new Employee("23232", "Baker", "Amy", 100000);

   employees[6] = new Employee("45454", "Perez", "Ava", 105000);

   employees[7] = new Employee("67765", "Jones", "Don", 140000);
   //array list
   ArrayList<Employee> empList = new ArrayList<Employee>(Arrays.asList(employees));
   //enhanced for loop to display the employees
   System.out.println("All Employees\n");
  
   empList.forEach((e)->System.out.println(e));
}
}

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


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.TreeMap;

class Employee {
   private String id;

   private String lastName;

   private String firstName;

   private int salary;

   public Employee(String _id, String ln, String fn, int sal) {

       id = _id;

       firstName = fn;

       lastName = ln;

       salary = sal;

   }

   // get the id and return the id
   public String getId() {

       return id;

   }

   // get the first name and return the first name
   public String getFirstName() {

       return firstName;

   }

   // get the last name and return the last name
   public String getLastName() {

       return lastName;

   }

   // get the salary and return the salary
   public int getSalary() {

       return salary;

   }

   // display the employees attributes
   public String toString() {
       // format the salary
       NumberFormat formatter = NumberFormat.getInstance(Locale.US);

       return "ID " + id + ":" + lastName + ", " + firstName + ", salary $" + formatter.format(salary);

   }

   public static void main(String[] args) {

       // array of 8 employee objects
       Employee[] employees = new Employee[8];

       employees[0] = new Employee("12345", "Baker", "Tom", 200000);

       employees[1] = new Employee("56789", "Jones", "Dan", 130000);

       employees[2] = new Employee("24680", "Scott", "Ann", 90000);

       employees[3] = new Employee("13579", "Jones", "Pat", 80000);

       employees[4] = new Employee("11111", "Scott", "Bob", 65000);

       employees[5] = new Employee("23232", "Baker", "Amy", 100000);

       employees[6] = new Employee("45454", "Perez", "Ava", 105000);

       employees[7] = new Employee("67765", "Jones", "Don", 140000);
       // array list
       ArrayList<Employee> empList = new ArrayList<Employee>(Arrays.asList(employees));

       // displaying the employees using enhanced for loop
       for (Employee e : empList)
           System.out.println(e);
       // creating Tree Map
       TreeMap<String, Employee> map = new TreeMap<String, Employee>();
       // adding objects to TreeMap
       for (Employee e : empList)
           map.put(e.getId(), e);

       // enhanced for loop to display the employees
       System.out.println("All Employees\n");
       // using lamda expressions to display map
       map.forEach((k, v) -> System.out.println((k + ":" + v)));

   }
}

Add a comment
Know the answer?
Add Answer to:
In Java: Executable Class create an array of Employee objects. You can copy the array you...
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
  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • 4. Create a Business class. This class should store an array of Employee objects. Add a...

    4. Create a Business class. This class should store an array of Employee objects. Add a method to add employees to the Business, similar to addFoodItem in the Meal class. Add two more methods for the Business class: getHighestPaid(), which returns the Employee who has been paid the most in total, and getHighestTaxed(), which returns the Employee who has been taxed the most in total. Note: you do not need to handle the case of ties. These are all the...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each...

    HOW TO FiX EXCEPTIONS??? In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

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