1

I'm super new to coding in java.

The purpose of this code is to add animal objects to an arraylist (river). Each animal object contains a string (what animal it is), a boolean gender, and a float strength. In the arraylist river, each animal either attempts to move into an adjacent array cell or stay where it is, depending on the kind of animal, its gender, and strength. My animal objects are either bears, fish, or null.

After my arraylist is made, I have it shuffled so that the outcome of this "game" is random.

My problem is that when I make a for loop to access the elements in my arraylist, I have my conditionals that look for the kind of animal, the gender, and the strength. How can I access these specific elements of these objects? I made a getType method, but I get an error saying that it cannot find symbol .getType().

import java.util.ArrayList;
import java.util.Collections;
import java.util.*;

public class Animal<First, Second, Third>{
    private String type;
    private boolean gender;
    private float strength;

    public Animal(String type, boolean gender, float strength){
        this.type = type;
        this.gender = gender;
        this.strength = strength;
    }

    public String toString() {
        return "("+this.type+", "+this.gender+", "+this.strength+")";
    }

    public String getType(){
        return type;
    }

    public Boolean getGender(){
        return gender;
    }

    public Float getStrength(){
        return strength;
    }

    public static void main(String[] args) {
        ArrayList river=new ArrayList();
        Animal<String, Boolean, Float> b1 = new Animal<String, Boolean, Float>("bear", true, 15.6f);       //true is male
        Animal<String, Boolean, Float> b2 = new Animal<String, Boolean, Float>("bear", true, 13.4f);
        Animal<String, Boolean, Float> b3 = new Animal<String, Boolean, Float>("bear", false, 12.5f);   //false is female
        Animal<String, Boolean, Float> b4 = new Animal<String, Boolean, Float>("bear", false, 10.7f);
        Animal<String, Boolean, Float> f1 = new Animal<String, Boolean, Float>("fish", true, 5.6f);
        Animal<String, Boolean, Float> f2 = new Animal<String, Boolean, Float>("fish", true, 3.4f);
        Animal<String, Boolean, Float> f3 = new Animal<String, Boolean, Float>("fish", false, 2.5f);
        Animal<String, Boolean, Float> f4 = new Animal<String, Boolean, Float>("fish", false, 0.7f);
        Animal<String, Boolean, Float> n1 = new Animal<String, Boolean, Float>("null", false, 0f);
        river.add(b1);
        river.add(b2);
        river.add(b3);
        river.add(b4);
        river.add(f1);
        river.add(f2);
        river.add(f3);
        river.add(f4);
        river.add(n1);
        river.add(n1);
        river.add(b1);
        river.add(b2);
        river.add(b3);
        river.add(b4);
        river.add(f1);
        river.add(f2);
        river.add(f3);
        river.add(f4);
        river.add(n1);
        river.add(n1);
        Collections.shuffle(river);
        System.out.println("Before : "+river);
        for(int i=0; i<river.size()-1;i++){
            Object h = river.get(i);
            Object h2 = river.get(i+1);
            if(!h.getType().equals(h2.getType())){
                if(h.getType().equals("bear")&&h2.getType().equals("fish")){
                    river.remove(i+1);
                    river.add(i,n1);
                }
            }
        }
    }
}
1
  • 3
    As an aside, you don't need the generic types <First, Second, Third> here. They're never used. Commented Mar 4, 2017 at 20:45

2 Answers 2

1

The type of h and h2 is Object, which doesn't have a getType() method. You'll need to either cast them to Animal when you get them from the list

Animal h = (Animal) river.get(i);
Animal h2 = (Animal) river.get(i+1);

Or better yet, use generics on the list.

ArrayList<Animal> river = new ArrayList<Animal>();

// snip...

Animal h = river.get(i);
Animal h2 = river.get(i+1);
Sign up to request clarification or add additional context in comments.

Comments

0
 Object h = river.get(i);
            Object h2 = river.get(i+1);

Object does not have getType. Animals do. You need to cast it to Animal. Or make river ArrayList

Edit

Casting:

Animal h2 = (Animal)river.get(i+1);

1 Comment

would be appreciated if you could provide that to him/her. Example of casting from Object to type Animal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.