How do i write a method in java that will represent an animal or prey object as
ID: 3531963 • Letter: H
Question
How do i write a method in java that will represent an animal or prey object as if it is running away or playing dead
each method will have a percentage for returning a false value.
examples of these methods where i need to add code:
public boolean chasePrey(Prey p) {
//program to chase prey p(specifically for tiger)
// tiger gets an 80% chance of catching prey successfully
System.out.println(strName + "chases" + p.getName());
return false;
}
public boolean runAway() {
//program to run away(Specifically for human)
//human gets a 30% chance to run away successfully
return false;
}
Explanation / Answer
public boolean chasePrey(Prey p) {
//program to chase prey p(specifically for tiger)
// tiger gets an 80% chance of catching prey successfully
double prob = Math.random()*100;
System.out.println(strName + " chases " + p.getName());
if(prob < 80)
{
System.out.println(strName + " caught " + p.getName());
return true;
}
else
{
System.out.println(p.getName() + " ran away from " +strName);
return false;
}
}
public boolean runAway() {
//program to run away(Specifically for human)
//human gets a 30% chance to run away successfully
double prob = Math.random()*100;
System.out.println(strName + " is running ");
if(prob < 30)
{
System.out.println(strName + " ran away ");
return true;
}
else
{
System.out.println(strName+" got caught ");
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.