class B
19、下面是一个类的定义,完成程序填空? public class Youwrite {
int x;
_ Youwrite _( ) {x=0;} Youwrite ( int x)
{ _ this.x=x ____; } }
20.下面是定义一个接口ITF的程序,完成程序填空? public __interface_____ ITF {
public static final double PI=Math.PI;
public _abstract_____ double area(double a, double b); }
21.下面是定义一个接口A的程序,完成程序填空。(final ”;” )public interface A {
public static _final__ double PI=3.14159;
public abstract double area(double a, double b)__;___ }
22.写出程序的运行结果?
public class ClassX{
public static void main(String args[ ]){ int a[ ] = { 45,18, 98, 56, 304 }; for (int i = a.length-1 ; i >=0; i -- )
System.out.print(a[i ]+ \ } }
答案: 304 56 98 18 45 23.写出程序的运行结果?
public class C {
public static void main(String args[]) { String s1 = new String(\ String s2 = new String(\VA.\ A s = new A(s1, s2); System.out.println(s1 + s2); System.out.println(s.toString()); } }
class A { String s1; String s2;
A(String str1, String str2) { s1 = str1; s2 = str2; str1 = \ str2 = \ System.out.println(str1 + str2); }
11
public String toString() { return s1 + s2; } }
答案: No pain ,no gain!
Hello!I love JAVA. Hello!I love JAVA.
24.写出程序的运行结果?
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 T {
public static void main(String args[]) { Child myC = new Child(); myC.printAll(); } }
答案:parent
child child
25.回答问题?
1: public class Output1 {
2: public static void main(String args[]) { 3: int i=0;
4: for ( int ch = 1; ch<8; ch++,i++) { 5: if( i % 4 == 0 )
6: System.out.println(\
7: System.out.print(\ 8: } 9: } 10: }
问题:(1)程序第5、6行的if语句的功能是什么?答:if语句的功能是换行
(2)程序输出的结果是什么?答: 1 2 3 4 5 26.回答问题?
定义类A和类B如下.: class A{ int a=3;
12
6 7
double d=6.0; void show( ) {
System.out.println(\,d=\ } }
class B extends A{
float a=5.0f; String d=\ void show( ) {
super.show( );
System.out.println(\ a=\,d=\ } }
问题:(1) 若在应用程序的main方法中有以下语句:
A a=new A(); a.show();
则输出的结果如何? 答:Class A:a=3,d=6.0
(2) 若在应用程序的main方法中定义类B的对象b: B b=new B(); b.show();
则输出的结果如何? 答: Class A: a=3,d=6.0
Class B: a=5.0, d=Hello Java.
五、编程题
1、编写一个Java应用程序,用循环结构打印如下的数值列表? N 10*N 100*N 1000*N 1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000 答案:
public class Xiti1 {
public static void main(String[] args) {
System.out.println(\ for (int i = 1; i <= 5; i++)
System.out.println(i + \ } }
2、用while循环求n2大于12000的最小数n? 答案:
public class Xiti2 {
public static void main(String[] args) { int n=1;
while(n*n<=12000) n++;
13
* 1000); System.out.println(\大于12000的最小数为\}
3、打印2到10000的所有素数,每行显示8个素数? 答案:
public class Xiti3 {
public static void main(String[] args) { int k = 0; int m=0;
for (int n = 2; n <= 10000; n++) { m=0;
for(int j=1;j<=n;j++) if(n%j==0) m++;
if(m==2)
{System.out.print(n + \ k++;
if (k%8 == 0)
System.out.println();}} } }
5.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。 具体要求如下:
(1)Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型)
和电子邮件地址email(String类型);
(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String
类型);
(3)Faculty类中的属性有:学位degree(String类型),级别level(String类型); (4)Staff类中的属性有:职务称号duty(String类型); class Person { String name ; String address; String telphone; String email; public String toString() { return \ } }
class Employee extends Person { String office; Double wage; String hiredate; public String toString() {
14
return \ } }
class Faculty extends Employee { String degree; String level; public String toString() { return \ } }
class Staff extends Employee { String duty; public String toString() { return \ } }
6. 定义一个Person类,它包括的属性有“姓名”和“性别”,为Person类派生出一个子类Student类,为Student子类添加两个属性年龄和成绩等级(用A,B,C,D,E表示),在子类中打印出学生的姓名、性别、年龄及成绩等级?
class Person{ }
class Student extends Person{ }
public class Test {
protected String name; protected String sex;
protected int age; protected char grade;
public Student(String name,String sex,int age,char grade){ }
super.name=name; super.sex=sex; this.age=age; this.grade=grade;
}
public void print(){
System.out.println(name); System.out.println(sex); System.out.println(age); System.out.println(grade);
public static void main(String[] args) {
Student s1=new Student(\,\男\,20,'A'); s1.print();
15