第四次实训

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

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

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

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

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

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

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

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

package A;
public class Electricity {
         private int premonth;
         private int curmonth;
         public Electricity(){
     }
         public Electricity(int premonth,int curmonth){
             this.premonth=premonth;
             this.curmonth=curmonth;
     }
         public void setpremonth(int premonth){
             this.premonth=premonth;
     }
         public int getpremonth(){
             return premonth;
     }
         public void setcurmonth(int curmonth){
             this.curmonth=curmonth;
     }
         public int getcurmonth(){
             return curmonth;
     }
        public void myprint() {
             System.out.println("上月电表读数:"+premonth+"本月电表读数:"+curmonth);
     }}
测试:
package A;
public class TestElectricity {
        public static void main(String[] args) {
            Electricity s1=new Electricity();
            s1.setpremonth(1000);
            s1.setcurmonth(1200);
            s1.myprint();
            System.out.println("本月电费:"+(s1.getcurmonth()-s1.getpremonth())*1.2);
            Electricity s2=new Electricity(1200,1450);
            s2.setcurmonth(1500);
            s2.myprint();
            System.out.println("本月电费:"+(s2.getcurmonth()-s2.getpremonth())*1.2);
        }     
    }

1634264-20190428170938791-1868245917.png

编写“圆柱体”类及其测试类。

3.1 “圆柱体”类
•私有属性:圆底半径、高,
•构造方法:带两个参数
•方法1:计算底面积
•方法2:计算体积
•方法3:打印圆底半径、高、底面积和体积。

3.2 测试类
•创建2个对象,并调用方法

package A;
public class YuanZhuTi {
        final float PI=3.14f;
        private double r,h;
        public YuanZhuTi(double r,double h) {
            this.r=r;
            this.h=h;
        }
        public void s() {
            System.out.println("底面积:"+(PI*r*r));
         }
        public void v() {
            System.out.println("体积:"+((PI*r*r)*h));
         }
        public void myprint() {
            System.out.println("圆底半径:"+r+"高:"+h);
            System.out.println("底面积:"+(PI*r*r));
            System.out.println("体积:"+((PI*r*r)*h));
        }
    }
测试:
package A;
public class TestYuanZhuTi {
        public static void main(String[] args) {
            YuanZhuTi s1=new YuanZhuTi(1.0,2.0);
            s1.s();
            s1.v();
            s1.myprint();
            System.out.println();
            YuanZhuTi s2=new YuanZhuTi(2.0,1.0);
            s2.s();
            s2.v();
            s2.myprint();
        }
    }

1634264-20190428170950511-1026980681.png

4、编写“四则运算类”及其测试类。
4.1 应用场景
•计算器。能实现简单的四则运算,要求:只进行一次运算。
4.1 “四则运算”类
•私有属性:操作数一、操作数二、操作符
•构造方法:带两个参数
•构造方法:带三个参数
•方法一:对两个操作数做加运算
•方法二:对两个操作数做减运算
•方法三:对两个操作数做乘运算
•方法四:对两个操作数做除运算
4.2 测试类
•从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。

package A;
public class Number {
         private int a1,a2;
         private char b;
         public Number(int a1,int a2) {
             this.a1=a1;
             this.a2=a2;
         }
         public Number(int a1,int a2,char b) {
             this.a1=a1;
             this.a2=a2;
             this.setB(b);
         }
         public void add() {
             System.out.println(a1+a2);
         }
         public void reduce() {
             System.out.println(a1-a2);
         }
         public void multiplication() {
             System.out.println(a1*a2);
         }
         public void division() {
             System.out.println(a1/a2);
         }
        public char getB() {
            return b;
        }
        public void setB(char b) {
            this.b = b;
        }
    }
测试:
package A;
import java.util.*;
public class TestNumber {
        public static void main(String[] args) {
                int a1 = 0,a2 = 0;
                char b = 0;
                Scanner rd=new Scanner(System.in);
                System.out.println("请输入两个操作数和一个操作符:");
                a1=rd.nextInt();
                a2=rd.nextInt();
                b=rd.next().charAt(0);  
                Number sun=new Number(a1,a2,b);
                if(b=='+')
                sun.add();
                else if(b=='-')
                sun.reduce();
                else if(b=='*')
                sun.multiplication();
                else if(b=='/')
                sun.division();   
              }}

1634264-20190428171003362-1947507371.png

转载于:https://www.cnblogs.com/xiaoqiuyueming/p/10785082.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值