Как реализовать код перед супер без создания другого метода? - PullRequest
0 голосов
/ 02 ноября 2018

Итак, я хочу реализовать код, который бы шифровал слова !!

Это вопрос с домашним заданием.

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

, а затем отправьте слово в качестве аргумента для super (arg);

Хотя не будет ли это незаконным и будет ли ошибка, если я поставлю какой-либо код перед супер ???

Примечание: я также не могу создавать какие-либо переменные вне конструктора.

Примечание 2: ScrambledWordPuzzle - это конструктор для класса ScrambledWordPuzzle, который расширяет другой абстрактный класс

Редактировать 2: дополнительная информация

Класс для внесения изменений:

    public class ScrambledWordPuzzle extends AbstractWordPuzzle {

/**
 * The solution to the puzzle
 */
private String solution;

/**
 * Creates a scrambled word puzzle given the solution word.
 * 
 * @param solutionWord
 *            the puzzle word
 */
public ScrambledWordPuzzle(String solutionWord) {
    // COMPLETE THIS
    // Hint: You need to scramble the letters of the solution word
    // to generate the puzzle word and then set the puzzle word.
    // The easiest way to scramble the letters is to put them
    // into a list, use Collections.shuffle, and then convert the
    // the shuffled list of letters back into a string.

    super();        
    this.solution = solutionWord;

}

/**
 * Get the solution for this reverse word puzzle.
 * 
 * @return the solution for this reverse word puzzle
 */
@Override
public String getSolution() {
    // COMPLETE THIS
    return this.solution;
}

   }

Абстрактный класс:

public abstract class AbstractWordPuzzle {
/**
 * The puzzle word.
 */
private String puzzle;

/**
 * Initializes this puzzle to the empty string.
 */
public AbstractWordPuzzle() {
    // COMPLETE THIS
    this.puzzle="";

}

/**
 * Initializes this puzzle to the specified puzzle word.
 * 
 * @param puzzleWord
 *            the puzzle word
 */
public AbstractWordPuzzle(String puzzleWord) {
    // COMPLETE THIS
    this.puzzle=puzzleWord;
}

/**
 * Get the solution word. For word puzzles with more than one solution this
 * method returns the solution that comes first in dictionary order.
 * 
 * @return the solution word that comes first in dictionary order
 */
public abstract String getSolution();

/**
 * Get the puzzle word
 * 
 * @return the puzzle word
 */
public final String getPuzzleWord() {
    // ALREADY IMPLEMENTED; DO NOT MODIFY
    return this.puzzle;
}

/**
 * Set the puzzle word for this puzzle.
 * 
 * @param puzzleWord
 *            the puzzle word
 */
public final void setPuzzleWord(String puzzleWord) {
    // COMPLETE THIS
    this.puzzle=puzzleWord;
}
}

Мне нужен код перед super (), потому что, если он будет после super code, я не смогу вызвать переменную или что бы то ни было в super (arg).

1 Ответ

0 голосов
/ 02 ноября 2018

Хорошо, поэтому методы запрещены. Тогда все должно быть встроено.

public ScrambledWordPuzzle(String solutionWord) {
    super(new MyCollection(solutionWord.split("")).shuffle().toString());
}
...