《《JAVA程序设计》(习题)》由会员分享,可在线阅读,更多相关《《JAVA程序设计》(习题)(13页珍藏版)》请在金锄头文库上搜索。
1、JAVA 程序设计练习题写出下面程序的运行结果1、 import java.io.*; public class abc public static void main(String args ) AB s = new AB(Hello!,I love JA VA.); System.out.println(s.toString( ); class AB String s1; String s2; public AB(String str1, String str2) s1 = str1; s2 = str2; public String toString( ) return s1+s2; 运
2、行结果: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 ( ai%3 = = 0 ) s += ai ; System.out.println(s=+s); 运行结果:S=180 /*3 、import java.io.* ; public clas
3、s abc public static void main(String args ) System.out.println(a=+a+nb=+b); 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
4、.out.println(a=+a+nb=+b+nc=+c); 运行结果: a=60 b=20 c=30 4、以下程序的输出结果为 _Peter is17 years old !_ 。public class Person String name; int age; public Person(String name, int age) this.name = name; this.age = age; public static void main(String args) Person c = new Person(Peter, 17); System.out.println(c.name
5、 + is + c.age + years old!); 5 、 以 下 程 序 的 输 出 结 果 为 _ 课 程 号 : 101 课 程 名 :ASP 学分:3_ 。public class Course private String cNumber; private String cName; private int cUnit; public Course(String number, String name, int unit) cNumber = number; cName = name; cUnit = unit; public void printCourseInfo() Sy
6、stem.out.println(课程号: + cNumber + 课程名 : + cName + 学分: + cUnit); class CourseTest public static void main(String args) Course c; c = new Course(101, ASP, 3); c.printCourseInfo(); 6、以下程序的输出结果为 _汤姆猫体重: 20.0斤_ 。public class Tom private float weight; private static String name; public void setWeight(floa
7、t weight) this.weight = weight; private void out() System.out.println(name + 体重: + weight + 斤); public static void main(String args) Tom.name = 汤姆猫 ; Tom cat = new Tom(); cat.setWeight(20); cat.out(); 7、 以下程序的输出结果 _姓名: Tom 年龄:15 家庭住址:金水区 电话: _66123456 学校:九中 _ 。public class Father String name, addres
8、s, tel; int age; public Father(String name, int age) this.name = name; this.age = age; void out() System.out.print(姓名: + name); System.out.print( 年龄: + age); void outOther() System.out.print( 家庭住址 : + address); System.out.print( 电话: + tel); class Son extends Father String school; public Son(String n
9、ame, int age) super(name, age); void out() super.out(); super.outOther(); System.out.println( 学校: + school); public static void main(String args) Son son = new Son(Tom, 15); son.address = 金水区 ; son.school = 九中 ; son.tel = 66123456; son.out(); 8、下列程序的运行结果是 _1 23 4 5_ 。public class MyClass int a = 1,
10、2, 3, 4, 5 ; void out() for (int j = 0; j a.length; j+) System.out.print(aj + ); public static void main(String args) MyClass my = new MyClass(); my.out(); 程序填空题1.public class Sum public static void main(String args) int j=10; System.out.println(j is : +j); calculate(j); System.out.println(At last,
11、j is : +j); static void calculate (int j) for (int i = 0;i10;i+) j+; System.out.println(j in calculate() is: +j); 输出结果为:j is : (1) 10 j in calculate() is : (2) 20 At last j is : (3) 10 2. 按要求填空abstract class SuperAbstract void a()abstract void b(); abstract int c(int i); interface AsSuper void x();
12、abstract class SubAbstract extends SuperAbstract implements AsSuper public void b()abstract String f(); public class InheritAbstract extends SubAbstract public void x()public int c(int i ) public String f()public static void main(String args) InheritAbstract instance=new InheritAbstract(); instance.
13、x(); instance.a(); instance.b(); instance.c(100); System.out.println(instance.f(); 在以上这段程序中:抽象类有: SuperAbstract和(1)(写出类名 )SubAbstract 非抽象类有:(2)(写出类名 )InheritAbstract 接口有:(3)(写出接口名 )AsSuper AsSuper中的 x()方法是抽象(4)方法,所以在 InheritAbstract 中必须对它进行覆盖和实现(5)3. 按注释完成程序public class Leaf private int i = 0; /此属性值
14、用于检验Leaf increment() /定义方法 increment(),返回值是 Leaf 类的对象i+; return this (1) ;/将当前对象的地址作为返回值返回 void print() System.out.println( i = + i); public static void main(String args) Leaf x = new Leaf() (2) ; /创建 Leaf 类的对象 x x.increment().increment().increment().print(); / 多次调用方法 increment(),返回的都是 x 的地址, i 值表示调
15、用次数 输出结果为i = 3 (3) 程序阅读题1、阅读下面的程序代码,并回答问题(问 3 分,问 3 分,共 6 分)。String s1 = new String(abcde); String s2 = new String(abcde); boolean b1= s1.equals(s2); boolean b2 = s1= s2; System.out.print(b1+ +b2); 程序段执行后,在命令行的输出结果如何?答:True false 解释输出 (1)的结果的原因?略2、阅读下面的程序,并回答问题(问 3 分,问 3 分,共 6 分)。import java.io.*; p
16、ublic class Test public static void main(String args) throws IOException BufferedReader buf=new BufferedReader( new InputStreamReader(System.in); while(true) String str = buf.readLine(); if(str.equals(quit) break; int x=Integer.parseInt(str); System.out.println(x*x); 编译运行上面的程序:从键盘输入 10,回车后输出的结果如何?答:100 从键盘输入 exit,回车后程序能正确执行吗?为什么?答:不能,略3、阅读下面的程序,回答问题(问 3 分,问 3 分,共 6 分)。import java.awt.*; import javax.swing.*; public class T extends JFrame public T ( ) super(GridLayout); Container con=this.getCont