Mengenai nama class anda dapat rubah sesuai program yang kalian buat
import java.io.*;
public class kalkulator {
double bil1,bil2;
public kalkulator(double bil1,double bil2)//nama class{
this.bil1=bil1;
this.bil2=bil2;
}
double tambah(){
return bil1+bil2;
}
double kurang(){
return bil1-bil2;
}
double kali(){
return bil1*bil2;
}
double bagi(){
return bil1/bil2;
}
public static void main (String[] args) {
kalkulator plus,minus,b,k;//nama objek
plus = minus = b = k = new kalkulator(0,0);
DataInputStream input = new DataInputStream(System.in);
double b1=0,b2=0;
try{
System.out.print ("Masukkan Nilai Bilangan 1 : ");
b1=Double.parseDouble(input.readLine());
System.out.print ("Masukkan Nilai Bilangan 2 : ");
b2=Double.parseDouble(input.readLine());
plus.bil1=b1;
plus.bil2=b2;
minus.bil1=b1;
minus.bil2=b2;
b.bil1=b1;
b.bil2=b2;
k.bil1=b1;
k.bil2=b2;
}catch (Exception e){System.out.println (e);}
System.out.println ("");
System.out.println (b1+" + "+b2+" = "+plus.tambah());
System.out.println (b1+" - "+b2+" = "+minus.kurang());
System.out.println (b1+" / "+b2+" = "+b.bagi());
System.out.println (b1+" * "+b2+" = "+k.kali());
}
}
Monday, 16 November 2015
Home »
Contoh Program
» Contoh Program menggunakan Class Dan Objek