第四次上机作业

 


  1. 编写“电费管理类”及其测试类。
  • 第一步 编写“电费管理”类
  • 私有属性:上月电表读数、本月电表读数
  • 构造方法:无参、2个参数
  • 成员方法:getXXX()方法、setXXX()方法
  • 成员方法:显示上月、本月电表读数
  • 第二步 编写测试类
  • 创建对象一:上月电表读数为1000,本月电表读数为1200。

   要求:调用无参构造方法创建对象;

         调用setXXX()方法初始化对象;

         假设每度电的价格为1.2元,计算并显示本月电费。

  • 创建对象二:上月电表读数1200,本月电表读数为1450。

   要求:调用2个参数的构造方法创建并初始化对象;

   调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);

假设每度电的价格为1.2元,计算并显示本月电费。

 1 class Elc{
 2     private double last
 3     private double now;
 4     Elc() {
 5          
 6     }
 7     Elc(double last,double now) {
 8         this.last=last;
 9         this.now=now;
10     }
11     double getlast() {
12         return this.last;
13     }
14     double getnow() {
15         return this.now;
16     }
17     void setlast(double last) {
18         this.last=last;
19     }
20     void setnow(double now) {
21         this.now=now;
22     }
23     void showElc() {
24         System.out.println("上月电表读数为:"+this.last);
25         System.out.println("本月电表读数为:"+this.now);
26     }
27 }
28  
29 public class ElcTest {
30  
31     public static void main(String[] args) {
32         Elc a=new Elc();
33         a.setlast(1000);
34         a.setnow(1200);
35         a.showElc();
36         System.out.println("本月电费为:"+(a.getnow()-a.getlast())*1.2);
37         Elc b=new Elc(1200,1450);
38         b.setnow(1500);
39         b.showElc();
40         System.out.println("本月电费为:"+(b.getnow()-b.getlast())*1.2);
41     }
42  
43 }

 

posted @ 2019-05-03 08:47  孙贤超  阅读(142)  评论(0编辑  收藏  举报