In: Computer Science
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