1.编写“电费管理类”及其测试类。
第一步 编写“电费管理”类
私有属性:上月电表读数、本月电表读数
构造方法:无参、2个参数
成员方法:getXXX()方法、setXXX()方法
成员方法:显示上月、本月电表读数
第二步 编写测试类
创建对象一:上月电表读数为1000,本月电表读数为1200。
要求:调用无参构造方法创建对象;
调用setXXX()方法初始化对象;
假设每度电的价格为1.2元,计算并显示本月电费。
public class charge {
private double lmonth ;
private double tmonth ;
//无参构造法
public charge(){ //构造方法 无参数
}
public charge(double lmonth,double tmonth){ //构造方法 两个参数
this.lmonth = lmonth;
this.tmonth = tmonth;
}
public double getlmonth(){ //获取上个月电表读数
return lmonth;
}
public void setlmonth(double lmonth){ //设置上个月的电表读数
this.lmonth = lmonth;
}
public double gettmonth(){ //获取这个月电表读数
return tmonth;
}
public void settmonth(double tmonth){
this.tmonth = tmonth;
}
public static void main(String[] args){
charge a = new charge();
double tip;
a.setlmonth(1000);
a.settmonth(1200);
tip = (a.gettmonth() - a.getlmonth())*1.2;
System.out.println("上个月电表读数 = "+a.getlmonth()+" 这个月电表读数 = "+a.gettmonth()+" 本月电费: "+tip);
}
}