Как настроить сеттеры и геттеры для прохождения тестовых случаев. Проблема с типом шифрования Ceasar

#java #arrays #getter-setter

#java #массивы #сеттер-сеттер

Вопрос:

Мне нужна помощь в очистке некоторых тестовых примеров от основного метода, который был предоставлен. Мне пришлось настроить класс, в котором есть сеттеры и геттеры, а также перевод, но я застрял.

Это класс с тестовыми примерами.

  // 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") amp;amp; 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") amp;amp; 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!") amp;amp; 
     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.") amp;amp; 
     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;
    }


}
 

Я попытался сохранить значение внутри метода получения, изменив

  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)
 

затем попытался сделать это таким образом, но это не позволяло мне из-за ошибки.

Спасибо за любую помощь, которую вы можете предоставить.

Комментарии:

1. Я не понимаю смысла вашего приложения. Что должны делать translateSaurToEng и translateEngToSaur?

Ответ №1:

Вы близко! Проблема на самом деле в ваших методах установки. В дополнение к установке переданной строки в одно поле, вы должны перевести переданную строку и установить этот перевод в другое поле.

т.е.

 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;
   }