CS 101

advertisement
CS 101
LAB 11 Tuesday
Habitat Design
In this system, you store the dog type, current energy, number of feed, required daily
energy, required hunting energy amount and energy amount of a prey for each members
of dog family
The class hierarchy should be as follows:
Dog_Family
Fox
Wolf
Rufus
Note: The goal for the problem is avoid the duplicating code between three types of
dog Any duplication of code will be penalized by 20 points.
After generating the Dog_family class and its subclasses you are going to code the tester
program.
A Dog_Familty should carry the following behaviors:
DOG_FAMILY BEHAVIORS:
Dog_Family(String type, double daily_energy, double hunting_energy, double
prey_energy ) // (constructor) initializes the object and sets the required daily energy,
required hunting energy amount and energy amount of a prey . (5p)
void breed(double amount) // adds the amount of food to the energy and increments
the feed number (5p)
void hunt() // if the required amount of energy for hunting is less than or equal to the
total energy , then this method subtracts the amount from total energy, adds gathered
energy to total energy, otherwise set alive status false. (5p)
void dailyEnergy() // decreases total energy, in case of total energy is less than 0, set
alive status false (5p)
void endofLifeReport() // displays the its type and total number of feed number . Use
formatted output for the energy such as using printf (5p)
MEMBERS of DOG FAMILY:
Hint_1: The default constructor for each subclass should invoke the super class’
constructor to set the dog type to “Fox”, “Wolf”, “Rufus” or “Adistus”.
Hint_2: Use the Random class to test the probability:
Random rnd = new Random();
if (rnd.nextInt(100)>=60) // 0.4 probability case
Fox : There is 10 kcal required daily energy, 10 kcal required energy for hunting and a
prey has 50 kcal energy of this kind.. There is 0.5 probability for an attempt to hunt will
be successful. (15p).
Hint_3 : Consider overriding
Refer : Hint_2
Wolf : There is 20 kcal required daily energy, 70 kcal required energy for hunting and a
prey has 100 kcal energy of this kind.. There is 0.4 probability for an attempt to hunt will
be successful. (15p)
Refer : Hint_2, Hint_3
Rufus ; There is 20 kcal required daily energy, 70 kcal required energy for hunting and a
prey has 150 kcal energy of this kind.. There is 0.4 probability for an attempt to hunt will
be successful.. (15p)
Refer : Hint_3
Caution: The ancestor of Rufus is Wolf
TEST THE HABITAT:
-Write a test program that initializes each type of dog with initially 100 kcal energy (5p).
-Implement one month transaction (assume 30 days per month) and make an attempt for
hunting for each day. (10p)
- At the end of the month, for the dogs which has a status not alive, report the total
number of feed. (5p)
Hint_4 : Consider endofLifeReport() method
CORRECT RUN = 10 points
//
Dog_Family
public class Dog_Family {
private String type;
private double energy = 0;
private int feed_number = 0;
private double hunting_energy, daily_energy,prey_energy;
private boolean alive = true;
public Dog_Family()
{ type = "" ;
}
public Dog_Family(String type,double daily_energy,
double hunting_energy, double prey_energy){
setType(type);
setDaily_energy(daily_energy);
setHunting_energy(hunting_energy);
setPrey_energy(prey_energy);
}
public void endofLifeReport(){
System.out.println("Type " + type + " Feed number " +
feed_number);
}
public void dailyEnergy(){
if (energy>daily_energy)
energy -= daily_energy;
else{
energy = 0;
setAlive(false);
}
}
public void breed(double amount){
energy += amount;
feed_number++;
}
public void hunt(){
if (energy>=hunting_energy) {
energy -= hunting_energy;
breed(prey_energy);
}
else{
energy =0;
setAlive(false);
}
}
public double getEnergy() {
return energy;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getTransactions() {
return feed_number;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public int getFeed_number() {
return feed_number;
}
public void setDaily_energy(double daily_energy) {
this.daily_energy = daily_energy;
}
public void setHunting_energy(double hunting_energy) {
this.hunting_energy = hunting_energy;
}
public void setPrey_energy(double prey_energy) {
this.prey_energy = prey_energy;
}
}
//
Wolf
import java.util.Random;
public class Wolf
extends Dog_Family{
public Wolf(String type,double daily_energy,
double hunting_energy, double prey_energy){
super(type,daily_energy,hunting_energy,prey_energy);
}
public void hunt(){
Random rnd = new Random();
if (rnd.nextInt(100)>=60)
super.hunt();
}
}
//
Fox
import java.util.Random;
public class Fox extends Dog_Family{
public Fox(String type,double daily_energy,
double hunting_energy, double prey_energy){
super(type,daily_energy,hunting_energy,prey_energy);
}
public void hunt(){
Random rnd = new Random();
if (rnd.nextInt(100)>=50)
super.hunt();
}
}
//
Rufus
import java.util.Random;
public class Rufus extends Wolf{
public Rufus(String type,double daily_energy,
double hunting_energy, double prey_energy){
super(type,daily_energy,hunting_energy,prey_energy);
}
public void hunt(){
super.hunt();
}
}
// TESTER
public class Tester {
public static void main(String[] args){
Fox f = new Fox("Fox",10,10,50);
Wolf w = new Wolf("Wolf",20,70,100);
Rufus r = new Rufus("Rufus",20,70,150);
r.breed(100);
f.breed(100);
w.breed(100);
for (int i = 0; i < 30; i++) {
if (f.isAlive())
{
f.hunt();
f.dailyEnergy();
}
if (r.isAlive())
{
r.hunt();
r.dailyEnergy();
}
if (w.isAlive())
{
w.hunt();
w.dailyEnergy();
}
}
if (!f.isAlive())
{
f.endofLifeReport();
}
if (!r.isAlive())
{
r.endofLifeReport();
}
if (!w.isAlive())
{
w.endofLifeReport();
}
}
}
Download