program menghitung java

program menghitung java


langkah pertama ketikkan coding dibawah ini:



========================================================================







import java.util.Scanner;




class Menghitung{

public static void main(String args[]){

Scanner baca = new Scanner(System.in);

Hitung hitung = new Hitung();




int pilihan = 0;

double angka1=0, angka2=0;

String inputString;




do {

System.out.println("Menu Pilihan");

System.out.println("1. Penjumlahan");

System.out.println("2. Pengurangan");

System.out.println("3. Perkalian");

System.out.println("4. Pembagian");

System.out.println("0. Selesai");

System.out.println("Menu Dipilih:");




try{

pilihan = baca.nextInt();

}catch (Exception ex) {

inputString = baca.nextInt();

pilihan = -1;

System.out.println("Nilai pilihan salah, kesalahan: "+ex.getMessage());

}




if (pilihan > 0) {

System.out.println();

System.out.print("Angka pertama: "); angka1 = baca.nextDouble();

System.out.print("Angka kedua: "); angka2 = baca.nextDouble();

}




System.out.println();

switch (pilihan){

case 0: System.out.print("Selesai");

break;

case 1: System.out.print(angka1+" + "+angka2+" = "+hitung.tambah(angka1, angka2));

break;

case 2: System.out.print(angka1+" - "+angka2+" = "+hitung.kurang(angka1, angka2));

break;

case 3: System.out.print(angka1+" * "+angka2+" = "+hitung.kali(angka1, angka2));

break;

case 4: System.out.print(angka1+" : "+angka2+" = "+hitung.bagi(angka1, angka2));

break;

}

System.out.println();

} while (pilihan != 0);

}

}



========================================================================

save dengan nama Menghitung.java

========================================================================



kemudian buat project java lagi, isikan dengan coding dibawah:

========================================================================


public abstract class OperasiAritmatika implements Aritmatika{

public double tambah(double op1, double op2){

return op1+op2;

}




public double kurang(double op1, double op2){

return op1-op2;

}




public abstract double kali(double op1, double op2);

public abstract double bagi(double op1, double op2);

}





========================================================================

simpan dengan nama OperasiAritmatika.java

========================================================================

selanjutnya buat project java baru lagi, ini codingnya:
========================================================================

public interface Aritmatika{

public double tambah(double op1, double op2);

public double kurang(double op1, double op2);

}





========================================================================

simpan dengan nama Aritmatika.java



========================================================================











Share this:

Enter your email address to get update from Kompi Ajaib.

Tidak ada komentar