最新java实验2文档格式.docx

上传人:b****4 文档编号:17801519 上传时间:2022-12-10 格式:DOCX 页数:19 大小:21.11KB
下载 相关 举报
最新java实验2文档格式.docx_第1页
第1页 / 共19页
最新java实验2文档格式.docx_第2页
第2页 / 共19页
最新java实验2文档格式.docx_第3页
第3页 / 共19页
最新java实验2文档格式.docx_第4页
第4页 / 共19页
最新java实验2文档格式.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

最新java实验2文档格式.docx

《最新java实验2文档格式.docx》由会员分享,可在线阅读,更多相关《最新java实验2文档格式.docx(19页珍藏版)》请在冰豆网上搜索。

最新java实验2文档格式.docx

(4)编码实现多态在工资系统中的应用:

给出一个根据雇员类型利用abstract方法和多态性完成工资单计算的程序。

Employee是抽象类,Employee的子类有Boss(每星期发给他固定工资,而不计工作时间)、CommissionWorker(除基本工资外还根据销售额发放浮动工资)、PieceWorker(按其生产的产品数发放工资)、HourlyWorker(根据工作时间长短发放工资)。

该例的Employee的每个子类都声明为final,因为不需要再继承它们生成子类。

在主测试类Test中测试各类雇员工资计算结果。

提示:

对所有雇员类型都使用earnings()方法,但每个人挣的工资按他所属的雇员类计算,所有雇员类都是从超类Employee派出生的。

在超类中声明earnings()为抽象方法,并且对于每个子类都提供恰当的earnings()的实现方法。

为了计算雇员的工资,程序仅仅使用雇员对象的一个超类引用并调用earnings()方法。

在一个实际的工资系统中,各种Employee对象的引用可以通过一个Employee引用数组来实现。

程序依次使用数组的每个元素(Employee引用)调用每个对象的earnings()方法。

Employee类定义如下:

abstractclassEmployee

{

privateStringfirstName;

privateStringlastName;

publicEmployee(Stringfirst,Stringlast)

{

firstName=first;

lastName=last;

}

publicStringgetEmployeeName()

returnfirstName;

}

publicStringgetLastName()

returnlastName;

publicStringtoString()

returnfirstName+lastName;

publicabstractStringearnings();

(5)异常的捕获:

计算两数相除并输出结果。

使用三个catch子句,分别捕捉输入输出异常、除数为0的异常和参数输入有误异常。

importjava.io.*;

classEx1

publicstaticvoidmain(Stringargs[])

try{

BufferedReaderstrin=newBufferedReader(

newInputStreamReader(System.in));

//建立输入流缓冲区

System.out.print("

请输入除数:

"

);

Stringcl=strin.readLine();

//键盘输入

inta=Integer.parseInt(cl);

System.out.print("

请输入被除数:

cl=strin.readLine();

intb=Integer.parseInt(cl);

intc=b/a;

System.out.println("

商为:

+c);

//捕获与I/O有关的异常(空白处补全捕获语句)

//捕获数值转化时的异常,如不能将字符转化成数值

//捕获除数为0的异常

编译并运行,当产生输入输出异常时显示异常信息;

当输入除数为0时,出现算术异常,提示除数为0,并要求重新输入;

当输入的不是整数时,如将30输成了3o,出现数值格式异常,提示输入整数。

思考:

是否还有其他异常需要捕获处理?

(6)编写程序包含自定义异常MyException,当100被13和4除时抛出该异常,其余除数显示商值。

要求:

(1)注意选用适当的类成员修饰符(private、protected、public等),比较它们的使用情况;

(2)养成良好的编程习惯,严格按照命名规则为包、类及类成员命名,将每个程序打包,包的命名方式如two.num1表示实验二的第一题;

(3)学会使用Eclipse的各种调试方法;

(4)学会查阅JavaAPI文档,如查找异常类的使用方法。

程序清单:

(建议程序中适当添加注释信息,增强可读性;

较长程序可分栏书写,保证报告排版整洁美观。

(1)代码如下:

packageshiyan2_1;

publicclasscomplexnumber

privatedoublereal,imag;

publiccomplexnumber(doubler,doublei)

this.real=r;

this.imag=i;

publiccomplexnumberadd(complexnumberc)

complexnumbertemp=newcomplexnumber(0,0);

temp.real=this.real+c.real;

temp.imag=this.imag+c.imag;

returntemp;

publiccomplexnumbersub(complexnumberc)

temp.real=this.real-c.real;

temp.imag=this.imag-c.imag;

publicbooleanequals(complexnumberc)

if(this==c||this.real==c.real&

&

this.imag==c.imag)

{

returntrue;

}

returnfalse;

publicStringtoString()

returnthis.real+"

+"

+this.imag+"

i"

;

publicstaticvoidmain(String[]args)

complexnumbera=newcomplexnumber(1,2);

System.out.println("

第一个虚数为:

+a.toString());

complexnumberb=newcomplexnumber(1,2);

第二个虚数为:

+b.toString());

两个徐庶的和:

+a.add(b).toString());

两个徐庶的差:

+a.sub(b).toString());

if(a.equals(b))

