Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an application that determines the outcome of a fight between a horse-sized duck and a duck-sized horse. The fight occurs among random wind and

Write an application 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 animal differently.

Both HorseSizedDuck and DuckSizedHorse extend the abstract class Animal. Here is some of the code for Animal. IMPORTANT: use the package declaration shown in the code below; this will make it much easier for me to download your code into Eclipse for grading.

package lab1;

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

}

Part A, 25 points: complete the code for Animal.

Part B:, 35 points: extend Animal with the two concrete classes HorseSizedDuck and DuckSizedHorse.

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 = 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 = 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, 40 points

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.121586 DuckSizedHorse named Mr. Ed created with power = 0.2 and accuracy = 0.100000 Fight begins with wind level 0.878 and rain level 0.651 Daffy is attacking Mr. Ed Daffy's attack missed Mr. Ed is attacking Daffy Mr. Ed's attack missed Daffy is attacking Mr. Ed 0.10 damage taken

[many lines omitted]

Mr. Ed is attacking Daffy Mr. Ed's attack missed Daffy is attacking Mr. Ed 0.10 damage taken Mr. Ed has died Duck wins!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions