面向对象程序设计作业参考答案Word下载.docx
《面向对象程序设计作业参考答案Word下载.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计作业参考答案Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
如Testert1;
t1
t1=newTester();
:
Tester
8.编写Java应用程序,用一个对话框显示当前时间
importjavax.swing.*;
importjava.util.*;
publicclassTest{
publicstaticvoidmain(String[]args){
Datetoday=newDate();
JOptionPane.showMessageDialog(null,today);
}
}
9.下面的代码段会有什么样的输出:
classQ2main{
QuestionTwoq2;
q2=newQuestionTwo();
q2.init();
q2.increment();
System.out.println(q2.getCount());
classQuestionTwo{
privateintcount;
publicvoidinit(){
count=1;
publicvoidincrement(){
count=count+1;
publicintgetCount(){
returncount;
输出结果:
3
10.编写可以根据用户的年龄和身高给出推荐的体重的程序,利用下面的公式计算出推荐的体重:
recommandedWeight=(height-100+age/10)*0.9
定义名为Height(身高)的公共服务类,他应该有可以根据身高得出推荐体重的方法
publicclassTest{
publicstaticvoidmain(String[]args){
Weightw1=newWeight();
System.out.println(w1.getRecommendedWeight(30,170));
classWeight{
publicdoublegetRecommendedWeight(intage,intheight){
return(height-100+age/10)*0.9;
作业二
1.假如x的值为10,y的值为20,z的值为30,求出下列布尔表达式的值:
a)x>
y&
&
y>
x:
false
b)(x<
y+z)&
(x+10<
=20):
true
c)!
(x<
y+z)||!
(x+10<
d)!
((x==y))&
(x!
=y)&
(x<
y||y<
x):
2.用switch语句重写下面的if语句。
selection=Inter.parseInt(JOptionPane.showInputDialog(null,”Enterselection:
”));
if(selection==0)
System.out.println(“YouselectedMagenta”);
elseif(selection==1)
System.out.println(“YouselectedRed”);
elseif(selection==2)
System.out.println(“YouselectedBlue”);
elseif(selection==3)
System.out.println(“YouselectedGreen”);
else
System.out.println(“Invalidselection”);
改写为:
swith(selection){
case0:
break;
case1:
case2:
System.out.println(“YouselectedBlue”);
case3:
System.out.println(“YouselectedGreen”);
default:
System.out.println(“Invalidselection”);
)
3.画出下面两个switch语句的控制流程图
a)switch(choice){
case1:
a=0;
break;
case2:
b=1;
case3:
c=2;
case4:
d=3:
b)switch(choice){
default:
d=3;
a)
b)
4.分别用for、do-while和while语句计算下面的累加和:
a)1+1/2+1/3+1/4+…+1/15
for循环:
intx;
doubleresult=0.0F;
for(doublei=1;
i<
=15;
i++){
result+=1/i;
System.out.println(result);
do-while循环:
doublex=1;
do{
result+=1/x;
x++;
}while(x<
=15);
while循环:
while(x<
=15){
};
5+10+15+…+50
intx;
intresult=0;
for(inti=1;
=10;
result+=i*5;
intx=1;
result+=x*5;
=10);
=10){
};
5.编写一个计算闰年的程序,要求用户输入一个年份,如果用户输入的年份不在0~3000年内则给予用户提示要求其重新输入,否则判断该年份是否为闰年并返回结果。
LeapYearly=newLeapYear();
System.out.println(puteLeapYear(1998));
System.out.println(puteLeapYear(1900));
System.out.println(puteLeapYear(2000));
classLeapYear{
publicbooleancomputeLeapYear(intyear){
if(year%4==0&
year%100!
=0)returntrue;
if(year%100==0&
year%400==0)returntrue;
returnfalse;
6.下列哪一组重载方法是不合法的?
a)publicvoidcompute(intnum){…}
publicintcompute(intnum){…}
b)publicvoidmove(doublelength){…}
publicvoidmove(){…}
c)publicintadjust(doubleamount){…}
publicvoidadjust(doubleamount,doublecharge){…}
d)publicvoiddoWork(){…}
publicvoiddoWork(Stringname){…}
publicintdoWork(doublenum){…}
第a)组
7.完成下面这个类中的前四个构造方法。
其中每一构造方法都是用关键字this调用第五个构造方法:
classCat{
privatestaticfinalStringDEFAULT_NAME="
NONAME"
;
privatestaticfinalintDEFAULT_HGT=6;
privatestaticfinaldoubleDEFAULT_WGT=10.0;
privateStringname;
privateintheight;
privatedoubleweight;
publiccat(){
//分配缺省值给个成员变量
this(“”,0,0);
publiccat(Stringname){
//将参数值赋给name这个成员变量,height和weight赋缺省值
this(name,0,0);
publiccat(Stringnameintheight){
//将参数值分别赋给name和height两个成员变量,weight赋缺省值
this(name,height,0);
publiccat(Stringnameintweight){
//将参数值分别赋给name和weight两个成员变量,height赋缺省值
this(name,0,weight);
publiecat(Stringname,intheight,doubleweight){
this.name=name;
this.height=height;
this.weight=weight;
....
8.为Fraction定义一个类方法compare,compare接受两个Fraction对象f1和f2,并返回:
(1)如果f1小于f2,返回-1
(2)如果f1等于f2,返回0
(3)如果f1大于f2,返回1
publicstaticintcompare(Fractionf1,Fractionf2){
doublef1_dec=f1.decimal();
doublef2_dec=f2.decimal();
if(f1_dec<
f2_dec){
return-1;
}elseif(f1_dec=f2_dec){
return0;
}else{
return1;
作业三
1.什么是异常?
Java的异常处理机制是?
异常(exception)表示在程序正常运行过程中可能发生的错误的情形。
Java通过面向对象的方法来处理异常。
在一个方法的运行过程中,如果发生了异常,则这个方法生成代表该异常的一个对象,并把它交给运行时系统,运行时系统寻找相应的代码来处理这一异常。
把生成异常对象并把它提交给运行时系统的过程称为抛弃(throw)一个异常。
运行时系统在方法的调用栈中查找,从生成异常的方法开始进行回朔,直到找到包含相应异常处理的方法为止,这一个过程称为捕获(catch)一个异常。
2.分别输入-1,0和12XY,请写出这段代码的输出结果。
intnumber1;
try{
number1=Integer.parseInt(JOptionPane.showInputDialog(null,"
input"
));
if(number1!
=0){
thrownewException("
NotZero"
);
}catch(NumberFormatExceptione){
System.out.println("
Cannotconverttoint"
}catch(Exceptione){
System.out.println("
Error:
"
+e.getMessage());
}finally{
finallyClauseExecuted"
输入-1时:
NotZero
finallyClauseExecuted
输入0时:
输入12XY时:
Cannotconverttoint
3.分析下面程序代码的输出结果:
classMyExceptionextendsException{
publicMyException(){}
publicMyException(Stringmsg){super(msg);
publicclassExceptionTest{
publicstaticvoidf()throwsMyException{
The1stlineoff()"
thrownewMyException("
Originatedinf()"
System.out.println("
The1stlineofmain()"
try{
The2ndlineofmain()"
f();
The3rdlineofmain()"
}catch(MyExceptione){
System.out.println(e.getMessage());
finally{
The4thlineofmain()"
The5thlineofmain()"
输出结果为:
The1stlineofmain()
The2ndlineofmain()
The1stlineoff()
Originatedinf()
The4thlineofmain()
The5thlineofmain()
4.分析下面程序代码的输出结果:
classShape{
voiddraw(){
Shape.draw()"
classCircleextendsShape{
voiddraw(){
Circle.draw()"
classSquareextendsShape{
Square.draw()"
classShapeGenerator{
publicShapegetShape(intindex){
switch(index){
case0:
returnnewCircle();
returnnewSquare();
publicvoidshapeDraw(Shapes){
s.draw();
publicclassShapes{
privatestaticShapeGeneratorgen=
newShapeGenerator();
Shape[]s=newShape[3];
for(inti=0;
i<
s.length;
i++)
s[i]=gen.getShape(i);
gen.shapeDraw(s[i]);
Circle.draw()
Square.draw()
5.this和super关键字的意义和作用是?
this它代表当前对象名,可用来调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句),在程序中易产生二义性之处,应使用this来指明当前对象;
super:
它引用当前对象的直接父类中的成员,可以用来调用基类中的某一个构造函数(应该为构造函数中的第一条语句,)也可以用来在基类与派生类中有相同成员定义时直接访问父类中被隐藏的父类中成员数据或函数。
6.抽象方法和抽象类的特点和作用是?
一个方法的前面有修饰符abstract,说明这个方法是个抽象方法。
抽象方法没有方法体,只有方法声明。
抽象类前面有修饰符abstract,如果一个类的包含一个或者多个抽象方法,则这个类必须定义为抽象的。
不能产生抽象类的任何实例。
7.按以下要求给出相应的程序代码段:
a)创建一个抽象类AbstractBase,该类包含一个抽象方法finBase();
b)创建一个接口MyInterface,该类包含方法finInterface();
c)创建一个非抽象类MyClass,该类继承AbstractBase并实现MyInterface。
TestClasst1,t2;
t1=newTestClass();
t1.testFun();
t2=newTestClass(10);
t2.testFun();
abstractclassAAbstractBase{
abstractvoidtestFun();
classTestClassextendsAAbstractBase{
inti;
TestClass(){
this(0);
TestClass(inti){
this.i=i;
voidtestFun(){
System.out.println(i);