System.out.println("

两个虚数相等"

else

两个虚数不相等"

(2)代码如下:

Point类:

packageshiyan2_2;

publicclassPoint

publicintx,y;

publicPoint(intx,inty)

this.x=x;

this.y=y;

publicPoint()

this(0,0);

return"

Point("

+this.x+"

"

+this.y+"

)"

figure类:

publicclassfigure

publicPointpoint1;

protectedStringshape;

protectedfigure(Pointpoint)

this.point1=point;

protectedfigure(Pointpoint,Stringshape)

this.shape=shape;

protectedfigure()

this(newPoint());

Triangle类:

publicclassTriangleextendsfigureimplementsArea,Perimeter

publicPointpoint2,point3;

protecteddoublea,b,c;

publicTriangle(Pointp1,Pointp2,Pointp3)

super(p1,"

三角形"

this.point2=p2;

this.point3=p3;

this.a=Math.sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y));

this.b=Math.sqrt((point2.x-point3.x)*(point2.x-point3.x)+(point2.y-point3.y)*(point2.y-point3.y));

this.c=Math.sqrt((point3.x-point1.x)*(point3.x-point1.x)+(point3.y-point1.y)*(point3.y-point1.y));

publicTriangle(Pointp1,doublea,doubleb,doublec)

this.a=a;

this.b=b;

this.c=c;

publicdoublearea()

doubles=(a+b+c)/2;

returnMath.sqrt(s*(s-a)*(s-b)*(s-c));

publicdoubleperimeter()

returna+b+c;

returnthis.getClass().getName()+this.shape+"

三点坐标"

+this.point1+"

+(this.point2==null?

null"

:

this.point2.toString())+"

+(this.point3==null?

this.point3.toString())+"

三边长"

+String.format("

%1.2f,%1.2f,%1.2f"

this.a,this.b,this.c);

Area接口:

publicinterfaceArea

publicabstractdoublearea();

Perimeter接口:

publicinterfacePerimeter

publicabstractdoubleperimeter();

Test类:

publicclasstext

publicstaticvoidmain(String[]args)

Triangletr=newTriangle(newPoint(100,100),newPoint(100,130),newPoint(140,130));

Areaar=tr;

System.out.println(tr.toString());

面积为:

%1.2f"

ar.area()));

Perimeterpe=tr;

周长为:

pe.perimeter()));

(3)代码如下:

packagemypackage;

publicclasscalculator

privatedoublex;

publiccalculator(doublexx)

this.x=xx;

publicdoubleadd(calculatorc)

return(this.x+c.x);

publicdoublesub(calculatorc)

return(this.x-c.x);

publicdoublemul(calculatorc)

return(this.x*c.x);

publicdoublediv(calculatorc)

return(this.x/c.x);

"

publicclassPackageDemo

calculatoraa=newcalculator

(1);

calculatorbb=newcalculator

(1);

加法:

+aa.toString()+"

+bb.toString()+"

="

+aa.add(bb));

减法:

-"

+aa.sub(bb));

乘法:

*"

+aa.mul(bb));

除法:

/"

+aa.div(bb));

(4)代码如下:

Employee类:

packageshiyan2_4;

publicabstractclassEmployee

publicEmployee()

privateStringfirstName;

privateStringlastName;

publicEmployee(Stringfirst,Stringlast)

publicStringgetEmployeeName()

returnfirstName;

publicStringgetLastName()

returnfirstName+lastName;

publicabstractStringearnings();

}

publicfinalclassBossextendsEmployee

publicStringearnings()

通过Boss的工资计算公式计算出老板的工资"

CommissionWorker类:

publicfinalclassCommissionWorkerextendsEmployee

通过CommissionWorker的工资计算公式计算出老板的工资"

HourlyWorke类:

publicfinalclassHourlyWorkerextendsEmployee

通过HourlyWorker的工资计算公式计算出老板的工资"

PieceWorker类:

publicfinalclassPieceWorkerextendsEmployee

通过PieceWorker的工资计算公式计算出老板的工资"

Text类:

Employeeem=newBoss();

System.out.println(em.earnings());

em=newCommissionWorker();

em=newPieceWorker();

em=newHourlyWorker();

(5)代码如下:

packageshiyan2_5;

publicclassEx1

publicstaticvoidmain(Stringargs[])

Stringcl=null;

try

BufferedReaderstrin=newBufferedReader(newInputStreamReader(System.in));

cl=strin.readLine();

catch(IOExceptionex)

I/O有关的异常"

catch(NumberFormatExceptionex)

System.out.println(cl+"

字符串不能转换成整数"

catch(ArithmeticExceptionex)

除数不能0"

(6)代码如下:

自定义异常类:

packageshiyan2_6;

publicclassMyExceptionextendsException

publicMyException(Strings)

super(s);

publicMyException()

super();

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

publicclassDivide

publicstaticdoublediv(doublea,doubleb)throwsMyException

if(a==100&

(b==4||b==13))thrownewMyException("

不符合规范"

elsereturna/b;

doublea,b;

BufferedReaderstrin=newBufferedReader(newInputStreamReader(System.in));

a=Double.parseDouble(cl);

cl=strin.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 历史学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1