Menghitung Keliling Persegi panjang

public class persegiPanjang
{
public static void main(String[] args)
{
int luas, lebar, keliling;
luas = 20;
lebar = 45;
keliling = 2 * (luas + lebar);
System.out.println(“Hasil Keliling: “+ keliling);
}
}

program matriks

nama filenya “penjumlahan”

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class penjumlahan {

public static void main(String[] args) {

int a1;
int a2;
int a3;
int a4;
int b1;
int b2;
int b3;
int b4;
int c1;
int c2;
int c3;
int c4;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println(“Matriks penjumlahan ordo 2×2”);
System.out.println();
System.out.println(“Matriks A”);
System.out.print(“Masukan elemen 1,1 : “);
a1= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 1,2 : “);
a2= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 2,1 : “);
a3= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 2,2 : “);
a4= Integer.parseInt(br.readLine());
System.out.println();
System.out.println(“Matriks B”);
System.out.print(“Masukan elemen 1,1 : “);
b1= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 1,2 : “);
b2= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 2,1 : “);
b3= Integer.parseInt(br.readLine());
System.out.print(“Masukan elemen 2,2 : “);
b4= Integer.parseInt(br.readLine());
c1=a1+b1;
c2=a2+b2;
c3=a3+b3;
c4=a4+b4;
System.out.println();
System.out.println(“Hasil Matriks :”);
System.out.print(+c1);
System.out.print(” ” +c2);
System.out.println();
System.out.print(+c3);
System.out.print(” ” +c4);
System.out.println();
}
catch(Exception e) {
System.out.println(“Error : ” + e);
}
}
}

Program MIDlet pertambahan

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class qwerty extends MIDlet
{
layar myLayar;

public qwerty ()
{
myLayar = new layar();
}

public void startApp()
{
Display.getDisplay(this).setCurrent(myLayar);
}
public void destroyApp(boolean b) {notifyDestroyed();}
public void pauseApp(){}

}

class layar extends Canvas implements Runnable
{
int[] x, y;
int[] arahX, arahY;
int now;

public layar()
{
now = 0;
x = new int[5];
y = new int[5];
arahX = new int[5];
arahY = new int[5];

for (int i=0; i < 5; i++) {
x[i] = 0;
y[i] = 0;
arahY[i] = 0;
arahX[i] = 0;
}

Thread th = new Thread(this);
th.start();
}

public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);

for (int i=0; i < 5; i++) {
if (now == i) g.setColor(0x00ff00);
else g.setColor(0x0000ff);
g.fillRoundRect(x[i], y[i], 10, 10, 10, 10);
}
}

public void run()
{
while (true)
{
for (int i=0; i getWidth()) || (x[i] getHeight()) || (y[i] 4) now = 0;
arahX[now] = 1;
arahY[now] = 1;
break;
case KEY_NUM2:
now++;
if (now > 4) now = 0;
arahX[now] = 0;
arahY[now] = -1;
break;
case KEY_NUM4:
now++;
if (now > 4) now = 0;
arahX[now] = -1;
arahY[now] = 0;
break;
case KEY_NUM6:
now++;
if (now > 4) now = 0;
arahX[now] = 1;
arahY[now] = 0;
break;
case KEY_NUM8:
now++;
if (now > 4) now = 0;
arahX[now] = 0;
arahY[now] = 1;
break;
}
}
}

Program Menghitung Pecahan Uang

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tugas;

