“Java语言程序设计”期末复习题(续)(含答案)程序阅读部分
三.程序阅读题
1.阅读以下程序:
class A
{ public static void main(String[] args)
{String s,s1="";
char c;
s=args[0];
for (int i=0;i
{ c=s.charAt(i);
if(c>&#61;&#39;a&#39; && c<&#61;&#39;z&#39;){
s1&#61;s1&#43;Character.toUpperCase(c);
}else{ s1&#61;s1&#43;Character.toLowerCase(c); }
}
System.out.println(s1); }
}
若在dos命令行输入&#xff1a;java A hELLO&#xff0c;则输出为 Hello 。
2&#xff0e;写出以下程序的运行结果。
public class EqualOrNot
{ public static void main(String[] args) false
{ B b1&#61;new B(5); false
B b2&#61;new B(5);
System.out.println(b1&#61;&#61;b2);
System.out.println(b1.equals(b2));
}
}
class B
{ int x;
B( int y){ x&#61;y; }
}
3&#xff0e;阅读以下程序&#xff1a;
import java.io.*;
public class ReadLineTest{
public static void main(String[ ] args){
BufferedReader b&#61;new BufferedReader (new InputStreamReader(System.in));
String s;
System.out.flush();
s&#61;b.readLine();
System.out.println(s);
}
}
运行以上程序&#xff0c;若从键盘输入&#xff1a; Hello
则输出结果为 Hello 。
4&#xff0e;写出以下程序的功能。复制文件a.txt到文件b.txt
import java.io.*;
public class C {
public static void main(String[] args) throws IOException {
File inputFile &#61; new File(“a.txt");
File outputFile &#61; new File(“b.txt");
FileReader in &#61; new FileReader(inputFile);
FileWriter out &#61; new FileWriter(outputFile);
int c;
while ((c &#61; in.read() ) !&#61; -1) out.write(c);
in.close();
out.close(); }
}
5&#xff0e;写出以下程序的功能。计算命令行参数串大小写字母的个数并在屏幕上显示结果
class Test
{ public static void main(String[] args)
{ String s;
char c;
int upper,lower;
upper&#61;lower&#61;0;
s&#61;args[0];
for (int i&#61;0;i
{ c&#61;s.charAt(i);
if(c>&#61;&#39;a&#39; && c<&#61;&#39;z&#39;) lower&#43;&#43;;
if(c>&#61;&#39;A&#39; && c<&#61;&#39;Z&#39;) upper&#43;&#43;; }
System.out.println(upper&#43;”,”&#43;lower); }
}
6&#xff0e;以下程序段的输出结果为 1 2 3 4 5 8 9 。
public class C
{ public static void main(String args[ ]){
int i , j ;
int a[ ] &#61; { 2,1,4,8,9,5,3};
for ( i &#61; 0 ; i int k &#61; i; for ( j &#61; i ; j if ( a[j] int temp &#61;a[i]; a[i] &#61; a[k]; a[k] &#61; temp; } for ( i &#61;0 ; i System.out.print(a[i]&#43;" "); System.out.println( ); } } 7&#xff0e;写出以下程序的运行结果。 no1 no2 no3 import java.util.*; no1 no3 public class Vec{ public static void main(String[] args) { String[] s; s&#61;new String[2]; s[0]&#61;new String("no1"); s[1]&#61;new String("no2"); Vector v &#61; new Vector(); for(int i &#61; 0; i <2; i&#43;&#43;) v.addElement(s[i]); v.insertElementAt(new String("no3"),2); Enumeration e &#61; v.elements(); while(e.hasMoreElements()) System.out.print(e.nextElement()&#43;" "); System.out.println(); v.removeElement("no2" ); for(int i &#61; 0; i System.out.println(); } } 8&#xff0e;写出以下程序的运行结果。 class StaticTest { static int x&#61;1; int y; StaticTest() { y&#43;&#43;; } x&#61;2 public static void main(String args[ ]){ st.y&#61;1 StaticTest st&#61;new StaticTest(); st.y&#61;1 System.out.println("x&#61;" &#43; x); System.out.println("st.y&#61;" &#43; st.y); st&#61;new StaticTest(); System.out.println("st.y&#61;" &#43; st.y); } static { x&#43;&#43;;} } 9&#xff0e;写出以下程序的运行结果。 x&#61;0 class StaticStuff x&#61;5 { x&#61;2 static int x; static { System.out.println("x&#61;" &#43; x); x&#43;&#61;5; } public static void main(String args[ ]){ System.out.println("x&#61;" &#43; x); } static { System.out.println("x&#61;" &#43; x);x%&#61;3; } } 10&#xff0e;以下程序段的输出结果为 int , int 。 class Cruncher{ void crunch( int i ){ System.out.print(“int”); } void crunch(String s){ System.out.print(“String”); } public static void main(String args[ ]){ Cruncher crun&#61;new Cruncher ( ); char ch&#61;’h’; int i&#61;12; crun.crunch(ch); System.out.print(“,”); crun.crunch(i); } } 11&#xff0e;阅读以下程序&#xff0c;输出结果为 hellojava 。 import java.io.*; public class TestString { public static void main(String args[ ]) { StringC s &#61; new StringC ("hello","java"); System.out.println(s); } } class StringC { String s1; String s2; StringC( String str1 , String str2 ) { s1 &#61; str1; s2 &#61; str2; } public String toString( ) { return s1&#43;s2;} } 12&#xff0e;阅读以下程序&#xff0c;写出输出结果。 in Second class class First{ in Second class public First(){ aMethod(); } public void aMethod(){ System.out.println(“in First class”);} } public class Second extends First{ public Second(){ aMethod(); } public void aMethod(){ System.out.println(“in Second class”);} public static void main(String[ ] args){ new Second( ); } } 13&#xff0e;写出以下程序的运行结果。 26 public class A { public static void main(String[ ] args) { System.out.println( test(15,26,4) ); } static int test(int x, int y, int z) { return test( x, test(y,z) ); } static int test(int x,int y) { if(x>y) return x; else return y; } } 14&#xff0e;写出以下程序的运行结果。 5.0 class MyException extends Exception{ Caught negative public String toString( ){ return "negative"; } } public class ExceptionDemo{ public static void mySqrt(int a) throws MyException { if( a<0 ) throw new MyException(); System.out.println(Math.sqrt(a)); } public static void main( String args[] ){ try{ mySqrt(25 ); mySqrt(-5 ); } catch( MyException e ){ System.out.println("Caught "&#43;e); } } } 15&#xff0e;写出以下程序的运行结果。 s1&#61;&#61;s2 class StringTest1 { public static void main(String[] args) { String s1&#61;"hello"; String s2&#61; "hello"; if(s1&#61;&#61;s2){ System.out.println("s1&#61;&#61;s2"); }else{ System.out.println("s1!&#61;s2");} } } 16&#xff0e;写出以下程序的功能。从键盘输入一行字符&#xff0c;显示到屏幕上 import java.io.*; public class ReadString{ public static void main(String[ ] args){ BufferedReader br&#61;new BufferedReader (new InputStreamReader(System.in)); try{ System.out.println( br.readLine()); }catch(IOException e){} }} 17&#xff0e;写出以下程序的运行结果。2 4 6 8 import java.io.*; public class UseLabel { public static void main(String[] args) {Loop: for(int i&#61;2; i<10; i&#43;&#43;) { if( i%2!&#61;0) continue Loop; System.out.print(i&#43;" "); } } } 18&#xff0e;写出以下程序的运行结果。 in First class First { in Second First() { System.out.println ("in First"); } } public class Second extends First { Second() { System.out.println("in Second"); } public static void main(String[] args) { Second mine&#61; new Second(); } } 19&#xff0e;写出以下程序的运行结果。 1 import java.io.*; public class ATest{ public static void main(String args[]) { Sub sb &#61; new Sub( ); System.out.println(sb.method1( )); } } class Super{ int x&#61;1 , y&#61;2 ; int method1(){ return x } class Sub extends Super{ int mothod1( ) { return ((x>y)?x:y); } } 20&#xff0e;写出以下程序的功能。 将数组元素从大到小排序并显示到屏幕上 public class ABC { public static void main(String args[ ]){ int i , j ; int a[ ] &#61; { 9,7,5,1,3}; for ( i &#61; 0 ; i int k &#61; i; for ( j &#61; i ; j if ( a[j]>a[k] ) k &#61; j; int temp &#61;a[i]; a[i] &#61; a[k]; a[k] &#61; temp; } for ( i &#61;0 ; i System.out.print(a[i]&#43;" "); System.out.println( ); } } 四&#xff0e;编程题 1&#xff0e;编写一个完整的Java Application 程序。包含接口ShapeArea&#xff0c;MyRectangle类&#xff0c;MyTriangle类及Test类&#xff0c;具体要求如下&#xff1a; ⑴接口ShapeArea&#xff1a; double getArea()&#xff1a;求一个形状的面积 double getPerimeter ()&#xff1a;求一个形状的周长 ⑵类 MyRectangle&#xff1a; 实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a; 属性 width&#xff1a; double类型&#xff0c;表示矩形的长 height&#xff1a; double类型&#xff0c;表示矩形的高 方法 MyRectangle(double w, double h)&#xff1a;构造函数 toString()方法 &#xff1a;输出矩形的描述信息&#xff0c;如“width&#61;1.0,height&#61;2.0, perimeter&#61;6.0, area&#61;2.0” ⑶类MyTriangle&#xff1a; 实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a; 属性 x,y,z: double型&#xff0c;表示三角形的三条边 s: 周长的1/2(注&#xff1a;求三角形面积公式为&#xff0c;s&#61;(x&#43;y&#43;z)/2 &#xff0c;开方可用Math.sqrt(double)方法) 方法 MyTriangle(double x, double y, double z)&#xff1a;构造函数&#xff0c;给三条边和s赋初值。 toString()&#xff1a;输出矩形的描述信息&#xff0c;如“three sides:3.0,4.0,5.0,perimeter&#61;12.0,area&#61;6.0” ⑷Test类作为主类要完成测试功能 生成MyRectangle对象 ② 调用对象的toString方法&#xff0c;输出对象的描述信息 2&#xff0e;编写一个完整的Java Application 程序。包含接口ShapeArea&#xff0c;类Circle、Rectangle、Test&#xff0c;具体要求如下&#xff1a; ⑴接口ShapeArea&#xff1a; ①接口方法 double getArea()&#xff1a;求一个形状的面积 double getPerimeter ()&#xff1a;求一个形状的周长 ⑵类Circle&#xff1a; 实现ShapeArea接口&#xff0c;并有以下属性和方法&#xff1a; 属性 radius&#xff1a; double类型&#xff0c;表示圆的半径 方法 Circle(double r)&#xff1a;构造函数 toString()方法 &#xff1a;输出圆的描述信息&#xff0c;如“radius&#61;1.0, perimeter&#61;6.28, area&#61;3.14” ⑶Test类作为主类要完成测试功能 生成Circle对象 ② 调用对象的toString方法&#xff0c;输出对象的描述信息 共12页&#xff0c;第 页