2

I have an Array Class with two methods which are both mandatory. I have an algorithm "maxAtEnd" which compares a set of numbers and put the highest number at the end of the set but leaves the other numbers at their original place. The first method implements this algorithm for integer arrays. This works fine. And prints the order of the numbers before (first loop) and after (second loop).

Thei second method is generic and should look like shown in the code. This method should be tested with an array of players (name, score), i.e. it should compare the elements of the players array (each element consists of the name of the player and the score). Therefore I have a Class "Players" which is in the same package so I could create Player instances and put them in an array.

I have a test Class where I create the arrays and the instances of players. The first method works fine with arrays of integers. But the second method only prints the reference of the array but not the elements (Players) in the appropriate order (before/after) with the highest Player at the end. I get always the references.

public class MArrays {


//First method
static public void maxAtEnd(int [] array){  
    int max = array[0];
    int index = 0;
    for (int i=1; i< array.length; i++){
        System.out.print(array[i]+", ");
        if (array[i] > max) {
            index = i;
            max = array[i];
        }
    }
    System.out.println("Before: Max = "+max+" at position "+ (index)+" ---First method");


    int l = array[array.length-1];
    int m = array[index];
    array[array.length-1] = m;
    array[index] = l;

    for (int i=0; i< array.length; i++){
        System.out.print(array[i]+", ");
    }
    System.out.println("After: Max = "+max+" at position "+ (array.length)+" ---second method");


}


// second method
static public <E extends Comparable <? super E>> void maxAtEnd(E[] array){

    E max = array[0];
    int index = 0;
    for (int i=1; i< array.length; i++){
        System.out.print(array[i]+", ");
        if ( array[i].compareTo(array[index]) > 0) {
            index = i;
            max = array[i];
    }
}
    System.out.println("SecondMethod-Before: Max = "+max+" at position "+ (index+1)+" +++second method");

    E l = array[array.length-1];
    E m = array[index];
    array[array.length-1] = m;
    array[index] = l;


    for (int i=0; i< array.length; i++){
        System.out.print(array[i]+", ");            
    }
    System.out.println("SecondMethod-After: Max = "+max+" at position "+ (array.length)+" +++second method");
    }
public static void maxAtEnd(Spieler E) {
    // TODO Auto-generated method stub

}
}

The Test Class looks like this:

public class MArraysTest {

public static void main(String[] args) {

    int array1[] = {100, 200, 420, 300, 130 };
    String array2[] = {"Peter", "Brad", "Anthony", "Michael", "Claudia"};

    Player player1 = new Player("Peter", 100);
    Player player2 = new Player("Brad", 20);
    Player player3 = new Player("Claudia", 150);

    Player[] PlayerList1 = {player1};
    Player[] PlayerList2 = {player1, player2, player3};



    MArrays.maxAtEnd(array1); //this works fine
    System.out.println();

    MArrays.maxAtEnd(array2); // this works fine too, it accesses the second method
    System.out.println();

    MArrays.maxAtEnd(PlayerList1); // these two only print the references and not the name and score
    MArrays.maxAtEnd(PlayerList2);      
}
}

The Player Class looks like this which is in the same package so I can access it from the other class to create instances of Player:

public class Player implements Comparable<Player>{


private String Name;
private int Score;

    public Player(String Name, int Score) {
    this.Name=Name;
    this.Score=Score;
    }

public String getName(){
    return Name;
}
public int getScore(){
    return Score;
}

So how can I implement a print method to print the Name and Score when call the generic method in MArrays with an array of Player objects from the MArraysTest? I have tried several things with toString() and so on, but nothing works. I always get the references of the objects. I have Java 8 installed.

1 Answers1

-1

You need to override toString() in Player class first.

Then use

Arrays.toString(arr)

Eg:

override toString()

@Override
public String toString() {
    return "Player{" +
            "Name='" + Name + '\'' +
            ", Score=" + Score +
            '}';
}

Now

Player player1 = new Player("Peter", 100);
Player player2 = new Player("Brad", 20);
Player player3 = new Player("Claudia", 150);

Player[] PlayerList1 = {player1};
Player[] PlayerList2 = {player1, player2, player3};
System.out.println(Arrays.toString(PlayerList1));
System.out.println(Arrays.toString(PlayerList2));

Out put:

[Player{Name='Peter', Score=100}]
[Player{Name='Peter', Score=100}, Player{Name='Brad', Score=20}, Player{Name='Claudia', Score=150}]
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115