/**
*
* @author hendrik
*/
import javax.swing.JOptionPane;
public class pecahan{ // Membuat class pecahan.
public static void main(String args[]){
int uang; //mendeklarasikan var uang sebagai input pertama.
JOptionPane.showMessageDialog(null,”Menghitung Jumlah Uang”,”Pesan”,JOptionPane.WARNING_MESSAGE);
do{ //Menampilkan pesan pertama.
String str=JOptionPane.showInputDialog(“Masukkan jumlah uang”); //Menampilakan proses penginputan.
uang=Integer.parseInt(str); //merubah interger uang menjadi String.
int j100rbu, j50rbu, j20rbu, j10rbu, j5rbu, j2rbu, j1rbu, j5rtus, j2rtus, j1rtus, j50; //mendeklarasikan jenis mata uang.
j100rbu=uang/100000; //membagi(division) uang dengan jenis pecahan sehingga mendapatkan jumlah pecahan.
uang%=100000;
j50rbu=uang/50000;
uang%=50000;
j20rbu=uang/20000;
uang%=20000;
j10rbu=uang/10000;
uang%=10000;
j5rbu=uang/5000;
uang%=5000;
j2rbu=uang/2000;
uang%=2000;
j1rbu=uang/1000;
uang%=1000;
j5rtus=uang/500;
uang%=500;
j2rtus=uang/200;
uang%=200;
j1rtus=uang/100;
uang%=100;
j50=uang/50;
uang%=50;

JOptionPane.showMessageDialog(null,”Jumlah uang seratus ribu = “+j100rbu
//Menampilkan jenis pecahan.
‘\n’+”Jumlah uang lima puluh ribu = “+j50rbu+
‘\n’+”Jumlah uang dua puluh ribu = “+j20rbu+
‘\n’+”Jumlah uang sepuluh ribu = “+j10rbu+
‘\n’+”Jumlah uang lima ribu = “+j5rbu+
‘\n’+”Jumlah uang dua ribu = “+j2rbu+
‘\n’+”Jumlah uang seribu = “+j1rbu+
‘\n’+”Jumlah uang lima ratus = “+j5rtus+
‘\n’+”Jumlah uang dua ratus = “+j2rtus+
‘\n’+”Jumlah uang seratus = “+j1rtus+
‘\n’+”Jumlah uang lima puluh = “+j50);
} //Akhir JOptionPane.
while(JOptionPane.showConfirmDialog(null,”Hitung lagi?”,”Konfirmasi”, //mengulang proses jika tekan ‘yes’.
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)== JOptionPane.YES_OPTION);
} //Akhir method main.
} // Akhir kelas pecahan.

contoh-program-painter-java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter extends JFrame {
private int pointCount = 0;
private Point points[] = new Point[1000];

public Painter () {
super (“Program menggambar sederhana”);

getContentPane().add(new JLabel(“Drag mouse to draw”), BorderLayout.SOUTH);

addMouseMotionListener (
new MouseMotionAdapter() {
public void mouseDragged (MouseEvent e) {
if (pointCount < points.length) {
points[pointCount] = e.getPoint();
++pointCount;
repaint();
}
}
} //end of anonymous class
); //end method addMotionListener

setSize (300,150);
setLocationRelativeTo(null);
setVisible(true);
}

public void paint (Graphics g) {
super.paint(g);
for (int i = 0; i < points.length && points[i] != null; i++) {
g.setColor(Color.red);
g.fillOval (points[i].x, points[i].y, 4,4);
}
}

public static void main (String args[]) {
Painter test = new Painter();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Contoh Program Event Handling di Java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ClickMe2 extends JFrame {

private JButton tombol, btnExit;

public ClickMe2() {

super (“Event Handling”);

Container container = getContentPane();

container.setLayout(new FlowLayout());

ClickListener cl = new ClickListener ();

tombol = new JButton (“Click Me!”);

tombol.addActionListener(cl);

container.add(tombol);

btnExit = new JButton (“Exit”);

btnExit.addActionListener(cl);

container.add(btnExit);

setSize (200,100);

setVisible (true);

}

public static void main (String arg[]) {

ClickMe2 test = new ClickMe2();

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//inner class

private class ClickListener implements ActionListener {

public void actionPerformed (ActionEvent e) {

if (e.getSource() == tombol) {

JOptionPane.showMessageDialog(null, “You click me again, guys !!!”);

} else if (e.getSource() == btnExit){

JOptionPane.showMessageDialog(null, “See you, guys !”);

System.exit(0);

}

}

}

}

Selamat mencoba, semoga bermanfaat

Iklan

6 responses »

  1. Avis Car Rental berkata:

    I am continually browsing online for posts that can help me. Thx!

  2. Jules Sudduth berkata:

    My dream retirement would almost certainly be a couple several years in unique countries, enjoy and absorbing the globe around me with a loved one!

  3. rumahradhen berkata:

    thank you for your visit, if you’ll like shopping, please visit this blog http://radenfashion.wordpress.com/

  4. Drusilla Wogan berkata:

    I just want to mention I’m all new to blogs and definitely enjoyed you’re blog. Most likely I’m want to bookmark your website . You really have good posts. Many thanks for sharing your website.

  5. white whall berkata:

    Healing’s Dragon

    to search out matters to boost my web page!I suppose its okay to help make utilization of several of the concepts!!

  6. Indah berkata:

    sebelumnya makasih
    Coba Program yang lain dong.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s