Decorator pattern

merv

Mitglied
Hallo! Ich habe folgende Aufgabe, aber kenne mich damit nicht so gut aus. Ich muss den Decorator Pattern verwenden und zwei Klassen schreiben (MorseReader und ROT13Writer). Kann mir da jemand helfen, wie ich anfangen könnte?

Aufgabenstellung:
Scan the class-javadocs of java.io classes. Which ones are suitable for your needs? Can you spot other implementations of the decorator pattern in this package?

Write two decorator classes: One that decorates an existing Java class for (i) reading and one for (ii) writing character streams.

Use the following class names:
(i) MorseReader
(ii) ROT13Writer

Make sure that each one of your two decorator classes decorates an existing Java class from the java.io package. Which class should you decorate, such that you really benefit from the pattern?

Note: the assignment is about CHARACTER streams (see above).

The reading decorator shall implement a morse code translator [3,4]. For example, a file containing .... . .-.. .-.. --- will be read as hello.

The writing decorator shall implement the ROT13 cipher algorithm.
 

merv

Mitglied
Ich hab die zwei Algorithmen implementiert aber das mit Decorator Pattern versteh ich nicht so..
Die zwei Algorithmen funktionieren soweit .

Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class MorseReader {
    public static void main ( String [] args ) throws IOException{
        System.out.println( "Your input text: " );
        FileReader fr = new FileReader("input.txt");
        BufferedReader br = null;
        String input = "";
        try {
            br = new BufferedReader(fr);

            do{
              input = br.readLine();
              System.out.println(input);
            }
            while (input != null);
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println( morseToinput(input));
    }

    public static String decode (String s) {
        String morse = s;

        if (s.equalsIgnoreCase(".-"))
            morse = "a";
        if (s.equalsIgnoreCase("-..."))
            morse = "b";
        if (s.equalsIgnoreCase("-.-."))
            morse = "c";
        if (s.equalsIgnoreCase("-.."))
            morse = "d";
        if (s.equalsIgnoreCase("."))
            morse = "e";
        if (s.equalsIgnoreCase("..-."))
            morse = "f";
        if (s.equalsIgnoreCase("--."))
            morse = "g";
        if (s.equalsIgnoreCase("...."))
            morse = "h";
        if (s.equalsIgnoreCase(".."))
            morse = "i";
        if (s.equalsIgnoreCase(".---"))
            morse = "j";
        if (s.equalsIgnoreCase("-.-"))
            morse = "k";
        if (s.equalsIgnoreCase(".-.."))
            morse = "l";
        if (s.equalsIgnoreCase("--"))
            morse = "m";
        if (s.equalsIgnoreCase("-."))
            morse = "n";
        if (s.equalsIgnoreCase("---"))
            morse = "o";
        if (s.equalsIgnoreCase(".--."))
            morse = "p";
        if (s.equalsIgnoreCase("--.-"))
            morse = "q";
        if (s.equalsIgnoreCase(".-."))
            morse = "r";
        if (s.equalsIgnoreCase("..."))
            morse = "s";
        if (s.equalsIgnoreCase("-"))
            morse = "t";
        if (s.equalsIgnoreCase("..-"))
            morse = "u";
        if (s.equalsIgnoreCase("...-"))
            morse = "v";
        if (s.equalsIgnoreCase(".--"))
            morse = "w";
        if (s.equalsIgnoreCase("-..-"))
            morse = "x";
        if (s.equalsIgnoreCase("-.--"))
            morse = "y";
        if (s.equalsIgnoreCase("--.."))
            morse = "z";
        if (s.equalsIgnoreCase("-----"))
            morse = "0";
        if (s.equalsIgnoreCase(".----"))
            morse = "1";
        if (s.equalsIgnoreCase("..---"))
            morse = "2";
        if (s.equalsIgnoreCase("...--"))
            morse = "3";
        if (s.equalsIgnoreCase("....-"))
            morse = "4";
        if (s.equalsIgnoreCase("....."))
            morse = "5";
        if (s.equalsIgnoreCase("-...."))
            morse = "6";
        if (s.equalsIgnoreCase("--..."))
            morse = "7";
        if (s.equalsIgnoreCase("---.."))
            morse = "8";
        if (s.equalsIgnoreCase("----."))
            morse = "9";
        if (s.equalsIgnoreCase("|"))
            morse = "";

        return morse;
    }

    public static String morseToinput( String input )
    {
       String output = ""; //sets string for output
       String nextMorseCharacter; //sets string for nextMorseCharacter
       String convertedInput; //sets string for convertedInput
       String[] morseChars = input.split(" ");
      
       for (int i = 0; i < morseChars.length; i++){
           //Select the next Morse character
           nextMorseCharacter = morseChars[i];
           boolean endOfWord = nextMorseCharacter.endsWith("|");
           if(endOfWord)
               nextMorseCharacter = nextMorseCharacter.substring(0, nextMorseCharacter.length() - 1);
           // Convert the string
           convertedInput = decode(nextMorseCharacter);

           output = output + convertedInput;
           if (endOfWord)
           {
               output = output + " ";
           }
       }
       return output;
    }





}

und

Code:
import java.util.Scanner;

public class ROT13Writer {



    public static void main(String[] args) {
      
         Scanner input = new Scanner( System.in );
         String answer = input.nextLine();
       
        // Display the encrypted message
        System.out.print("Encrypted message: ");
        for (int i = 0; i < answer.length(); i++)
          System.out.print(encrypt(answer.charAt(i)));
        System.out.println();
      }

      // Returns the encrypted version of ch using the "rot13"
      // algorithm
      private static char encrypt(char ch) {
        if (!Character.isLetter(ch))
          return ch;
        if (Character.isUpperCase(ch))
          return (char) (((ch - 'A') + 13) % 26 + 'A');
        return (char) (((ch - 'a') + 13) % 26 + 'a');
      }
   

}
 

Neue Themen


Oben