当前位置:首页 > 《java程序设计》期末复习题
Inher3_1(){ //x1 a=1; }
Inher3_1(int a){ //x2 this.a=a; } }
public class Inher3 extends Inher3_1{ int b;
Inher3(int a, int b){ //x3 super(a); //x4 this.b=b; //x5 }
public static void main(String[] args){ Inher3 ob=new Inher3(10,20); //x6 System.out.println(ob.a+” “+ob.b); //x7 }}
32、class Inher6_1{ void hello(){
System.out.println(“Hi~~”); } }
public class Inher6 extends Inher6_1{ void hello(){ //覆盖
super.hello(); //调用继承而来的hello()方法 System.out.println(“高兴”); //功能的扩展 }
public static void main(String[] args){ Inher6 ob=new Inher6(); ob.hello(); }}
33、 class Super{ int a=10; void hi(){
System.out.println(\你好~~\ } }
class Sub extends Super{ int b=20; void hi(){
System.out.println(\ }
void bye(){
System.out.println(\ }}
public class Inher9{
public static void main(String[] args){ Super ob=new Sub(); ob.a=20; ob.hi();
//ob.b=30;错误 //ob.bye();错误 }}
34、public class Excep2{
public static void main(String[] args){ System.out.println(\开始程序\ int a=1,b=0; try{
37
int c=a/b; //x1 System.out.println(c); //x2 }catch(ArithmeticException ae){ //x3
System.out.println(\不能被0除!\ }
System.out.println(\退出程序\ }}
35、 public class Excep7{
static void go() throws NegativeArraySizeException{ int[] a=new int[-1]; //x1 }
static void hi() throws NegativeArraySizeException{ go(); //x2 }
public static void main(String[] args){ try{ hi();
}catch(NegativeArraySizeException e){
System.out.println(\捕获来自远方的异常.\ } } }
36、import java.io.IOException; public class Excep10{
public static void main(String[] args){ int a=0;
System.out.print(\请敲入字符>> \ try{
a=System.in.read(); }catch(IOException e){
System.out.println(\错误~~~~\ }
System.out.println(\敲入的字符为: \ }}
37、阅读程序,回答问题 1、
1: public class Output1 {
2: public static void main(String arge[]) { 3: int i=0;
4: for ( char ch = 97; ch<113; ch++,i++) { 5: if( i % 8 == 0 )
6: System.out.println(\7: System.out.print(\8: } 9: } 10: }
(1) 程序第 5 、 6 行的 if 语句的功能是什么? (2) 程序输出的结果有几行?
【 答案 】 (1) 每打印 8 个字符,则换行。 (2) 输出的结果有 2 行。 38、写出下面程序的运行结果。 1、import java.io.*; public class abc
{ public static void main(String args[ ]) { AB s = new AB(\ System.out.println(s.toString( )); } }
class AB { String s1; String s2;
38
AB( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;} }
答:Hello! I love JAVA. 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 = 180
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(\ }
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=60 b=20 c=30
4、 import java.io.*; public class abc {
public static void main(String args[]) {
String s1 = \
39
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 }; 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 110 6、 import java.io.*; class Parent {
void printMe() {
System.out.println(\ }}
class Child extends Parent {
void printMe() {
System.out.println(\ }
void printAll() {
super.printMe(); this.printMe(); printMe(); }}
public class Class1 {
public static void main(String args[ ]) {
Child myC = new Child( ); myC.printAll( ); }}
答:parent child child
39、编写一个字符界面的Java Application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。
答:参考程序如下: import java.io.*; public class SUM {
public static void main (String[] args) {
int i , n=-1 , sum=0 ; do{ try {
BufferedReader br =
new BufferedReader( new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine( ));
40
共分享92篇相关文档