Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.pdfimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Note : You do not need to create your own ArrayList class . Please make sure you are using the in-built ArrayList data structure in this assignment. This is a requirement. The program should have exactly one class

To Submit

  1. Avengers.java
  2. 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 ts = new ArrayList0; 3) Every time you encounter a limestamp, you first check to see if 1000 secs has passed since the earliest timestamp in your list. If yes, then the corresponding avenger has fought off the earliest enemy in your list and has a "slot open" to now fight the current enemy. 4) If 1000 secs have NOT passed since the earliest, then do one of the following a. If the latest avenger is capable of simultaneously fighting more enemics than he is currently fighting, then assign the current enemy to one of the "empty slots" b. If all avengers are at full capacity, then introduce a new avenger, increment minimum number of avengers needed by 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

Draw and explain the operation of LVDT for pressure measurement

Answered: 1 week ago