Java程序设计习题集 下载本文

Java程序设计习题集

public class Applet1 extends Applet { }

答:

10.import java.io.*; public class abc

{ public static void main(String args[]) { SubClass sb = new SubClass( ); System.out.println(sb.max( )); } }

class SuperClass

{ int a = 10 , b = 20 ; }

class SubClass extends SuperClass

{ int max( ) { return ((a>b)?a:b); } } 答:

public void paint( Graphics g ) { }

g.drawLine( 30,5,100,45 ); g.drawRect( 30,50,50,20 ); g.drawOval( 30,80,50,40 );

g.drawString( \

五、写出下面程序的运行结果

1.import java.io.*;

public class abc

{ public static void main(String args[ ]) { AB s = new AB(\ System.out.println(s.toString( )); } }

class AB {

- 29 - 第 页

Java程序设计习题集

String s1; String s2;

AB( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;} } 答:

2.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 =

3. import java.io.* ; public class abc {

public static void main(String args[ ]) { SubSubClass x = new SubSubClass(10 , 20 , 30); x.show(); } }

class SuperClass

{ int a,b;

SuperClass(int aa , int bb) { a=aa; b=bb; }

void show( )

{ System.out.println(\ }

- 30 - 第 页

Java程序设计习题集

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= b= c=

4. import java.io.*;

public class abc {

public static void main(String args[]) {

String s1 = \

String s2 = new String(\ System.out.println(s1.concat(s2)); }

}

答:Hello!World!

5. import java.io.* ; public class ABC {

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

int a[ ] = { 11,22,33,44,55,66,77,88,99 };

第- 31 - 页

Java程序设计习题集

for ( i = 0 ; i <= a.length / 2 ; i ++ )

System.out.print( a[i]+a[a.length-i-1]+\ }

System.out.println( );

}

答:110 110 110 110

6. import java.io.*; class Parent { } { }

public class Class1 {

public static void main(String args[ ]) {

Child myC = new Child( ); }

myC.printAll( ); }

void printMe() { }

System.out.println(\

class Child extends Parent

}

void printMe() { System.out.println(\}

void printAll() { super.printMe(); this.printMe(); printMe();

答:

- 32 - 第 页