ada 2 class gan, jangan salah. jadi setelah buat project baru, terus new file jenis class.. ini untuk class satunya..
class 1
package konversi;
import java.util.Scanner;
/**
*
* @author khanif
*/
public class Konversi {
public static void main(String args[]) {
System.out.println("Program Konversi Bilangan Desimal ke Biner");
System.out.println();
Scanner masuk = new Scanner(System.in);
System.out.print("Masukkan Angka: ");
int bil = masuk.nextInt();
int lihat = bil;
NewClass stack = new NewClass();
if (bil == 0) {
System.out.println("0");
}
while (bil != 0) {
int a = bil / 2;
int b = bil % 2;
stack.push(b);
bil =a;
}
System.out.print("Biner dari Angka "+lihat+" adalah: ");
stack.view();
System.out.println(" ");
}
}
import java.util.Scanner;
/**
*
* @author khanif
*/
public class Konversi {
public static void main(String args[]) {
System.out.println("Program Konversi Bilangan Desimal ke Biner");
System.out.println();
Scanner masuk = new Scanner(System.in);
System.out.print("Masukkan Angka: ");
int bil = masuk.nextInt();
int lihat = bil;
NewClass stack = new NewClass();
if (bil == 0) {
System.out.println("0");
}
while (bil != 0) {
int a = bil / 2;
int b = bil % 2;
stack.push(b);
bil =a;
}
System.out.print("Biner dari Angka "+lihat+" adalah: ");
stack.view();
System.out.println(" ");
}
}
class 2
package konversi;
import java.util.NoSuchElementException;
/**
*
* @author khanif
*/
public class NewClass {
public class Element {
public Object data;
public Element next;
public Element( Object data, Element next ) {
this.data = data;
this.next = next;
}
}
public Element top;
public void push( Object data ) {
if( isEmpty() ) {
top = new Element( data, null );
} else {
top = new Element( data, top );
}
}
public Object pop() throws NoSuchElementException {
if( isEmpty() ) {
throw new NoSuchElementException();
} else {
Object data = top.data;
top = top.next;
return data;
}
}
public Object peek() throws NoSuchElementException {
if( isEmpty() ) {
throw new NoSuchElementException();
} else {
return top.data;
}
}
public boolean isEmpty() {
return top == null;
}
public void view(){
while (!isEmpty()) {
System.out.print(pop());
}
}
}
import java.util.NoSuchElementException;
/**
*
* @author khanif
*/
public class NewClass {
public class Element {
public Object data;
public Element next;
public Element( Object data, Element next ) {
this.data = data;
this.next = next;
}
}
public Element top;
public void push( Object data ) {
if( isEmpty() ) {
top = new Element( data, null );
} else {
top = new Element( data, top );
}
}
public Object pop() throws NoSuchElementException {
if( isEmpty() ) {
throw new NoSuchElementException();
} else {
Object data = top.data;
top = top.next;
return data;
}
}
public Object peek() throws NoSuchElementException {
if( isEmpty() ) {
throw new NoSuchElementException();
} else {
return top.data;
}
}
public boolean isEmpty() {
return top == null;
}
public void view(){
while (!isEmpty()) {
System.out.print(pop());
}
}
}
No comments:
Post a Comment