Running TranslatorTester.java

el estudiante: student
Expected: student
la profesora: I don't know that word
Expected: I don't know that word
aprender: to learn
Expected: to learn
azul: blue
Expected: blue
muy: very
Expected: very
la estudiante: student
Expected: student

CheckStyle

Translator.java:

Ok
pass

Student files

Translator.java:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
/**
 * Models a translator that can translate
 * a word from Spanish to English
 */
public class Translator
{
    private SpanishWord word;
    
    /**
     * Creates a Translator with the given SpanishWord
     * @param spanish the Spanish 
     * word encapsulated in a SpanishWord object
     */
    public Translator(SpanishWord spanish)
    {
        word = spanish;
    }
    
    /**
     * Gets the Spanish word
     * @return the Spanish word
     */
    public String getSpanish()
    {
        return word.getWord();
    }
    
    /**
     * Sets a new word for this Translator
     * @param spanish the new word to set 
     */
    public void setSpanish(String spanish)
    {
        word = new SpanishWord(spanish);
    }
    /**
     * Gets the English transation of the
     * word encapsulated in SpanishWord object
     * @return the English transation of the
     * word encapsulated in SpanishWord object
     */
    public String getEnglish()
    {
        String spanish = word.getWord();
        if (spanish.substring(0,3).equals("el ")
           || spanish.substring(0,3).equals("la "))
        {
            spanish = spanish.substring(3);  
        }
        
        String english = "I don't know that word";
        if (spanish.equals("estudiante"))
        {
            english = "student";
        }
        if (spanish.equals("aprender"))
        {
            english = "to learn";
        }
        if (spanish.equals("azul"))
        {
            english = "blue";
        }
        if (spanish.equals("muy"))
        {
            english = "very";
        }
        
        return english;
    }
}

Provided files

SpanishWord.java:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
/**
 * Models a word in Spanish with English 
 * tranlation
 */
public class SpanishWord
{
    private String word;
    private String partOfSpeech;
    
    /**
     * Constructs a SpanishWord
     * @param word the Spanish word
     * @param partOfSpeech the part of speech of the word
     */
    public SpanishWord(String word, String partOfSpeech)
    {
        this.word = word;
        this.partOfSpeech = partOfSpeech;
    }
 
        /**
     * Constructs a SpanishWord with this word and the empty string for the part of speach
     * @param word the Spanish word
     */
    public SpanishWord(String word)
    {
        this.word = word;
        this.partOfSpeech = "";
    }
    /**
     * Gets the word
     * @return the word
     */
    public String getWord()
    {
        return word;
    }
    
    /**
     * Gets the oart of speech
     * @return the part of speech
     */
    public String getPartOfSpeech()
    {
        return partOfSpeech;
    }
    
    /**
     * Sets a new word in the SpanishWord
     * @param newWord the new word to assign to this object
     */
    public void setWord(String newWord)
    {
        word = newWord;
    }

     /**
     * Sets a new part of speech for the SpanishWord
     * @param newPart the new part of speech to assign to this object
     */
    public void setPart(String newPart)
    {
        word = newPart;
    }
}

TranslatorTester.java:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
/**
 * Tests the Translator class
 *
 */
public class TranslatorTester
{
    public static void main(String[] args)
    {
        SpanishWord myWord = new SpanishWord("el estudiante", "noun");
        Translator translator = new Translator(myWord);
        System.out.println("el estudiante: " + translator.getEnglish());
        System.out.println("Expected: student");
        
        translator.setSpanish("la profesora");
        System.out.println("la profesora: " + translator.getEnglish());
        System.out.println("Expected: I don't know that word");
        
        translator.setSpanish("aprender");
        System.out.println("aprender: " + translator.getEnglish());
        System.out.println("Expected: to learn");
        
        myWord = new SpanishWord("azul", "adjective");
        translator = new Translator(myWord);
        System.out.println("azul: " + translator.getEnglish());
        System.out.println("Expected: blue");
        
        myWord = new SpanishWord("muy", "adverb");
        translator = new Translator(myWord);
        System.out.println("muy: " + translator.getEnglish());
        System.out.println("Expected: very");
        
        myWord = new SpanishWord("la estudiante", "noun");
        translator = new Translator(myWord);
        System.out.println("la estudiante: " + translator.getEnglish());
        System.out.println("Expected: student");
        
    }
}

Score

7/7

Download

2015-09-26T00:33:12Z