实训作业4
- 编写“电费管理类”及其测试类。
- 第一步 编写“电费管理”类
- 私有属性:上月电表读数、本月电表读数
- 构造方法:无参、2个参数
- 成员方法:getXXX()方法、setXXX()方法
- 成员方法:显示上月、本月电表读数
- 第二步 编写测试类
- 创建对象一:上月电表读数为1000,本月电表读数为1200。
要求:调用无参构造方法创建对象;
调用setXXX()方法初始化对象;
假设每度电的价格为1.2元,计算并显示本月电费。
- 创建对象二:上月电表读数1200,本月电表读数为1450。
要求:调用2个参数的构造方法创建并初始化对象;
调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
假设每度电的价格为1.2元,计算并显示本月电费。
public class dianfeiguanli{ private double lastmonth; private double thismonth; public dianfeiguanli(){ } public dianfeiguanli(double lastmonth,double thismonth){ this.lastmonth=lastmonth; this.thismonth=thismonth; } public double getLast(){ return lastmonth; } public void setLast(double lastmonth){ if(lastmonth<0){ this.lastmonth=0; } else{ this.lastmonth=lastmonth; } } public double gehtThis(){ return thismonth; } public void setThis(double thismonth){ if(thismonth<0){ this.thismonth=0; } else{ this.thismonth=thismonth; } } public void printMessage(){ System.out.println("lastmonth="+lastmonth+",thismonth="+thismonth); } }
public class test1 { public static void main(String[] args){ dianfeiguanli s1=new dianfeiguanli(); s1.setThis(1200); s1.setLast(1000); s1.printMessage(); double p=1.2; double sum=0; sum=(1200-1000)*p; System.out.println("第一家本月电费为:"+sum); dianfeiguanli s2=new dianfeiguanli(1200,1450); s2.setThis(1500); s2.printMessage(); sum=(1500-1200)*p; System.out.println("第二家本月电费为:"+sum); } }
3.1 “圆柱体”类
- 私有属性:圆底半径、高,
- 构造方法:带两个参数
- 方法1:计算底面积
- 方法2:计算体积
- 方法3:打印圆底半径、高、底面积和体积。
package class1;
public class cylinder {
private double r;
private double h;
double area;
double v;
public cylinder(double r,double h){
this.r=r;
this.h=h;
}
public double getR(){
return r;
}
public void setR(double r){
if(r<=0){
this.r=0;
}
else{
this.r=r;
}
}
public double getH(){
return h;
}
public double countarea() {
final double PI=3.14;
area=PI*r*r;
return area;
}
public double countv() {
final double PI=3.14;
area=PI*r*r;
v=area*h;
return v;
}
public void output() {
System.out.println("圆柱体底面半径为:"+r+"高为:"+h);
System.out.println("圆柱体的底面积是:"+area);
area=countarea();
System.out.println("圆柱体的体积是:"+v);
v=countv();
}
}
3.2 测试类
- 创建2个对象,并调用方法
-
package class1; public class testyuanzhu { double area; double v; public static void main(String[] args){ cylinder s1=new cylinder(1,2); s1.countarea(); s1.countv(); s1.output(); cylinder s2=new cylinder(2,2); s2.countarea(); s2.countv(); s2.output(); } }
4、编写“四则运算类”及其测试类。
4.1 应用场景
- 计算器。能实现简单的四则运算,要求:只进行一次运算。
4.1 “四则运算”类
- 私有属性:操作数一、操作数二、操作符
- 构造方法:带两个参数
- 构造方法:带三个参数
- 方法一:对两个操作数做加运算
- 方法二:对两个操作数做减运算
- 方法三:对两个操作数做乘运算
- 方法四:对两个操作数做除运算
package class1; public class calculate2 { private double a; private double b; private String m; double sum; public calculate2(double a,double b){ } public calculate2(double a,double b,String m){ this.a=a; this.b=b; this.m=m; } public double geta() { return a; } public double getb() { return b; } public String getm() { return m; } public void count1(double a,double b){ sum=a+b; System.out.println("计算结果为:"+sum); } public void count2(double a,double b){ sum=a-b; System.out.println("计算结果为:"+sum); } public void count3(double a,double b){ sum=a*b; System.out.println("计算结果为:"+sum); } public void count4(double a,double b){ sum=a/b; System.out.println("计算结果为:"+sum); } }
4.2 测试类
- 从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。
package class1; import java.util.Scanner; public class testjisuan { public static void main(String[] args){ double a=0; double b=0; String m = null; calculate2 s1=new calculate2(a,b,m); Scanner rd=new Scanner(System.in); System.out.println("请输入第一个操作数:"); a=rd.nextDouble(); System.out.println("请输入第二个操作数:"); b=rd.nextDouble(); System.out.println("请输入操作数符:"); m=rd.next(); switch(m){ case "+": s1.count1(a,b);break; case "-": s1.count2(a,b);break; case "*": s1.count3(a,b);break; case "/": s1.count4(a,b);break; } } }