Java程序设计习题集(含答案) 下载本文

内容发布更新时间 : 2024/12/23 14:29:54星期一 下面是文章的全部内容请认真阅读。

Java程序设计习题集

System.out.println( Max+\

System.out.println( ); } }

答:

5. public class ABC {

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

int a[ ] = { 12,67,8,98,23,56,124,55,99,100 }; for ( i = 0 ; i < a.length-1; i ++ ) { int k = i;

for ( j = i ; j < a.length ; j++ ) if ( a[j]

a[k] = temp; }

for ( i =0 ; i

System.out.println( ); } }

6.import java.io.*;

public class Class1 {

public static void main( String args[] ) {

Fact N = new Fact( 4 ); System.out.println( N.fact( ) ); } }

class Fact { int n ;

Fact( int nn ) { n = nn; } int fact( ) { int i , f = 1;

for ( i=1; i<=n; i++ )

- 26 - 第 页

Java程序设计习题集

}

}

f = f*i;

return f;

答:

7.import java.awt.*;

import java.applet.Applet;

public class DrawMyImage extends Applet {

Image myImage; // 定义一个图像类Image的对象myImage public void init( ) {

myImage = getImage(getDocumentBase( ),\}

public void paint(Graphics g) {

g.drawImage(myImage,0,0,this); } }

答:

8. import java.io.*;

public class Class1 {

public static void main( String args[ ] ) { SubClass a = new SubClass( 10,3 ); System.out.println( a.exp( )); } }

class SuperClass { float x;

int n;

SuperClass( float xx , int nn ) { x = xx ; n = nn; } }

class SubClass extends SuperClass { SubClass( float xx , int nn ) { super( xx , nn ); } float exp( ) {

float s = 1;

for ( int i = 1; i<=n; i++ ) s = s*x; return s;

- 27 - 第 页

Java程序设计习题集

} }

答:

9. import java.awt.*; import java.applet.Applet;

public class Applet1 extends Applet { }

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( \

答:

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); } } 答:

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

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

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

- 28 - 第 页

Java程序设计习题集

} }

class AB { 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( )

- 29 - 第 页

Java程序设计习题集

{ System.out.println(\ }

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)); } } 答:

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

{

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

第- 30 - 页