Question

In: Computer Science

Below is a school problem of mine. WHAT I KNOW AND HAVE DONE. i have three...

Below is a school problem of mine.
WHAT I KNOW AND HAVE DONE.
i have three variables for input
n for nunber of wnemies
k for fight capacity

an arraylist set to the size of n because it only needs to be as large as the number of enemies coming.

and x which is just the time stamps that will go into the arraylist.

i also have tHe array sorted from least to greatest because it doesnt matter what order they enter the times at but the time of the oppenets matters.

HELP NEEDED HERE.
I need help on how to handle the conditions of the program.
i know that it takes 1000secs to defeat an enemy. if an enemy appears and it is before 1000secs and less than an avengers capacity i need to add an avenger.
im having trouble on how to implent this into my program.


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.

Please make sure you are using an ArrayList<Integer> in this assignment. This is a requirement.
he timestamps are in chronological order. i.e. they will be entered by the user in increasing order.
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 t=300, defeats the first enemy at t=1300, can thus fight

the next enemy at t=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, 2nd enemy arrives, A1 can handle 2

enemies at once (see value of K), so still just 1 avenger needed. Next enemy arrives at t=499,

which is less than (500+1000), which means A1 is still fighting off 2 enemies at t=1499. Hence a

2nd 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 200

A1 A1

earliest

100 200 345

A1 A1 A1

earliest

100 200 345 980

A1 A1 A1 A2

earliest

1123 200 345 980

A1 A1 A1 A2

earliest

1123 1242 345 980

A1 A1 A1 A2

earliest

1123 1242 1466 980

A1 A1 A1 A2

earliest

1123 1242 1466 1777

A1 A1 A1 A2

earliest

1123 1242 1466 1777 1900

A1 A1 A1 A2 A2

earliest

1123 1242 1466 1777 1900 2000

A1 A1 A1 A2 A2 A2

earliest

1123 1242 1466 1777 1900 2000 2000

A1 A1 A1 A2 A2 A2 A3

earliest

1123 1242 1466 1777 1900 2000 2000 2001

A1 A1 A1 A2 A2 A2 A3 A3

earliest

2500 1242 1466 1777 1900 2000 2000 2001

A1 A1 A1 A2 A2 A2 A3 A3

earliest

2500 3000 1466 1777 1900 2000 2000 2001

A1 A1 A1 A2 A2 A2 A3 A3

Approach

1) Each timestamp represents the time at which an enemy arrives.

2) You can represent the above data structure using an arraylist.

ArrayList<Integer> arr = new ArrayList<Integer>();

3) Every time you encounter a timestamp, 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 enemies 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

Solutions

Expert Solution

here is the solution i have tried

and came up witha solution in java

If you find any issues or errors you can ask me here

import java.util.Scanner;
import java.util.ArrayList;


public class HelloWorld {
    

        public static void main(String[] args) {
                // TODO Auto-generated method stub

                Avengers_Info in = new Avengers_Info(); 
                System.out.println("Enter the number of enemies: ");
                
                Scanner input = new Scanner(System.in);
                int enemies = input.nextInt() ;
                
            System.out.println("Enter fighting capcity of each avenger : ");
                
            int fightCap = input.nextInt();
            fightCap = fightCap * 1000;
            
            System.out.println("Enter the time where the enemies are coming: " + " (Type a negative number to stop)");
         
            
          int enterTime = 0;
           do { 
                enterTime = input.nextInt();
           } while (enterTime > 0);
           
           
                
           //section 2
           in.setTimeOfArrival(enterTime);;
           in.setEnemies(enemies);
           in.setFightCap(fightCap);
           
              
           //Prints out statement
           
          System.out.println(in.AvengerNeeded(enterTime, fightCap) + " avenger(s) is needed to fight off the army" ); 
                        
                }
           
        }
         class Avengers_Info {

        ArrayList<Integer> timeOfArrival = new ArrayList <Integer>();
         
        int enemies ; 
        int fightCap;
    int avengers;
        
        public Avengers_Info() {
                
                int enemies = 0; 
                int fightCap = 0;
                int avengers = 0;
                
        }
        
        //---------Setters and Getters-------------
        public int getEnemies() {
                
                return enemies;
        }

        public void setEnemies(int enemies) {
                this.enemies = enemies;
        }

        public int getFightCap() {
                return fightCap;
        }

        public void setFightCap(int fightCap) {
                this.fightCap = fightCap;
        }
        
        public ArrayList<Integer> getTimeOfArrival() {
                return timeOfArrival;
        }

        public void setTimeOfArrival(int enterTime) {
                timeOfArrival.add(enterTime);
        }
        //-----------------------------------------------------


        
        
        //find the number of avengers needed to fight off enemies
        public int AvengerNeeded(int enterTime, int fightCap) {
                
                
                if (fightCap > enterTime ) { 
                 return  avengers++;
                }
                else if (enterTime > fightCap) {
                        return avengers + 2;
                }
                        
                return avengers;
        }
        


        
        
}

