We need to stop for a second.
You have to understand scope before we can fix this problem.
Actually, firstly you should know that you should not have variables with the same name as a function/method.
Right now you have a method named "populateDeckOf Cards()".
You also have declared an ArrayList named "populateDeckOf Cards" in your constructor method....and for some reason you're trying to access it in your populateDeckOfC ards() method but.......
This is a very confusing thing to do and is probably the source of your problems.
With that aside we have to go over the concept of scope and classes.
As I stated before, a class is like a blue print for creating Objects. An Object is something that exists in memory.
Objects/Classes have members. Members can be simple variables (like Integers, and Strings and such), but they can also be methods that will modify Object's state or do things.
In your case you have a Card class.
A card has a few different members:
You have declared them as part of the class. This means that these variables will exist as long as the Card Object exists in memory. Once you're finished with the Card Object, they are destroyed and are no longer accessible. The scope of these methods are at the class level.
The card class also has a bunch of methods:
These methods in the Card class have access to class members (the variables above)....and if you had declared the Strings as "private" the these strings would Only be accessible to the methods that are members of the Card class. (But you declared them public...which is weird...especia lly since you've provided get methods to retrieve them....)
Let's add another method to the Card class...let's create a method named PrintCard. Since this PrintCard method is part of the Card class it has access to all of the member variables of the Card class and so it is able to print the card details to the screen:
The variable "theOutput" in the PrintCard method has a method level scope. This means that theOutput cannot be accessed out side of the PrintCard method. It is only available within the PrintCard method. It also means that theOuput is destroyed as soon as the method is finished executing.
Hope you're with me so far.
Say we have a loop and we declare a variable inside the loop. That variable only exists until the loop is finished executing. It cannot be access outside of the loop.
As you can see, variables have life-spans. It is important to take care in where you declare them because this will have a big impact on how things work.
Now let's look at the GameOfSet class.
It has one class member variable:
This means that this variable is accessible to all of the methods in the GameOfSet class.
The important thing is that this variable has to exist before you can use it....or else you're going to get Null Reference errors (like I explained before).
That is the whole purpose to the Constructor method. The constructor method is used to instantiate all of the class member variables. It is called when you use the "new" keyword to instantiate an Object of the class.
For example, when you create a new instance of the GameOfSet like:
GameOfSet theGame = new GameOfSet();
The constructor method is called. It is the constructor's responsibility to make sure that all of the class member variables are instantiated.
Therefore, your constructor needs to at least have the following:
Now your "cards" ArrayList variable is instantiated and can be used by all of the methods in the GameOfSet class.
Your GameOfSet has one method:
This method doesn't return anything...
What is the purpose of having this method then?
It is supposed to populate the class member variable "cards" with Card Objects.
You should call this method after you have instantiated the "cards" ArrayList in the constructor. That way the "cards" variable will not only be instantiated, but it will also have data in it!
So your constructor should look like:
Now to address your problem on line 50.
You have:
populateDeckOfC ards().add(theC ard);
First of all populateDeckOfC ards is the name of the method. In Java you execute a method by calling it's name and providing it with parameters (eg: populateDeckOfC ards() will call the populateDeckOfC ards method). Then you have .add(theCard) but how is that supposed to work (especially since the method doesn't return anything)? .... In otherwords this really doesn't make any sense and that is why the compiler is telling you that there is a problem.
Secondly, you cannot access any variables that were declared in the constructor method in the populateDeckOfC ards() method. So even if you changed the name of the variable that was declared in the constructor method, it is not accessible in the populateDeckOfC ards() because this is how scope works.
But the "cards" variable is accessible in the populateDeckOfC ards() method.
Because it is has been declared at the class level...therefo re has scope for every method in the class.
Later I'll tell you about "public" and "private" scope.
For now I'll let you figure out how to populate the cards variable :)
-Frinny
You have to understand scope before we can fix this problem.
Actually, firstly you should know that you should not have variables with the same name as a function/method.
Right now you have a method named "populateDeckOf Cards()".
You also have declared an ArrayList named "populateDeckOf Cards" in your constructor method....and for some reason you're trying to access it in your populateDeckOfC ards() method but.......
This is a very confusing thing to do and is probably the source of your problems.
With that aside we have to go over the concept of scope and classes.
As I stated before, a class is like a blue print for creating Objects. An Object is something that exists in memory.
Objects/Classes have members. Members can be simple variables (like Integers, and Strings and such), but they can also be methods that will modify Object's state or do things.
In your case you have a Card class.
A card has a few different members:
- public String number;
- public String color;
- public String pattern;
- public String shape;
You have declared them as part of the class. This means that these variables will exist as long as the Card Object exists in memory. Once you're finished with the Card Object, they are destroyed and are no longer accessible. The scope of these methods are at the class level.
The card class also has a bunch of methods:
- public Card(String number, String color, String pattern, String shape) <-- the constructor
- public String getNumber()
- public String getColor()
- public String pattern()<--you should to rename this to getPattern because you already have a String variable named pattern.
- public String shape()<-- you should to rename this to getShape because you already have a String variable named shape.
These methods in the Card class have access to class members (the variables above)....and if you had declared the Strings as "private" the these strings would Only be accessible to the methods that are members of the Card class. (But you declared them public...which is weird...especia lly since you've provided get methods to retrieve them....)
Let's add another method to the Card class...let's create a method named PrintCard. Since this PrintCard method is part of the Card class it has access to all of the member variables of the Card class and so it is able to print the card details to the screen:
Code:
public void PrintCard(){ StringBuilder theOutput=new StringBuilder(); theOutput.append(number); theOutput.append(color); theOutput.append(pattern); theOutput.append(shape); System.out.println(theOutput.toString() ); }
Hope you're with me so far.
Say we have a loop and we declare a variable inside the loop. That variable only exists until the loop is finished executing. It cannot be access outside of the loop.
As you can see, variables have life-spans. It is important to take care in where you declare them because this will have a big impact on how things work.
Now let's look at the GameOfSet class.
It has one class member variable:
- ArrayList<Card> cards;
This means that this variable is accessible to all of the methods in the GameOfSet class.
The important thing is that this variable has to exist before you can use it....or else you're going to get Null Reference errors (like I explained before).
That is the whole purpose to the Constructor method. The constructor method is used to instantiate all of the class member variables. It is called when you use the "new" keyword to instantiate an Object of the class.
For example, when you create a new instance of the GameOfSet like:
GameOfSet theGame = new GameOfSet();
The constructor method is called. It is the constructor's responsibility to make sure that all of the class member variables are instantiated.
Therefore, your constructor needs to at least have the following:
Code:
public GameOfSet() { cards = new ArrayList<Card>(); }
Your GameOfSet has one method:
- public void populateDeckOfC ards()
This method doesn't return anything...
What is the purpose of having this method then?
It is supposed to populate the class member variable "cards" with Card Objects.
You should call this method after you have instantiated the "cards" ArrayList in the constructor. That way the "cards" variable will not only be instantiated, but it will also have data in it!
So your constructor should look like:
Code:
public GameOfSet() { cards = new ArrayList<Card>(); populateDeckOfCards(); }
You have:
populateDeckOfC ards().add(theC ard);
First of all populateDeckOfC ards is the name of the method. In Java you execute a method by calling it's name and providing it with parameters (eg: populateDeckOfC ards() will call the populateDeckOfC ards method). Then you have .add(theCard) but how is that supposed to work (especially since the method doesn't return anything)? .... In otherwords this really doesn't make any sense and that is why the compiler is telling you that there is a problem.
Secondly, you cannot access any variables that were declared in the constructor method in the populateDeckOfC ards() method. So even if you changed the name of the variable that was declared in the constructor method, it is not accessible in the populateDeckOfC ards() because this is how scope works.
But the "cards" variable is accessible in the populateDeckOfC ards() method.
Because it is has been declared at the class level...therefo re has scope for every method in the class.
Later I'll tell you about "public" and "private" scope.
For now I'll let you figure out how to populate the cards variable :)
-Frinny
Comment