Pertama buat class konversiSuhu.java dimana terdapat dua atribut celcius dan hasil, kemudian buat method-method nya seperti dibawah
package suhu;
public class konversiSuhu {
public double celcius;
private double hasil;
public double CtoR(){
double reamur;
reamur = this.celcius * 0.8;
return reamur;
}
public double CtoK(){
double kelvin;
kelvin = this.celcius+273.15;
return kelvin;
}
public double CtoF(){
double fahrenheit;
fahrenheit = this.celcius*1.8+3.2;
return fahrenheit;
}
public void tmapilhasil(){
System.out.printf("%.2f\n",this.CtoR());
System.out.printf("%.2f\n",this.CtoK());
System.out.printf("%.2f\n",this.CtoF());
}
}
Setelah itu buat class testernya sepereti dibawah:
package suhu;
public class konversisuhutester {
public static void main(String[] args) {
konversiSuhu kon = new konversiSuhu();
kon.celcius=30;
kon.tmapilhasil();
kon.celcius=45;
kon.tmapilhasil();
kon.celcius=50.5;
kon.tmapilhasil();
kon.celcius=100;
kon.tmapilhasil();
}
}
beres deh
No comments:
Post a Comment