Question

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 Employee object in the ArrayList, and calculate their monthly payments, using the calcMonthlyPayment() method.
  • Create a second FOR loop that goes through every Employee object in the ArrayList, and set their monthly payments to -1, only if their leaveDays is higher than 21.
  • In a third FOR loop, go through every Employee object in the ArrayList, and call their makePayment() method.

For the 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 -1 was made to employee 2.

A payment of 11666 was made to employee 3.

A payment of -1 was made to employee 4.

A payment of 10833 was made to employee 5.

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

import java.util.*;
public class Question{
   public static void main(String[] args) {

       int id = 1,leaveDays;
       int salary;
       int loop_terminator;

       Scanner input=new Scanner(System.in);

       ////ArrayList declaration ////
       ArrayList<Employee> list = new ArrayList<Employee>();

       System.out.println("For the input ...");
       while(true)
       {
           id = input.nextInt();

           ///condition to break the loop///
           if(id == -1)
               break;


           salary = input.nextInt();
           leaveDays = input.nextInt();
           Employee obj = new Employee(id,leaveDays,salary);

           list.add(obj); ////appending the object of Employee in the ArrayList
       }

       int length = list.size(); ///getting length of ArrayList

       System.out.println("Output will be....");
       System.out.println("You added " + length + " employees! Thank you!");
      
       for(int i =0 ;i<length ; i++)
       {
           list.get(i).calcMonthlyPayment();
           System.out.println("Monthly payment for employee " + (i+1) + " is calculated.");
       }

       for(int i =0 ;i<length ; i++)
       {
           System.out.println("A payment of " + list.get(i).makePayment() + " was made to employee " + (i+1));
       }
   }
}


/////Employee class definition///////
class Employee
{
   int id, leaveDays;
   int salary , monthly_payment;

   ///class constructor /////
   Employee(int id, int leaveDays, int salary)
   {
       this.id = id;
       this.leaveDays = leaveDays;
       this.salary = salary;
   }

   ////method to calculate montly payment ////
   public void calcMonthlyPayment()
   {
       if(leaveDays > 21 )
       {
           monthly_payment = -1;
          
       }
       else
       {
          
           monthly_payment = (int) (salary / 12);
       }
   }

   ////method to return payment made to the employee ///
   public int makePayment()
   {
       return monthly_payment;
   }
}

Add a comment
Know the answer?
Add Answer to:
I need to write a Java program. Declare and initialize an ArrayList of type Employee. In...
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
  • 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...

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

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

  • Java Description Write a program to compute bonuses earned this year by employees in an organization....

    Java Description Write a program to compute bonuses earned this year by employees in an organization. There are three types of employees: workers, managers and executives. Each type of employee is represented by a class. The classes are named Worker, Manager and Executive and are described below in the implementation section. You are to compute and display bonuses for all the employees in the organization using a single polymorphic loop. This will be done by creating an abstract class Employee...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • in java PART ONE ================================================== Write a program that will read employee earnings data from an...

    in java PART ONE ================================================== Write a program that will read employee earnings data from an external file and then sort and display that data. Copy and paste the input file data below into an external file, which your program will then read. You will want to use parallel arrays for this program. Modify the select sort algorithm to receive both arrays as parameters as well as the size of the arrays. Use the algorithm to sort your earnings array....

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own...

    WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own class in a program To write a constructor with parameters, an method and a toString) method. To use ArrayList To use a for-each loo . p *l . To use text file for output 2 The Context This is a single-class project, the first for which I have not given you code to start from. It is not realistic, or useful, but it gives...

  • In Java Main method Your main program will prompt the user for a test name and...

    In Java Main method Your main program will prompt the user for a test name and the number of scores the user will enter. After creating an arraylist to hold the scores, prompt the user for all the scores to hold in the arraylist. After all the scores are entered, then repeat back the score & letter grade. GradeBook Object Create an instance of a GradeBook object and pass the test name and arraylist to set the instance variables. At...

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