您好,欢迎来到尚车旅游网。
搜索
您的当前位置:首页2016年《Java程序设计》复习题

2016年《Java程序设计》复习题

来源:尚车旅游网


《Java程序设计》复习题

一、选择题:

1. 哪个关键字可以抛出异常? A、transient B、 finally C、 throw D、 static 2.对于下列代码:

String str1=\"java\"; String str2=\"java\";

String str3=new String(\"java\");

StringBuffer str4=new StringBuffer(\"java\"); 以下表达式的值为true的是 A) str1= =str2; B) str1= =str4; C) str2= =str3; D) str3= =str4;

3. Java的核心包中,提供编程应用的基本类的包是 。

A.java.lang B.java.util C.java.applet D.java.rmi 4.下面程序段的输出结果是

StringBuffer buf1=new StringBuffer( ); System.out.println(buf1.length); A. 0 B. null C. 16 D. 8

5. 在编译Java程序时,用于指定生成.class文件位置的选项是 。

A.-g B.-d C.-verbose D.-vonarn 6. 不匹配正则表达式“a?b+[.,:]\\\\d”的字符串是 。

A.abb.3 B.bb:2 C.ab,d D.bbb.4 7. 在方法内部使用,代表对当前对象自身引用的关键字是 。

A.super B.This C.Super D.this

8. 在读取二进制数据文件的记录时,为了提高效率常常使用的一种辅助类

是 。

A.InputStream B.FileInputStream C.BufferedInputStream D.BufferedReader 9. 下列事件监听器中,无法对TextField对象进行事件监听和处理的是 。

A.ActionListener B.FocusListener C.MouseMotionListener D.WindowListener

10.在java语言中,下列表达式返回true的是哪些项目。( )

A. \"hello\"==\"hello\" B. \"word\".equals(\"word\") C. \"john\"=\"john\" D. \"john\".equals(new Button(\"john\")) 11.整型数据类型中,需要内存空间最少的是 。

A.short B.long C.int D.byte 12.下列标识符(名字)命名原则中,正确的是 。

A.类名的首字母小写 B.变量和方法名的首字母大写 C.接口名的首字母小写 D.常量完全大写 13.Java中定义常量的保留字是 。

A.const B.final C.finally D.native 14.在调用方法时,若要使方法改变实参的值,可以 。

A.用基本数据类型作为参数 B.用指针作为参数 C.用对象作为参数 D.以上都不可以 15.下列有关Java布尔类型的描述中,正确的是 。

A.一种基本的数据类型,它的名称为boolean B.用int表示类型 C.其值可以赋给int类型的变量 D.有两个值:1代表真,0代表假 16.Java中所有类的父类是 。

A.Father B.Lang C.Exception D.Object 17.容器被重新设置大小后,哪种布局管理器的容器的组件大小不随容器大小变

化而改变?

A. CardLayout B. FlowLayout C. BorderLayout D. GridLayout 18.下列包中,包含Scanner类的是 。

A.javax.swing B.java.lang C.java.util D.java.applet 19.用于在子类中调用被重写父类方法的关键字是 。

A.this B.super C.This D.Super 20.Java中的抽象类Reader和Writer所处理的流是 。

A.图像流 B.对象流 C.字节流 D.字符流 21.java Swing中下列哪个类中方法能提供消息对话框 。

A.AWT B.JFrame C.JOptionPane D.JDialog 22.下列为窗口事件的是 。

A.MouseEvent B.WindowEvent C.ActionEvent D.KeyEvent 23.用鼠标点击菜单项(MenuItem)产生的事件是 。

A.MenuEvent B.ActionEvent C.KeyEvent D.MouseEvent 24. 可以获得文件file.txt的父目录名。

A.String name=File.getParentName(“file.txt”); B.String name=(new File(“file.txt”)).getParent();

C.String name=(new File(“file.txt”)).getParentName(); D.String name=(new File(“file.txt”)).getParentFile(); 25. 可以构建一个BufferedStream流。

A.new BufferedInputStream(“in.txt”);

B.new BufferedInputStream(new File(“in.txt”));

C.new BufferedInputStream(new InputStream(“in.txt”)); D.new BufferedInputStream(new FileInputStream(“in.txt”));

26.将长整型(long)数据转换为较短的整数类型(int)数据,要进行 。

A.类型的自动转换 B.类型的强制转换 C.无需转换 D.无法实现 27.下列特点中不属于Java的是 。

A.多线程 B.多继承 C.跨平台 D.动态性 28.下列标识符定义中,不合法的是 。

A.int $x; B.int _123; C.int Sum_2; D.int #dim; 29.java中用来抛出异常的关键字是

A、try B、catch C、throw D、finally 30.类变量必须带有的修饰符是 。

A.static B.final C.public D.volatile 31.下列选项中属于字符串常量的是 。

A.’abc’ B.”abc” C.[abc] D.(abc) 32.Java程序默认引用的包是 。

A.java.ext包 B.java.awt包 C.java.lang包 D.java.util包 33.当方法遇到异常又不知如何处理时,下列哪种说法是正确的( ) A、捕获异常 B、抛出异常 C、声明异常 D、嵌套异常

34.下列数组a中,版本较新的能在程序运行时动态调整大小的是( )。. A. int a[] B. String[]a C. a=new ArrayList() D. a=new Array() 35.下列方法中,与方法public void add(int a){}为合理重载的方法是: 。 A.public int add(int a) B.public void add(long a) C.public void add(int a) D.public void add(int a,int b) 36.在java语言中,下列表达式返回true的是哪些项目。( )

A) \"hello\"==\"hello\" B) \"word\".equals(\"word\") C) \"john\"=\"john\" D) \"john\".equals(new Button(\"john\")) 37. 下列有关文件和标准File类的说法中,错误的是 。 A.File类在java.io包中定义,用于处理文件,还可以获取各类与文件相关的信息。

B.File类用于获取或设置文件属性,并支持从文件中读取或往文件中写入数据的功能。

C.Java中目录也被当成一种文件进行处理。 D.文件是许多程序的基本数据源点和终点。 38.JFrame的默认布局管理器是 。

A.BorderLayout B.FlowLayout C.GridLayout D.PanelLayout 39.Java中类ObjectOutputStream支持对象的写操作,这是一种字节流,它的直

接父类是 。

A.Writer B.DataOutput C.OutputStream D.ObjectOutput 40.下列 事件表明在一个java.awt.Component构件之上有一个按键

按下。

A. KeyEvent B. KeyDownEvent C. KeyPressEvent D. KeyTypedEvent 41. 创建一个监听类,当鼠标移动时,它可以接受事件。

A. 通过继承MouseListener接口 B. 通过实现MouseListener接口 C. 通过继承MouseMotionListener接口 D. 通过实现MouseMotionListener接口

42. 请说出下列代码的执行结果 : String s = \"abcd\";

String s1 = new String(s);

if (s = = s1) System.out.println(\"the same\"); if (s.equals(s1)) System.out.println(\"equals\"); A. the same equals B. equals C. the same D. 什么结果都不输出 43. 下列有关 Java 中接口的说法 是正确的?

A. 接口中含有具体方法的实现代码

B. 若一个类要实现一个接口,则用到 “implements” 关键字 C. 若一个类要实现一个接口,则用到“ extends ”关键字 D. 接口不允许继承

44. 下列代码的执行结果是 ?

String s1 = \"aaa\"; s1.concat(\"bbb\");

System.out.println(s1);

A. The string \"aaa\". B. The string \"aaabbb\". C. The string \"bbbaaa\". D. The string \"bbb\".

45. 如果有一个对象 myListener ( 其中 myListener 对象实现了 ActionListener

接口 ), 下列哪条语句使得 myListener 对象能够接受处理来自于

smallButton 按钮对象的动作事件 ? A. smallButton.add(myListener);

B. smallButton.addListener(myListener);

C. smallButton.addActionListener(myListener); D. smallButton.addItem(myListener);

46、设 x = 1 , y = 2 , z = 3,则表达式 y+=z--/++x 的值是 。 A. 3 B. 3. 5 C. 4 D. 5 47、不允许作为类及类成员的访问控制符的是 。 A. public B. private C. static D. protected 48、为AB类的一个无形式参数无返回值的方法 书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为 。

A. static void method( ) B. public void method( )

C. final void method( ) D. abstract void method( )

49、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的 ?

A、public static void main( )

B、public static void main( String args[]) C、public static int main(String [] arg ) D、public void main(String arg[] ) 50、声明并创建一个标签对象b,应该使用的语句是

A、Label b=new Label(\"hello\"); C、Label b=new b(\"hello\");

B、label b=new label(\"hello\"); D、b.setLabel(\"hello\");

二、填空题

1.捕获异常的统一出口通过_________语句实现。

2.用于在子类中调用被重写父类方法的关键字是 。

3. Java语言对简单数据类型进行了类包装,int对应的包装类是 。 4. 若类声明时加上修饰符 ,则表示该类不能有子类。 5. 对于重载的方法,Java运行时系统根据调用方法的 的类型来决定调

用哪个方法。

6. 类方法不但可以由对象调用,还可以由 调用。

7. Calendar类的static方法 可以初始化一个Calendar对象。 8.Throwable类有两个子类:_______类和Exception类。 9.Java中的抽象类Reader和Writer所处理的流是 。 10.数组声明后,必须使用 运算符分配内存空间。 11.Java中用 修饰的成员变量不能被不同包中的类访问。 12.用鼠标点击菜单项(MenuItem)产生的事件是 。 13.一个类只有实现了 接口,它的对象才是可串行化的。 14.String类和StringBuffer类的区别是 。

15.FileInputStream类用于打开一个输入文件,实现对本地文件系统中文本文件

进行顺序访问。在创建它的实例时,若指定文件名或文件的路径不正确,将引起文件不能读 出的错误,系统会抛出 异常。 16.RandomAccessFile所实现的接口是___ _和DataOutput 接口。

17.子类如果想用父类的构造方法,必须在子类的构造方法中用 关键

字调用。

18.Java程序默认引用的包是 。

19.Java中 类可以用来建立多行文本区。 20.Java对象串行化技术,主要是通过ObjectInputStream类和 类来实现。 21.Java不直接支持多继承,但可以通过 实现多继承。

22.Java Swing中通过 类的 方法提供文件对话框。 23.java中一个类通过使用 关键字声明自己使用一个或多个接口。 24.接口中的方法的默认访问权限是 。 25.在方法体中抛出异常的语句是 语句。

26.Java的类库中提供Throwable类来描述异常,它有Error和 两个直

接子类。

27、在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode

码占用 字节内存空间,这样,无论是中文字符还是英文字符,都是占用 字节内存空间。

28、设 x = 2 ,则表达式 ( x + + )/3 的值是 。

29、若x = 5,y = 10,则x < y和x >= y的逻辑值分别为 和 。 30、 方法是一种仅有方法头,没有具体方法体和操作实现的方法,该方法

必须在抽象类之中定义。 方法是不能被当前类的子类重新定义的方法。 31、创建一个名为 MyPackage 的包的语句是 ,该语句应该放在程序的位

置为: 。

32、设有数组定义:int MyIntArray[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70}; 则执

行以下几个语句后的输出结果是 。 int s = 0 ;

for (int i = 0 ; i < MyIntArray.length ; i + + ) if ( i % 2 = = 1 )

s += MyIntArray[i] ;

System.out.println(s);

33、在Java程序中,通过类的定义只能实现 重继承,但通过接口的定义可

以实现 重继承关系。

三、判断题

1.如果异常发生时,没有捕获异常的代码,程序会正常执行。( ) 2. Java源程序文件中是不区分字母的大小写的。() 3. 可以用new来创建一个类的实例,即\"对象\"。()

4.语句String[] s=new String[10];执行后,s[0] 为 未定义。() 5. Java的接口方法必须声明成public。( ) 6. Java中缺省浮点型是float。( )

7. protected修饰的类成员只能被其子类访问。() 8. Java可以在方法内定义内部类。()

9. 在构造方法中调用super()语句,必须使其成为构造方法的第一条语句。() 10.StringBuffer类型的字符串是常量字符串,其值不能被改变。 () 11.子类可以继承父类所有的成员变量及成员方法。() 12.Pattern类的作用是提供类似c++的类模板。()

13.重写后的方法的访问权限不能比被重写的方法的访问权限大。( ) 14.Java的GUI机制中对事件的处理是采用委托方式进行的。() 15.重写后的方法与被重写的方法的返回值可以不一样。() 16.内部类可以访问它所在类中的所有成员变量和方法。() 17.扩展的适配器类允许只重写想要使用的特定方法。()

18.程序中抛出异常时(throw …),只能抛出自己定义的异常对象。()

19.BorderLayout布局管理器使用的是组件的最佳尺寸(preferred size)。 () 20.抽象类中只能有抽象方法,不能包含非抽象的方法 。 ()

21.实现一个接口的非抽象类中一定要实现接口中的所有方法。 () 22.一个类可以实现多个接口。 ()

23.定义在接口中的常量都默认为public、static和final。 () 24.break语句可以用在循环和switch语句中。 ()

25.FileInputStream可以作为BufferedInputStream的参数。() 26.在使用BorderLayout时,最多可以放入五个组件。() 27.Date类在java.lang包中。()

28.RandomAccessFile并不是单纯的输入或输出流,它的直接父类是File类。() .在Swing用户界面的程序设计中,面板是分层的。()

四、阅读程序,写出程序的运行结果 1.public class Test{

① public static void main(String[] a){ ② int i = Integer.parseInt(a[0]); ③ switch (i) {

④ case 1:System.out.println(\"Frist season\");break; ⑤ case 2:System.out.println(\"Second season\"); ⑥ case 3:System.out.println(\"3th season\");break; ⑦ case 4:System.out.println(\"Last season\"); ⑧ } ⑨ } ⑩ }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编

译正确,用java Test 2 运行得到的输出结果是什么?

2.public class PrintTest{

public static void main(String args[]){ int a=4,b=6,c=8; String s=\"abc\";

System.out.println(a+b+s+c); System.out.println(); } }

给出程序运行结果

3. public class Test{

1) public static void main(String[ ] args) { 2) int x,y=2,i=0,j=0;

3) if(args.length<2) System.exit(-1); 4) x = Integer.parseInt(args[1]); 5) switch(x){

6) case 2:switch(y){

7) case 1:i++;break; 8) case 2:j++;break; 9) default:i++;j++;

10) }

11) case 3:i++;j++;break; 12) default:i++;j++; 13) }

