第四次作业

1.编写“电费管理类”及其测试类。
 第一步 编写“电费管理”类
1)私有属性:上月电表读数、本月电表读数
2)构造方法:无参、2个参数
3)成员方法:getXXX()方法、setXXX()方法
4)成员方法:显示上月、本月电表读数
 第二步 编写测试类
1)创建对象一:上月电表读数为1000,本月电表读数为1200。
要求:调用无参构造方法创建对象;
调用setXXX()方法初始化对象;
假设每度电的价格为1.2元,计算并显示本月电费。
2)创建对象二:上月电表读数1200,本月电表读数为1450。
要求:调用2个参数的构造方法创建并初始化对象;
调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
假设每度电的价格为1.2元,计算并显示本月电费。

 1     import java.util.Scanner;
 2 
 3     public class dianfei {
 4     Scanner in=new Scanner(System.in);
 5         private int count;
 6            private int count1;
 7            public dianfei(int i, int j) {
 8                count=in.nextInt();
 9                count1=in.nextInt();
10            }
11            public  int getcount(){
12                return count;
13            }
14            public void setcount(int count) {
15      
16               this.count=count;
17            }
18            public  int getcount1(){
19                return count1;
20            }
21            public void setcount1(int count1) { 
22               this.count1=count1;
23            }
24            public void myprint() {
25                System.out.println("上月电表读数: "+count+"本月电表读数为: "+count1);
26            }
27 }
28   
 1 public class Testdianfei {
 2    public static void main(String[] args) {
 3        dianfei b1=new dianfei(1000,1200);
 4        System.out.println("本月电费为: "+(b1.getcount()-b1.getcount1())*1.2);
 5        dianfei b2=new dianfei(1200,1300);
 6        b2.setcount1(1500);
 7        b2.myprint();
 8        System.out.println("本月电费为: "+(b2.getcount()-b2.getcount1())*1.2);
 9    }
10 }

 2、 编写“圆柱体”类及其测试类。
2.1 “圆柱体”类
 私有属性:圆底半径、高,
 构造方法:带两个参数
 方法1:计算底面积
 方法2:计算体积
 方法3:打印圆底半径、高、底面积和体积。
2.2 测试类
 创建2个对象,并调用方法

 1 public class cylinder {
 2     final static float PI=3.14159f;
 3    private float r;
 4    private float high;
 5    private float basearea;
 6    private float bulk;
 7    public  cylinder(float r,float high) {
 8        this.r=r;
 9        this.high=high;
10    }
11    public void myprint() {
12        System.out.println("圆柱的半径为: "+r+"高为: "+high);
13        }
14    public void Basearea(float r,float high){
15        System.out.println("计算圆柱的底面积:  ");
16        basearea=2*r*PI;
17    }
18    public void bulk(float r,float high){
19        System.out.println("计算圆柱的体积: ");
20        bulk=PI*r*r*high;
21    }
22   
23    public void println(float basearea,float bulk){
24        System.out.println("底面积为: "+basearea+"体积为: "+bulk);
25    }
26 public float getR() {
27     return r;
28 }
29 public void setR(float r) {
30     this.r = r;
31 }
32 public float getHigh() {
33     return high;
34 }
35 public void setHigh(float high) {
36     this.high = high;
37 }
38 public float getBasearea() {
39     return basearea;
40 }
41 public void setBasearea(float basearea) {
42     this.basearea = basearea;
43 }
44 public float getBulk() {
45     return bulk;
46 }
47 public void setBulk(float bulk) {
48     this.bulk = bulk;
49 }
50 }
 1 public class Testcylinder {
 2     public static void main(String[] args) {
 3         cylinder b1=new cylinder(2,5);
 4         b1.Basearea(2,5);
 5         b1.bulk(2,5);
 6         b1.myprint();
 7         System.out.println("圆柱的底面积为: "+b1.getBasearea()+"圆柱的体积为: "+b1.getBulk());
 8         cylinder b2=new cylinder(3,6);
 9         b2.Basearea(3,6);
10         b2.bulk(3,6);
11         b2.myprint();
12         System.out.println("圆柱的底面积为: "+b2.getBasearea()+"圆柱的体积为: "+b2.getBulk());
13     }
14 }

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

 1 public class count {
 2             private int num1;
 3             private int num2;
 4             private String operater;
 5             public count(int num1,int num2){
 6                 this.num1=num1;
 7                 this.num2=num2;
 8             }
 9             public void count(int num1,int num2,String operater){
10                 this.num1=num1;
11                 this.num2=num2;
12                 this.operater=operater;
13             }
14             public void add(int num1,int num2){
15                 int x1;
16                 x1=num1+num2;
17             }
18             public void subtraction(int num1,int num2){
19                 int x2;
20                 x2=num1-num2;
21             }
22             public void multiplication(int num1,int num2){
23                 int x3;
24                 x3=num1*num2;
25             }
26             public void division(int num1,int num2){
27                 int x4;
28                 x4=num1/num2;    
29             }
30             public void myprintln(int x1,int x2,int x3,int x4){
31                 System.out.println("输出本次运算的结果: "+x1 +x2 +x3 +x4);
32             }
33 }
 1 import java.util.Scanner;
 2 public class Testcount {
 3   public static void main(String[] args) {
 4       int num1,num2;
 5       String operater;
 6       System.out.println("请输入两个操作数和一个操作符: "); 
 7       Scanner in=new Scanner(System.in);
 8       num1=in.nextInt();
 9       num2=in.nextInt();
10       operater=in.next();
11       count b=new count(num1,num2);
12       if(operater.equals("add"))
13       {
14           b.add(num1,num2);
15           System.out.println("输出本次运算的结果: "+(num1+num2));
16       }
17      if(operater.equals("multiplication")){
18 
19           b.multiplication(num1,num2);
20           System.out.println("输出本次运算的结果: "+(num1-num2));
21       }
22      if(operater.equals("subtraction")){
23           b.subtraction(num1,num2);
24           System.out.println("输出本次运算的结果:  "+(num1*num2));
25       }
26       if(operater.equals("division")){
27           b.division(num1,num2);
28           System.out.println("输出本次运算的结果:   "+(num1/num2));
29       }
30   }
31 }

本次作业在第三个题目哪个比较方法哪里出了点错误,导致程序运行错误,经询问课代表后得以解决。。其它地方还行,翻阅书籍后能够自己写出来,然后途中出现了些许小错误,但经查阅书籍或者询问他人得以解决。

转载于:https://www.cnblogs.com/jjbbbb/p/10769410.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值