Java第四次作業
- 編寫“電費管理類”及其測試類。
- 第一步 編寫“電費管理”類
- 私有屬性:上月電錶讀數、本月電錶讀數
- 構造方法:無參、2個引數
- 成員方法:getXXX()方法、setXXX()方法
- 成員方法:顯示上月、本月電錶讀數
- 第二步 編寫測試類
- 建立物件一:上月電錶讀數為1000,本月電錶讀數為1200。
要求:呼叫無參構造方法建立物件;
呼叫setXXX()方法初始化物件;
假設每度電的價格為1.2元,計算並顯示本月電費。
- 建立物件二:上月電錶讀數1200,本月電錶讀數為1450。
要求:呼叫2個引數的構造方法建立並初始化物件;
呼叫setXXX()方法修改本月電錶讀數為1500(模擬讀錯了需修改);
假設每度電的價格為1.2元,計算並顯示本月電費。
import java.util.Scanner;
public class aaa {
Scanner in = new Scanner(System.in);
private int Lastmonth;
private int Thismonth;
public void print()
{Double M;
System.out.println("請輸入上個月與這個月的電費讀表");
Lastmonth = in.nextDouble();
Thismonth= in.nextDouble();
M=(Thismonth-Lastmonth)*1.2;
System.out.println("電費為:"+M);
}
public int getLastMonth() {
return Lastmonth;
}
public void setLastMonth(int Lastmonth) {
this.Lastmonth = Lastmonth;
}
public int getThisMonth() {
return Thismonth;
}
public void ThisMonth(int Thismonth) {
Double M;
this.Thismonth =Thismonth;
M=(Thismonth)*1.2;
System.out.println("電費為:"+M);
}
}
package bbb;
import java.util.Scanner;
public class text {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
Double Thismonth;
aaa c1=new aaa();
aaa c2=new aaa();
c1.print();
c2.print();
System.out.println("請重新輸入本月電費:");
Thismonth=in.nextInt();
c2.ThisMonth(Thismonth);
}
}