14) System.out.println(\"i=\"+i); 15) System.out.println(\"j=\"+j); 16) } 17) }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,用java Test 1 2 3 运行得到的运行结果是什么?

4. public class MultiplyList {

public static void main(String[] args) { String output=\"\";

blabel: for(int row=1;row<=9;row++){ for(int col=row;col<=9;col++){ if(row<8)continue blabel;

output+=col+\"*\"+row+\"=\"+(row*col)+\" \";

}

output+=\"\\n\"; }

System.out.println(output); System.exit(0); } }

给出程序运行结果

5. 下面的程序TestMonth.java: public class TestMonth{

public static void main(String []args){ try{ int month=Integer.parseInt(args[0]); if(month<0||month>12){ throw new ArithmeticException(\"没有\"+month+\"月份!\"); } System.out.println(\"您输入的月份为\"+month+\"月份\"); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(\"请输入月份!\"); }catch(ArithmeticException e){ System.out.println(\"捕获ArithmeticException异常\"); System.out.println(e.toString()); } } }

已知ArrayIndexOutOfBoundsException和ArithmeticException都是java.lang.*下的异常类,编译TestMonth.java后,用java TestMonth 13的运行结果是什么?

6. public class CatchTest{

public static void main(String args[]){ int[] x={0,1,2,3}; for(int i=0;i<3;i+=2){ try{

System.out.println(x[i+2]/x[i]+x[i+1]); }catch(ArithmeticException e){ System.out.println(“error1”); }catch(Exception e){

System.out.println(“error2”); } } } }

给出程序运行结果。

7. class GrandFather{

GrandFather(){System.out.println(“GrandFather”);}

}

class Book{

Book(){System.out.println(“Book”);} }

class Father extends GrandFather{

Father(){System.out.println(“Father”);}

}

public class Son extends Father { private Book b=new Book();

public Son(){System.out.println(“GrandFather”);} public static void main(String args[]){ new Son();

}

给出程序运行结果

8. class Count{

public static int counter = 0 ; public Count( ){ counter++ ; }

public int getCount(){ return counter; }

}

public class StaticTest{

public static void main(String[] args){ Count[] c = new Count[5]; for(int i=0;iSystem.out.println(c[i].getCount()); } } }

给出程序运行结果

9.下面的程序代码,若经编译和运行后,请写出打印结果。

1) class TestString{

2) public void stringReplace (String text) { 3) text = text.replace('j' , 'i'); 4) text=text+\"C\"; 5) }

6) public void bufferReplace (StringBuffer text) { 7) text.setCharAt(0,'i'); 8) text = text.append(\"C\"); 9) }

10) public void change(char ch[]){ 11) ch[0]= 'Y'; 12) }

13) public static void main(String args[]){ 14) String str1=\"java\";

15) StringBuffer str2=new StringBuffer(\"java\"); 16) char ch[]={'j','a','v','a'};

17) TestString t=new TestString(); 18) t.change(ch);

19) t.stringReplace (str1); 20) t.bufferReplace (str2); 21) System.out.println(str1);

22) System.out.println(str2.toString()); 23) System.out.println (new String(ch)); 24) } 25) }

10. package test;

public class OuterClass{ private class InterClass{ public InterClass(){

System.out.println(“InterClass”); } }

public OuterClass(){

InterClass ic= new InterClass(); System.out.println(“OuterClass”); }

public static void main(String args[]){ OuterClass oc=new OuterClass(); }

}

11. import java.io.*;

public class FileTest{

public static void main(String args[]){ File f=new File(\"document\ File d=new File(\"target\"); d.mkdir();

f.renameTo(new File(\"target\ String s[]=d.list();

for(int i=0;i12. class Electrical { String name;

Electrical (String s){ name=s; }

public void show(){

System.out.println(name); } }

class TV extends Electrical { TV(String s){ super(s); }

public void show() {

System.out.println(\"TV:\"+name); } }

class PC extends Electrical { PC(String s){ super(s); }

public void show() {

System.out.println(\"PC:\"+name); } }

public class ShowMessage{

public static void main(String args[]){ Electrical el;

el = new TV(\"长城牌电视机\"); el.show();

el = new PC(\"联想奔月5008PC机\");

el.show(); } }

13、 读下列程序,写出正确的运行结果。 class test {

public static void main (String [] args ){

int x=9, y; if (x>=0)

if (x>0)

y=1; else y=0;

else y=-1;

System.out.println(y); } }

14、 读程序,写出正确的运行结果。 public class Father{

int a=100;

public void miner(){

a--; }

public static void main(String[] args){

Father x = new Father(); Son y = new Son(); System.out.println(y.a);

System.out.println( y.getA()); y.miner();

System.out.println(y.a);

System.out.println(y.getA()); } }

class Son extends Father{

int a = 0;

public void plus(){ a++; }

public int getA() { return super.a; } }

15、 import java.io.*;

public class abc {

public static void main(String args [ ]) {

AB s = new AB(\"Hello!\VA.\");

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

class AB { String s1; String s2;

public AB(String str1, String str2) {

s1 = str1; s2 = str2; }

public String toString( ) {

return s1+s2; } }

16、 import java.io.* ; public class abc {

public static void main(String args[ ]) { int i, s = 0 ;

int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 }; for ( i = 0 ; i < a.length ; i ++ )

if ( a[i]%3 = = 0 ) s += a[i] ; System.out.println(\"s=\"+s); } }

17、import java.io.* ; public class abc {

public static void main(String args[ ]) {

System.out.println(\"a=\"+a+\"\\nb=\"+b); }

}

class SubClass extends SuperClass { int c;

SubClass(int aa, int bb, int cc) {

super(aa, bb);

c=cc; }

}

class SubSubClass extends SubClass { int a;

SubSubClass(int aa, int bb, int cc) { super(aa, bb, cc); A = aa+bb+cc; }

void show() {

System.out.println(\"a=\"+a+\"\\nb=\"+b+\"\\nc=\"+c); }

}

五、程序填空

1. 以下为一个applet的完整程序,它使用”宋体”字体,在applet窗口中显示背景色为黑色,前景色为绿色的字符串”您好!”。 import java.awt.*; import Java.applet.*;

public class DrawStringDemo Applet{ private Font afont= Font(“宋体”,Font.BOLD,18); public void init(){

(Color.black); }

public void paint(Graphics g){ g.setColor(Color.green); (afont); (“您好!”,10,40); } }

2.阅读下面的程序,修改程序中编译错误的地方(提示:共五处错误) interface Shape{ double PI;

public double area( );

public double perimeter( ); }

class Cycle extends Shape{ private double r;

public Cycle(double r){ this.r=r; }

double area( ){

System.out.println(PI*r*r); } }

public class Test{

public static void main(String args[]){ Cycle c=new Cycle(1.5);

System.out.println(\"面积为:\"+c.area()); } }

3.下面程序定义了一个字符串数组,并打印输出,捕获数组超越界限异常。请在横线处填入适当的内容完成程序。 public class HelloWorld{ int i=0;

String greetings[]={“Hello world!”, “No,I mean it!”,“HELLO WORLD!!” }; while(i<4) {

____________{

System.out.println(greeting[i]); }

_________________(ArrayIndexOutOfBoundsException e){ System.out.println(“Re-setting Index Value”); i=-1; }

finally{

System.out.println(“This is always printed”); } i++; } }

4. 下列程序从键盘输入若干个整数,当输入0时,输入终止;当输入的数小于0,或大于100时抛出异常。 import java.util.*;

class DigitException extends Exception {

//在下行空白处填上声明抛出DigitException异常的语句 void test(int x) { //在下行空白处填上抛出DigitException异常的语句 if(x<0 ||x>100) } }

public class MyInput{

public static void main(String args[]){ int input;

Scanner scanner=new Scanner( ); DigitException t=new DigitException(); while(scanner.hasNext()){ try{

input=scanner. ;

if(input==0)break;

t.test(input);

}catch(DigitException e){

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

} }

5. 该程序要求在输入对话框中输入一串字符串,并弹出一个消息对话框,报告字符串的长度。请在横线处填入适当内容,使程序能正确运行。 import javax.swing.*;

public class GetStringLength {

public static void main(String[] args) {

Integer len = 0;

String str = JOptionPane. (null,\"输入字符串\输入对话框\ ); len=str. ();

String strLen=\"字符串长度:\"+len.toString();

JOptionPane. (null,strLen,\"消息对话框\JOptionPane.PLAIN_MESSAGE); } }

6、补充下列求两点的中点坐标的程序。 class Spot {

private double x, y;

Spot (double u, double v) { this.setX(u); this.setY(v); } void setX(double x1) { x=x1; } void setY(double y1) { y=y1; } double getX() { return x; } double getY() { return y; } Spot midSpot(Spot s){ double midX= ; double midY= ; return ; } }

public class Test {

public static void main(String args[]) { Spot s1 = new Spot(3.0, 5.0); Spot s2 = new Spot(6.0, 8.0);

System.out.println(\"s1点的坐标:\"+ s1.getX()+\ System.out.println(\"s2点的坐标:\" + s2.getX()+\ Spot s = s1.midSpot(s2);

System.out.println(\"中点的坐标:\" + s.getX()+\ } }

7、补充程序求圆面积。 class point{ int x, y;

point( ){ x=0; y=0; }

point(int x, int y){ this.x=x; this.y=y; } double area( ) { return 0; } }

class circle extends point{ int radius;

circle(int r, int x, int y){ ;

radius=r; }

double area( ){

return Math.PI*radius*radius ; } }

public class Test {

public static void main(String args[ ]) { circle c1;

c1=new circle(1,1,1);

System.out.print(\"这个圆的面积是\"); System.out.println( ); } }

8、补充程序,完成下图所示图形用户界面。界面中,当点击“点我”按钮时,后面标签显示点击的次数,点击“清零”按钮时,后面标签中的显示点击次数清零。

import java.awt.*;

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

public class ButtonDemo extends JFrame ActionListener { protected JButton button1,button2; protected JLabel label1;

protected int clickedNum = 0; public ButtonDemo(String title) {

//设置窗口标题

label1 = new JLabel(\"共点击了\"+clickedNum+\"次\"); button1 = new JButton(\"点我\"); button2 = new JButton(\"清零\");

button1.addActionListener(this); button2.addActionListener(this); //当前窗口设置为顺序布局 this.setSize(400,200); this.add(button1); this.add(button2); this.add(label1); }

public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { }

if (e.getSource() == button2) { }

label1.setText(\"共点击了\"+clickedNum+\"次\") ; }

public static void main(String[] args) {

new ButtonDemo(\"ButtonDemo1\").setVisible(true); } }

六、编程题

1.使用 String 类的 public String toUpperCase() 方法可以将一个字符串中的小

写字母变成大写字母;使用 public String toLowerCase() 方法可以将一个字符串中的大写字母变成小写字母。编写一个程序,使用这个两个方法实现大小写的转换。

2.编写程序创建一个按钮和一个文本域,按钮上的文字为“change”,文本域中的文字为“color”,当单击按钮时,“color”的颜色在yellow和red之间切换。 3.设已定义了一个Book类,该类包括书名bookName和单价price属性。编写程序定义一个方法writeToFile(Book[] b),利用ObjectOutputStream类的writeObject()方法将若干本书籍的信息写入到当前路径下的文件book.dat中;定义一个方法readFromFile(),利用ObjectInputStream类的readObject()方法将若干本书籍的信息从当前路径下的文件book.dat中读出。

4.从命令行得到5个整数,放入一整型数组,然后打印输出,要求:如果输入数据不为整数,要捕获Integer.parseInt()产生的异常,显示“请输入整数”,捕获输入参数不足5个的异常(数组越界),显示“请输入至少5个整数”。 5. 创建一个标题为“按钮”的窗框,窗框中显示有“按下我”字样的按钮盒一个文本框,当单击时会在文本框上显示“Hello Java”。

6. 编写一个具有标准的菜单条的独立应用程序,包含菜单Dialog,Dialog菜单又包含菜单项“警告”和“确认”,当单击菜单项“警告”时弹出警告对话框;当单击菜单项“确认”时弹出确认对话框。

7 编写程序提取test.txt文件中的数字单词并输出。

8、声明一个圆柱体类,使得该类中的方法可以求圆柱体的表面积和体积,然后求底面半径是12.5,高是6.2的圆柱体的表面积和体积,进行输出。

9、已有类Person,代码如下,现在要构造学生类Student,在Person的基础上增加学号,班级号等属性,试写出Student的定义,要求Student继承Person,对Person中的print方法进行重写。 class Person {

protected String Name; protected char Sex;

protected String Birthday;

protected String ethnic; //民族

public Person() { Name = \"\"; Sex =’’; Birthday = \"\"; ethnic = \"\"; }

public Person(String Name,char Sex, String Birthday, String ethnic) { this. Name = Name; this.Sex = Sex; this.Birthday = Birthday; this.ethnic = ethnic; }

public String getName(){return Name;} public char getSex(){return Sex;}

public String getBirthday(){return Birthday;} public String getEthnic(){return ethnic;}

public void setName(String S){PName = S;} public void setSex(char C){Sex = C; }

public void setBirthday(String L){Birthday = L; } public void setEthnic(String E){ethnic = E; }

public void print () { System.out.println (\"Name:\"+ Name); System.out.println (\"Sex:\"+Sex); System.out.println (\"Birthday:\"+ Birthday); System.out.println (\"Ethnic:\"+ethnic); } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务