Question

I have most of the code for this assignment but need some help getting the rest....

I have most of the code for this assignment but need some help getting the rest. Go to bottom to see what I have. Please help me figure out the foreach loops and the put() statement.

In this assignment, you will be using a new data structure, HashMaps, to repeat Assignment 8.3.

Main Method

  • (5 points) Declare and initialize an HashMap with values of type of type Employee, and keys of type integer. In a while loop, keep initializing objects of type Employee, storing them in the HashMap, and allow the user to enter ID, salary, and leaveDays of the each employee one by one and in one line. Once the user entered a -1, stop initializing new employees, and print "You entered [actual length of array] employees. Thank you!"
  • (5 points) In a foreach loop, go through every Employee object in the HashMap, and calculate their monthly payments, using the calcMonthlyPayment() method.
  • (10 points) In a second foreach loop, go through every Employee object in the HashMap, and call their makePayment method only if their leaveDays is NOT higher than 21.

For this question if you receive 20/30 points, you have received the full points!

Sample Run: For the below input:

1 120000 6
2 50000 25
3 140000 4
4 112000 22
5 130000 4
-1

The output will be:

You added 5 employees! Thank you!
Monthly payment for employee 1 is calculated.
Monthly payment for employee 2 is calculated.
Monthly payment for employee 3 is calculated.
Monthly payment for employee 4 is calculated.
Monthly payment for employee 5 is calculated.
A payment of 10000 was made to employee 1.
A payment of 11666 was made to employee 3.
A payment of 10833 was made to employee 5.

import java.util.Scanner;
import java.util.HashMap;

class Employee{
   private int ID;
   private int salary;
   private int leaveDays;
   private int monthlyPayment;
   public int getID() {
       return ID;
   }
   public void setID(int iD) {
       this.ID = iD;
   }
   public int getSalary() {
       return salary;
   }
   public void setSalary(int salary) {
       this.salary = salary;
   }
   public int getLeaveDays() {
       return leaveDays;
   }
   public void setLeaveDays(int leaveDays) {
       this.leaveDays = leaveDays;
   }
   public int getMonthlyPayment() {
       return monthlyPayment;
   }
   public void setMonthlyPayment(int monthlyPayment) {
       this.monthlyPayment = monthlyPayment;
   }
   public void calcMonthlyPayment() {
       this.monthlyPayment = this.salary/12;
       System.out.println("Monthly payment for employee " + this.ID + " is calculated.");

   }
   public void makePayment() {
       System.out.println( "A payment of " + this.monthlyPayment + " was made to employee " + this.ID + ".");
   }
  
}
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

// Declare and initialize an arrayList of type Employee

HashMap <Integer, Employee> Person = new HashMap<Integer, Employee>();


int cnt = 0;

//int number = scnr.nextInt();
int ID = 0;

while(ID!=-1)

{

Employee emp = new Employee();

// System.out.println("Enter ID Salary Leave-days:");
ID = scnr.nextInt();
if (ID == -1){
break;
}
int salary = scnr.nextInt();
int leaveDays = scnr.nextInt();

emp.setID(ID);
emp.setSalary(salary);
emp.setLeaveDays(leaveDays);

person.put(ID, person);

person.add(emp);


cnt++;
}

System.out.println("You added "+ cnt + " employees! Thank you!");

for(int i=0;i<cnt;i++)

{

person.get(i).calcMonthlyPayment();


}

for();

{

if(person.get(i).getLeaveDays()>21)

{
person.get(i).makePayment();

}

}

}

}

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

import java.util.Scanner;
import java.util.HashMap;

class Employee{
private int ID;
private int salary;
private int leaveDays;
private int monthlyPayment;
public int getID() {
return ID;
}
public void setID(int iD) {
this.ID = iD;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getLeaveDays() {
return leaveDays;
}
public void setLeaveDays(int leaveDays) {
this.leaveDays = leaveDays;
}
public int getMonthlyPayment() {
return monthlyPayment;
}
public void setMonthlyPayment(int monthlyPayment) {
this.monthlyPayment = monthlyPayment;
}
public void calcMonthlyPayment() {
this.monthlyPayment = this.salary/12;
System.out.println("Monthly payment for employee " + this.ID + " is calculated.");

}
public void makePayment() {
System.out.println( "A payment of " + this.monthlyPayment + " was made to employee " + this.ID + ".");
}
  
}
public class Main {
   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);

// Declare and initialize an arrayList of type Employee

       HashMap <Integer, Employee> Person = new HashMap<Integer, Employee>();
       int cnt = 0;

//int number = scnr.nextInt();
       int ID = 0;

       while(ID!=-1)
       {
           Employee emp = new Employee();
           // System.out.println("Enter ID Salary Leave-days:");
           ID = scnr.nextInt();
           if (ID == -1){
               break;
           }
           int salary = scnr.nextInt();
           int leaveDays = scnr.nextInt();
           emp.setID(ID);
           emp.setSalary(salary);
           emp.setLeaveDays(leaveDays);
           Person.put(ID, emp);
           //Person.add(emp);
           cnt++;
       }
       System.out.println("You added "+ cnt + " employees! Thank you!");
       for(int i=1;i<=Person.size();i++)
       {
           Person.get(i).calcMonthlyPayment();
       }
       for(int i=1;i<=Person.size();i++)
       {
           if(Person.get(i).getLeaveDays()<21)
           {
               Person.get(i).makePayment();
           }

       }

   }

}



deepika@deepika-TravelMate-P243-M: /Desktop File Edit View Search Terminal Help deepikaodeepika-TravelMate-P243-M:/Desktop$ j

Add a comment
Know the answer?
Add Answer to:
I have most of the code for this assignment but need some help getting the rest....
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 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;...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

  • I need to write a Java program. Declare and initialize an ArrayList of type Employee. In...

    I need to write a Java program. Declare and initialize an ArrayList of type Employee. In a while loop, keep initializing objects of type Employee, storing them in the array, and allow the user to enter ID, salary, and leaveDays of the each employee one by one and in one line. Once the user entered a -1, stop initializing new employees, and print "You entered [actual length of array] employees. Thank you!" Create a FOR loop that goes through every...

  • 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;...

  • Hey Guys , I need help with this assignment: This component of the final exam will...

    Hey Guys , I need help with this assignment: This component of the final exam will have two data structures. The two data structures will be a stack and the other will be a hashmap. The hashmap object will be added to the stack every time the user picks an answer. The hashmap will have a key and value. The key will be the timestamp (system time when the user selected a choice) and the value will be the choice...

  • 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...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • (JAVA) I need to write an accessor method for the employeeDatabase field. The method is supposed...

    (JAVA) I need to write an accessor method for the employeeDatabase field. The method is supposed to be named getEmployeeDatabase() and have a return type of Hashmap<Integer,Employee>. I have also included the test case below that is being used for this code to see if it works properly private HashMap<Integer, Employee> employeeDatabase; public CompanyO f Scanner employeeDatabasepopulateEmployeeDatabase(keyboard) keyboard - new Scanner(System.in); public static void main(String[ args) private HashMap<Integer, Employee> populateEmployeeDatabase(Scanner keyboard) int numobjects -keyboard.nextIntO; String words HashMap <Integer, Employee mapnew...

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