如何设置 setter 和 getter 以通过测试用例。凯撒密码类型问题
How to set up setters and getters to pass the test cases. Ceasar Cipher type problem
我需要帮助从提供的主要方法中清除一些测试用例。我必须设置具有设置器和 getters 以及翻译的 class,但我被卡住了。
这是带有测试用例的class。
// 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());
}
}
这是包含所有方法的class。
// 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;
}
}
我试图通过更改
在 getter 方法中存储一个值
public void translateSaurToEng(String saurianToEnglish)
和
public void translateEngToSaur(String englishToSaurian)
进入
public String translateSaurToEng(String saurianToEnglish)
public String translateEngToSaur(String englishToSaurian)
这样我就可以 return 一个值并存储它以备后用。
然后我做了这个
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;
}
但该方法不兼容,因为
translateSaurToEng(saurian)
无效,这就是为什么我将其更改为
public String translateSaurToEng(String saurianToEnglish)
然后尝试这样做,但由于错误而不允许我这样做。
感谢您提供的任何帮助。
你很接近!问题实际上出在您的 setter 方法中。除了将传递的字符串设置为一个字段外,您还应该翻译传递的字符串并将该翻译设置为另一个字段。
即
public void setSaurian(String newSaurian)
{
//this is what you have now:
saurian = newSaurian;
//this is what you need to add:
english = translateSaurToEnglish();
}
如果你在两个 setter 方法中都这样做,那么你 getter 方法应该能够 return 它们的相应成员而无需任何其他代码。
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;
}
我需要帮助从提供的主要方法中清除一些测试用例。我必须设置具有设置器和 getters 以及翻译的 class,但我被卡住了。
这是带有测试用例的class。
// 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());
}
}
这是包含所有方法的class。
// 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;
}
}
我试图通过更改
在 getter 方法中存储一个值 public void translateSaurToEng(String saurianToEnglish)
和
public void translateEngToSaur(String englishToSaurian)
进入
public String translateSaurToEng(String saurianToEnglish)
public String translateEngToSaur(String englishToSaurian)
这样我就可以 return 一个值并存储它以备后用。
然后我做了这个
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;
}
但该方法不兼容,因为
translateSaurToEng(saurian)
无效,这就是为什么我将其更改为
public String translateSaurToEng(String saurianToEnglish)
然后尝试这样做,但由于错误而不允许我这样做。
感谢您提供的任何帮助。
你很接近!问题实际上出在您的 setter 方法中。除了将传递的字符串设置为一个字段外,您还应该翻译传递的字符串并将该翻译设置为另一个字段。
即
public void setSaurian(String newSaurian)
{
//this is what you have now:
saurian = newSaurian;
//this is what you need to add:
english = translateSaurToEnglish();
}
如果你在两个 setter 方法中都这样做,那么你 getter 方法应该能够 return 它们的相应成员而无需任何其他代码。
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;
}