Tugas Array dan Inheritance Kelompok 09


KELOMPOK 9 KELAS SI4003

IRENE IRAWATY (1202160043)

STEFI MUTIA SARI (1202160051)

KARINA TARIGAN (1202164188)

NO 2.

a. Soal

Create an interface MessageDecoder that has a single abstract method decode(cipherText), where cipherText is the message to be decoded. The method will return the decoded message. Modify the classes SubstitutionCipher and ShuffleCipher, as described in Exercises 16 and 17, so that they implement MessageDecoder as well as the interface MessageEncoder that Exercise 15 describes. Finally, write a program that allows a user to encode and decode messages entered on the keyboard.

b. Penjelasan soal

Jadi ini adalah untuk membuat MessageDecoder antarmuka yang memiliki metode abstrak tunggal decode (ciphertext),Memodifikasi kelas SubstitutionCipher dan ShuffleCipher, dan bagaimana menerapkan MessageDecoder serta MessageEncoder

c. Script code :

Class pertama

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package decod;

import java.util.Scanner;

/**

*

* @author Asus

*/

public class CodeProgram {

public static void main(String[]args){

System.out.println("Selamat Datang di program pembaca encode dan decode");

Scanner a = new Scanner(System.in);

System.out.println("masukan kunci yang anda mau untuk code anda ?");

int shift = a.nextInt();

SubstitutionCipher code = new SubstitutionCipher(shift);

boolean beres = true;

String rest = a.nextLine();

System.out.println("Masukkan pesan yang anda inginkan.");

String message = a.nextLine();

System.out.println("Encode (E) atau Decode (D)");

String respon = a.next();

respon = respon.toLowerCase();

if(respon.equals("e")){

System.out.println("Encode ke: "+ code.encode(message));

}else if(respon.equals("d")){

System.out.println("Decodes ke: "+ code.decode(message));

}

System.out.println("Apakah anda ingin melakukan pesan lagi ? (Y)");

respon = a.next();

respon = respon.toLowerCase();

if(!respon.equals("y")){

beres = true;

}

}

}

Class kedua

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package decod;

/**

*

* @author Asus

*/

class SubstitutionCipher {

private int shift;

public SubstitutionCipher(int n){

shift = n;

}

public String encode(String plainText){

String cipherText = " ";

for (int i = 0; i < plainText.length(); i++) {

Character c = plainText.charAt(i);

cipherText = cipherText + codeCharacter(c);

}

return cipherText;

}

private Character codeCharacter(Character c){

return (char)(c + shift);

}

public String decode(String cipherText){

String plainText = " ";

for (int i = 0; i < cipherText.length(); i++) {

Character c = cipherText.charAt(i);

plainText = plainText + decodeCharacter(c);

}

return plainText;

}

private Character decodeCharacter(Character c){

return (char)(c – shift);

}

}

d. Outputnya


Leave a Reply