In: Computer Science
In Java please. I put down my code and what I was able to achieve so far:
public class Animal {
private String gender; //stores the gender of the
animal
private String type; //stores the type of the
animal(bear of fish)
private int strength; //stores the strength of the
animal
public Animal() {
gender = "none";
type = "none";
strength = 0;
}
public Animal (String g, String t, int s)
{
g = gender;
t = type;
s = strength;
}
public boolean setGender() {
Random ranGen = new Random();
boolean g =
ranGen.nextBoolean();
if (g == true)
gender =
"Male";
else
gender =
"Female";
return g;
}
public String getGender() {
return gender;
}
public boolean setType() {
Random ranGen = new Random();
boolean t =
ranGen.nextBoolean();
if (t == true)
type =
"Bear";
else
type =
"Fish";
return t;
}
public String getType() {
return type;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getStrength() {
return strength;
}
public boolean compareGender(Animal animal) {
if(this.getGender().equals(animal.getGender()))
{
return
true;
}
else {
return
false;
}
}
public boolean compareType(Animal animal) {
if(this.getClass().equals(animal.getClass())){
return true;
}
else{
return
false;
}
}
public boolean compareStrength(Animal animal) {
if(this.getStrength() ==
(animal.getStrength())){
return true;
}
else{
return
false;
}
}
public void increaseStrength() {
this.setStrength(this.getStrength()
+ 4);
}
public String toString() {
String str = new String ("The
animal: " + type + " + gender + strength);
return str;
}
}
public class River {
private Animal[] river;
private int numAnimals;
public River() throws NumberFormatException{
numAnimals = 0;
Random randNum = new Random();
int num = randNum.nextInt(20);
river = new Animal[num];
while(numAnimals < river.length/2)
{
int place =
randNum.nextInt(river.length);
if(river[place] == null)
{
river[place] =
new Animal();
numAnimals++;
}
}
}
public River(Animal[] size) throws
NumberFormatException{
setRiver(size);
}
public void setRiver(Animal[] s) throws
NumberFormatException
{
river= s;
if (s.length
>= 10 && s.length <= 20)
river = s;
else
throw new
NumberFormatException("Invalid Number: number out of range");
}
public Animal[] getRiver() {
return river;
}
public void play() {
Random randNum = new
Random();
int moviment;
for(int i=0; i<river.length;
i++) {
if(river[i] !=
null) {
moviment = randNum.nextInt();
if(moviment == 1) {
}
}
}
So my problem is on the play method. I need to iterate through the array.
If I find an animal I need to randomly decide if the animal id going to move back, forward or stay in place.
If the animal is moving back I need to make sure that he is not at the beginning of the array.
If the animal is moving forward I need to make sure that he is not at the last position of the array.
If the animal stays in place nothing needs to be done.
If the animal moves back/forward the index position needs to be checked if to see if its empty.
If is empty the animal can just move there. If is not the animal type needs to be checked.
If they are of the same type its gender needs to be checked. Same gender produces a baby, different gender they fight and the weaker animal dies (meaning it will be erased from the array).
If the animals are from different types one of the animals is going to be eaten.
**I really do not know how to do this method, I tried to start but I do not know how to move forward, so I would really appreciate if you put comments through the code. Also, can you please check if the parameterized constructor is right?
Thank you so much
import java.io.*;
import java.util.*;
class Animal
{
private String gender; //stores the gender of the animal
private String type; //stores the type of the animal(bear of
fish)
private int strength; //stores the strength of the animal
public Animal() {
this.gender = "none";
this.type = "none";
this.strength = 0;
}
public Animal (String g, String t, int s) {
this.gender = g;
this.type = t;
this.strength = s;
}
public boolean setGender() {
Random ranGen = new Random();
boolean g = ranGen.nextBoolean();
if (g == true)
this.gender = "Male";
else
this.gender = "Female";
return g;
}
public String getGender() {
return this.gender;
}
public boolean setType() {
Random ranGen = new Random();
boolean t = ranGen.nextBoolean();
if (t == true)
this.type = "Bear";
else
this.type = "Fish";
return t;
}
public String getType() {
return this.type;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getStrength() {
return this.strength;
}
public boolean compareGender(Animal animal) {
if(this.getGender().equals(animal.getGender()))
return true;
else
return false;
}
public boolean compareType(Animal animal) {
if(this.getClass().equals(animal.getClass()))
return true;
else
return false;
}
public boolean compareStrength(Animal animal)
{
if(this.getStrength() == (animal.getStrength()))
return true;
else
return false;
}
public void increaseStrength() {
this.setStrength(this.getStrength() + 4);
}
public String toString() {
String str = "The animal: " + type + " "+ gender+ " " +
strength;
return str;
}
}
public class River extends Animal{
private Animal[] river;
private int numAnimals;
public River() throws NumberFormatException {
numAnimals = 0;
Random randNum = new Random();
int num = randNum.nextInt(20);
river = new Animal[num];
while(numAnimals < river.length/2)
{
int place = randNum.nextInt(river.length);
if(river[place] == null)
{
river[place] = new Animal();
numAnimals++;
}
}
}
public River(Animal[] size) throws NumberFormatException
{
setRiver(size);
}
public void setRiver(Animal[] s) throws
NumberFormatException
{
river = s;
if (s.length >= 10 && s.length <= 20)
river = s;
else
throw new NumberFormatException("Invalid Number: number out of
range");
}
public Animal[] getRiver() {
return river;
}
public void play() {
Random randNum = new Random();
int moviment = 0;
int length = river.length;
int index = 0;//New place where animal will move
for(int i=0; i<length; i++)
{
Animal animal2 = river[i];
if(animal2 != null && animal2 instanceof Animal)
{
//Will generate random nos between 0 to 2
moviment = randNum.nextInt(3);
//Assume if moviment == 0, animal will move back
if(moviment == 0)
{
index = i-1;
if(index <= 0)
index = length - 1;
}
//Assume if moviment == 1, animal will move forward
else if(moviment == 1)
{
index = i+1;
if(index >= length)
index = 0;
}
//Assume if moviment == 2, animal will stay at same place
else if(moviment == 2)
{
index = i;
}
Animal animal1 = river[index];
if(animal1 == null)
{
river[index] = animal2;
river[i] = null;
}
else
{
//Animal type is same
if(animal1.compareType(animal2))
{
//Animal gender is same, will produce baby
if(animal1.compareGender(animal2))
{
//Please addw what needs to be done for this
}
else
{
//animal1's strength is high, so animal2 earased from array
if(animal1.getStrength() > animal2.getStrength())
river[i] = null;
else //animal2's strength is high, so animal1 earased from
array
river[index] = null;
}
}
else //Animal type is different
{
//animal1's strength is high, so animal2 earased from array
if(animal1.getStrength() > animal2.getStrength())
river[i] = null;
else //animal2's strength is high, so animal1 earased from
array
river[index] = null;
}
}
}
}
}
public void printArray()
{
System.out.println("\nArray after play function execution
::\n");
for (Animal animal : river)
{
System.out.println(animal);
}
}
public static void main(String[] args)
{
Animal[] r = new Animal[11];
r[0] = new Animal("Bear","Male",2);
r[1] = null;
r[2] = null;
r[3] = null;
r[4] = new Animal("Bear","Female",3);
r[5] = null;
r[6] = null;
r[7] = null;
r[8] = null;
r[9] = null;
r[10] = null;
River arr = new River();
arr.setRiver(r);
arr.play();
arr.printArray();
}
}