import java.io.*;
import java.util.Scanner;
class makanan
{
public int porsi;
public double harga;
}
class nasi extends makanan
{
public void setharga()
{
harga = 1000;
}
}
class lauk extends makanan
{
public void setharga()
{
harga = 2000;
}
}
class sayur extends makanan
{
public void setharga(double h) //set harga lewat input user
{
harga = h;
}
public double getharga() //mengambil info harga dr class
{
return(harga);
}
}
public class program //utk menjalankan program utama
{
public static void main(String[] args)
{
double h;
sayur s = new sayur(); //create object utk masing2 class
lauk l = new lauk();
nasi n = new nasi();
Scanner sc = new Scanner(System.in);
System.out.print("Harga : ");
h = sc.nextDouble();
s.setharga(h);
System.out.println("Harga sayur :"+s.getharga());
n.harga = 1000; //akses langsung ke atribut
n.porsi = 2;
biaya = n.harga * n.porsi;
System.out.println("Biaya :"+biaya);
//menu program dan manipulasi object yg lain
//bisa diletakkan di sini
}
}
Contoh Program Queue Array Sederhana dalam Java
import java.io.*;
public class queue_array
{
private int maxsize; //maksimum ukuran queue
private double [] queuearray; //array untuk menyimpan queue
private int front; //indeks elemen terdepan
private int rear; //indeks elemen paling belakang
private int jumlah; //menyimpan jumlah total elemen yang ada
public void inisiasi(int s) //inisialisasi queue
{
maxsize = s;
queuearray = new double [maxsize];
front = rear = jumlah = 0;
}
public void enqueue(double data)
{
if (jumlah==maxsize)
System.out.println("Queue Penuh. "+data+" Tidak Bisa Masuk");
else
{
//System.out.print(rear+" ");
queuearray[rear] = data;
rear = (rear+1)%maxsize;
jumlah++;
System.out.println(data +" Masuk ke Queue");
}
}
public double dequeue()
{
double temp;
if (jumlah==0)
{
System.out.println("Queue Sudah Kosong");
return(-1);
}
else
{
temp = queuearray[front];
front = (front+1)%maxsize;
jumlah--;
System.out.println(temp + " Keluar dari Queue");
return (temp);
}
}
public void view()
{
System.out.print("Isi Queue: ");
if (jumlah > 0 && front < rear) //belum terjadi circular
for(int i=front; i0 && rear <= front) //terjadi circular
{
for(int i=front; i<=maxsize-1; i++) //bagian depan
System.out.print(queuearray[i] + " ");
for(int i=0; i<rear; i++) //bagian belakang
System.out.print(queuearray[i] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
queue_array queue = new queue_array();
queue.inisiasi(3);
queue.enqueue(2);
queue.enqueue(5);
queue.enqueue(3);
queue.enqueue(7);
queue.view();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.view();
queue.enqueue(8);
queue.enqueue(9);
queue.enqueue(7);
queue.enqueue(10);
queue.view();
queue.dequeue();
queue.view();
}
}
//Selamat Mencoba ^_^
Download Source Code
public class queue_array
{
private int maxsize; //maksimum ukuran queue
private double [] queuearray; //array untuk menyimpan queue
private int front; //indeks elemen terdepan
private int rear; //indeks elemen paling belakang
private int jumlah; //menyimpan jumlah total elemen yang ada
public void inisiasi(int s) //inisialisasi queue
{
maxsize = s;
queuearray = new double [maxsize];
front = rear = jumlah = 0;
}
public void enqueue(double data)
{
if (jumlah==maxsize)
System.out.println("Queue Penuh. "+data+" Tidak Bisa Masuk");
else
{
//System.out.print(rear+" ");
queuearray[rear] = data;
rear = (rear+1)%maxsize;
jumlah++;
System.out.println(data +" Masuk ke Queue");
}
}
public double dequeue()
{
double temp;
if (jumlah==0)
{
System.out.println("Queue Sudah Kosong");
return(-1);
}
else
{
temp = queuearray[front];
front = (front+1)%maxsize;
jumlah--;
System.out.println(temp + " Keluar dari Queue");
return (temp);
}
}
public void view()
{
System.out.print("Isi Queue: ");
if (jumlah > 0 && front < rear) //belum terjadi circular
for(int i=front; i0 && rear <= front) //terjadi circular
{
for(int i=front; i<=maxsize-1; i++) //bagian depan
System.out.print(queuearray[i] + " ");
for(int i=0; i<rear; i++) //bagian belakang
System.out.print(queuearray[i] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
queue_array queue = new queue_array();
queue.inisiasi(3);
queue.enqueue(2);
queue.enqueue(5);
queue.enqueue(3);
queue.enqueue(7);
queue.view();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.view();
queue.enqueue(8);
queue.enqueue(9);
queue.enqueue(7);
queue.enqueue(10);
queue.view();
queue.dequeue();
queue.view();
}
}
//Selamat Mencoba ^_^
Download Source Code
Contoh Program Stack Array Sederhana dalam Java
import java.io.*;
public class stack_array
{
private int maxsize; //penentu batas elemen stack maksimum
private double [] stackarray; //array untuk menyimpan stack
private int top; //indeks array
public void inisiasi(int s) //menentukan ukuran kapasitas stack
{
maxsize = s;
stackarray = new double [maxsize];
top = -1;
}
public void push(double data)
{
if (top>=maxsize-1)
System.out.println("Stack Penuh. "+data+" Tidak Bisa Masuk");
else
{
top++;
stackarray[top] = data;
System.out.println(data +" Masuk ke Stack");
}
}
public double pop()
{
double temp;
if (top>=0)
{
temp = stackarray[top];
System.out.println(temp + " Keluar dari Stack");
top--;
return (temp);
}
else
{
System.out.println("Stack Sudah Kosong");
return(-1);
}
}
public void view()
{
System.out.print("Isi Stack: ");
for(int i=0; i<=top; i++)
System.out.print(stackarray[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
stack_array stack = new stack_array();
stack.inisiasi(3);
stack.push(3);
stack.push(4);
stack.push(2);
stack.view();
stack.push(5);
stack.push(1);
stack.pop();
stack.pop();
stack.view();
stack.pop();
stack.pop();
stack.pop();
stack.push(6);
stack.push(8);
stack.push(7);
stack.push(9);
stack.pop();
stack.view();
}
}
//Selamat mencoba ^_^
public class stack_array
{
private int maxsize; //penentu batas elemen stack maksimum
private double [] stackarray; //array untuk menyimpan stack
private int top; //indeks array
public void inisiasi(int s) //menentukan ukuran kapasitas stack
{
maxsize = s;
stackarray = new double [maxsize];
top = -1;
}
public void push(double data)
{
if (top>=maxsize-1)
System.out.println("Stack Penuh. "+data+" Tidak Bisa Masuk");
else
{
top++;
stackarray[top] = data;
System.out.println(data +" Masuk ke Stack");
}
}
public double pop()
{
double temp;
if (top>=0)
{
temp = stackarray[top];
System.out.println(temp + " Keluar dari Stack");
top--;
return (temp);
}
else
{
System.out.println("Stack Sudah Kosong");
return(-1);
}
}
public void view()
{
System.out.print("Isi Stack: ");
for(int i=0; i<=top; i++)
System.out.print(stackarray[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
stack_array stack = new stack_array();
stack.inisiasi(3);
stack.push(3);
stack.push(4);
stack.push(2);
stack.view();
stack.push(5);
stack.push(1);
stack.pop();
stack.pop();
stack.view();
stack.pop();
stack.pop();
stack.pop();
stack.push(6);
stack.push(8);
stack.push(7);
stack.push(9);
stack.pop();
stack.view();
}
}
//Selamat mencoba ^_^
Contoh Program Java Swing Sederhana
Berikut ini adalah sebuah contoh implementasi library swing pada bahasa Java untuk membuat program dengan tampilan GUI (Graphical User Interface). Program sederhana ini adalah tentang penghitungan luas sebuah persegi. User diminta memasukkan data panjang dan lebar, kemudian akan dihitung dan ditampilkan luasnya setelah user menekan tombol "Hitung Luas". Project ini terdiri atas dua class yang terpisah, yaitu class persegi dan class hitung. Class persegi berfungsi untuk pengaturan tampilan antarmuka program, sedangkan class hitung berperan sebagai action yang akan dilakukan sebagai respon terjadinya penekanan tombol oleh user. Berikut adalah source code lengkapnya. Dan terima kasih atas kunjungannya. Semoga bermanfaat :)
isi class persegi:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
class persegi {
public static void main(String args[]) {
//deklarasikan komponen2
JFrame frame;
Container contentPane;
JTextField tfpanjang, tflebar, tfluas;
JButton button;
FlowLayout layout;
frame = new JFrame();
//inisialisasi/ setting tiap2 komponen
frame.setTitle("Program Perhitungan Luas Persegi");
contentPane = frame.getContentPane();
tfpanjang = new JTextField("Masukkan panjang", 10);
tflebar = new JTextField("Masukkan lebar", 10);
tfluas = new JTextField("", 10);
button = new JButton("Hitung Luas");
//sambungkan button dgn perintah yg akan dijalankan
//kirimkan komponen2 ke action listener
button.addActionListener(new hitung(tfpanjang,tflebar,tfluas));
//rakitkan komponen2
contentPane.add(tfpanjang);
contentPane.add(tflebar);
contentPane.add(button);
contentPane.add(tfluas);
layout = new FlowLayout();
contentPane.setLayout(layout);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
isi class hitung:
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class hitung implements ActionListener {
JTextField tp,tl,tf;
hitung(JTextField tfp,JTextField tfl,JTextField tfL) {
//tampung data dari class container ke local
//by refference
tp = tfp;
tl = tfl;
tf = tfL;
}
public void actionPerformed(ActionEvent e) {
int temp,p,l;
//tampung isi textfield ke string,
//kemudian ekstrak data menjadi angka.
String sp = tp.getText();
p = Integer.parseInt(sp);
String sl = tl.getText();
l = Integer.parseInt(sl);
temp = p*l; //hitung luas persegi
//kirimkan hasil temp ke tf
//tf & tfL adalah identik, yaitu text field Luas
//di class pemanggil actionlistener
tf.setText(String.valueOf(temp));
}
}
isi class persegi:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
class persegi {
public static void main(String args[]) {
//deklarasikan komponen2
JFrame frame;
Container contentPane;
JTextField tfpanjang, tflebar, tfluas;
JButton button;
FlowLayout layout;
frame = new JFrame();
//inisialisasi/ setting tiap2 komponen
frame.setTitle("Program Perhitungan Luas Persegi");
contentPane = frame.getContentPane();
tfpanjang = new JTextField("Masukkan panjang", 10);
tflebar = new JTextField("Masukkan lebar", 10);
tfluas = new JTextField("", 10);
button = new JButton("Hitung Luas");
//sambungkan button dgn perintah yg akan dijalankan
//kirimkan komponen2 ke action listener
button.addActionListener(new hitung(tfpanjang,tflebar,tfluas));
//rakitkan komponen2
contentPane.add(tfpanjang);
contentPane.add(tflebar);
contentPane.add(button);
contentPane.add(tfluas);
layout = new FlowLayout();
contentPane.setLayout(layout);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
isi class hitung:
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class hitung implements ActionListener {
JTextField tp,tl,tf;
hitung(JTextField tfp,JTextField tfl,JTextField tfL) {
//tampung data dari class container ke local
//by refference
tp = tfp;
tl = tfl;
tf = tfL;
}
public void actionPerformed(ActionEvent e) {
int temp,p,l;
//tampung isi textfield ke string,
//kemudian ekstrak data menjadi angka.
String sp = tp.getText();
p = Integer.parseInt(sp);
String sl = tl.getText();
l = Integer.parseInt(sl);
temp = p*l; //hitung luas persegi
//kirimkan hasil temp ke tf
//tf & tfL adalah identik, yaitu text field Luas
//di class pemanggil actionlistener
tf.setText(String.valueOf(temp));
}
}
Another Example about Insert and Delete Linked List
Below is another example about linked list implementation using C++ language. In this source code there are three basic operation described, those are insertion, deletion and show the nodes inside the linked list. You are recommended to modify some unpractical part of the code, one of it is insertion part. The insertion part will be better if you use an iteration to make it more efficient. Thanks for visiting :)
#include "iostream.h"
#include "string.h"
struct node{
char name [20];
int distance; //data
struct node *next;//pointer to point to the next node
};
typedef node * nodeptr;
void insert(nodeptr &head, int data, char nama []){
nodeptr temp;
temp=new node;
temp->distance=data;
strcpy(temp->name,nama);
temp->next=NULL;
if (head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
}
};
char * delnode(nodeptr &head){
nodeptr temp;
char hold[20];
if (head!=NULL) {
temp=head;
strcpy(hold,temp->name);
head=temp->next;
delete temp;
return hold;}
else{
cout<<"Link list is empty"<<endl; return(0);}
}
void main()
{ nodeptr head=NULL, ptr;
int jarak;
char kota [20];
insert(head, 15, "surabaya");
insert(head, 12, "mojokerto");
insert(head, 23, "lamongan");
cout<>kota;
cout<>jarak;
insert(head,jarak,kota);
//show the begining
for(ptr=head; ptr!=NULL; ptr=ptr->next)
cout<distance<<" "<name<<endl;
//delete the front node
strcpy(kota,delnode(head));
cout<<"deleted = " <<kota<next)
cout<distance<<" "<name<<endl;
}
#include "iostream.h"
#include "string.h"
struct node{
char name [20];
int distance; //data
struct node *next;//pointer to point to the next node
};
typedef node * nodeptr;
void insert(nodeptr &head, int data, char nama []){
nodeptr temp;
temp=new node;
temp->distance=data;
strcpy(temp->name,nama);
temp->next=NULL;
if (head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
}
};
char * delnode(nodeptr &head){
nodeptr temp;
char hold[20];
if (head!=NULL) {
temp=head;
strcpy(hold,temp->name);
head=temp->next;
delete temp;
return hold;}
else{
cout<<"Link list is empty"<<endl; return(0);}
}
void main()
{ nodeptr head=NULL, ptr;
int jarak;
char kota [20];
insert(head, 15, "surabaya");
insert(head, 12, "mojokerto");
insert(head, 23, "lamongan");
cout<>kota;
cout<>jarak;
insert(head,jarak,kota);
//show the begining
for(ptr=head; ptr!=NULL; ptr=ptr->next)
cout<distance<<" "<name<<endl;
//delete the front node
strcpy(kota,delnode(head));
cout<<"deleted = " <<kota<next)
cout<distance<<" "<name<<endl;
}
Subscribe to:
Posts (Atom)