Jaxle7
Jaxle7

Reputation: 1

How to finish my encode & decode program?

Question at bottom

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

Targeted output

Enter the message you want to encode and hit enter: Hello world!

Your encoded message is: [82, 111, 118, 118, 121, 42, -127, 121, 124, 118, 110, 43]

Your decoded message is : Hello world!

My question: Under a single class, My encoder/decoder(with offset) works great. My problem is separating this program into a Encoder & Decoder class(As shown in code), then making it all work together in a main class. Above is my attempt, I honestly don't know what to put in the Main class to tie everything together. Any help is greatly appreciated.

Upvotes: 0

Views: 173

Answers (1)

scigs
scigs

Reputation: 624

So the idea here is that you want to have public methods in the classes. Then instantiate the other classes in the main method, then invoke those methods.

Main Class

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

Decoder Class

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

Encoder Class

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

Upvotes: 1

Related Questions