JAVA期末考试题库中的编程题Word下载.docx
《JAVA期末考试题库中的编程题Word下载.docx》由会员分享,可在线阅读,更多相关《JAVA期末考试题库中的编程题Word下载.docx(12页珍藏版)》请在冰豆网上搜索。
doublegetArea():
求一个形状的面积
doublegetPerimeter():
求一个形状的周长
⑵类Rectangle
实现ShapeArea接口,并有以下属性和方法:
①属性
width:
double型,表示长方形的宽度
height:
double型,表示长方形的长度
②方法
Rectangle(doublew,doubleh):
构造函数
toString():
输出矩形的描述信息,如“width=1.0,height=2.0,perimeter=6.0,area=2.0”
⑶类Test作为主类要完成测试功能
①生成Rectangle对象,长方形的宽度和长度分别为1和2
②调用对象的toString方法,输出对象的描述信息
(注:
程序框架已给出,请填写完整。
)
importjava.io.*;
interfaceShapeArea//接口定义2分
{
}
classRectangleimplementsShapeArea
{
//属性定义1分
Rectangle(doublew,doubleh)//构造函数定义(1分)
}
publicdoublegetPerimeter()//方法定义(1分)
publicdoublegetArea()//方法定义(1分)
publicStringtoString()//方法定义(2分)
publicclassTest
publicstaticvoidmain(Stringargs[])//main方法定义(2分)
6.编写一个完整的JavaApplication程序,包括ShapeArea接口、MyTriangle类、Test类,具体要求如下:
⑴接口ShapeArea:
⑵类MyTriangle:
实现ShapeArea接口,另有以下属性和方法:
③属性
x,y,z:
double型,表示三角形的三条边
s:
周长的1/2(注:
求三角形面积公式为,s=(x+y+z)/2,开方可用Math.sqrt(double)方法)
④方法
MyTriangle(doublex,doubley,doublez):
构造函数,给三条边和s赋初值。
输出矩形的描述信息,如“threesides:
3.0,4.0,5.0,perimeter=12.0,area=6.0”
⑶Test类作为主类要完成测试功能
②生成MyTriangle对象
7.编写一个完整的JavaApplication程序。
该程序包含类Person、Student、TestStudent,具体要求如下:
⑴类Person
①属性
name:
String对象,表示一个人姓名
sex:
char类型,用来表示性别
id:
String对象,表示身份证号
phone:
String对象,表示联系电话
email:
String对象,表示E-mail地址
②方法
Person(Namename,charsex,Stringid):
StringgetId():
获得身份证号
voidsetEmail(Stringemail):
设置E-mail地址
voidsetPhone(Stringphone):
设置联系电话
publicStringtoString():
返回个人的各项信息,包括姓名、性别等上述属性
⑵类Student
从Person类派生,增加了以下属性和方法:
sNo:
long类型,表示学生的学号
sClass:
String对象,表示学生的班级
Student(longsNo,Stringname,charsex,Stringid):
setClass(StringsClass):
设置学生的班级信息
返回学生的各项信息,包括学号、班级、姓名等上述属性
(3)类TestStudent作为主类要完成测试功能
(注:
classPerson
protectedStringname;
protectedcharsex;
protectedStringid;
protectedStringphone;
protectedStringemail;
Person(Stringname,charsex,Stringid)//定义构造方法(2分)
StringgetId()//定义方法(1分)
StringgetName()//定义方法(1分)
voidsetEmail(Stringemail)//定义方法(1分)
voidsetPhone(Stringphone)//定义方法(1分)
publicStringtoString()
{Strings=newString("
\n\t姓名:
"
+name+"
\n\t性别:
+sex);
if(id!
=null)s+="
\n\t身份证号:
+id;
if(phone!
\n\t联系电话:
+phone;
if(email!
\n\t联系email:
+email;
returns;
}
classStudentextendsPerson
//定义属性(1分)
Student(longsNo,Stringname,charsex,Stringid)//定义构造方法(2分)
voidsetClass(StringsClass)//定义方法(1分)
+sex+"
\n\t学号:
+sNo);
if(sClass!
=null)s+="
\n\t班级:
+sClass;
publicclassTestStudent
{publicstaticvoidmain(Stringargs[])
{StudentaStudent=newStudent(2004001,"
王非"
'
女'
"
2202198506070222"
);
aStudent.setPhone("
88078549"
aStudent.setEmail("
wangfei@"
System.out.println("
studentinfo:
+aStudent);
8.编写一个完整的JavaApplication程序。
包含抽象类Shape,MyCircle类及Test类,具体要求如下:
⑴抽象类Shape:
⑵类MyCircle:
由Shape类派生的具体类,除实现Shape类中的方法外,另有以下属性和方法:
radius:
double类型,表示圆的半径
MyCircle(doubler):
toString()方法:
输出圆的描述信息,如“radius=1.0,perimeter=6.28,area=3.14”
③生成MyCircle对象,半径为1.0
abstractclassShape//抽象类定义(3分)
classMyCircleextendsShape
finaldoublePI=3.14;
doubleradius;
MyCircle(doubler)//构造函数定义(1分)
publicdoublegetPerimeter()//方法定义(1分)
publicdoublegetArea()//方法定义(1分)
----------------------答案------------------------------------------------
1.①(application占2分)
publicclassWelcome
{ publicstaticvoidmain(Stringargs[])(1分)
{ System.out.println("
HelloWorld!
}(1分)
②(applet占4分)
importjava.awt.Graphics;
importjava.applet.Applet;
(1分)
publicclassWelcomeextendsApplet{(1分)
Strings;
publicvoidinit(){s="
HelloWorld!
;
}(1分)
publicvoidpaint(Graphicsg){g.drawString(s,25,25);
2.importjava.io.*;
publicclassStudentFile
{staticFileOutputStreamfos;
publicstaticfinalintlineLength=81;
publicstaticvoidmain(Stringargs[])throwsIOException
{byte[]phone=newbyte[lineLength];
byte[]name=newbyte[lineLength];
try{fos=newFileOutputStream("
phoneno.dat"
catch(FileNotFoundExceptione){}
while(true)
{System.out.println("
Enteraname(enter'
quit'
toquit)"
readLine(name);
if("
.equalsIgnoreCase(newString(name,0,0,4)))
{break;
Enterthephonenumber"
readLine(phone);
for(inti=0;
phone!
=0;
i++)
{fos.write(phone);
fos.write('
'
name!
i++)
{fos.write(name);
\n'
fos.close();
privatestaticvoidreadLine(byteline[])throwsIOException
{inti=0,b=0;
while((i<
LINELENGTH-1)&
&
((B=SYSTEM.IN.READ())!
='
\N'
))
{line[i++]=(byte)b;
}
line=(byte)0;
3.(6分)
①//Welcome.java
WelcometoJavaWorld!
②
//Welcome.html
(2分)
4.(8分)
{byte[]no=newbyte[lineLength];
studentno.dat"
Enterthestudentnumber"
readLine(no);
no!
{fos.write(no);
5.publicclassTest{//主类定义2分
publicstaticvoidmain(Stringargs[])
{Rectangler=newRectangle(1,2);
System.out.println(r.toString());
{publicabstractdoublegetPerimeter();
publicabstractdoublegetArea();
classRectangleimplementsShapeArea{
doublewidth,height;
//属性定义1分
Rectangle(doublew,doubleh)//构造函数定义1分
{width=w;
height=h;
publicdoublegetPerimeter()//1分
{return2*(width+height);
publicdoublegetArea()//1分
{returnwidth*height;
publicStringtoString()//2分
{return"
width="
+width+"
height="
+height+
perimeter="
+getPerimeter()+"
area="
+getArea();
6.//参考源程序如下,具体评分标准见程序注释
publicclassTestShape//主类定义1分
{MyTrianglet=newMyTriangle(3.0,4.0,5.0);
System.out.println(t.toString());
classMyTriangleimplementsShapeArea{
doublex,y,z;
doubles;
MyTriangle(doublet1,doublet2,doublet3)//构造方法1分
{x=t1;
y=t2;
z=t3;
s=(x+y+z)/2;
publicdoublegetPerimeter(){returnx+y+z;
}//求周长方法2分
publicdoublegetArea(){returnMath.sqrt(s*(s-x)*(s-y)*(s-z));
}//求面积方法2分
publicStringtoString()//toString()2分
threesides:
+x+"
+y+"
+z+"
+"
perimeter="
7.
//定义构造方法(2分)
Person(Stringname,charsex,Stringid)
{this.name=name;
this.sex=sex;
this.id=id;
//定义方法(1分)
StringgetId()
{returnid;
StringgetName()
{returnname;
voidsetEmail(Stringemail)
{this.email=email;
voidsetPhone(Stringphone)
{this.phone=phone;
longsNo;
StringsClass;
Student(longsNo,Stringname,charsex,Stringid)
{super(name,sex,id);
this.sNo=sNo;
voidsetClass(StringsClass)
{this.sClass=sClass;