《Java程序设计》练习题 联系客服

发布时间 : 星期一 文章《Java程序设计》练习题更新完毕开始阅读

}catch(Exception e){

System.err.println(e.getMessage()); // 2 }

finally{

System.out.println(\ } }

public static void method1() throws Exception { method2(); //4 }

public static void method2() throws Exception { method3(); //5 }

public static void method3() throws Exception{

throw new Exception( \ } }

34、阅读下面的程序:

public class Timer extends Thread{ int time=0;

public Timer(int time) { this.time=time; }

public void run(){ try{

for(int i=0;i

System.out.print (i+“-”); } }

catch(Exception e) {

System.out.println(e.toString()); } }

public static void main(String args[]){ Timer timer=new Timer(10); timer.start(); } }

以上程序经编译后运行结果是:

五、程序填空题

1、阅读下面的程序,写出带划线语句的注释,并写出运行结果。

第 33 页 共 48 页

import java.awt.*; import javax.swing.*;

public class Test {// 继承JFrame private int screenOffset = 80; public Test(){

super( \ ; // 设置JFrame的大小为300*300 show(); // }

public void paint( Graphics g ){

for ( int i = 1; i <= 8; i++ ) {

int origin = screenOffset + 80 - i * 10; g.drawOval(origin, origin, i * 20, i * 20); } }

public static void main( String args[] ){ Test application = new Test();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); } }

2、阅读下面的程序,写出带划线语句或注释,并写出该程序的作用: import java.io.*; public class Test {

public static void main(String args[]) { scanFiles(\ }

public static void scanFiles(String path) { if (path == null) return;

File f = new File(path); // if (!f.exists()) return;

if (f.isFile()) // System.out.println(f.getAbsolutePath()); else {

File dir[] = f.listFiles(); for (int i = 0; i < dir.length; i++)

scanFiles(dir[i].getAbsolutePath());// } } }

3、仔细阅读下面的程序代码,请将划线上①~⑤的语句补充完整。

第 34 页 共 48 页

abstract class Person{

private String name;

public Person(String n){ name = n; }

public ____①________ String getDescription(); public ____②_________ (){ return name; } }

class Student ____③ _________ Person{

private String major;

public Student(String n, String m){ super(n); major = m; }

public String ____④_________(){ return \学生专业是:\ } }

public class TestPerson{

public static void main(____⑤________){

Person p = new Student(\王甜甜\计算机科学\

System.out.println(p.getName() + \ } }

4、阅读下面的程序MyAdd.java,填写划线部分的语句或注释,并写出程序执行结果。 public class MyAdd { //继承线程类 static int total = 10; int n;

public MyAdd(int n){

this.n=n; }

public void run() { try{

sleep(n); //含义是: total=total+n;

第 35 页 共 48 页

System.out.println(total); }

catch(Exception e){

System.out.println(“EXCEPTION!”); }

}

public static void main(String[] args) { MyAdd t1 = new MyAdd(3000);

; //启动线程t1 } }

5、阅读下面的程序Test.java,先填写空格的内容,然后写出运行结果:

21) import java.io.*; 22) public class Test{

23) public static void main(String argv[]){

24) ; //创建Test对象,对象名为t 25) System.out.println(t.fliton()); 26) }

27) public int fliton(){ 28) try{

//第9行的含义是: 29) FileInputStream din = new FileInputStream(\30) din.read();

31) }catch(IOException ioe){ //第11行的含义是: 32) System.out.println(\33) return -1; 34) }

35) finally{

36) System.out.println(\37) }

38) return 0; 39) } 40) }

如果文件test.txt与Test.java在同一个目录下,test.txt中仅有一行字符串“hello world!”,运行结果是什么?

6、阅读下面的程序代码,根据注释要求补充、完成代码(划线是需要补充的地方),最后简要说明该程序的功能。

1. ___________________{ //声明一个接口,接口名为Area 2. public double CalsulateArea(); 3. }

4. class MyCircle implements Area{ 5. double r;

第 36 页 共 48 页