Как завершить sh мою программу кодирования и декодирования? - PullRequest
0 голосов
/ 20 июня 2020

Вопрос внизу

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

public class MainTest {

    public static void main(String[] args) {

    }
}   
 class Encoder {

    public static void main(String[] args) {


        // input scanner created
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the message you want to encode and hit enter: ");
        String text = sc.nextLine();

        // Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
        byte[] bytes = text.getBytes(StandardCharsets.UTF_8);

        // for each position in the 'string', starting with the first(0) position, add ten to that value then move to the next position
        // until i is less than the length of the 'string'
        for (int i = 0; i < bytes.length; ++i) 
            bytes[i] += 10;
            System.out.println("Your encoded message is: " + " " + Arrays.toString(bytes)); //prints array 

    }
}

class Decoder {

    public static void main(String[] args) {
        for (int i = 0; i < bytes.length; ++i) 
        bytes[i] -= 10;
        String stringA = new String(bytes);
        System.out.println("Your decoded message is :" + " " + stringA);
    }
  }

Целевой вывод

Введите сообщение вы хотите закодировать и нажать Enter: Hello world!

Ваше закодированное сообщение: [82, 111, 118, 118, 121, 42, -127, 121, 124, 118, 110, 43]

Ваше декодированное сообщение: Hello world!

Мой вопрос: В рамках одного класса мой кодировщик / декодер (со смещением) отлично работает. Моя проблема состоит в том, чтобы разделить эту программу на класс Encoder & Decoder (как показано в коде), а затем заставить все работать вместе в основном классе. Выше моя попытка, честно говоря, я не знаю, что поместить в основной класс, чтобы ie все вместе. Любая помощь приветствуется.

1 Ответ

1 голос
/ 20 июня 2020

Итак, идея здесь в том, что вы хотите иметь методы publi c в классах. Затем создайте экземпляры других классов в основном методе, затем вызовите эти методы.

Основной класс

public class MainTest {
  public static void main(String[] args) {
    Encoder encoder = new Encoder();
    Decoder decoder = new Decoder();
    byte[] bytes = encoder.encode();
    decoder.decode(bytes);
  }
}

Класс декодера

public class Decoder {
  public void decode(byte[] bytes) {
    for (int i = 0; i < bytes.length; ++i) {
      bytes[i] -= 10;
    }
    String stringA = new String(bytes);
    System.out.println("Your decoded message is :" + " " + stringA);
  }
}

Класс кодировщика

public class Encoder {
  public byte[] encode() {
    // input scanner created
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the message you want to encode and hit enter: ");
    String text = sc.nextLine();

    // Encodes this String into a sequence of bytes using the named charset, storing the result into
    // a new byte array.
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8);

    // for each position in the 'string', starting with the first(0) position, add ten to that value
    // then move to the next position
    // until i is less than the length of the 'string'
    for (int i = 0; i < bytes.length; ++i) {
      bytes[i] += 10;
      System.out.println(
          "Your encoded message is: " + " " + Arrays.toString(bytes)); // prints array
    }
    return bytes;
  }
}

...