Program kamus sederhana untuk memahami konsep object oriented programming pada java.
Kelas translator
public abstract class Translator {
protected String input=null;
protected String[] indo={"ikan","ayam","kambing","tas","kucing","dompet","buku","sabuk","lari","makan"};
protected String[] eng={"fish","chicken","goat","bag","cat","wallet","book","belt","run","eat"};
protected String temp="Data tidak ditemukan";
public String transIE()
{
for(int i=0;i<10;i++)
{
if(input.equals(indo[i]))
{
temp=eng[i];
}
}
return temp;
}
public String transEI()
{
for(int i=0;i<10;i++)
{
if(input.equals(eng[i]))
{
temp=indo[i];
}
}
return temp;
}
public void setInput(String input)
{
this.input=input;
}
public String getInput()
{
return input;
}
abstract boolean compare(String a,String b);
}
Kelas Translator X
public class TranslatorX extends Translator{
TranslatorX(){
super();
}
boolean compare(String a, String b) {
return(a.equals(b));
}
}
Kelas Utama
import java.util.Scanner;
public class TPModul6 {
public static void main(String[] args) {
Translator X= new TranslatorX();
Scanner in=new Scanner(System.in);
Scanner i=new Scanner(System.in);
System.out.println("MENU");
System.out.println("1.Indonesia - Inggris");
System.out.println("2.Inggris - Indonesia");
System.out.println("3.Membandingkan kata");
System.out.println("\nMasukkan pilihan anda(1/2):");
int x=in.nextInt();
if(x==3){
System.out.println("Masukkan kata yang pertama :");
String a=in.nextLine();
System.out.println("Masukkan kata yang kedua :");
String b=in.nextLine();
System.out.println("Kata yang anda masukkan sama = "+X.compare(a, b));
}
else{
System.out.println("Masukkan kata yang ingin ditranslasi :");
String temp=i.nextLine();
X.setInput(temp);
switch(x){
case 1:
{
System.out.println("Arti kata yang anda masukkan adalah :"+X.transIE());
break;
}
case 2:
{
System.out.println("Arti kata yang anda masukkan adalah :"+X.transEI());
break;
}
}
}
}
}
Comments
Post a Comment