第四次实训作业

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

public class electric {
private int next,the;
double x=1.2,y;
public electric(){

}
 public electric(int next,int the){
 this.next=next;
 this.the=the;
 }

public void setNext(int next){
 this.next=next;
 }

 public void setThe(int the){
 this.the=the;
 }
 
 public void printE(){
 System.out.println("上个月电表数:"+next+"这个月电表数:"+the);
 }
 
public int printF(){
 y=(the-next)*x;
 System.out.println("电费数:"+y);
 return 0;
 }

}

•第二步 编写测试类
•创建对象一:上月电表读数为1000,本月电表读数为1200。

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

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

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

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

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

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

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

public class text {

	 public static void main(String[] args){
	 electric x1=new electric(1000,1200);
	 x1.printE();
	 x1.setNext(800);
	 x1.printE();
	 x1.printF();
	 electric x2=new electric(1200,1450);
	 x2.printE();
	 x2.setThe
	 (1500);
	 x2.printE();
	 x2.printF();
     }

}

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

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

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

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

package 四则运算;
public class num {
int a,b;
char c;
public void add() {
System.out.println(a+b);
}
public void reduce() {
System.out.println(a-b);
}
public void multiplication() {
System.out.println(a*b);
}
public void division() {
System.out.println(a/b);
}
}

4.2 测试类
•从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。

package 四则运算;
import java.util.;
public class text {
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
num s=new num ();
System.out.println("请输入两个操作数和一个操作符:");
s.a=rd.nextInt();
s.b=rd.nextInt();
s.c=rd.next().charAt(0);
if(s.c'+')
s.add();
else if(s.c
'-')
s.reduce();
else if(s.c=='
')
s.multiplication();
else if(s.c=='/')
s.division();
}

}

运行结果:

posted @ 2019-05-06 17:21  海诺音  阅读(107)  评论(0编辑  收藏  举报