当前位置:首页 > 期末复习题
}
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;
double d=6.0; void show( ) {
System.out.println(\,d=\ } }
class B extends A{
float a=5.0f; String d=\
17
6 7
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 + \+ i * 1000); } }
2、用while循环求n2大于12000的最小数n? 答案:
public class Xiti2 {
public static void main(String[] args) { int n=1;
while(n*n<=12000) n++;
System.out.println(\大于12000的最小数为\}
3、打印2到10000的所有素数,每行显示8个素数?
18
答案:
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() {
19
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 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;
}
protected String name; protected String sex;
public void print(){
System.out.println(name); System.out.println(sex); System.out.println(age); System.out.println(grade);
20
共分享92篇相关文档