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);
}
}
}
}
}
<First, Second, Third>here. They're never used.