Here is the driver:
import java.util.ArrayList;
public class KrahnkeStephanieWeek5CatMouse
{
public static void main(String [] args)
{
Cat sylvester = new Cat();
ArrayList<Mouse> mice = new ArrayList<Mouse>();
mice.add(new Mouse());
mice.add(new Mouse());
mice.add(new Mouse());
mice.get(0).setSex(true);
mice.get(1).setSex(false);
mice.get(2).setSex(false);
while (mice.size() > 1 && mice.size() < 10)
{
for (Mouse m:mice)
{
m.grow();
sylvester.grow();
Mouse.mate(mice);
sylvester.eat(mice);
} // end for
} // end while
for (int i = 0; i<10; i++)
{
if (mice.size() >= 10)
{
System.out.println("Mice RULE, Cats Drool Mice Population: " + mice.size());
} // end if
else
{
System.out.println("Cats RULE, Mice Drool Cat Weight (in mice): %.2f" + sylvester.getWeight());
} // end else
} // end for
System.out.println("Press any key to continue...");
}
}
Here is the Mammal class:
public class Mammal
{
private String name = "";
private int age = 0;
private double weight = 0.0;
private boolean isMale = false;
//public Mammal()
// {} // end mammal()
public Mammal()
{
age = 1;
} // end mammal()
public void grow()
{
this.age = age++;
} // end grow
public void setName(String name)
{
this.name = name;
} // end setName
public void setAge(int age)
{
this.age = age;
} // end setAge
public void setWeight(double weight)
{
this.weight = weight;
} // end setWeight
public void setIsMale(boolean isMale)
{
this.isMale = isMale;
} // end setIsMale
public String getName()
{
return name;
} // end getName
public int getAge()
{
return age;
} // end getAge
public double getWeight()
{
return weight;
} // end getWeight
public boolean getIsMale()
{
return isMale;
} // end getIsMale
} // end Mammal
Here is the Cat class:
import java.util.Random;
import java.util.ArrayList;
public class Cat extends Mammal
{
Random random = new Random();
public void eat(ArrayList<Mouse> mice)
{
double catWeight;
double mouseWeight;
int deadMouse;
int percentage = (int) (Math.random() * 100);
if (percentage >= 70)
{
deadMouse = (int) (random.nextInt(mice.size()));
catWeight = this.getWeight();
mouseWeight = (mice.get(deadMouse)).getWeight();
this.setWeight(catWeight + mouseWeight);
} // end if
} // end eat
public void grow()
{
int age = this.getAge();
this.setAge(age++);
} // end grow
} // end Cat
and Here is the Mouse class:
import java.util.ArrayList;
import java.util.Random;
public class Mouse extends Mammal
{
boolean sex;
public Mouse()
{
this.setIsMale(sex);
this.setAge(1);
this.setWeight(1);
} // end mouse constructor
public void grow()
{
int mouseAge = this.getAge();
double mouseWeight = this.getWeight();
this.setAge(mouseAge++);
this.setWeight(mouseWeight + (mouseWeight * 0.01));
} // end grow
public static void mate(ArrayList<Mouse> mice)
{
Random random2 = new Random();
int mouse1 = random2.nextInt(mice.size());
int mouse2 = random2.nextInt(mice.size());
if ((mice.get(mouse1)).getIsMale() == true && (mice.get(mouse2)).getIsMale() == false && (mice.get(mouse1)).getAge() > 1 && (mice.get(mouse2)).getAge() > 1)
{
int randomBabyAmount = random2.nextInt(4);
for (int i = 0; i < randomBabyAmount; i++)
{
mice.add(new Mouse());
} // end for
} // end if
} // end mate
public void setSex(boolean sex)
{
this.setIsMale(sex);
} // end setSex
} // end Mouse

New Topic/Question
Reply


MultiQuote





|