Question

In: Computer Science

In Java please. I put down my code and what I was able to achieve so...

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

Solutions

Expert Solution

package Game;

import java.util.Random;

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;
   }
}

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;
   }
      
   void ifSameType(int animal)
   {
       int pos = 0;
       for( ; pos < numAnimals; pos++)
           if(river[pos] == null)
               break;
       river[pos] = new Animal();
       river[pos].setGender();
       river[pos].setType();
       river[pos].setStrength(1);
   }
  
   public void play()
   {
       Random randNum = new Random();
       int movement;
       int animal;
       for(int i = 0; i < river.length; i++)
       {
           movement = randNum.nextInt(2);
           animal = randNum.nextInt(river.length);
           if(river[animal] != null)
           {
               System.out.println(river[animal] + " Moves forward.");
               if(movement == 1)
               {
                   if(animal == river.length - 1)
                       System.out.println("Cannot move forward at the end.");
              
                   else if(river[animal + 1] == null)
                   {
                       river[animal + 1] = river[animal];
                       river[animal] = null;
                   }
                   else if(river[animal].getType().compareTo
                           (river[animal + 1].getType()) == 0)
                       ifSameType(animal);
                   else
                       if(river[animal].getStrength() < river[animal + 1].getStrength())
                           river[animal] = null;
                       else
                           river[animal + 1] = null;              
               }
               else
               {
                   System.out.println(river[animal] + " Moves backward.");
                   if(animal == 0)
                       System.out.println("Cannot move backward at the beginning.");
                   else if(river[animal - 1] == null)
                   {
                       river[animal -1] = river[animal];
                       river[animal] = null;
                   }
                   else if(river[animal].getType().compareTo
                           (river[animal - 1].getType()) == 0)
                       ifSameType(animal);
                   else
                       if(river[animal].getStrength() < river[animal - 1].getStrength())
                           river[animal] = null;
                       else
                           river[animal - 1] = null;
               }
           }
           System.out.println(river[i]);
       }
   }

  
   public String toString()
   {
       String result = "";
       for(int c = 0; c < numAnimals; c++)
           if(river[c] != null)
               result += river[c].toString() + "\n";
       return result;
   }
}

public class AnimalFight
{
   public static void main(String []s)
   {
       Random strength = new Random();
       Animal animals[] = new Animal[11];
      
       for(int c = 0; c < animals.length; c++)
       {
           animals[c] = new Animal();
           animals[c].setGender();
           animals[c].setType();
           animals[c].setStrength(strength.nextInt(20));
       }
       River river = new River();
       river.setRiver(animals);
       System.out.println("\n Initial State \n" + river);
      
       river.play();
   }
}

Sample Output:


Initial State
The animal: Bear Male 12
The animal: Fish Female 15
The animal: Bear Female 11
The animal: Fish Male 15
The animal: Bear Female 18
The animal: Fish Female 13

The animal: Bear Male 18 Moves forward.
The animal: Bear Male 18 Moves backward.
The animal: Bear Male 12
The animal: Bear Male 18 Moves forward.
The animal: Fish Female 15
The animal: Fish Female 15 Moves forward.
The animal: Fish Female 15 Moves backward.
The animal: Bear Female 11
The animal: Fish Male 15 Moves forward.
The animal: Fish Male 15 Moves backward.
The animal: Fish Male 15
The animal: Bear Female 18
The animal: Fish Male 16 Moves forward.
The animal: Fish Male 16 Moves backward.
The animal: Fish Female 13
null
The animal: Fish Female 15 Moves forward.
The animal: Fish Female 15 Moves backward.
The animal: Bear Male 18
The animal: Fish Male 16
The animal: Bear Female 10 Moves forward.
Cannot move forward at the end.
null
The animal: Fish Female 15 Moves forward.
The animal: Bear Female 10


Related Solutions

JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
In which folder of XAMPP would I put in my PHP code
In which folder of XAMPP would I put in my PHP code
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT