JAVA期末考试习题库中的编程题_第1页
JAVA期末考试习题库中的编程题_第2页
JAVA期末考试习题库中的编程题_第3页
JAVA期末考试习题库中的编程题_第4页
JAVA期末考试习题库中的编程题_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、大二上学期JAVA期末考试题库中的编程题1(6分) 编写一个输出"Hello World!"的程序,用两种方式实现(Application、Applet)。Application(2分)Applet(4分)2(8分) 用输入/输出写一个程序,让用户输入一些姓名和电话。每一个姓名和电话将加在文件里。用户通过输入"quit"来告诉系统整个列表已输入完毕。 如果用户输完整个列表,程序将创建一个输出文件phoneno.dat。 文件phoneno.dat中的数据格式如:,张三,李四 3编写一个输出"Welcome to Java World!"

2、;的Applet程序和嵌入该Applet的HTML页面。 / Welcome.java(4分)/Welcome.html(2分)4(8分) 用输入/输出写一个程序,让用户输入一些学生的姓名和学号。将每一个姓名和学号都加在文件里。用户通过输入"quit"来告诉系统整个列表已输入完毕。 如果用户输完整个列表,程序将创建一个输出文件studentno.dat。 文件studentno.dat中的数据格式如:2004001,杨一2004002,张三2004003,李四5编写一个完整的Java Application 程序。包含接口ShapeArea、类Rectangle和Test,

3、具体要求如下:接口ShapeArea接口方法double getArea():求一个形状的面积double getPerimeter ():求一个形状的周长类Rectangle实现ShapeArea接口,并有以下属性和方法: 属性width: double型,表示长方形的宽度height:double型,表示长方形的长度 方法Rectangle(double w, double h):构造函数toString():输出矩形的描述信息,如“width=1.0,height=2.0,perimeter=6.0,area=2.0”类Test作为主类要完成测试功能 生成Rectangle对象,长方形的

4、宽度和长度分别为1和2 调用对象的toString方法,输出对象的描述信息( 注:程序框架已给出,请填写完整。)import java.io.*;interface ShapeArea /接口定义2分class Rectangle implements ShapeArea /属性定义1分Rectangle(double w, double h) /构造函数定义 (1分) public double getPerimeter() /方法定义 (1分) public double getArea() /方法定义(1分) public String toString() / 方法定义(2分)publ

5、ic class Test public static void main(String args) /main方法定义 (2分)6编写一个完整的Java Application 程序,包括ShapeArea接口、MyTriangle类、Test类,具体要求如下:接口ShapeArea:double getArea():求一个形状的面积double getPerimeter ():求一个形状的周长类MyTriangle:实现ShapeArea接口,另有以下属性和方法: 属性x,y,z: double型,表示三角形的三条边s: 周长的1/2(注:求三角形面积公式为 ,s=(x+y+z)/2 ,开

6、方可用Math.sqrt(double)方法) 方法MyTriangle(double x, double y, double z):构造函数,给三条边和s赋初值。toString():输出矩形的描述信息,如“three sides:3.0,4.0,5.0,perimeter=12.0,area=6.0”Test类作为主类要完成测试功能 生成MyTriangle对象 调用对象的toString方法,输出对象的描述信息7编写一个完整的Java Application 程序。该程序包含类Person、Student、TestStudent,具体要求如下:类Person属性name : String

7、对象,表示一个人姓名sex: char类型,用来表示性别id:String对象,表示身份证号phone:String对象,表示联系电话email :String对象,表示E-mail地址 方法Person(Name name, char sex, String id):构造函数String getId() :获得身份证号void setEmail(String email) :设置E-mail地址void setPhone(String phone) :设置联系电话public String toString() :返回个人的各项信息,包括姓名、性别等上述属性类Student从Person类派

