PALINDROME
Nama: Rayhan Qalby Ramadhan
NPM : 21082010008
- Tampilan NetBeans
- Tampilan Output Palindrome
- Tampilan Output Bukan Palindrome
SOURCE CODE:
Class AppPalindrome :
package pkgStackPalindrome;
import java.util.Scanner;
public class appPalindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
cStackPalindrome stack = new cStackPalindrome();
int pilih = 0;
do{
System.out.println("\n==============================================");
System.out.println("\t\t PALINDROME\n" +"\t\tMari Kita Coba!"
+ "\n==============================================");
System.out.println("1. Cek Palindrome");
System.out.println("2. Exit");
System.out.print("Pilih (1/2): ");
pilih = sc.nextInt();
switch(pilih){
case 1:
System.out.print("Masukkan kata: ");
String kata = sc.next();
for(int i = 0; i < kata.length(); i++){
cElemenPalindrome e = new cElemenPalindrome(Character.toString(kata.charAt(i)));// Membuat objek baru
stack.Push(e);
}
String output = "";
for(int i = 0; i < kata.length(); i++){
String e = stack.Pop();
output= output + e;
}
System.out.println("");
if(kata.equals(output)){
System.out.println("Kata = "+kata+": PALINDROM");
}else{
System.out.println("Kata = "+kata+": BUKAN PALINDROM");
}
break;
case 2:
System.out.println("================= Terima Kasih ================");
break;
}
}while(pilih!=2);
}
}
Class cElemenPalindrome :
package pkgStackPalindrome;
public class cElemenPalindrome {
private String nama;
cElemenPalindrome next;
cElemenPalindrome (String n){
nama = n;
System.out.println("Object "+n+" dibuat");
}
public String getNama(){
return nama;
}
}
Class cStackPalindrome :
package pkgStackPalindrome;
public class cStackPalindrome {
cElemenPalindrome top, bottom;
int jumlah;
cStackPalindrome(){
top = bottom = null;
jumlah = 0;//untuk mengawali
}
public void Push(cElemenPalindrome baru){
if(top == null){//jika isi stack masih kosong
top = bottom = baru;
}else{//jika isi stack sudah ada isinya
baru.next = top;
top = baru;
}
System.out.println("Push Sukses!");
}
public String Pop(){
if(top == null){
System.out.println("Isi stack kosong!");
return null;
}
else if(top.next==null){//isi stack = 1
cElemenPalindrome temp = top;
top = bottom = null;
System.out.println("Pop Sukses!");
return temp.getNama();
}else{//isi stack > 1 elemen
cElemenPalindrome temp = top;
top = top.next;
temp.next = null;
System.out.println("Pop Sukses!");
return temp.getNama();
}
}
}
Hasil Output :
==============================================
PALINDROME
Mari Kita Coba!
==============================================
1. Cek Palindrome
2. Exit
Pilih (1/2): 1
Masukkan kata: KATAK
Object K dibuat
Push Sukses!
Object A dibuat
Push Sukses!
Object T dibuat
Push Sukses!
Object A dibuat
Push Sukses!
Object K dibuat
Push Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Kata = KATAK: PALINDROM
==============================================
PALINDROME
Mari Kita Coba!
==============================================
1. Cek Palindrome
2. Exit
Pilih (1/2): 1
Masukkan kata: KAMAR
Object K dibuat
Push Sukses!
Object A dibuat
Push Sukses!
Object M dibuat
Push Sukses!
Object A dibuat
Push Sukses!
Object R dibuat
Push Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Pop Sukses!
Kata = KAMAR: BUKAN PALINDROM
==============================================
PALINDROME
Mari Kita Coba!
==============================================
1. Cek Palindrome
2. Exit
Pilih (1/2): 2
================= Terima Kasih ================
BUILD SUCCESSFUL (total time: 1 minute 23 seconds)
Comments
Post a Comment