0

I know that the subject exists on StackOverFlow, but I don't understand really the logic. Comparing two class objects with a method

I have a class Player with 3 variables:

  ...
  public String name;
  public int age;
  public boolean sex;


  public Player(String name, int age, boolean sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
  }

  public void display(int number){
    System.out.println("-----------------------");
    System.out.println("Number : " + number);
    System.out.println("Name : " + name);
    System.out.println("Age : " + age);
    System.out.println("Sex : " + sex);

  }
  ...

Then, I create 2 objects into my file Main as below:

List<Player> players = new ArrayList <Player>();
players.add(new Player("Eric", 31, true));
players.add(new Player("Juliette", 27, false));

I would like to compare the variable age from the object1 to the object2 and after, I have to display the name of the Player who is the oldest player. So here, it's Eric.

I am stuck into my method displayOldestAge() I don't understand how to compare 2 ages by adding the name of the player.

public void displayOldestAge(int[] arrayPlayer){
    int oldestAge = 0;

    for(int i=0; i<arrayPlayer.length; i++){
      if(arrayPlayer[i].age > oldestAge.age){
        oldestAge = arrayPlayer[i];
      }
    }
    System.out.println("The player oldest age is " + ???);
  }
  • `arrayPlayer` is an int array, not a `Player` array. This will not compile. – Liftoff Apr 13 '21 at 16:49
  • `oldestAge` is an integer also, so `oldestAge.age` doesn't mean anything. Also if you have a `List`, why is the input to the `displayOldestAge` a normal array? – Henry Twist Apr 13 '21 at 16:50

2 Answers2

1

You've got many issues with your displayOldestAge method. Here's a revised one.

Key changes:

  • Your input param should be an array of Player objects, not integers.
  • There's no need to track the oldest age, just the oldest player.
  • If the array is empty, it should exit.
  • Echo the oldest player's info at the end of the method with the stored oldestPlayer.
public void displayOldestAge(Player[] arrayPlayer){

    if(arrayPlayer.length == 0) return;

    Player oldestPlayer = arrayPlayer[0];

    for(int i = 1; i < arrayPlayer.length; i++)
    {
        if(arrayPlayer[i].age > oldestPlayer.age)
        {
            oldestPlayer = arrayPlayer[i];
        }
    }

    System.out.println("The player oldest age is " + oldestPlayer.name + ", Age: " + oldestPlayer.age)
}
Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • Thank you for your explanations. I have an ask. what do you call the displayOldestAge() method into the file main ? I have this : `displayOldestAge(players);` but I have an error message --> `Main.java:23: error: cannot find symbol displayOldestAge(players);` –  Apr 13 '21 at 17:04
  • 1
    You have to call it either from within the class itself, or make it static and call it under the namespace of the class. – Liftoff Apr 13 '21 at 17:50
0

I can see that OldestAge is an int primitive. And you want to compare with Player.age which is not correct. Compare oldestAge with player.age and have a reference of Player object created and assign that to array player[i]. And now printing reference.name should be giving the right result.