I'm doing an assignment that's a separate class for a program called "MadChoosers" and THIS class creates an array of four MadChoosers called chooserList and the four chooserList arrays are chooserList[0]-to hold proper names; chooserList[1]-to hold adjectives; chooserList[2]-for nouns; and chooserList[3]- for verbs. I am new to arrays...still learning them in class and I understand the "idea" behind them and what not...I get the feeling that my code is wrong, I have been looking at the tutorial on here and that helps a little but when I try to use the same codes for Strings, it doesn't work...this is the code I have so far (I'll post both classes so you can see what's going on)
public class MadChooser { //intstance variables String wordlist[]; int firstEmpty; public static void main(String[] args) { } /*default constructor creates a size 20 wordlist(no strings yet) starts firstEmpty = 0*/ public MadChooser() { wordlist = new String[20]; int firstEmpty = 0; } /*parameterized constructor that takes an array of Strings as a parameter. Set wordlist to this array and firstEmpty to the length of the array (it is totally filled). EC+ 10: instead of just setting worldist to the list passed in, make wordlist a new array of the same size and copy all the Strings over from the other wordlist.*/ public MadChooser(String[] b) { wordlist = new String[b.length]; for (int v = 0; v < wordlist.length; v++) { wordlist[v] = b[v]; } firstEmpty = wordlist.length; } /*parameterized constructor that takes integer and makes wordlist that size (no words in it yet so firstEmpty is still 0)*/ public MadChooser(int a) { a = 0; firstEmpty = a; } /*method add(String newWord) which given a string, adds it to wordlist in the first * empty position, unless the array is full (check firstEmpty against the * size of the array); when you add a new string, remember to update firstEmpty*/ public void Add(String newWord) { if (firstEmpty < wordlist.length) { wordlist[firstEmpty] = newWord; firstEmpty++; } } /*Overload Add with a second method add(String[] newWords) which, given an * array of Strings adds all of them to wordlist(until it runs out of room) * Think abt what the loop needs to look like to make sure you don't run off * either array (there are several correct ways to do this)*/ public void Add(String[] newWords) { for (int i = 0; i < newWords.length; i++) { this.Add(newWords[i]); } } /*Method pickWord(), returns a randomly chosen string from wordlist(it is * possible wordlist may not be full, make sure to only use words from that * are filled in (use firstEmpty); if wordlist has no words, return null.*/ public String pickWord() { int w = (int) (Math.random() * firstEmpty); return wordlist[w]; } /*EC+20: add method find, give String parameter, returns true if that string is already in wordlist, false otherwise. (hint == doesn't work with Strings, need something like "firstString.equals(secondString)" Have the add() method use find to check if a word being added is already in the list if so, don't add it*/ public String find(String h) { if (h.equals(wordlist)) { } return h; } }
**THIS IS THE CLASS I'M CONFUSED ON**
public class ChooserList { /*write a program that creates an array of four MadChoosers, chooserList. * chooserList[0] will be used to hold proper names, chooserList[1] will hold * adjectives, chooserList[2] nouns, and chooserList[3] verbs.*/ String[] chooserListNames; //do I even need these? String[] chooserListAdj; String[] chooserListNouns; String[] chooserListVerbs; chooserList = new String [4]; //this isn't working right }
Thx in advance guys!