第4次实训作业

  1. 编写“电费管理类”及其测试类。
  • 第一步 编写“电费管理”类
  • 私有属性:上月电表读数、本月电表读数
  • 构造方法:无参、2个参数
  • 成员方法:getXXX()方法、setXXX()方法
  • 成员方法:显示上月、本月电表读数
  • public class electric {
    private int next,the;
    double x=1.2,y;
    public electric(){

    }
     public electric(int next,int the){ this.next=next; this.the=the; } public void setNext(int next){ this.next=next; } public void setThe(int the){ this.the=the; } public void printE(){ System.out.println("上个月电表数:"+next+"这个月电表数:"+the); } public int printF(){ y=(the-next)*x; System.out.println("电费数:"+y); return 0; } }
  • 第二步 编写测试类
  • 创建对象一:上月电表读数为1000,本月电表读数为1200。

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

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

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

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

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

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

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

public class text {

     public static void main(String[] args){ electric x1=new electric(1000,1200); x1.printE(); x1.setNext(800); x1.printE(); x1.printF(); electric x2=new electric(1200,1450); x2.printE(); x2.setThe (1500); x2.printE(); x2.printF(); }

}

转载于:https://www.cnblogs.com/Leslie-718/p/10821255.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值