Kalkulator Sederhana dengan Java

Code :

import jeliot.io.*;

public class MyClass { //membuat kelas bernama MyClass
    public static void tambah(double A, double B){ //fungsi penambahan
        System.out.println(A + " + " + B + " = " + (A+B));
    }

    public static void kurang(double A, double B){ //fungsi pengurangan
        System.out.println(A + " - " + B + " = " + (A-B));
    }

    public static void kali(double A, double B){ //fungsi perkalian
        System.out.println(A + " x " + B + " = " + (A*B));
    }

    public static void bagi(double A, double B){ //fungsi pembagian
        System.out.println(A + " : " + B + " = " + (A/B));
    }

    public static void main() {
    double A,B;
    int tanda;

    System.out.println("PROGRAM 2 ANGKA");
    System.out.println("==========================");
    System.out.print("Masukan angka pertama (A) : ");
    A=Input.readDouble(); //meminta inputan user berupa data integer
    System.out.print("Masukan angka kedua (B): ");
    B=Input.readDouble(); //meminta inputan user berupa data integer
    System.out.println();
    System.out.println("A : " + A); //menampilkan nilai a
    System.out.println("B : " + B); //menampilkan nilai b
    System.out.println();
    System.out.println("OPERATOR");
    System.out.println("[1] tambah");
    System.out.println("[2] kurang");
    System.out.println("[3] kali");
    System.out.println("[4] bagi");
    System.out.println();
    System.out.print("Pilih Operasi : ");

    tanda=Input.readInt();
    switch(tanda){
        case 1 : tambah(A,B);break; //tanda=1, fungsi tambah akan dipanggil
        case 2 : kurang(A,B);break; //tanda=2, fungsi kurang akan dipanggil
        case 3 : kali(A,B);break; //tanda=3, fungsi kali akan dipanggil
        case 4 : bagi(A,B);break; //tanda=4, fungsi bagi akan dipanggil
    }
    }
}

Output :

PROGRAM 2 ANGKA
==========================
Masukan angka pertama (A) : 2.0
Masukan angka kedua (B): 3.0

A : 2.0
B : 3.0

OPERATOR
[1] tambah
[2] kurang
[3] kali
[4] bagi

Pilih Operasi : 3
2.0 x 3.0 = 6.0

Posted on April 23, 2011, in Algoritma & Pemrograman and tagged , , , . Bookmark the permalink. Tinggalkan komentar.

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

%d blogger menyukai ini: