plainButton = new JButton( \ container.put( plainButton );
fancyButton = new JButton( \ container.put( fancyButton );
ButtonHandler handler = new ButtonHandler(); fancyButton.addItemListener( handler ); plainButton.addItemListener( handler ); setSize( 275, 100 ); setVisible( true ); }
public static void main( String[] args ) {
ButtonTest application = new ButtonTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); }
private class ButtonHandler implements ActionListener {
public void itemStateChanged( ActionEvent event ) {
JOptionPane.showMessageDialog( null, \
event.getActionCommand() );
} } }
13、写出下列程序代码的运行结果:
1) class MinusException extends Exception { 2) int number;
3) public MinusException (String msg, int i) { 4) super(msg);
5) this.number = i; 6) } 7) } 8)
9) class Salary {
10) private String name; 11) private int salary;
12) public Salary(String n, int s ) throws MinusException { 13) this.name=n;
14) if (s<0) throw new MinusException(\工资不能为负!\15) this.salary=s; 16) }
17) public void print() {
18) System.out.println(name+\的工资为\元。\19) } 20) } 21)
第 21 页 共 48 页
22) class TestSalary {
23) public static void main(String [] args) { 24) try {
25) Salary s1=new Salary(“张三”,1000); 26) s1.print( );
27) Salary s2=new Salary(“李四”,-10); 28) s2.print( ); 29) }
30) catch(MinusException e) {
31) System.out.println(\异常:\32) } 33) } 34) }
14、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 1. public class Outer {
2. private int size = 1; 3.
4. public class Inner{ 5. private int size=2;
6. public void doStuff(int size){ 7. size++;
8. this.size++;
9. Outer.this.size++;
10. System.out.println(\
11. System.out.println(\12. System.out.println(\of the Outer class: \13. } 14. } 15.
16. Inner i=new Inner(); 17.
18. public void increaseSize(int s){ 19. i.doStuff(s); 20. } 21. 22.
23. public static void main(String[] a){ 24. Outer outer=new Outer(); 25. outer.increaseSize(10); 26. } 27. }
15、阅读下面的程序代码,写出程序运行的打印结果。
第 22 页 共 48 页
1. class ParentClass{ 2. int x=0;
3. int sum(int a,int b, int c){ 4. return a+b+c; 5. }
6. int sum(int a,int b){ 7. return a+b; 8. } 9. }
10. class ChildClass extends ParentClass{ 11. public ChildClass( ) {x=10;} 12. int sum(int a,int b){ 13. return a+b+1; 14. } 15. }
16. class Test{
17. public static void main(String args[]){ 18. ParentClass p=new ChildClass();
19. System.out.println(p.sum(5,5,5)); 20. System.out.println(p.sum(5,5)); 21. System.out.println(p.x); 22. } 23. }
16、阅读下面的程序,修改程序中错误的地方:
1) public class Test implements Runnable { 2) String str[]=new String[10]; 3) static {
4) for (int i = 0; i < str.length; i++) { 5) str[i]=i+”-”; 6) } 7) }
8) public void run() { 9) try {
10) for (int i = 0; i < str.length; i++) { 11) sleep(1000);
12) System.out.print(str[i]); 13) }
14) } catch (InterruptedException e) { 15) e.printStackTrace(); 16) } 17) }
18) public static void main(String args[]) { 19) Test t = new Test();
第 23 页 共 48 页
20) t.run(); 21) } 22) }
17、仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。
1) class TestString{
2) public void stringReplace (String text) { 3) text = text.replace('j' , 'i'); 4) text=text+\5) }
6) public void bufferReplace (StringBuffer text) { 7) text.setCharAt(0,'i'); 8) text = text.append(\9) }
10) public void change(char ch[]){ 11) ch[0]= 'Y'; 12) }
13) public static void main(String args[]){ 14) String str1=\
15) StringBuffer str2=new StringBuffer(\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) }
18、阅读下面的程序代码,判断18~27行(带划线部分)各语句编译是否通过,如果编译通过,直接写出该行的打印结果。
1. class TestString{
2. public static String stringReplace (String text) { 3. text = text.replace('j' , 'i'); 4. text=text+\5. return text; 6. }
7. public static StringBuffer bufferReplace (StringBuffer text) { 8. text.setCharAt(0,'i'); 9. text = text.append(\10. return text; 11. }
第 24 页 共 48 页