Reputation: 5
I need help clearing some test cases from a main method that was provided. I had to set up the class that has the setters and getters as well as the translation but I am getting stuck.
This is the class with the test cases.
// This is the driver for the Saurian class
// Use this website to compare with your translator:
// https://saurian.krystalarchive.com/
// Game that Saurian originated from:
// https://en.wikipedia.org/wiki/Star_Fox_Adventures
// http://starfox.wikia.com/wiki/Dino
public class SaurianDriver
{
public static void main(String[] args)
{
// create the Saurian object using the default constructor
Saurian saur = new Saurian();
// TEST #1
// Test the English word "College"
// It should translate to "Seccowo"
saur.setEnglish("College");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Seccowo"))
System.out.println("TEST #1 Passed!");
else
System.out.println("TEST #1 Failed!");
System.out.println();
// TEST #2
// Test the English phrase: "Hello World!"
// It should translate to "Xocce Nehct!"
saur.setEnglish("Hello World!");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Xocce Nehct!"))
System.out.println("TEST #2 Passed!");
else
System.out.println("TEST #2 Failed!");
System.out.println();
// TEST #3
// Test the Saurian word "Faqqu"
// It should translate to "Pizza"
saur.setSaurian("Faqqu");
System.out.println(saur.getEnglish());
if (saur.getEnglish().equals("Pizza"))
System.out.println("TEST #3 Passed!");
else
System.out.println("TEST #3 Failed!");
System.out.println();
// TEST #4
// Test the Saurian phrase: "Kubadw faskihoj aj vid."
// It should translate to "Taking pictures is fun."
saur.setSaurian("Kubadw faskihoj aj vid.");
System.out.println(saur.getEnglish());
if (saur.getEnglish().equals("Taking pictures is fun."))
System.out.println("TEST #4 Passed!");
else
System.out.println("TEST #4 Failed!");
System.out.println();
// TEST #5
// Translate "De edo sud tovouk GENERAL SCALES!"
// It should translate to "No one can defeat GENERAL SCALES!"
saur.setSaurian("De edo sud tovouk");
System.out.println(saur.getEnglish() + " GENERAL SCALES!");
if (saur.getEnglish().equals("No one can defeat"))
System.out.println("TEST #5 Passed!");
else
System.out.println("TEST #5 Failed!");
System.out.println();
// TEST #6
// Translate "Soccer is a way of life!"
// It should translate to "Jessoh aj u nuo ev cavo!"
saur.setEnglish("Soccer is a way of life!");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Jessoh aj u nuo ev cavo!"))
System.out.println("TEST #6 Passed!");
else
System.out.println("TEST #6 Failed!");
System.out.println();
// TEST #7
// Test the English word "College"
// It should translate to "Seccowo"
// create the Saurian object and initialize the data
Saurian saur2 = new Saurian("College", true); // true means 1st
parameter is in English
System.out.println(saur2.getSaurian());
if (saur2.getEnglish().equals("College") && saur2.getSaurian().equals("Seccowo"))
System.out.println("TEST #7 Passed!");
else
System.out.println("TEST #7 Failed!");
System.out.println();
// TEST #8
// Test the Saurian word "Faqqu"
// It should translate to "Pizza"
// create the Saurian object and initialize the data
Saurian saur3 = new Saurian("Faqqu", false); // false means 1st parameter is in Saurian
System.out.println(saur3.getEnglish());
if (saur3.getEnglish().equals("Pizza") && saur3.getSaurian().equals("Faqqu"))
System.out.println("TEST #8 Passed!");
else
System.out.println("TEST #8 Failed!");
System.out.println();
// TEST #9
// Test the English word "Hello World!"
// It should translate to "Xocce Nehct!"
// create the Saurian object and initialize the data
Saurian saur4 = new Saurian("Hello World!", true); // true means 1st parameter is in English
System.out.println(saur4.getSaurian());
if (saur4.getEnglish().equals("Hello World!") &&
saur4.getSaurian().equals("Xocce Nehct!"))
System.out.println("TEST #9 Passed!");
else
System.out.println("TEST #9 Failed!");
System.out.println();
// TEST #10
// Test the Saurian word "Kubadw faskihoj aj vid."
// It should translate to "Taking pictures is fun."
// create the Saurian object and initialize the data
Saurian saur5 = new Saurian("Kubadw faskihoj aj vid.", false); //
false means 1st parameter is in Saurian
System.out.println(saur5.getEnglish());
if (saur5.getEnglish().equals("Taking pictures is fun.") &&
saur5.getSaurian().equals("Kubadw faskihoj aj vid."))
System.out.println("TEST #10 Passed!");
else
System.out.println("TEST #10 Failed!");
System.out.println();
// TEST #11
// Test the toString()
System.out.println(saur.toString());
System.out.println();
System.out.println(saur2.toString());
System.out.println();
System.out.println(saur3.toString());
System.out.println();
System.out.println(saur4.toString());
System.out.println();
System.out.println(saur5.toString());
}
}
This is the class with all of the methods.
// Programmer:
// Date:
// The Saurian class has the ability to translate English to Saurian
// and Saurian to English
import java.util.Arrays;
public class Saurian
{
// data
// constants used for translating
// note M = M and m = m so M and m are not needed
public static final Character[] ENGLISHARR = {'A','B','C','D','E','F','G','H','I','J','K','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static final Character[] SAURIANARR = {'U','R','S','T','O','V','W','X','A','Z','B','C','D','E','F','G','H','J','K','I','L','N','P','O','Q','u','r','s','t','o','v','w','x','a','z','b','c','d','e','f','g','h','j','k','i','l','n','p','o','q'};
public static final int ARRLENGTH = ENGLISHARR.length; // should be the same length for ENGLISHARR and SAURIANARR
private String saurian = "";
private String english = "";
public Saurian()
{
english = "";
saurian = "";
}
public Saurian(String phrase, Boolean which)
{
//If the string is in english the boolean will be stored as true and I will pass the phrase on to english as well
if(which == true)
{
english = phrase;
saurian = translateEngToSaur(phrase);
}
else
{
saurian = phrase;
english = translateSaurToEng(phrase);
}
}
public void setEnglish(String newEnglish)
{
english = newEnglish;
//Just seeing what the output is
System.out.println("This is newEnglish " + newEnglish);
}
public String getEnglish()
{
//Just seeing what the output is
System.out.println("This is getEnglish "+ english);
return english;
}
public void setSaurian(String newSaurian)
{
saurian = newSaurian;
}
public String getSaurian()
{
//Just seeing what the output is
System.out.println("This is getSaurian "+ saurian);
return this.saurian;
}
public String translateSaurToEng(String saurianToEnglish)
{
String word = saurianToEnglish;
// String that will be used to store the word after it has been
// translated and will be built using the for loops
// For loop that loops as long as the input is Ex. "Hello World" is 11
// characters long
for (int i = 0; i < word.length(); i++)
{
// indexOfYellow should store the index number for which the letter in the string was located in the array.
int indexOfYellow = Arrays.asList(ENGLISHARR).indexOf(word.charAt(i));
// Should Check if the character at index i is present in ENGLISHARR then it will save it to saurianToEnglish
if (indexOfYellow != -1)
{
saurian += SAURIANARR[indexOfYellow];
// This is just here to see if the if statement passed
}
else
{
saurian += word.charAt(i);
// This is just here to see if the if statement failed
}
}
return saurian;
}
public String translateEngToSaur(String englishToSaurian)
{
String word = englishToSaurian;
// String that will be used to store the word after it has been
// translated and will be built using the for loops
// For loop that loops as long as the input is Ex. "Hello World" is 11
// characters long
for (int i = 0; i < word.length(); i++)
{
// indexOfYellow should store the index number for which the letter in the string was located in the array.
int indexOfYellow = Arrays.asList(SAURIANARR).indexOf(word.charAt(i));
// Should Check if the character at index i is present in ENGLISHARR then it will save it to saurianToEnglish
if (indexOfYellow != -1)
{
english += ENGLISHARR[indexOfYellow];
// This is just here to see if the if statement passed
}
else
{
english += word.charAt(i);
// This is just here to see if the if statement failed
}
}
return english;
}
}
I have tried to store a value inside of a getter method by changing
public void translateSaurToEng(String saurianToEnglish)
and
public void translateEngToSaur(String englishToSaurian)
into
public String translateSaurToEng(String saurianToEnglish)
public String translateEngToSaur(String englishToSaurian)
so that I can return a value and store it for later use.
I then did this
public String getSaurian()
{
//Just seeing what the output is
this.saurian = translateEngToSaur(english);
return this.saurian;
}
public String getEnglish()
{
this.english = translateSaurToEng(saurian);
//Just seeing what the output is for english
System.out.println("This is getEnglish "+ english);
return this.english;
}
but the method is incompatible because
translateSaurToEng(saurian)
is void, that is why I changed it to
public String translateSaurToEng(String saurianToEnglish)
then tried to do it this way but it was not letting me because of an error.
Thanks for any help you can provide.
Upvotes: 0
Views: 162
Reputation: 1
public class Saurian
{
// data
// attribute;
private String english;
private String saurian;
// constants used for translating
// note M = M and m = m so M and m are not needed
public static final char[] ENGLISHARR = {'A','B','C','D','E','F','G','H','I','J','K','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static final char[] SAURIANARR = {'U','R','S','T','O','V','W','X','A','Z','B','C','D','E','F','G','H','J','K','I','L','N','P','O','Q','u','r','s','t','o','v','w','x','a','z','b','c','d','e','f','g','h','j','k','i','l','n','p','o','q'};
public static final int ARRLENGTH = ENGLISHARR.length; // should be the same length for ENGLISHARR and SAURIANARR
// Constructors;
public Saurian() {
this.english = "";
this.saurian = "";
}
// Constructors
public Saurian (String text, boolean isEnglish) {
if (isEnglish) { // true;
this.english = text;
this.saurian = translateEnglishToSaurian(text);
} else {
this.saurian = text;
this.english = translateSaurianToEnglish(text);
}
}
// Setters;
public void setEnglish(String english) {
this.english = english;
this.saurian = translateEnglishToSaurian(english);
}
public void setSaurian(String saurian) {
this.saurian = saurian;
this.english = translateSaurianToEnglish(saurian);
}
// Getters;
public String getEnglish() {
return english;
}
public String getSaurian() {
return saurian;
}
// translation methods;
private String translateEnglishToSaurian(String english) {
StringBuilder result = new StringBuilder();
char[] chars = english.toCharArray(); // convert string into character array;
for(int i = 0; i < chars.length; i++) { // iterate through each character using an index;
char c = chars[i];
boolean found = false;
for(int j = 0; j < ARRLENGTH; j++) {
if (c == ENGLISHARR[j]) {
result.append(SAURIANARR[j]);
found = true; // mark that the character has been found;
break; // exit the loop
}
}
if (!found) {
result.append(c); // if the character is not found, keep it unchanged
}
}
return result.toString();
}
private String translateSaurianToEnglish(String saurian) {
StringBuilder result = new StringBuilder();
char[] chars = saurian.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
boolean found = false;
for(int j = 0; j < SAURIANARR.length; j++) {
if (c == SAURIANARR[j]) {
result.append(ENGLISHARR[j]);
found = true;
break;
}
}
if(!found) {
result.append(c);
}
}
return result.toString();
}
@Override
public String toString() {
return "English: " + english + ", Saurian: " + saurian;
}
}
// demo class
public class SaurianDriver
{
public static void main(String[] args)
{
// create the Saurian object using the default constructor
Saurian saur = new Saurian();
// TEST #1
// Test the English word "College"
// It should translate to "Seccowo"
saur.setEnglish("College");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Seccowo"))
System.out.println("TEST #1 Passed!");
else
System.out.println("TEST #1 Failed!");
System.out.println();
// TEST #2
// Test the English phrase: "Hello World!"
// It should translate to "Xocce Nehct!"
saur.setEnglish("Hello World!");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Xocce Nehct!"))
System.out.println("TEST #2 Passed!");
else
System.out.println("TEST #2 Failed!");
System.out.println();
// TEST #3
// Test the Saurian word "Faqqu"
// It should translate to "Pizza"
saur.setSaurian("Faqqu");
System.out.println(saur.getEnglish());
if (saur.getEnglish().equals("Pizza"))
System.out.println("TEST #3 Passed!");
else
System.out.println("TEST #3 Failed!");
System.out.println();
// TEST #4
// Test the Saurian phrase: "Kubadw faskihoj aj vid."
// It should translate to "Taking pictures is fun."
saur.setSaurian("Kubadw faskihoj aj vid.");
System.out.println(saur.getEnglish());
if (saur.getEnglish().equals("Taking pictures is fun."))
System.out.println("TEST #4 Passed!");
else
System.out.println("TEST #4 Failed!");
System.out.println();
// TEST #5
// Translate "De edo sud tovouk GENERAL SCALES!"
// It should translate to "No one can defeat GENERAL SCALES!"
saur.setSaurian("De edo sud tovouk");
System.out.println(saur.getEnglish() + " GENERAL SCALES!");
if (saur.getEnglish().equals("No one can defeat"))
System.out.println("TEST #5 Passed!");
else
System.out.println("TEST #5 Failed!");
System.out.println();
// TEST #6
// Translate "Soccer is a way of life!"
// It should translate to "Jessoh aj u nuo ev cavo!"
saur.setEnglish("Soccer is a way of life!");
System.out.println(saur.getSaurian());
if (saur.getSaurian().equals("Jessoh aj u nuo ev cavo!"))
System.out.println("TEST #6 Passed!");
else
System.out.println("TEST #6 Failed!");
System.out.println();
// TEST #7
// Test the English word "College"
// It should translate to "Seccowo"
// create the Saurian object and initialize the data
Saurian saur2 = new Saurian("College", true); // true means 1st parameter is in English
System.out.println(saur2.getSaurian());
if (saur2.getEnglish().equals("College") && saur2.getSaurian().equals("Seccowo"))
System.out.println("TEST #7 Passed!");
else
System.out.println("TEST #7 Failed!");
System.out.println();
// TEST #8
// Test the Saurian word "Faqqu"
// It should translate to "Pizza"
// create the Saurian object and initialize the data
Saurian saur3 = new Saurian("Faqqu", false); // false means 1st parameter is in Saurian
System.out.println(saur3.getEnglish());
if (saur3.getEnglish().equals("Pizza") && saur3.getSaurian().equals("Faqqu"))
System.out.println("TEST #8 Passed!");
else
System.out.println("TEST #8 Failed!");
System.out.println();
// TEST #9
// Test the English word "Hello World!"
// It should translate to "Xocce Nehct!"
// create the Saurian object and initialize the data
Saurian saur4 = new Saurian("Hello World!", true); // true means 1st parameter is in English
System.out.println(saur4.getSaurian());
if (saur4.getEnglish().equals("Hello World!") && saur4.getSaurian().equals("Xocce Nehct!"))
System.out.println("TEST #9 Passed!");
else
System.out.println("TEST #9 Failed!");
System.out.println();
// TEST #10
// Test the Saurian word "Kubadw faskihoj aj vid."
// It should translate to "Taking pictures is fun."
// create the Saurian object and initialize the data
Saurian saur5 = new Saurian("Kubadw faskihoj aj vid.", false); // false means 1st parameter is in Saurian
System.out.println(saur5.getEnglish());
if (saur5.getEnglish().equals("Taking pictures is fun.") && saur5.getSaurian().equals("Kubadw faskihoj aj vid."))
System.out.println("TEST #10 Passed!");
else
System.out.println("TEST #10 Failed!");
System.out.println();
// TEST #11
// Test the toString()
System.out.println(saur.toString());
System.out.println();
System.out.println(saur2.toString());
System.out.println();
System.out.println(saur3.toString());
System.out.println();
System.out.println(saur4.toString());
System.out.println();
System.out.println(saur5.toString());
}
}
Upvotes: 0
Reputation: 16
You're close! The problem is actually in your setter methods. In addition to setting the passed String to one field, you should translate the passed String and set that translation to the other field.
i.e.
public void setSaurian(String newSaurian)
{
//this is what you have now:
saurian = newSaurian;
//this is what you need to add:
english = translateSaurToEnglish();
}
If you do this in both setter methods, then you getter methods should just be able to return their corresponding member without any other code.
public String getEnglish()
{
//this is no longer necessary:
//this.english = translateSaurToEng(saurian);
//Just seeing what the output is for english
System.out.println("This is getEnglish "+ english);
return this.english;
}
Upvotes: 0