8、生,增加了以下属性和方法: 属性sNo: long类型,表示学生的学号sClass: String对象,表示学生的班级 方法Student(long sNo, String name, char sex, String id):构造函数setClass(String sClass): 设置学生的班级信息public String toString() :返回学生的各项信息,包括学号、班级、姓名等上述属性(3)类TestStudent作为主类要完成测试功能(注:程序框架已给出,请填写完整。)import java.io.*;class Person protected String name;p

9、rotected char sex; protected String id; protected String phone; protected String email; Person(String name, char sex, String id) /定义构造方法 (2分) String getId() /定义方法 (1分)String getName()/定义方法 (1分)void setEmail(String email) /定义方法 (1分)void setPhone(String phone) /定义方法 (1分) public String toString() Strin

10、g s = new String( "nt 姓名: " + name + "nt 性别: " + sex);if (id != null) s += "nt 身份证号: " + id;if (phone != null) s += "nt 联系电话: " + phone;if (email != null) s += "nt 联系email: " + email;return s; class Student extends Person/定义属性 (1分)Student(long sNo, S

11、tring name, char sex, String id) /定义构造方法 (2分) void setClass(String sClass) /定义方法 (1分) public String toString() String s = new String( "nt 姓名: " + name + "nt 性别: " + sex +"nt 学号:"+ sNo );if(sClass!=null) s+="nt 班级:"+sClass;if (id != null) s += "nt 身份证号: &q

