Pertama buat class kalkulator yang isinya berupa method-method seperti untuk pembagian, perkalian dan sebagainya
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testingapp;
/**
*
* @author ikbal
*/
public class Kalkulator {
double operan1;
double operan2;
void isiOperan1(double x){
operan1 = x;
}
void isiOperan2(double y){
operan2 = y;
}
double tambah(){
return operan1+operan2;
}
double kurang(){
return operan1 - operan2;
}
double kali(){
return operan1*operan2;
}
double bagi(){
return operan1/operan2;
}
double pangkat(){
return Math.pow(operan1,operan2);
}
}
Dan untuk mengetest apakah jalan atau tidak kita buat klas lagi yaitu kalkulatortester langsung aja:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testingapp;
/**
*
* @author ikbal
*/
public class Kalkulatortester {
public static void main (String[] aghs ){
Kalkulator ikbal = new Kalkulator();
ikbal.isiOperan1(10);
ikbal.isiOperan2(3);
System.out.println("Tambah : "+ikbal.tambah());
System.out.println("Kurang : "+ikbal.kurang());
System.out.println("kali : "+ikbal.kali());
System.out.println("Bagi : "+ikbal.bagi());
System.out.println("Pangkat : "+ikbal.pangkat());
}
}
ok sekiatn dulu, jika ingin bertanya silahkan wasalah...
No comments:
Post a Comment