LATIHAN BAHASA JAVA PEMROGRAMAN (STACK)
PALINDROM
Nama : Khoirunisa Jannatuzzahra
NPM : 21082010089
Prodi : Sistem Informasi
NPM : 21082010089
Prodi : Sistem Informasi
SOURCE CODE
*CLASS ITEM
package Stack;
public class cItem {
private String nama;
cItem next;
cItem (String n){
nama=n;
System.out.println("Item "+n+" dibuat.");
}
public String getNama(){
return nama;
}
}
*CLASS
package Stack;
public class cStack {
cItem top, bottom;
int jumlah;
cStack(){
top=bottom=null;
jumlah=0;
}
public void push(cItem baru){
if (top==null){
top=bottom=baru;
}
else{
baru.next=top;
top=baru;
}
System.out.println("Push sukses!!!");
System.out.println("..............");
}
public String pop(){
if (top.next==null){
//stack sisa 1 item
cItem t=top;
top=bottom=null;
t.next=null;
System.out.println("Pop sukses!!!!");
System.out.println("..............");
return t.getNama();
}
else{
//stack sisa >1 item
cItem t=top;
top=top.next;
System.out.println("Pop sukses!!!!");
System.out.println("..............");
t.next=null;
return t.getNama();
}
}
}
*CLASS PALINDROM
package Stack;
import java.util.Scanner;
public class Polindrom {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
cStack stack=new cStack();
int pilih=0;
System.out.println("==========PALINDROMEDIA==========");
System.out.println("-------------Welcome-------------");
do{
System.out.println("1. Cek kata");
System.out.println("2. Selesai");
System.out.print("Pilih = ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.print("Input = ");
String in = sc.next();
for (int i = 0; i < in.length(); i++) {
cItem n = new cItem(Character.toString(in.charAt(i)));
stack.push(n);
}
String t ="";
for (int i = 0; i < in.length(); i++) {
String a =stack.pop();
t=t+a;
}
if(in.equals(t)){
System.out.println("Output = PALINDROM");
System.out.println("Kata "+in+" adalah PALINDROM");
System.out.println("--------------------------------");
System.out.println("");
}
else {
System.out.println("Output = BUKAN PALINDROM");
System.out.println("Kata "+in+" adalah BUKAN PALINDROM");
System.out.println("--------------------------------");
System.out.println("");
}
break;
case 2:
System.out.println("------------THANK YOU-----------");
break;
}
}while(pilih!=2);
}
}
OUTPUT
Selamat mencoba dan semangat untuk menuntut ilmu :)



Komentar
Posting Komentar