《Java程序设计》练习题(7)

2018-11-20 17:17

}

30、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 class myException extends Exception{} public class Sample{ public void foo(){ try{

System.out.print(1); bar();

System.out.print(2); }catch(myException e){ System.out.print(3); } finally{

System.out.print(4); } }

public void bar() throws myException{ throw new myException(); }

public static void main(String args[]){ Sample s=new Sample(); s.foo(); } }

31、请简要画出编译运行下面程序的界面效果图。 import java.awt.*; import javax.swing.*;

public class ColorSelect extends JFrame { private JButton ok, cancel;

private JCheckBox background, foreground; private JComboBox colorList; private JPanel panel, panel2; private Container c; public ColorSelect(){

super( \ c=getContentPane();

c.setLayout(new BorderLayout());

colorList = new JComboBox(); colorList.addItem( \

c.add( colorList, BorderLayout.NORTH ); panel = new JPanel();

第 31 页 共 48 页

background = new JCheckBox( \ foreground = new JCheckBox( \ panel.add( background ); panel.add( foreground );

c.add( panel, BorderLayout.CENTER ); ok = new JButton( \

cancel = new JButton( \ panel2 = new JPanel(); panel2.add( ok ); panel2.add( cancel );

c.add( panel2, BorderLayout.SOUTH ); setSize( 300, 125 ); setVisible(true); }

public static void main ( String args[] ){ ColorSelect app = new ColorSelect();

app.setDefaultCloseOperation( EXIT_ON_CLOSE ); } }

32、仔细阅读下面的程序,简单的画出GUI的界面 import java.awt.*;

public class Test extends Frame { public Test() { super(\

setLayout(new BorderLayout());

add(new Button(\ add(new Button(\ add(new Button(\ add(new Button(\ add(new Button(\ }

public static void main(String args[]){ Test t=new Test(); t.pack(); t.show(); } }

33、仔细阅读下面的程序,写出程序的执行顺序(写出编号): public class UsingExceptions {

public static void main( String args[] ){ try{

method1(); // 1

第 32 页 共 48 页

}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 页


《Java程序设计》练习题(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2010高考政治 选择题汇编 新人教版必修2

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: