如何设置setter和getter来传递测试用例。凯撒密码型问题

问题描述 投票:0回答:1

我需要帮助从提供的主要方法中清除一些测试用例。我不得不设置具有setter和getter以及翻译的课程,但我遇到了困难。

这是带有测试用例的类。

 // 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());
     }
 }

这是包含所有方法的类。

// 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)

这样我就可以返回一个值并将其存储起来供以后使用。

然后我做了这个

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)

然后尝试这样做,但它没有让我因为一个错误。

感谢您的任何帮助,您可以提供。

java arrays getter-setter
1个回答
0
投票

你很亲密!问题实际上在你的setter方法中。除了将传递的String设置为一个字段外,还应转换传递的String并将该转换设置为另一个字段。

public void setSaurian(String newSaurian)
{
     //this is what you have now:
    saurian = newSaurian;

     //this is what you need to add:
    english = translateSaurToEnglish();
}

如果你在两个setter方法中都这样做,那么getter方法应该只能返回相应的成员而不需要任何其他代码。

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;
   }
© www.soinside.com 2019 - 2024. All rights reserved.