In class we've been working with Packages and recursion.
I have two constructors -- public Phrase (Word word, Phrase suffix) and public Phrase (Word word).
Now, I'm not used to seeing Constructors like this. I thought it had to be String word, String Suffix. But then again, we're working with packages. I have a package "student" in which my Word class is in. Should I put my Phrase class in the student package, or should I put it in a student.Phrase package? Phrase is supposed to inherit some error checking from the Word class, but I'm not doing this correctly.
Here's my code, I've placed in comments a description of the other aspects of the program I haven't described here. :)
package student.Phrase; //should I just leave it as plain package student?
import student.Word;
public class Phrase extends Word{
final private String LEADINGWORD;
final private String SUFFIX;
final private String WORD;
//defines a multi-word phrase, where word is the leading word and suffix
//is the suffix phrase.
public Phrase (Word word, Phrase suffix){
this(word,suffix);
if (word == null || suffix == null){
throw new IllegalArgumentException("word or suffix == null");
}
LEADINGWORD = word;
SUFFIX = suffix;
}
//defines a single-word Phrase. Parameter word error checked to ensure that it is
//non-null. Placed into an appropriate instance variable.
//In this constructor, there is no suffix phrase, so the instance variable representing
//the suffix is null. This is the only way suffix can be null.
public Phrase (Word word){
this (word);
SUFFIX = null;
if (word == null){
throw new IllegalArgumentException("word == null");
}else{
word = word;
//instance variable
}
}
//returns the suffix phrase, which may be null when the phrase is a single-word phrase
public Phrase getSuffix(){
return SUFFIX;
}
//returns the leading word, which will never be null since no constructor ever
//allows it to be null
public Word getWord(){
return LEADINGWORD;
}
//returns the entire phrase as a String. If the suffix phrase is null, it just
//returns the leading word after converting it to a String using the word's
//getWord() method. Otherwise, it returns the leading word (converted to a String)
//with getWord(), followed by a space, followd by the suffix phrase (also converted
// to a String using its toString()
public String toString(){
if(SUFFIX == null){
return getWord();
}else{
return getWord() + " " + getSuffix();
}
}
}
Any clues on what I'm doing wrong? I'm really confused. Thanks!
