JAVA实验2
- 格式:pdf
- 大小:189.14 KB
- 文档页数:14
用数组的每个元素(Employee 引用)调用每个对象的 earnings()方法。
Employee 类定义如下: abstract class Employee { private String firstName; private String lastName; public Employee(String first,String last)
//捕获与 I/O 有关的异常(空白处补全捕获语句)
//捕获数值转化时的异常,如不能将字符转化成数值
//捕获除数为 0 的异常 } } 编译并运行,当产生输入输出异常时显示异常信息;当输入除数为 0 时,出现算术异常,提 示除数为 0,并要求重新输入;当输入的不是整数时,如将 30 输成了 3o,出现数值格式异 常,提示输入整数。 思考:是否还有其他异常需要捕获处理? (6)编写程序包含自定义异常 MyException,当 100 被 13 和 4 除时抛出该异常,其余除数 显示商值。
提示:对所有雇员类型都使用 earnings()方法,但每个人挣的工资按他所属的雇员
类计算,所有雇员类都是从超类 Employee 派出生的。在超类中声明 earnings()为抽象 方法,并且对于每个子类都提供恰当的 earnings()的实现方法。为了计算雇员的工资, 程序仅仅使用雇员对象的一个超类引用并调用 earnings()方法。在一个实际的工资系 统中,各种 Employee 对象的引用可以通过一个 Employee 引用数组来实现。程序依次使
this.real=r; this.imag=i; } public complexnumber add(complexnumber c) { complexnumber temp=new complexnumber(0,0); temp.real=this.real+c.real; temp.imag=this.imag+c.imag; return temp; } public complexnumber sub(complexnumber c) {
二、实验仪器和设备
奔腾以上计算机,Windows 操作系统,装有 JDK1.7 和 Eclipse 软件。
三、实验过程
(1) 设计复数类,成员变量包括实部和虚部,成员方法包括实现复数加法、减法、字 符串描述、比较是否相等等操作。 (2) 设计三角形类,继承图形抽象类,实现面积接口和周长接口,计算三角形面积和 周长。 (3) 包的建立与使用:设计计算器类 Calculator,计算加、减、乘、除,并且打包 为 mypackage。观察源文件目录下是否生成了 mypackage 文件夹,在该文件夹中是否有 Calculate.class 文件。编辑 PackageDemo.java,保存在 Calculator.java 同一目录下, 引用计算器类的各方法显示计算结果。 (4) 编码实现多态在工资系统中的应用:给出一个根据雇员类型利用 abstract 方法 和多态性完成工资单计算的程序。Employee 是抽象类,Employee 的子类有 Boss(每星 期发给他固定工资,而不计工作时间)、CommissionWorker(除基本工资外还根据销售 额发放浮动工资)、PieceWorker(按其生产的产品数发放工资)、HourlyWorker(根据 工作时间长短发放工资)。该例的 Employee 的每个子类都声明为 final,因为不需要再 继承它们生成子类。在主测试类 Test 中测试各类雇员工资计算结果。
super(p1,"三角形"); this.point2=p2;
this.point3=p3;
this.a=Math.sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-poin t2.y)*(point1.y-point2.y));
this.b=Math.sqrt((point2.x-point3.x)*(point2.x-point3.x)+(point2.y-poin t3.y)*(point2.y-point3.y));
public String toString() {
return this.getClass().getName()+this.shape+",三点坐标 "+this.point1+","+(this.point2==null?"null":this.point2.toString())+","+(th is.point3==null?"null":this.point3.toString())+",三边长 "+String.format("%1.2f,%1.2f,%1.2f",this.a,this.b,this.c);
return true; } return false; } public String toString() { return this.real+"+"+this.imag+"i"; } public static void main(String[] args) { complexnumber a=new complexnumber(1,2); System.out.println("第一个虚数为:"+a.toString()); complexnumber b=new complexnumber(1,2); System.out.println("第二个虚数为:"+b.toString()); System.out.println("两个徐庶的和:"+a.add(b).toString()); System.out.println("两个徐庶的差:"+a.sub(b).toString()); if(a.equals(b)) {
要求:
(1) 注意选用适当的类成员修饰符(private、protected、public 等),比较它们的使 用情况;
(2) 养成良好的编程习惯,严格按照命名规则为包、类及类成员命名,将每个程序打包, 包的命名方式如 two.num1 表示实验二的第一题;
(3) 学会使用 Eclipse 的各种调试方法; (4) 学会查阅 Java API 文档,如查找异常类的使用方法。
super(p1,"三角形"); this.a=a; this.b=b; this.c=c; } public double area() { double s=(a+b+c)/2; return Math.sqrt(s*(s-a)*(s-b)*(s-c)); } public double perimeter() { return a+b+c; }
{ firstName=first; lastName=last; } public String getEmployeeName() { return firstName; } public String getLastName() { return lastName; } public String toString() { return firstName+lastName; } public abstract String earnings(); } (5)异常的捕获:计算两数相除并输出结果。使用三个 catch 子句,分别捕捉输入输出异 常、除数为 0 的异常和参数输入有误异常。 import java.io.*; class Ex1 { public static void main(String args[ ]) {
try{ BufferedReader strin=new BufferedReader( new InputStreamReader(System.in));//建立输入流缓冲区 System.out.print("请输入除数:"); String cl=strin.readLine();//键盘输入 int a=Integer .parseInt(cl); System .out .print("请输入被除数:"); cl=strin .readLine(); int b=Integer .parseInt(cl); int c=b/a; System .out .println("商为:"+c); }
this.c=Math.sqrt((point3.x-point1.x)*(point3.x-point1.x)+(point3.y-poin t1.y)*(point3.y-point1.y));
} public Triangle(Point p1,double a,double b,double c) {
this.x=x; this.y=y; } public Point() { this(0,0); } public String toString() { return "Point("+this.x+","+this.y+")"; } } figure 类: package shiyan2_2; public class figure { public Point point1; protected String shape; protected figure(Point point) { this.point1=point; } protected figure(Point point,String shape) { this.point1=point; this.shape=shape; } protected figure() { this(new Point()); }