Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Linear to OO program  RSS feed

 
Rob Hermann
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again folks,
I am working through the HeadFirst text, and it's going quite well. A few sections back there was a program for the PhraseOMatic where all of the code is located in a single source file with a main() method.
I am testing my mojo so far by trying to split the single file into "objects".

It all compiles and runs. The only problem is: when it prints out the "phrase" it returns the random number generated and not the cooresponding item in the arrray.
See code below:

---------------------------------------------------------
public class ListOne //there are two additional lists similar to this one
{
String[] wordListOne ={"Cool","Stable","Visionary","Dark","Annoying","Instantaneous","Mismanaged","Dynamic","Flexible","Ostentatious","Unwavering","Persistent"};

int oneLength = wordListOne.length;

int word = (int) (Math.random() * oneLength);

int a = wordListOne[5];

}
---------------------------------------------
public class Phrase
{
public void startPhrase()
{


ListOne w1;
ListTwo w2;
ListThree w3;

w1 = new ListOne();
w2 = new ListTwo();
w3 = new ListThree();

int word1;
int word2;
int word3;

word1 = w1.word;
word2 = w2.word;
word3 = w3.word;


String phrase = word1.a + " " + word2 + " " + word3;



System.out.println("What we need here is a " + phrase);

}
}
-----------------------------------------------------
public class Phraser
{
public static void main(String [] args)
{
Phrase p = new Phrase();
p.startPhrase();
}
}
--------------------------------------------

When run the program returs:
"What we need here is a 11 3 6" though the numbers are different each time, as you would expect.


Thanks!!
Rob
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Hermann:
... When run the program returs:
"What we need here is a 11 3 6" though the numbers are different each time, as you would expect...

Your String "phrase" includes ints because word1, word2, and word3 are just ints (corresponding to w1.word, w2.word, and w3.word respectively).

What you want to do is use these int values as indexes in the respective arrays, like...

w1.wordListOne[word1]
 
Rob Hermann
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked like a charm, thank you.

So in other words w1.wordListOne[word1] says to the JVM find "word1" in class file "w1" in array "wordListOne"?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Hermann:
...So in other words w1.wordListOne[word1] says to the JVM find "word1" in class file "w1" in array "wordListOne"?

Close. But word1 has no meaning in w1 (which is an instance of ListOne).

word1 is a local variable in a method of the class Phrase. (It's "local" because it's declared inside the method body of startPhrase, so its scope is limited to that method.) word1 simply holds an int value.

w1 is also a local variable in the same method. w1 references an an instance of ListOne.

The variable w1 is used to access the array in ListOne. So w1.wordList is a name for an array.

The int value word1 is just supplied as the array index. So w1.wordList[word1] is a name for a particular String in the array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!