0

(Apologies if I misused any terms, I'm not the best with vocabulary) I'm trying to write a program that iterates through a list of Person objects and outputs an event from a randomized list. It works fine for events involving only one Person, but I'm having trouble implementing a good method for multiple. I have gotten it to work by manually calling the Event methods from the main file, but that's very clunky and difficult to expand later. I came up with the idea to use the ArrayList and iteration as part of the argument parameters, since that's much cleaner and easier to expand later, but I'm getting a weird error that I don't know exactly how to fix.

Here's the 3 files I'm working with.

import java.util.*;

public class LifeSimulator {
    public static void main(String[] args) {
        String P1Name = "P1";
        String P2Name = "P2";
        String P3Name = "P3";
        String P4Name = "P4";
        String P5Name = "P5";
        String P6Name = "P6";
        ArrayList<Person> pList = new ArrayList<Person>();

        Person P1 = new Person(P1Name);
        Person P2 = new Person(P2Name);
        Person P3 = new Person(P3Name);
        Person P4 = new Person(P4Name);
        Person P5 = new Person(P5Name);
        Person P6 = new Person(P6Name);

        pList.add(P1);
        pList.add(P2);
        pList.add(P3);
        pList.add(P4);
        pList.add(P5);
        pList.add(P6);
for(int s = 1; s <= 3; s++){
        System.out.println("Session 1, Section " + s);
        Collections.shuffle(pList);
        for(int i = 0; i < pList.size(); i++){
            if (pList.get(i).alreadyActed == false){
            pList.get(i).getEvent(pList.get(i), pList, i);
        }
    }

        for(int i = 0; i < pList.size(); i++){
            if(pList.get(i).alive == false){
                System.out.println(pList.get(i).getName() + " - DEAD");
                pList.remove(i);
                i--;
            }else{
                pList.get(i).refresh();
            }
        }
     }
  }
}
import java.util.*;
public class Person {
    private int lives;
    private String name;
    public boolean alive;
    public boolean alreadyActed;


    public Person(String name){
        this.name = name;
        lives = 3;
        alive = true;
        alreadyActed = false;
    }

    public String getName(){return name;}
    public int getLives(){return lives;}
    
    public void loseLife(){
        lives -= 1;
        if (lives == 0){
            System.out.println(name + " has lost their final life and is now dead!");
            alive = false;}
    }
    public void refresh(){
        alreadyActed = false;
    }

    public void getEvent(Person self, ArrayList list, int place){
        Random rand = new Random();
        int determiner = rand.nextInt(10);
        switch(determiner){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            Events.fillerEvent(self);
            break;
            case 6:
            case 7:
            case 8:
            if (place != list.size()-1){
                Events.groupEvent(self, list.get(place+1));
            }else{
                Events.fillerEvent(self);
            }
            break;
            case 9:
            case 10:
            Events.fatalEvent(self);
            default:

        }
        alreadyActed = true;

        }

    }
import java.util.Random;
public class Events {
    static Random rand = new Random();
    
    public static void fillerEvent(Person p){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p.getName() + " works.");
        break;
        case 1:
        System.out.println(p.getName() + " crafts.");
        break;
    }
}
public static void groupEvent(Person p1, Person p2){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p1.getName() + " helps " + p2.getName() + ".");
        break;
        case 1:
        System.out.println(p1.getName() + " chats with " + p2.getName() + ".");
        break;
    }
}
public static void fatalEvent(Person p){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p.getName() + " died.");
        death(p);
        break;
        case 1:
        System.out.println(p.getName() + " died.");
        death(p);
    }
}
public static void death(Person p){
    p.loseLife();
    if(p.alive){
    System.out.println(p.getName() + " has " + p.getLives() + " lives left.");
    }
}
}

Whenever I try to run the code, when it tries to call [Events.groupEvent(self, list.get(place+1))], it throws this error:

Exception has occurred: java.lang.Error
"java.lang.Error: Unresolved compilation problem: 
    The method groupEvent(Person, Person) in the type Events is not applicable for the arguments (Person, Object)
"

The ArrayList contains Person objects, at least in the main file, so I'm not sure why it's just treating them as generic Objects here. Am I doing something wrong when using the list as a parameter, or is this just not a thing that you can do with ArrayLists?

  • Does this answer your question? [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Tim Moore Mar 10 '23 at 21:38

2 Answers2

0

It's because your list is basically list of Objects. Your parameter declaration:

ArrayList list

Effectively means ArrayList<Object> due to the type erasure.

You should either change this parameter to

ArrayList<Person> list // or even List<Person> 

or explicitly cast the variable:

Events.groupEvent(self, (Person)list.get(place+1));

Read more:

m.antkowicz
  • 13,268
  • 18
  • 37
0

You declared:

ArrayList<Person> pList

But the method loses the Type:

getEvent(Person self, ArrayList list, int place)

You have to keep the Type in the declaration otherwise it is lost and becomes an inferred ArrayList<Object> list:

getEvent(Person self, ArrayList<Person> list, int place)
Bill Mair
  • 1,073
  • 6
  • 15