Ask Programming Language Expert

I have written the program for the below assignment, but I am not getting the correct output. Below is the assignment and the program I have written along with the output I get. Please help me troubleshoot!

Write a program that simulates the battle between a cat and mice.

Use this class hierarchy:

Cat

· Kills 1 mouse a day

· Does not reproduce

Mice

· Have a chance to reproduce as long as conditions are met

· Reproduction only happens when mice are over 1 and 1 of each sex is present

Simulation Control

· Simulation continues as long as population is greater than 1 and less than 10

Driver main method should be as shown below: (replacing comment with missing piece)

import java.util.ArrayList;

public class LastFirstWeek5CatMouse
{
public static void main(String [] args)
{

Cat sylvester = new Cat();
ArrayList mice = new ArrayList();
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);
}
//INCLUDE CODE FOR OUTPUT HERE.
}
}

Output code should output:

Depending on if the population of mice is greater than or equal 10:

Mice RULE, Cats Drool Mice Population: ## (integer value)
or
Cats RULE, Mice Drool Cat Weight (in mice): ##.## (double value, 2 decimal places)

Output should output results 10 times. Modification of or before while loop may be required. See sample output below.

Mammal.java class

Instance variables:

name (string)
age (integer)
weight (double)
isMale (Boolean)

Mammal constructor : (default constructor)

Set age to 1.

grow method :

Increases age of mammal by 1.

Accessor / mutator methods for each instance variable above:

Set or returns values as appropriate for data type specified.

Cat.java class

eat method: (receive mouse arraylist as argument)

Randomly removes a mouse from the population 70% of the time and increases cat weight by the chosen mouse weight. Only increase weight if mouse is removed/eaten. (See chapter 5, lottery example, for random example)


grow method:

Set the cats age to the current age plus 1. (use accessor/mutator methods)

Mouse.java class

Mouse constructor: (default constructor)

Randomly choose sex and assign to isMale as appropriate.

Set age to 1.

Set weight to 1.

grow method:

Increase age of mouse by 1 and weight of mouse by 1% of current weight.

mate method: (static method, receive mouse arraylist as argument)

Randomly choose 2 mice objects from arraylist and if conditions are correct, proceed with mating.

Successful mating conditions are:

· 1 male, 1 female mouse

· Both mice older than 1 day

If successful mating, randomly create between 0-4 mice and append to arraylist received as argument.

Sample session (requires no user input):

Mice RULE, Cats Drool Mice Population: 11
Cats RULE, Mice Drool Cat Weight (in mice): 2.03
Mice RULE, Cats Drool Mice Population: 10
Cats RULE, Mice Drool Cat Weight (in mice): 2.05

Press any key to continue . . .

This is what I have.

/**Mammal.java**/

public class Mammal
{
private String name; //name of the mammal
private int age; //age of mammal
private double weight; //weight of the mammal
private boolean isMale; //sex of the mammal

public Mammal()
{
age=1;
}

public void grow()
{
age++;
}

public void setName(String n)
{
name=n;
}

public void setAge(int a)
{
age=a;
}

public void setWeight(double w)
{
weight=w;
}

public void setSex(boolean value)
{
isMale=value;
}

public String getName()
{
return name;
}

public int getAge()
{
return age;
}

public double getWeight()
{
return weight;
}

public boolean getSex()
{
return isMale;
}
}//end of class

/**Cat.java**/

import java.util.ArrayList;
import java.util.Random;

public class Cat extends Mammal
{
Random r;

public Cat()
{
}

public void eat(ArrayListmice)
{
r=new Random();
Mouse m=mice.get(r.nextInt(mice.size()));
this.setAge(this.getAge()+m.getAge());
mice.remove(m);
}

public void grow()
{
super.grow();
}
}//end of class

/**Mouse.java**/

import java.util.Random;
import java.util.ArrayList;

public class Mouse extends Mammal
{
public static Random r; //creates a new instance of Mouse

public Mouse()
{
r=new Random();
this.setSex(r.nextBoolean());
this.setAge(1);
this.setWeight(1.0);
}

public void grow()
{
super.grow();
double weight=this.getWeight();
weight += weight * 0.01;
this.setWeight(weight);
}

public static void mate(ArrayList mice)
{
Mouse m1=mice.get(r.nextInt(mice.size()));
Mouse m2=mice.get(r.nextInt(mice.size()));

if((m1.getAge()>=1) && (m2.getAge()>=1))
{
int num = r.nextInt(5);
for(int i=0;i mice.add(new Mouse());
}
}
}//end of class

/**FirstLastWeek5CatMouse.java**/

import java.util.ArrayList;
import java.util.Scanner;

public class FirstLastWeek5CatMouse
{
public static void main(String [] args)
{
Cat sylvester = new Cat();
ArrayListmice = new ArrayList();
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);
Scanner scan=new Scanner(System.in);

while (mice.size()>1 && mice.size()< 10)
{
for (Mouse m:mice)
m.grow();
sylvester.grow();
Mouse.mate(mice);
sylvester.eat(mice);
System.out.println("Mice RULE, Cats Drool Mice Population: " + mice.size());
System.out.println("Cats RULE, Mice Drool Cat Weight(in mice): " + sylvester.getWeight());
System.out.println("");

}
}
}//end of class

My Ouput is as Follows:

Mice RULE, Cats Drool Mice Population: 4
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 5
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 5
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 4
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 3
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 3
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 5
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 7
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 9
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Mice RULE, Cats Drool Mice Population: 11
Cats RULE, Mice Drool Cat Weight(in mice): 0.0

Press any key to continue . . .

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9901499

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As