Reputation: 539
I am trying to access a variable that is in the WordSelect
class, then i'm trying to use it in the Game
class. The code is as follows:
The WordSelect Class:
import static java.lang.System.*;
import java.util.*;
public class WordSelect {
public WordSelect(){
}
public void Start(int diff){
String words[] = new String[26];
switch(diff){
case 1:
words[0] = "cat";
words[1] = "dog";
words[2] = "book";
words[3] = "breakfeast";
words[4] = "telephone";
words[5] = "mixture";
words[6] = "music";
words[7] = "animal";
words[8] = "school";
words[9] = "plant";
words[10] = "pen";
words[11] = "pencil";
words[12] = "paper";
words[13] = "note";
words[14] = "fog";
words[15] = "smoke";
words[16] = "bake";
words[17] = "alone";
words[18] = "drive";
words[19] = "town";
words[20] = "city";
words[21] = "sunny";
words[22] = "shine";
words[23] = "polish";
words[24] = "cap";
words[25] = "hat";
break;
case 2:
words[0] = "president";
words[1] = "exclamation";
words[2] = "statement";
words[3] = "television";
words[4] = "physics";
words[5] = "algebra";
words[6] = "geometry";
words[7] = "difficult";
words[8] = "extreme";
words[9] = "procedure";
words[10] = "ship";
words[11] = "soldier";
words[12] = "lunch";
words[13] = "hockey";
words[14] = "tennis";
words[15] = "soccer";
words[16] = "football";
words[17] = "basketball";
words[18] = "bias";
words[19] = "magazine";
words[20] = "computer";
words[21] = "internet";
words[22] = "allegedly";
words[23] = "system";
words[24] = "unison";
words[25] = "excited";
break;
case 3:
words[0] = "amalgamation";
words[1] = "proclomation";
words[2] = "establishment";
words[3] = "rehabilitation";
words[4] = "rhinoceros";
words[5] = "velociraptor";
words[6] = "declaration";
words[7] = "announcement";
words[8] = "binomial";
words[9] = "polynomial";
words[10] = "congregation";
words[11] = "obligation";
words[12] = "structure";
words[13] = "description";
words[14] = "perscription";
words[15] = "subscribe";
words[16] = "address";
words[17] = "township";
words[18] = "mischievous";
words[19] = "bewildered";
words[20] = "accusation";
words[21] = "designation";
words[22] = "disgusting";
words[23] = "prolonged";
words[24] = "restoration";
words[25] = "regeneration";
}
int i = words.length;
Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
String wd = words[choice];
// Not sure what to put here to make wd available in the other class
}
}
And the Game Class:
import static java.lang.System.*;
import java.util.*;
public class Game {
public static void game(){
out.println(wd); //Trying to print out the wd variable from WordSelect
}
}
Upvotes: 0
Views: 203
Reputation: 15229
You can change the variable to be static variable
, because in you case, I think that the wd
variable is not related to any objects in your Class. So, the static variable
must fit your need. Then, you just use it in the form of Classname.variable
.
In the WordSelect
class :
public class WordSelect {
public static String wd = words[choice];
public WordSelect(){
....
....
}
public void Start(int diff){
....
....
}
Then, in the GameCalss
:
public class Game {
public static void game(){
system.out.println(WordSelect.wd);
}
}
NOTE: The static variable
should be defined outside any methods.
Upvotes: 1
Reputation: 7141
There are several ways you could go about this.
1 You could change the start method to return a String.
public String start(int diff){
...
String wd = words[choice];
return wd;
}
Then
WordSelect ws = new WordSelect();
out.println(ws.start());
note I changed the Start method to start, methods should start lowercase.
2 You could save this as a class attribute in WordSelect
public class WordSelect {
private String word;
...
public String start(int diff){
...
String wd = words[choice];
word = wd;
}
...
public String getWord(){
return word;
}
}
Then
WordSelect ws = new WordSelect();
ws.start();
out.println(ws.getWord());
3 You could also try saving the attribute as a public attribute and not having a getter. This is not recommended though as you're losing one of the best features of OO, encapsulation.
4 You could also save it as a static variable as mentioned but these should be kept to a minimum. Static variables have their place, such as constants, but they're not really the OO way. Remember a static variable will apply to all objects of the same class. One of the main benefits of OO is that you can have different objects from the same class, each with different "state".
I think I would have designed these classes a bit differently. Why initialize the array every time you want to get a random word? It might be better doing this sort of thing elsewhere, maybe in a constructor, then having a single method to get a random word. Keep the array private to that class so that it's encapsulated. That way you can easily change it in future to use an ArrayList or whatever.
Upvotes: 0
Reputation: 6432
The best thing you can do is declare an ivar for your class. Here I'm calling it wordSelection
.
Then in the constructor method you can assign an initial value (empty) to the ivar.
Also, you have to add what's know as an "accessor method" to be able to read the ivar. Here I'm calling it getWordSelection
.
In your Game
class you will need an instance
of the WordSelection
class. You can accomplish that by using the new
keyword. Please see the code for an example.
Finally, I don't see a main
method anywhere in your code. You will need one to be able to run and test your program.
import static java.lang.System.*;
import java.util.*;
public class WordSelect {
// Add a priate ivar here...
private String wordSelection;
public WordSelect(){
// give the ivar an initial value
this.wordSelection = "";
}
// Add the method to access the ivar...
public String getWordSelection()
{
// this will return your saved word
return wordSelection;
}
public void Start(int diff){
String words[] = new String[26];
switch(diff){
// Your switch code here
// I removed the switch code to keep the answer short
}
int i = words.length;
Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
// This is what you have to do
wordSelection = words[choice];
}
}
And the Game Class:
import static java.lang.System.*;
import java.util.*;
public class Game {
public static void game(){
// This is how you instantiate your WordSelect class
WordSelect wordSelect = new WordSelect();
// call the "Start" method
wordSelect.Start(30);
// This is how you can access the ivar
out.println(wordSelect.getWordSelection());
}
}
Hope this helps!
Upvotes: 0
Reputation: 3108
you can achieve this by:
import static java.lang.System.*;
import java.util.*;
public class WordSelect {
public WordSelect(){
}
private String wd = "";
public void Start(int diff){
String words[] = new String[26];
switch(diff){
case 1:
words[0] = "cat";
words[1] = "dog";
words[2] = "book";
words[3] = "breakfeast";
words[4] = "telephone";
words[5] = "mixture";
words[6] = "music";
words[7] = "animal";
words[8] = "school";
words[9] = "plant";
words[10] = "pen";
words[11] = "pencil";
words[12] = "paper";
words[13] = "note";
words[14] = "fog";
words[15] = "smoke";
words[16] = "bake";
words[17] = "alone";
words[18] = "drive";
words[19] = "town";
words[20] = "city";
words[21] = "sunny";
words[22] = "shine";
words[23] = "polish";
words[24] = "cap";
words[25] = "hat";
break;
case 2:
words[0] = "president";
words[1] = "exclamation";
words[2] = "statement";
words[3] = "television";
words[4] = "physics";
words[5] = "algebra";
words[6] = "geometry";
words[7] = "difficult";
words[8] = "extreme";
words[9] = "procedure";
words[10] = "ship";
words[11] = "soldier";
words[12] = "lunch";
words[13] = "hockey";
words[14] = "tennis";
words[15] = "soccer";
words[16] = "football";
words[17] = "basketball";
words[18] = "bias";
words[19] = "magazine";
words[20] = "computer";
words[21] = "internet";
words[22] = "allegedly";
words[23] = "system";
words[24] = "unison";
words[25] = "excited";
break;
case 3:
words[0] = "amalgamation";
words[1] = "proclomation";
words[2] = "establishment";
words[3] = "rehabilitation";
words[4] = "rhinoceros";
words[5] = "velociraptor";
words[6] = "declaration";
words[7] = "announcement";
words[8] = "binomial";
words[9] = "polynomial";
words[10] = "congregation";
words[11] = "obligation";
words[12] = "structure";
words[13] = "description";
words[14] = "perscription";
words[15] = "subscribe";
words[16] = "address";
words[17] = "township";
words[18] = "mischievous";
words[19] = "bewildered";
words[20] = "accusation";
words[21] = "designation";
words[22] = "disgusting";
words[23] = "prolonged";
words[24] = "restoration";
words[25] = "regeneration";
}
int i = words.length;
Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
this.wd = words[choice];
}
//// setter and getter of wd;
public void setWd(String wd) {
this.wd = wd;
}
public String setWd() {
return this.wd;
}
}
Game.java
import static java.lang.System.*;
import java.util.*;
public class Game {
public static void game(){
WordSelect ws = new WordSelect();
out.println(ws.getWd());
}
}
Upvotes: 0
Reputation: 1722
You would have to do a few things. First, you need to create a variable to hold the String in your WordSelect class as follows:
String wd;
next you need a return method that returns a string:
public String returnWord()
{
return wd;
}
Now we need to do a few things in your main. First we need to create a WordSelect object, then we need to call the Start method then we need to call the returnWord method and assign it to a string.
WordSelect words = new WordSelect();
words.Start(1);
String str = words.returnWord();
System.out.println(str);
Upvotes: 0