第四次实训作业

 1 //编写“电费管理类”及其测试类。
 2 // 第一步 编写“电费管理”类
 3 //1)私有属性:上月电表读数、本月电表读数
 4 //2)构造方法:无参、2个参数
 5 //3)成员方法:getXXX()方法、setXXX()方法
 6 //4)成员方法:显示上月、本月电表读数
 7 package 第四次实训;
 8 public class 管理电费 {
 9     private int premonth;
10     private int curmonth;
11     public 管理电费() {
12         //无参构造方法
13     }
14     public 管理电费(int premonth,int curmonth) {
15         this.premonth=premonth;
16         this.curmonth=curmonth;
17     }
18     public void setpremonth(int premonth) {
19         this.premonth=premonth;
20         //设置上月电费
21     }
22     public void setcurmonth(int curmonth) {
23         this.curmonth=curmonth;
24         //设置本月电费
25     }
26     public int getpremonth() {
27         return premonth;
28     }
29     public int getcurmonth() {
30         return curmonth;
31     }
32     public void myprint() {
33         System.out.println("上月电表读数:"+premonth+"本月电表读数:"+curmonth);
34         
35     }
36     /* 第二步 编写测试类
37      1)创建对象一:上月电表读数为1000,本月电表读数为1200。
38      要求:调用无参构造方法创建对象;
39      调用setXXX()方法初始化对象;
40      假设每度电的价格为1.2元,计算并显示本月电费。*/
41     public static void main(String[] agrs) {
42         管理电费 a1=new 管理电费();
43         a1.setpremonth(1000);
44         a1.setcurmonth(1200);
45         a1.myprint();
46         System.out.println("本月需要缴纳电费:"+1.2*(a1.getcurmonth()-a1.getpremonth()));
47     }
48 
49 }
/*3.1 “圆柱体”类
私有属性:圆底半径、高,
构造方法:带两个参数
方法1:计算底面积
方法2:计算体积
方法3:打印圆底半径、高、底面积和体积。*/
package 第四次实训;
import java.util.*;
public class 圆柱体 {
    private float r;//圆柱的半径
    private float h;//圆柱的高
    final float PI=3.14f;
    public 圆柱体(float r,float h) {
        this.r=r;
        this.h=h;
    }
    public void s() {
        System.out.println("该圆柱的底面积为:"+(r*r*PI));
    }
    public void v() {
        System.out.print("该圆柱的体积为:"+((r*r*PI)*h));
    }
    public void myprint() {
        System.out.println("该圆柱的圆底半径:"+r+"该圆柱的高:"+h+"该圆柱的底面积:"+(r*r*PI)+"该圆柱的体积:"+((r*r*PI)*h));
    }
/*3.2 测试类
    创建2个对象,并调用方法*/
    public static void main() {
        圆柱体 a1=new 圆柱体(2,3);
        Scanner reader =new Scanner(System.in);
        float r;
        float h;
        r=reader.nextFloat();
        h=reader.nextFloat();
        a1.s();
        a1.v();
        a1.myprint();
        System.out.println();
        
        
        
    }
    
    
    

}
 1 /*4.1 应用场景
 2 计算器。能实现简单的四则运算,要求:只进行一次运算。
 3 4.1 “四则运算”类
 4 私有属性:操作数一、操作数二、操作符
 5 构造方法:带两个参数
 6 构造方法:带三个参数
 7 方法一:对两个操作数做加运算
 8 方法二:对两个操作数做减运算
 9 方法三:对两个操作数做乘运算
10 方法四:对两个操作数做除运算*/
11 package 第四次实训;
12 import java.util.*;
13 public class 四则运算 {
14     private int num1,num2;
15     private  String operator;
16     public 四则运算(int num1,int num2) {
17         this.num1=num1;
18         this.num2=num2;
19     }
20     public 四则运算(int num1,int num2,String operator) {
21         this.num1=num1;
22         this.num2=num2;
23         this.operator=operator;
24     }
25     public void add() {
26         System.out.println(num1+num2);
27     }
28     public void minus() {
29         System.out.println(num1-num2);
30     }
31     public void multiplication() {
32         System.out.println(num1*num2);
33     }
34     public void divisionmethod() {
35         System.out.println(num1/num2);
36     }
37 /*3.3 测试类
38  从键盘输入两个操作数和一个操作符,计算之后,输出运算结果*/
39     public static void main() {
40         Scanner reader =new Scanner(System.in);
41         int num1=0;
42         int num2=0;
43         四则运算 a=new 四则运算(num1, num2);
44         String operator;
45         num1=reader.nextInt();
46         num2=reader.nextInt();
47         operator=reader.next();
48         
49         System.out.println("请输入两个数和一个操作符号:");
50         if(operator=="+")
51             a.add();
52         if(operator=="-")
53             a.minus();
54         if(operator=="*")
55             a.multiplication();
56         if(operator=="/")
57             a.divisionmethod();
58     }
59 }

实训体会:

对于定义类和类的构造我相比前两周是进步了的,但是我不知道为什么我后两个程序控制台不显示我想要的东西,好像是输入方面出现了错误但是我找不出来

转载于:https://www.cnblogs.com/Mxuan0303/p/10781192.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值