Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Arrays problem with Java

Options
  • 04-05-2013 11:55am
    #1
    Registered Users Posts: 4,049 ✭✭✭


    Folks.

    I am currently doing a Java course and I am spending the weekend looking at arrrays. I have a basic understanding of how to declare them but I am doing out a test question and I need some help as to where I am going wrong.

    I have to create an array within a class (called Nim) that represents a group of 3 coins. Each group has a different amount of coins and the class represents a game where there are 2 players and each player takes a certain amount of coins one at a time until there are no coins in any of the groups left.

    The first part of code I wanted to input into the class was to declare an instance variable called coinGroup to represent the array so I typed
    private int[] coinGroup;

    Then i attempted to write a constructor for the class so I typed
    public Nim()
    {
    super();
    this.coinGroups = new int[3];
    }


    This compiled without any errors so then I went into the workspace as requested in the question and I was asked to type the following

    int[coins] = {9, 11, 13];
    Nim aGame = new Nim(coins);


    I did this and executed each line one line at a time. The first line executed fine but when I executed the second line I got the following error
    Error: line 1 - constructor Nim in class Nim cannot be applied to given types;
    required: no arguments
    found: int[]
    reason: actual and formal argument lists differ in length


    I take it I should have put an argument into the constructor but Im not sure where and what to put so I would really appreciate any help.

    Thanks in advance


Comments

  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    gazzer wrote: »
    Folks.

    I am currently doing a Java course and I am spending the weekend looking at arrrays. I have a basic understanding of how to declare them but I am doing out a test question and I need some help as to where I am going wrong.

    I have to create an array within a class (called Nim) that represents a group of 3 coins. Each group has a different amount of coins and the class represents a game where there are 2 players and each player takes a certain amount of coins one at a time until there are no coins in any of the groups left.

    The first part of code I wanted to input into the class was to declare an instance variable called coinGroup to represent the array so I typed
    private int[] coinGroup;

    Then i attempted to write a constructor for the class so I typed
    public Nim()
    {
    super();
    this.coinGroups = new int[3];
    }


    This compiled without any errors so then I went into the workspace as requested in the question and I was asked to type the following

    int[coins] = {9, 11, 13];
    Nim aGame = new Nim(coins);


    I did this and executed each line one line at a time. The first line executed fine but when I executed the second line I got the following error
    Error: line 1 - constructor Nim in class Nim cannot be applied to given types;
    required: no arguments
    found: int[]
    reason: actual and formal argument lists differ in length


    I take it I should have put an argument into the constructor but Im not sure where and what to put so I would really appreciate any help.

    Thanks in advance


    You need a constructor to match the call new Nim(coins) , that error you are getting means java is looking for a constructor that takes no parameters, so you need to create a constructor that takes the parameters you require for your game.


  • Registered Users Posts: 403 ✭✭counterpointaud


    In addition to the above, int[coins] = {9, 11, 13]; is not valid Java syntax as far as I know, should probably be int[] coins = {9, 11, 13];

    You have variable name coinGroup, and coinGroups too, I assume these should be the same name ?


  • Registered Users Posts: 4,049 ✭✭✭gazzer


    Thanks for the help folks.

    I walked away from the laptop for a bit and when I came back something clicked and I (think) have the correct code.

    public class Nim
    {
    private int[] coinGroups = new int[3];


    public Nim(int[] coins)
    {
    coinGroups = coins;
    }
    }


    When I tested out the code in the workspace it behaved as expected.

    Thanks again.


Advertisement