Question: Write an application in java that determines the outcome of a fight between a horse-sized duck and a duck-sized horse. The fight occurs among random
Write an application in java that determines the outcome of a fight between a horse-sized duck and a duck-sized horse. The fight occurs among random wind and rain conditions, which affect the two types of Animals differently.
Both HorseSizedDuck and DuckSizedHorse extend the abstract class Animal. Here is some of the code for Animal:
package lab
import java.util.Random;
public abstract class Animal {
Random r = new Random();
double health = 1.0; // if health reaches 0, the Animal dies
double accuracy; // probability (0 to 1) that an attack by this Animal hits its target.
double power; // amount of damage (0 to 1) done by a successful attack by this Animal
String name;
public boolean isAlive(){
// write code here to return true if the Animal's health is a positive number, otherwise false
}
// receiveInjury() is run when this animal is the victim of a successful attack
public void receiveInjury(double d){
// write code here to do the following:
// reduce this Animal's health by d, the parameter
// print a message about the damage
// print a message if the Animal is now dead
// otherwise, print a message showing the Animal's current health
}
public void attack(Animal target){
// write code here to do the following:
// print a message indicating whom this Animal is attacking (name of the other Animal)
// get a random double between 0 and 1
// if the random double is less than accuracy, the attack succeeds. In that case,
// call receiveInjury(power) on target. The argument is *this* Animal's power.
// otherwise, the attack fails. Print a message indicating that the attack missed.
}
// code a getName() here
}
HorseSizedDucks attack with the blunt force of their beaks, which have power of 0.1. However, because of their feathers and comparatively light weight, the accuracy of their attacks is reduced by wind. A HorseSizedDuck's accuracy is found with the formula accuracy = 0.1/wind.
DuckSizedHorses have accuracy of 0.1 with their attacks, which consist of kicks from their strong but small legs. Their tendency to slip in mud when their kicks connect weakens their attacks during rainy conditions. Their power is found by the formula power = 0.1/rain.
Each type of Animal needs a constructor and a toString(). Since the weather conditions do not change during the fight, the wind or rain value can be taken as a parameter in the constructor.
Part C
Write Fight, the driver class. Fight contains a main() which does the following:
Instantiates a Random and gets two random doubles, each between 0 and 1. One random number represents the wind conditions and the other represents the rain conditions.
Instantiates a DuckSizedHorse and a HorseSizedDuck and prints the output of each one's toString()
Print a message indicating the beginning of the fight and the current wind and rain conditions
As long as both Animals are alive, each attacks the other. Make sure no dead Animal either attacks or is attacked. When only one Animal remains, print a message showing that it is the victor.
Here is some of the output from my solution:
HorseSizedDuck named Daffy created with power = 0.1 and accuracy = 0.16
DuckSizedHorse named Mr. Ed created with power = 0.15 and accuracy = 0.1
Fight begins with wind level 0.62 and rain level 0.63
Daffy attacks Mr. Ed but misses!
Mr. Ed attacks Daffy but misses!
Daffy attacks Mr. Ed which suffers 0.1 damage
Mr. Ed remaining health = 0.9
Mr. Ed attacks Daffy but misses!
many lines omitted
Mr. Ed attacks Daffy which suffers 0.16 damage
Daffy remaining health = 0.06
Daffy attacks Mr. Ed but misses!
Mr. Ed attacks Daffy but misses!
Daffy attacks Mr. Ed which suffers 0.1 damage
Mr. Ed dies!
Daffy wins!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
