Question
Objective : The objective of this assignment is help you familiarize yourself with using Arraylists and how they can be an advantage over arrays in
Objective : The objective of this assignment is help you familiarize yourself with using Arraylists and how they can be an advantage over arrays in many situations.
Problem description : HW3.pdf
Note : You do not need to create your own ArrayList class . Please make sure you are using the in-built ArrayList
To Submit
- Avengers.java
- A screenshot of your output when running it against the test class provided above
Please make sure you are uploading the .java files not the .class file.
Documentation
1. Please make sure your code is well-documented similar to the sample shown above. In general, every non-trivial line of code MUST have a comment justifying its existence in your program. For example, in the draw/display methods in your House class, you should have a comment before every loop you write clearly telling the reader, what it is doing.
2. A poorly documented code will not only be penalized 20% in the documentation criteria, but will most likely also be penalized in the Readability Criteria on the grading rubric. (available in the problem description).
HW3 After many sacrifices and multiple time-heists, our beloved Avengers, finally managed to reverse Thanos' snap and bring everyone back to Earth. Now, only the final battle remains. Thanos' army is huge and unpredictable, and the Avengers need to know when they will be attacking. Luckily through Dr. Strange's time travelling skills, he has seen the future and knows the arrival time for each enemy. It is your job to find out how many avengers are needed to fight off Thanos' army. To simplify this problem, we will make the following assumptions, 1) Every avenger can fight off equal number of enemies at once. 2) Each enemy takes 1000 seconds to defeat. 3) Multiple enemies may arrive at the same time. Inputs Your program will take the following inputs. N: The total number of enemies to defeat K: the number of enemies each avenger can handle at once. A list of N numbers, representing timestamps (in seconds). Output Your program should output the MINIMUM number of avengers needed to fight off Thanos' army, Sample Cases Test Run 1 Input Enter number of enemies (N) : 2 Enter fighting capacity of each avenger(K) : 1 Enter time of arrival for each enemy 300 1500 Output 1 avenger(s) are needed to fight off the army Explanation: Avenger 1 starts fighting at =300, defeats the first enemy at E1300, can thus fight the next enemy al =1500. No additional avengers needed. Test Run 2 Input Enter number of enemies (N) : 3 Enter fighting capacity of each avenger(K) : 2 Enter time of arrival for each enemy 500 510 1499 Output 2 avenger(s) are needed to fight off the army Explanation: Avenger 1 starts fighting at t=500, at t=510,294 enemy arrives, Al can handle 2 enemies at once (see value of K), so still just l avenger needed. Next enemy arrives at 1=499, which is less than (500+1000), which means A1 is still fighting off 2 enemics at t-1499. Hence a 2-1 avenger is needed, to deal with the last enemy. So minimum number - 2 Test Run 3 Input Enter number of enemies (N) 14 Enter fighting capacity of each avenger(K) : 3 Enter time of arrival for each enemy 100 200 345 980 1123 1242 1466 1777 1900 2000 2000 2001 2500 3000 Output 3 avenger(s) are needed to fight off the army Explanation for Test Run 3 (A1, A2, A3 represents each avenger) earliest 100 A1 earliest 100 A1 200 A1 earliest 100 345 200 A1 A1 A1 earliest 100 A1 200 A1 345 A1 980 A2 earliest 200 1123 A1 345 A1 980 AZ A1 earliest 345 1123 A1 1242 A1 980 A2 A1 1123 A1 1242 A1 1466 A1 earliest 980 A2 1123 A1 1242 A1 1466 A1 earliest 980 AZ 1777 A2 1242 1466 earliest 980 AZ 1123 A1 1777 1900 2 A1 A1 A2 earliest 1123 1466 1777 1900 1242 A1 2000 AZ A1 A1 A2 AZ earliest 1123 A1 1242 1777 1466 A1 2000 A2 1900 2000 A2 A3 A1 A2 earliest 1123 A1 1242 A1 1466 A1 2000 A2 1777 A2 1900 2000 2001 A2 A3 A3 earliest 1242 A1 1466 2500 A1 1777 2000 A2 1900 2000 2001 AZ A3 A1 A2 earliest 1466 2500 A1 3000 A1 2000 A2 1777 A2 1900 2000 2001 A2 A1 Approach 1) Each timestamp represents the time at which an enemy arrives. 2) You can represent the above data structure using an arraylist. ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started