An intrepid band of vampire hunters has just arrived at a town where a coven of
ID: 3724604 • Letter: A
Question
An intrepid band of vampire hunters has just arrived at a town where a coven of vampires is preying on the townspeople. Your job is to write a function vampire hunt (humans, vampires, hunters) that will simulate how the populations of townspeople and vampires will change over time as the vampire hunters set to work. The town is populated by humans people, is threatened by vampires scary vampires, and is protected by hunters vampire hunters. Each vampire can convert one person a day into a new vampire. (Luckily, the vampire hunters are all immune to vampire bites.) Each vampire hunter can destroy one vampire per day. Since we don't know how many days it might take to reduce one of the populations to 0 (i.e., humans or vampires), we cant use a for-loop to simulate the fight. Instead, we will use a while-loop, which will run until one of the twoExplanation / Answer
public class Vam_Hum_Main {
String compute(int humans, int vampires, int hunters) {
String last = null;
int days=0;
while(humans>0 && vampires >0) { // loop until anyone is greater than zero
days++;
if(vampires-hunters >0) {
vampires = vampires-hunters;
}
else { // if hunters > vampires, then hunters will kill all the vampires. so setting vampires to 0.
vampires = 0;
break;
}
if(humans-vampires >0) {
humans=humans-vampires;
vampires=vampires*2;
}
else {
vampires = humans+vampires;
humans=0;
break;
}
}
last = "["+humans+","+vampires+"]";
return last;
}
public static void main(String[] args) {
Vam_Hum_Main vhm = new Vam_Hum_Main();
String s = vhm.compute(61, 12, 7); // pass any set of values here
System.out.println("Remaining is "+s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.