-2

Objective: Retrieve array defined in class Player in another class to do additional operations on the array content;

Code so far:

public class Players {

//attributes

private int [] Player1 = new int [3];
private int [] Player2 = new int [3];
private int round=Math.random();

//Constructor

public Players(int [] p1, int [] p2 ){
[] player1 = [] p1
[] player2 = [] p2
}

Problem specification : Does not compile;

e.doroskevic
  • 1,969
  • 14
  • 25
  • 3
    I could be wrong, but I have a feeling you are not following OOP standards. You should make a `Player` class then instantiate two instances of it for player1/2 in a main method. – turbo Dec 10 '13 at 15:22
  • possible duplicate of http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java – Freak Dec 10 '13 at 15:32

5 Answers5

2

You have multiple syntax as well as type mismatch issues

1) Java is case sensitive

public Players(int [] p1, int [] p2 ){
 Player1 =  p1;
 Player1 =  p2;
}

So

Player1 !=player1 

2) And

[] player1 = [] p1

Is not a valid java syntax

3) Finally Math.random() returns double, Change it as

double random = Math.random();
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
1

You have to separate the parameters trough a comma

public Game(int[] p1, int[] p2) {

And this isn't even close to working (note the lack of semicolons as well):

[] player1 = [] p1
[] player2 = [] p2

You want to set the instance fields equal to the given parameters, but there's no need to redefine that they're arrays.

Instead use this:

this.player1 = p1;
this.player2 = p2;

Keep in mind naming conventions: fields start with lowercase (so player1 instead of Player1). Java is case sensitive so Player1 and player1 are not the same. Exactly one of the reasons why naming conventions exist.

Lastly: Math.random returns a double. If you cast it to an int you will lose its purpose (it returns a value between 0 and 1, if you cast it to int you will lose everything between those two boundaries). Instead make it as such:

private double round = Math.random();
Jeroen Vannevel
  • 41,258
  • 21
  • 92
  • 157
1

As per my comment, you might want to consider structuring your class like this:

public class Player {
    //attributes
    private int[] roundValues; 
    private double round=Math.random(); //not sure what you are using this for

    //Constructor
    public Player(int[] roundValues){
        this.roundValues = roundValues;
    }

    // use this to get the roundValues array
    public int[] getRoundValues() {
        return roundValues;
    }

    // use this to change the roundValues array
    public void setRoundValues(int[] roundValues) {
        this.roundValues = roundValues;
    }

    public static void main(String[] args) {
        int[] array1 = {1,2,3}; //just some dummy values
        Player player1 = new Player(array1); 
        // do this to get the player's round values
        int[] roundVals = player1.getRoundValues();
    }
}
turbo
  • 1,857
  • 2
  • 21
  • 35
0

There is several mistakes with you program. Check the one below:

public class Players {

    //attributes
    private int [] player1 = new int [3]; // use names with lowercase at beginning (convention)
    private int [] player2 = new int [3];
    private double round = Math.random();

    //Constructor
    public Players(int[] p1,  int[] p2) { // you missed a comma and a parenthesis
        player1 = p1; // no need to say again that they are arrays. add ';' at the end
        player2 = p2;
    } 

    // you could need to add these to access your private arrays from an other class
    public int[] getPlayer1(){
        return player1;
    }

    public int[] getPlayer2(){
        return player2;
    }
}

Hope it helps

Basile Perrenoud
  • 3,810
  • 1
  • 24
  • 47
0
  • Watch out for case sensitivity by player1 and Player1.
  • add some semicolons
  • remove some braces for setting array variables
  • the Math.random() method returns a double, not an integer
  • variables should begin with lower case letters

Like this:

public class Players {

    //attributes

    private int[] player1 = new int[3];
    private int[] player2 = new int[3];
    private double round = Math.random();

    //Constructor

    public Players(int[] p1, int[] p2) {
        player1 = p1;
        player2 = p2;
    }
}
bobbel
  • 3,031
  • 2
  • 24
  • 41