如何将值初始化为名为texti的字符串数组。我需要写一个junit测试(见下文)
MorsenEnglisch s;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("In setUpBeforeClass()");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("In tearDownAfterClass()");
}
@Before
public void setUp() throws Exception {
System.out.println("In setUp()");
s = new MorsenEnglisch();
}
@After
public void tearDown() throws Exception {
System.out.println("In tearDown()");
s = null;
}
@Test
public void FirstTest() {
System.out.println("In FirstTest.");
s.setTexti(); // I don't know how to give texti a value right here. Texti should have only one
// field which holds the value "A".
s.code(s);
assertTrue(s.getOutput().equals(".- ")); //Since my program is a morse code translator it should
//convert the letter A into .-
// fail("Not yet implemented");
}
}
这是我上课的所有方法。如您在下面看到的,方法“ setTexti”用于为Texti赋值。我不知道在编写junit测试时如何给Texti一个值。所有其他名称都是不言自明的。
private String[] texti; // the whole text split up into an array
private int amount; // the number of fields in the array texti
private String output = ""; // this is the translated text in a single string
MorsenEnglisch() {
}
MorsenEnglisch(String[] texti, int amount) {
this.texti = texti;
this.amount = amount;
}
public void setTexti(String[] texti) { //Here is the method that I need to use in my junit test
this.texti = texti;
}
public void setOutput(String output) {
this.output = output;
}
public void setAmount(int anzahl) {
this.amount = anzahl;
}
public String[] getTexti() {
return this.texti;
}
public String getOutput() {
return this.output;
}
public int getAmount() {
return this.amount;
}
public void code(MorsenEnglisch s) {
for (int i = 0; i <= amount - 1; i++) {
if (texti[i].equals("a") || texti[i].equals("A")) {
texti[i] = ".- ";
output = output + ".- ";
}
if (texti[i].equals("b") || texti[i].equals("B")) {
texti[i] = "-... ";
output = output + "-... ";
}
if (texti[i].equals("c") || texti[i].equals("C")) {
texti[i] = "-.-. ";
output = output + "-.-. ";
}
if (texti[i].equals("d") || texti[i].equals("D")) {
texti[i] = "-.. ";
output = output + "-.. ";
}
if (texti[i].equals("e") || texti[i].equals("E")) {
texti[i] = ". ";
output = output + ". ";
}
if (texti[i].equals("f") || texti[i].equals("F")) {
texti[i] = "..-. ";
output = output + "..-. ";
}
if (texti[i].equals("g") || texti[i].equals("G")) {
texti[i] = "--. ";
output = output + "--. ";
}
if (texti[i].equals("h") || texti[i].equals("H")) {
texti[i] = ".... ";
output = output + ".... ";
}
if (texti[i].equals("i") || texti[i].equals("I")) {
texti[i] = ".. ";
output = output + ".. ";
}
if (texti[i].equals("j") || texti[i].equals("J")) {
texti[i] = ".--- ";
output = output + ".--- ";
}
if (texti[i].equals("k") || texti[i].equals("K")) {
texti[i] = "-.- ";
output = output + "-.- ";
}
if (texti[i].equals("l") || texti[i].equals("L")) {
texti[i] = ".-.. ";
output = output + ".-.. ";
}
if (texti[i].equals("m") || texti[i].equals("M")) {
texti[i] = "-- ";
output = output + "-- ";
}
if (texti[i].equals("n") || texti[i].equals("N")) {
texti[i] = "-. ";
output = output + "-. ";
}
if (texti[i].equals("o") || texti[i].equals("O")) {
texti[i] = "--- ";
output = output + "--- ";
}
if (texti[i].equals("p") || texti[i].equals("P")) {
texti[i] = ".--. ";
output = output + ".--. ";
}
if (texti[i].equals("q") || texti[i].equals("Q")) {
texti[i] = "--.- ";
output = output + "--.- ";
}
if (texti[i].equals("r") || texti[i].equals("R")) {
texti[i] = ".-. ";
output = output + ".-. ";
}
if (texti[i].equals("s") || texti[i].equals("S")) {
texti[i] = "... ";
output = output + "... ";
}
if (texti[i].equals("t") || texti[i].equals("T")) {
texti[i] = "- ";
output = output + "- ";
}
if (texti[i].equals("u") || texti[i].equals("U")) {
texti[i] = "..- ";
output = output + "..- ";
}
if (texti[i].equals("v") || texti[i].equals("V")) {
texti[i] = "...- ";
output = output + "...- ";
}
if (texti[i].equals("w") || texti[i].equals("W")) {
texti[i] = ".-- ";
output = output + ".-- ";
}
if (texti[i].equals("x") || texti[i].equals("X")) {
texti[i] = "-..- ";
output = output + "-..- ";
}
if (texti[i].equals("y") || texti[i].equals("Y")) {
texti[i] = "-.-- ";
output = output + "-.-- ";
}
if (texti[i].equals("z") || texti[i].equals("Z")) {
texti[i] = "--.. ";
output = output + "--.. ";
}
if (texti[i].equals(" ")) {
texti[i] = " ";
output = output + " ";
}
if (texti[i].equals("1")) {
texti[i] = ".---- ";
output = output + ".---- ";
}
if (texti[i].equals("2")) {
texti[i] = "..--- ";
output = output + "..--- ";
}
if (texti[i].equals("3")) {
texti[i] = "...-- ";
output = output + "...-- ";
}
if (texti[i].equals("4")) {
texti[i] = "....- ";
output = output + "....- ";
}
if (texti[i].equals("5")) {
texti[i] = "..... ";
output = output + "..... ";
}
if (texti[i].equals("6")) {
texti[i] = "-.... ";
output = output + "-.... ";
}
if (texti[i].equals("7")) {
texti[i] = "--... ";
output = output + "--... ";
}
if (texti[i].equals("8")) {
texti[i] = "---.. ";
output = output + "---.. ";
}
if (texti[i].equals("9")) {
texti[i] = "----. ";
output = output + "----. ";
}
if (texti[i].equals("0")) {
texti[i] = "----- ";
output = output + "----- ";
}
if (texti[i].equals("ä") || texti[i].equals("Ä")) {
texti[i] = ".-.- ";
output = output + ".-.- ";
}
if (texti[i].equals("ö") || texti[i].equals("Ö")) {
texti[i] = "---. ";
output = output + "---. ";
}
if (texti[i].equals("ü") || texti[i].equals("Ü")) {
texti[i] = "..-- ";
output = output + "..-- ";
}
if (texti[i].equals("ß")) {
texti[i] = "...--.. ";
output = output + "...--.. ";
}
if (texti[i].equals(".")) {
texti[i] = ".-.-.- ";
output = output + ".-.-.- ";
}
if (texti[i].equals(",")) {
texti[i] = "--..-- ";
output = output + "--..-- ";
}
if (texti[i].equals(":")) {
texti[i] = "---... ";
output = output + "---... ";
}
if (texti[i].equals(";")) {
texti[i] = "-.-.-. ";
output = output + "-.-.-. ";
}
if (texti[i].equals("?")) {
texti[i] = "..--.. ";
output = output + "..--.. ";
}
if (texti[i].equals("-")) {
texti[i] = "-....- ";
output = output + "-....- ";
}
if (texti[i].equals("_")) {
texti[i] = "..--.- ";
output = output + "..--.- ";
}
if (texti[i].equals("(")) {
texti[i] = "-.--. ";
output = output + "-.--. ";
}
if (texti[i].equals(")")) {
texti[i] = "-.--.- ";
output = output + "-.--.- ";
}
if (texti[i].equals("'")) {
texti[i] = ".----. ";
output = output + ".----. ";
}
if (texti[i].equals("=")) {
texti[i] = "-...- ";
output = output + "-...- ";
}
if (texti[i].equals("+")) {
texti[i] = ".-.-. ";
output = output + ".-.-. ";
}
if (texti[i].equals("/")) {
texti[i] = "-..-. ";
output = output + "-..-. ";
}
if (texti[i].equals("@")) {
texti[i] = ".--.-. ";
output = output + ".--.-. ";
}
if (texti[i].equals("!")) {
texti[i] = "-.-.-- ";
output = output + "-.-.-- ";
}
}
}
public void decode(MorsenEnglisch s) {
for (int i = 0; i <= amount - 1; i++) {
if (texti[i].equals(".-")) {
texti[i] = "A";
output = output + "A";
}
if (texti[i].equals("-...")) {
texti[i] = "B";
output = output + "B";
}
if (texti[i].equals("-.-.")) {
texti[i] = "C";
output = output + "C";
}
if (texti[i].equals("-..")) {
texti[i] = "D";
output = output + "D";
}
if (texti[i].equals(".")) {
texti[i] = "E";
output = output + "E";
}
if (texti[i].equals("..-.")) {
texti[i] = "F";
output = output + "F";
}
if (texti[i].equals("--.")) {
texti[i] = "G";
output = output + "G";
}
if (texti[i].equals("....")) {
texti[i] = "H";
output = output + "H";
}
if (texti[i].equals("..")) {
texti[i] = "I";
output = output + "I";
}
if (texti[i].equals(".---")) {
texti[i] = "J";
output = output + "J";
}
if (texti[i].equals("-.-")) {
texti[i] = "K";
output = output + "K";
}
if (texti[i].equals(".-..")) {
texti[i] = "L";
output = output + "L";
}
if (texti[i].equals("--")) {
texti[i] = "M";
output = output + "M";
}
if (texti[i].equals("-.")) {
texti[i] = "N";
output = output + "N";
}
if (texti[i].equals("---")) {
texti[i] = "O";
output = output + "O";
}
if (texti[i].equals(".--.")) {
texti[i] = "P";
output = output + "P";
}
if (texti[i].equals("--.-")) {
texti[i] = "Q";
output = output + "Q";
}
if (texti[i].equals(".-.")) {
texti[i] = "R";
output = output + "R";
}
if (texti[i].equals("...")) {
texti[i] = "S";
output = output + "S";
}
if (texti[i].equals("-")) {
texti[i] = "T";
output = output + "T";
}
if (texti[i].equals("..-")) {
texti[i] = "U";
output = output + "U";
}
if (texti[i].equals("...-")) {
texti[i] = "V";
output = output + "V";
}
if (texti[i].equals(".--")) {
texti[i] = "W";
output = output + "W";
}
if (texti[i].equals("-..-")) {
texti[i] = "X";
output = output + "X";
}
if (texti[i].equals("-.--")) {
texti[i] = "Y";
output = output + "Y";
}
if (texti[i].equals("--..")) {
texti[i] = "Z";
output = output + "Z";
}
if (texti[i].equals("")) {
texti[i] = " ";
output = output + " ";
}
if (texti[i].equals(".----")) {
texti[i] = "1";
output = output + "1";
}
if (texti[i].equals("..---")) {
texti[i] = "2";
output = output + "2";
}
if (texti[i].equals("...--")) {
texti[i] = "3";
output = output + "3";
}
if (texti[i].equals("....-")) {
texti[i] = "4";
output = output + "4";
}
if (texti[i].equals(".....")) {
texti[i] = "5";
output = output + "5";
}
if (texti[i].equals("-....")) {
texti[i] = "6";
output = output + "6";
}
if (texti[i].equals("--...")) {
texti[i] = "7";
output = output + "7";
}
if (texti[i].equals("---..")) {
texti[i] = "8";
output = output + "8";
}
if (texti[i].equals("----.")) {
texti[i] = "9";
output = output + "9";
}
if (texti[i].equals("-----")) {
texti[i] = "0";
output = output + "0";
}
if (texti[i].equals(".-.-")) {
texti[i] = "Ä";
output = output + "Ä";
}
if (texti[i].equals("---.")) {
texti[i] = "Ö";
output = output + "Ö";
}
if (texti[i].equals("..--")) {
texti[i] = "Ü";
output = output + "Ü";
}
if (texti[i].equals("...--..")) {
texti[i] = "ß";
output = output + "ß";
}
if (texti[i].equals(".-.-.-")) {
texti[i] = ".";
output = output + ".";
}
if (texti[i].equals("--..--")) {
texti[i] = ",";
output = output + ",";
}
if (texti[i].equals("---...")) {
texti[i] = ":";
output = output + ":";
}
if (texti[i].equals("-.-.-.")) {
texti[i] = ";";
output = output + ";";
}
if (texti[i].equals("..--..")) {
texti[i] = "?";
output = output + "?";
}
if (texti[i].equals("-....-")) {
texti[i] = "-";
output = output + "-";
}
if (texti[i].equals("..--.-")) {
texti[i] = "_";
output = output + "_";
}
if (texti[i].equals("-.--.")) {
texti[i] = "(";
output = output + "(";
}
if (texti[i].equals("-.--.-")) {
texti[i] = ")";
output = output + ")";
}
if (texti[i].equals(".----.")) {
texti[i] = "'";
output = output + "'";
}
if (texti[i].equals("-...-")) {
texti[i] = "=";
output = output + "=";
}
if (texti[i].equals(".-.-.")) {
texti[i] = "+";
output = output + "+";
}
if (texti[i].equals("-..-.")) {
texti[i] = "/";
output = output + "/";
}
if (texti[i].equals(".--.-.")) {
texti[i] = "@";
output = output + "@";
}
if (texti[i].equals("-.-.--")) {
texti[i] = "!";
output = output + "!";
}
}
}
public String toString() {
return this.output;
}
}
最佳答案
如果我能很好地理解您,那么您想使用一个元素=“ A”来设置数组。因此,它是s.setTexti(new String[]{"A"});
。