12、uot; + id;if (phone != null) s += "nt 联系电话: " + phone;if (email != null) s += "nt 联系email: " + email;return s; public class TestStudent public static void main(String args) Student aStudent = new Student(2004001, "王非", '女',"2202");aStudent.setPhone(&qu

13、ot;");aStudent.setEmail("");System.out.println("student info: " + aStudent); 8编写一个完整的Java Application 程序。包含抽象类Shape,MyCircle类及Test类,具体要求如下:抽象类Shape:double getArea():求一个形状的面积double getPerimeter ():求一个形状的周长类 MyCircle:由Shape类派生的具体类,除实现Shape类中的方法外,另有以下属性和方法: 属性radius: double类型,表

14、示圆的半径 方法MyCircle(double r):构造函数toString()方法 :输出圆的描述信息,如“radius=1.0, perimeter=6.28, area=3.14” Test类作为主类要完成测试功能 生成MyCircle对象,半径为1.0 调用对象的toString方法,输出对象的描述信息( 注:程序框架已给出,请填写完整。)import java.io.*;abstract class Shape /抽象类定义 (3分)class MyCircle extends Shape final double PI=3.14;double radius; MyCircle(d

15、ouble r) /构造函数定义 (1分) public double getPerimeter()/方法定义 (1分) public double getArea()/方法定义(1分) public String toString() / 方法定义(2分)public class Test public static void main(String args) /main方法定义 (2分)-答案-1(application占2分)public class Welcome public static void main(String args) (1分) System.out.println

16、("Hello World!"); (1分) (applet占4分)import java.awt.Graphics; import java.applet.Applet; (1分)public class Welcome extends Applet (1分)String s;public void init() s=" Hello World!" (1分)public void paint(Graphics g) g.drawString(s,25,25); (1分)2import java.io.*; public class StudentFil

17、e static FileOutputStream fos;public static final int lineLength = 81;public static void main(String args) throws IOException byte phone = new bytelineLength;byte name = new bytelineLength;try fos = new FileOutputStream("phoneno.dat"); catch(FileNotFoundException e) while (true) System.out

18、.println("Enter a name (enter 'quit' to quit)");readLine(name);if ("quit".equalsIgnoreCase(new String(name,0,0,4) break; System.out.println("Enter the phone number"); readLine(phone);for (int i=0;phone!= 0;i+) fos.write(phone); fos.write(',');for (int i=

19、0;name!= 0;i+) fos.write(name); fos.write('n');fos.close(); private static void readLine(byte line) throws IOException int i=0,b=0; while (i<LINELENGTH-1)&&(B=SYSTEM.IN.READ()!='N') linei+ = (byte)b; line=(byte) 0; 3(6分)/Welcome.javaimport java.awt.Graphics; import java.ap

20、plet.Applet; (1分)public class Welcome extends Applet (1分)String s;public void init() s=" Welcome to Java World!" (1分)public void paint(Graphics g) g.drawString(s,25,25); (1分)/Welcome.html(2分)4(8分)import java.io.*; public class StudentFile static FileOutputStream fos;public static final int

21、 lineLength = 81;public static void main(String args) throws IOException byte no = new bytelineLength;byte name = new bytelineLength;try fos = new FileOutputStream("studentno.dat"); catch(FileNotFoundException e) while (true) System.out.println("Enter a name (enter 'quit' to q

22、uit)");readLine(name);if ("quit".equalsIgnoreCase(new String(name,0,0,4) break; System.out.println("Enter the student number"); readLine(no);for (int i=0;no!= 0;i+) fos.write(no); fos.write(',');for (int i=0;name!= 0;i+) fos.write(name); fos.write('n');fos.cl

23、ose(); private static void readLine(byte line) throws IOException int i=0,b=0; while (i<LINELENGTH-1)&&(B=SYSTEM.IN.READ()!='N') linei+ = (byte)b; line=(byte) 0; 5public class Test /主类定义2分public static void main(String args) Rectangle r=new Rectangle(1,2);System.out.println(r.toSt

24、ring(); interface ShapeArea /接口定义2分 public abstract double getPerimeter();public abstract double getArea();class Rectangle implements ShapeArea double width, height; /属性定义1分Rectangle(double w, double h) /构造函数定义1分 width=w; height=h; public double getPerimeter() /1分 return 2*( width+height);public dou

25、ble getArea() /1分 return width*height; public String toString() /2分 return "width="+width+",height="+height+",perimeter="+getPerimeter()+",area="+getArea();6/参考源程序如下,具体评分标准见程序注释public class TestShape /主类定义1分 public static void main(String args) MyTriangle t=ne

26、w MyTriangle(3.0,4.0,5.0);System.out.println(t.toString(); interface ShapeArea /接口定义2分 public abstract double getPerimeter();public abstract double getArea();class MyTriangle implements ShapeArea double x,y,z;double s;MyTriangle(double t1,double t2, double t3) /构造方法1分 x=t1; y=t2; z=t3; s=(x+y+z)/2;

27、public double getPerimeter() return x+y+z; /求周长方法2分public double getArea() return Math.sqrt(s*(s-x)*(s-y)*(s-z); / 求面积方法2分public String toString() / toString()2分 return "three sides:"+x+","+y+","+z+","+"perimeter="+getPerimeter()+",area="+g

28、etArea(); 7class Personprotected String name;protected char sex; protected String id; protected String phone; protected String email; /定义构造方法 (2分)Person(String name, char sex, String id) = name;this.sex = sex;this.id = id; /定义方法 (1分)String getId()return id; /定义方法 (1分)String getName() retur

29、n name; /定义方法 (1分)void setEmail(String email) this.email = email; /定义方法 (1分)void setPhone(String phone) this.phone = phone; public String toString() String s = new String( "nt 姓名: " + name + "nt 性别: " + sex);if (id != null) s += "nt 身份证号: " + id;if (phone != null) s +=

30、"nt 联系电话: " + phone;if (email != null) s += "nt 联系email: " + email;return s; class Student extends Person/定义属性 (1分)long sNo;String sClass;/定义构造方法 (2分)Student(long sNo, String name, char sex, String id) super(name,sex,id);this.sNo=sNo; /定义方法 (1分)void setClass(String sClass) this.sClass=sClass; public String toString()String s = new String( "nt 姓名: " + name + "nt 性别: " + sex +"nt 学号:"+ sNo );if(sClass!=null) s+="nt 班级:"+sClass;if (id != null) s += "nt 身份证号: " +

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论