Write a program that simulates battle between a cat and mice

Assignment Help Programming Languages
Reference no: EM13322197

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<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);
}
//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
<output should output results 10 times then stop>
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(ArrayList<Mouse>mice)
{
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<Mouse> 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<num;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();
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);
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 . . .

Reference no: EM13322197

Questions Cloud

What are specifications need to be specified for waterways : what are the specifications need to be specified while creating a design for waterways. what are their uses. how are they maintained.
Objective of demand strategies in comparison to supply : Discuss the major objective of demand strategies in comparison to supply strategies.
Calculate the viscous drag force on the plate : Calculate the viscous drag force on this plate, assuming boundary-layer flow with free-stream velocity of 15 ft/s and plate dimensions of L = 8 ft and W = 4 ft.
Fire detection system by security safety : Kuznicki made a contract for the installation of a fire detection system by Security Safety Corp. for $498. The contract was made one night and canceled at 9:00 the next morning.
Write a program that simulates battle between a cat and mice : 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.
What is length of consecutive wet days that has a occurence : Assuming that the number of days until the next dry day is geometrically distributed, what is the length of consecutive wet days that has a probability of occurence of no more than 0.05
What is probability that one can travel to city three : There are two ways to get from City One to City Three, (a) directly or (b) via City Two. Let A be the event that the road from City One to Three is open. Similarly, B is for City One to Two and C is for City Two to Three.
How to program print a one-year reminder list : Have the program print an error message and ignore a reminder if the corrsponding day is negative or larger than 31 (using the continue statement)
How hard and what direction must you pull on the third rope : One of your friends pulls on a rope with 3 units of force and other pulls on a second rope with 5 units of force, How hard and what direction must you pull

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd