浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
0
28、输出结果如下: i of methodI():21 i of Test11:3 i of Test1:2
29、输出结果如下: 150
30、输出结果如下: 134
31、输出结果如下:
32、输出结果如下:
33、执行的顺序为: 1-4-5-6-2-3
34、每隔1s打印一个数字和-,运行结果是:0-1-2-3-4-5-6-7-8-9-
五、程序填空题
1、
extends JFrame
调用父类JFrame的构造方法,使得窗口的标题为Concentric setSize(300,300);
使窗口为可见的,即可在屏幕上显示出来
输出结果为在窗口画8个同心圆,界面效果如下:
第 6 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
2、
创建path的File对象,对象引用名为f 判断f的对象是否为文件 递归调用scanFiles方法
该程序的作用是扫描C盘下所有的文件,并将文件的路径打印 3、
①abstract
②String getName ③extends
④getDescription() ⑤ String args[] 4、
extends Thread 让线程休眠n毫秒 t1.start();
程序的输出结果是: 3010 5、
Test t=new Test();
创建test.txt文件的FileInputStream对象,对象引用名为din 运行结果是: two 0 6、
interface Area
MyCircle c=new MyCircle(5);
MyRectangle r=new MyRectangle(2,3); 程序的功能是定义一个接口Area,其中包含一个计算面积的方法CalsulateArea(),然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法CalsulateArea(),分别计算圆和矩形的面积,最后写出测试以上类和方法的程序。
第 7 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
7、
①WindowListerner ②WindowEvent
③addWindowListerner ④setSize
8、略 9、
server=new ServerSocket(6000,50) socket=server.accept();
output = new ObjectOutputStream(socket.getOutputStream()); input = new ObjectInputStream(socket.getInputStream()); socket.close();
catch(IOException ioException) 或catch(Exception ioException) ioException可取任何变量
10~11、略
五、程序改错题 1、
(1)double PI; 改为double PI=Math.PI;
(2)class Cycle extends Shape 改为class Cycle implements Shape (3)double area( )改为public double area( )
(4)System.out.println(PI*r*r);改为 return PI*r*r; (5)在Cycle 类中增加以下方法
public double perimeter( ){ return 2*PI*r; }
或者去掉public double perimeter( ); 2、
第10行改为public String toString()
16~19行改为Person p=new Person(\张三\,20,\男\20行改为System.out.println(p.toString()); 3、
第4行的str.length()改为str.length 第8行start ()改为run()
第11行的sleep(1000);改为Thread.sleep(1000);
六、编程题
1、参考代码如下: class ForDemo{
第 8 页 共 32 页
,浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
public static void main(String args[]){ int n=0;
for(int i=10;i<=100;i++){ if(i%2==0&&i%3!=0){ n++;
System.out.print(i+\ }
if(n>=10){ n=0;
System.out.println(); } } } }
2、有两种方法可实现题目给定的getY : 方法一:若采用if语句实现,参考代码如下:
public double getY(double t){ double y=0.0;
if(t>=0&&t<1) y=t*t-1;
else if(t>=1&&t<3) y=t*t*t-2*t-3;
else if(t>=3&&t<5) y=t*t-t*Math.cos(t); else if(t>=5&&t<7) y=t+1; else y=t-1; return y;
}
方法二:若采用switch语句实现,参考代码如下:
public double getY(double t){ double y=0.0; switch((int)t){
case 0: y=t*t-1; break; case 1:
case 2: y=t*t*t-2*t-3;break; case 3:
case 4: y=t*t-t*Math.cos(t);break; case 5:
case 6: y=t+1;break; default: y=t-1; }
return y;
}
3、略
第 9 页 共 32 页
浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
4、参考代码如下:
interface AreaInterface{
double pai=Math.PI; double area(); }
5、参考代码如下: public class Student{ private String name; private int age;
public Student(){ name=\无名氏\ age=20; }
public void setName(String name){ this.name=name; }
public String getName(){ return name; }
public void setAge(int age){ this.age=age; }
public int getAge(){ return age; }
public boolean isSameAge(Student s){ if(s.age-this.age==0) return true; return false; }
public static void main(String args[]){ Student s1=new Student(); Student s2=new Student(); s2.age=20;
System.out.println(s1.isSameAge(s2)); } }
6:略
7、参考代码如下:
interface AreaInterface{ double pai=Math.PI;
第 10 页 共 32 页