第4次实训作业

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

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

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

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

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

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

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

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

public class shixun {
    int lmen,tmen;

    public shixun(int i, int j){
        lmen=i;
        tmen=j;
}

public int getlmen(){
    return lmen;
}

public void setlmen(int lmen){
    this.lmen =lmen;
}
public int gettmen(){
    return tmen;
}
public void gettmen(int tmen){
    
    this.tmen =tmen;
}

public void print(){
    System.out.print("\n上月电表读数:"+lmen+"\n本月电表读数:"+tmen);
}

public void my(){
    int a;
    double b;
    a= tmen -lmen;
    System.out.print("\n本月用电:"+a);
    b =1.2*a;
    System.out.println("\n本月电费:"+b);
}

public void fix(){
    tmen=1500;
}
}
public class testshixun {


public static void main(String[] args) {
shixun p1 = new shixun(1000,1200);
p1.print();
p1.my();
shixun p2 = newshixun(1200,1450);
p2.fix();
p2.print();
p2.my();

 
 

}
}

 

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

3.1 “圆柱体”类

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

3.2 测试类

扫描二维码关注公众号,回复: 6118178 查看本文章
  • 创建2个对象,并调用方法
  • public class shixun2{
        final float PI=3.14f;
        private int r,h;
        public shixun2(int r,int h) {
            this.r = r;
            this.h = h;
        }
        
        public void n() {
            System.out.print("底面半径:"+r+"\t高:"+h);
        }
    
        public void s() {
            System.out.print("\n圆柱的底面积为:"+(PI*r*r));
        }
        public void v() {
            System.out.println("\n圆柱的体积为:"+(PI*r*r*h));
        }
    
    
    
        }
    public class testshixun2 {
        public static void main(String[] args) {
            shixun2 c1 = new shixun2(3,4);
            c1.n();
            c1.s();
            c1.v();
            shixun2 c2 = new shixun2(1,2);
            c2.n();
            c2.s();
            c2.v();}}

4、编写“四则运算类”及其测试类。

4.1 应用场景

  • 计算器。能实现简单的四则运算,要求:只进行一次运算。

4.1 “四则运算”类

  • 私有属性:操作数一、操作数二、操作符
  • 构造方法:带两个参数
  • 构造方法:带三个参数
  • 方法一:对两个操作数做加运算
  • 方法二:对两个操作数做减运算
  • 方法三:对两个操作数做乘运算
  • 方法四:对两个操作数做除运算

4.2 测试类

  • 从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。
    import java.util.*;
    public class testshixun3 {
        public static void main(String[] args) {
            Scanner a = new Scanner(System.in);
            System.out.print("请输入两个数:");
            int x1 = a.nextInt();
            int x2 = a.nextInt();
            System.out.print("请输入一个运算符:");
            char y = a.next().charAt(0);
            shixun3 n = new shixun3(x1,x2,y);
            if(y=='+') {
                n.add();
            }
            if(y=='-') {
                n.minus();
            }
            if(y=='*') {
                n.multiply();
            }
            else if(y=='/') {
                n.divide();
            }
        }
    }
    
    public class shixun3 {
        private int x1,x2;
        private char y;
        public shixun3(int x1,int x2,char y) {
            this.x1 = x1;
            this.x2 = x2;
            this.y = y;
        }
        
        public void  add() {
            System.out.print(x1+x2);
        }
        public void  minus() {
            System.out.print(x1-x2);
    }
        public void  multiply() {
            System.out.print(x1*x2);
    }
        public void  divide() {
            System.out.print(x1/x2);
    }
    }

猜你喜欢

转载自www.cnblogs.com/zhouchengzi/p/10810365.html