ask me for help


Related Solutions

Below is a diffraction problem. I have the solutions to the problem but I don't know...
Below is a diffraction problem. I have the solutions to the problem but I don't know how to arrive at these solutions. A) In an experiment two slits are separated by 0.22mm and illuminated by light of wavelength 640nm. How far must a screen be placed in order for the bright fringes to be separated by 5 mm? (1.72 m) B) A soap film is illuminated by white light normal to its surface. The index of refraction of the film...
Hallstead Jewelers What have we done? Daddy would know what to do, but I don't. I...
Hallstead Jewelers What have we done? Daddy would know what to do, but I don't. I really thought growing this business would be an easy thing for us, but now I am not so sure. All of the work that we did in 2005 was supposed to set us up for new success, profits, and a bright future. But now, we are showing losses on both the historical investment and on our modernization and expansion. Gretchen Reeves was talking in...
I have a problem, and I have the answer but I don't know where the solution...
I have a problem, and I have the answer but I don't know where the solution comes from. ( I have to be able to solve these myself so please help me by answering the questions about the problem.) Here is the answer given to me by the professor: What is the density of SF4 vapor at 650 torr and 100 C? 650 torr (1atm/760 Torr)=0.855atm 100C=373K PV=nRT n/v=P/RT= 0.855atm/0.8206l-atm/molek) (373)=0.0279 mole/l M.W. of SF4=108.1 gm/mole Density=mass/volume 0.0279 mole/l(108.1gm/mole) Answer...
I have done the first part of this problem, but I cannot figure out the second...
I have done the first part of this problem, but I cannot figure out the second part. I know there are several different answers already posted on Chegg for this particular problem with different acceleration and time, but I need this one specifically, with a step by step solution. Again...just PART B....the answer to Part A is 201.72 A helicopter carrying Dr. Evil takes off with a constant upward acceleration of 4.20 m/s2. Secret agent Austin Powers jumps on just...
C++ - Almost done, I have a problem with read access violation error. This is the...
C++ - Almost done, I have a problem with read access violation error. This is the link to my assignment first part: https://pastebin.com/yvcvdqLY second part: https://pastebin.com/vY7MK1Bf, My header file: https://pastebin.com/pcJnctgu My .cpp file: https://pastebin.com/9jAfP9u8 My source file: https://pastebin.com/kFsidY2k
I know the answer for this problem is FALSE, but cannot seem to have a clear...
I know the answer for this problem is FALSE, but cannot seem to have a clear understanding of the answer. Please explain as to why it is FALSE with the problem below: In general, the basis of property to a corporation in a transfer that qualifies as a nontaxable exchange under § 351 is the basis in the hands of the transferor shareholder decreased by the amount of any gain recognized on the transfer
Is there a person that you know, or have done research in that is a Gender...
Is there a person that you know, or have done research in that is a Gender Leader and why?
I need to know the procedures of the experiment below : Separation of a Three-Component Mixture...
I need to know the procedures of the experiment below : Separation of a Three-Component Mixture by Extraction ​ benzoic acid     ethyl 4-hydroxybenzoate diphenyl sulfone A 1.5-gram sample of a 1:1:1 (by weight) mixture of benzoic acid, ethyl 4-hydroxybenzoate and diphenyl sulfone was added to a 125 mL separatory funnel. Isopropyl acetate (20 mL) was added to the separatory funnel and the mixture swirled until all solids had dissolved. The solution was sequentially extracted with saturated aqueous NaHCO3 solution (4...
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
FOR THE SCENARIO BELOW...I need to know if my hypotheses are directional or non-directional.(I already have...
FOR THE SCENARIO BELOW...I need to know if my hypotheses are directional or non-directional.(I already have the hypotheses) I know I need to use a two sample T test but need help with the calculations. (Provide a sample size and critical values in relation to the hypothesis.) -Discuss what the statistical analysis will do in answering the hypotheses and question(s) for the client. Also discuss any potential problems to watch out for, including an appropriate sample size to meet the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT