double s = (a+b+c)/2; return Math.sqrt(s*(s-a)*(s-b)*(s-c)); } public static void main(String[] args){ X5_3_1 shape = new X5_3_1(); System.out.println(\ System.out.println(\ System.out.println(\ } }
【运行结果】
The area of circle is: 314.1592653589793 The area of rectangle is: 200.0
The area of triangle is: 72.61843774138907 2.编写一个实现方法覆盖的程序。
【编程分析】方法覆盖是指在子类中重新定义了父类中的方法。本例中在Shape类、Circle类、Cylinder类中都定义了area方法,而且Cylinder类继承了Circle类、Circle类继承了Shape类,从而实现了area方法的覆盖。 【参考程序】
abstract class Shape { abstract protected double area(); }
class Circle extends Shape{ float r;
public Circle(float a) { r=a; }
public double area(){ System.out.print(\ \ return Math.PI*r*r; } }
class Cylinder extends Circle { float h;
public Cylinder(float a,float b) { super(a); h=b; }
public double area() { System.out.print(\ \ return 2*Math.PI*r*r+2*Math.PI*r*h; } }
11
public class EX5_3_2 { public static void main(String args[]) { Circle cl=new Circle(3); Cylinder cd=new Cylinder(2,5); System.out.println(cl.area()); System.out.println(cd.area()); } }
【运行结果】
Calculate the area of circle: 28.274333882308138 Calculate the area of cylinder: 87.96459430051421 3.编写一个实现数据成员隐藏的程序。 【编程分析】数据成员的隐藏是指子类和父类都有一个相同的数据成员,当子类对象调用该数据成员时,默认调用子类的该成员,而不是父类的该成员,相当于隐藏了父类的该成员。本例中Hide类中定义了数据成员x,该类的子类X_5_3_3中也定义了数据成员x,当子类对象调用x时,调用的是子类的x,而不是父类的x。 【参考程序】
public class X5_3_3 extends Hide { int x = 200; public static void main(String args[]) { X5_3_3 obj = new X5_3_3 (); System.out.println(\// 数据成员的隐藏 } }
class Hide{ int x = 100; }
【运行结果】
x = 200
4.编写一个使用this和super关键字的程序。
【编程分析】this关键字表示当前对象本身,而super关键字则是当前对象的直接父类对象。本例程序中定义了两个类X1和X2,其中都定义了成员变量x、y。此时就需要区分父类与子类的成员变量,采用super引用父类的成员变量和方法。
注意:父类和子类中都定义了show()方法,如果没有特别指定,则是调用当前类的方法。 【参考程序】
public class X5_3_4{ public static void main(String[] args){ X2 x = new X2(5); x.setY(); x.show(); System.out.println(\// 输出父类数据成员y System.out.println(\ // 输出子类数据成员y } }
12
class X1{ int x,y; X1(int i){ x=i; y=x*2; } int getY(){ return y; } void show(){ System.out.println(\ } }
class X2 extends X1{ int x,y; X2(int j){ super(3); // 利用super来调用父类的构造方法 this.x=j; // 利用this来调用本类的数据成员x } void show(){ System.out.println(\// 输出父类和子类的属性x } void setY(){ y=super.x+this.x; // 引用父类和子类中的数据成员x } }
【运行结果】
super.x=3 this.x=5 super.y=6 this.y=8
5.编写一个人类Person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。再编写一个学生类Student,它继承Person类,其中包含学号属性,包含构造方法以及显示学号的方法。最后编写一个主类X5_3_5,包含main()方法,在main()方法中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄。
【编程分析】本题主要考察类的继承问题。
第一步:定义Person类。
第二步:定义Student类,该类继承Person类。 第三步:定义主类。 【参考程序】
public class X5_3_5 {
public static void main(String[] args) {
Student s1=new Student(\
13
Student s2=new Student(\ s1.show(); s1.showID(); s2.show(); s2.showID(); } }
class Person{ String name; String sex; int age;
public Person(String n,String s,int a){ name = n; sex = s; age = a; }
void show(){
System.out.println(\ System.out.println(\ System.out.println(\ } }
class Student extends Person{ String sID;
public Student(String n,String s,int a,String sid){ super(n,s,a); sID = sid; }
void showID(){
System.out.println(\ } }
【运行结果】
name: Zhangsan sex: Male age: 20 sID: 102A name: Lisi sex: Female age: 18 sID: 108S
6.编一个程序,包含以下文件。
(1)Shape.java文件,在该文件中定义接口Shape,该接口在shape包中。属性:PI。
14
方法:求面积的方法area()。
(2)Circle.java文件,在该文件中定义圆类Circle,该类在circle包中,实现Shape接口。 属性:圆半径radius。
方法:构造方法;实现接口中求面积方法area();求周长方法perimeter()。 (3)“Cylinder.java”文件,在该文件中定义圆柱体类Cylinder,该类口在cylinder包中,
继承圆类。
属性:圆柱体高度height。
方法:构造方法;求表面积方法area();求体积方法volume()。
(4)X5_3_6.java文件,在该文件中定义主类X5_3_6,该类在默认包中,其中包含主方法main(),在主方法中创建两个圆类对象cir1和cir2,具体尺寸自己确定,并显示圆的面积和周长;再创建两个圆柱体类的对象cy1和cy2,具体尺寸自己确定,然后分别显示圆柱体cy1和cy2的底圆的面积和周长以及它们各自的体积和表面积。
【编程分析】本题主要考察接口、包、继承、封装等问题。编程步骤如下:
第一步:首先创建p1包,在其中创建Shape接口
// Shape.java文件
package p1; // 创建p1包 public interface Shape{ // 定义Shape接口 … }
第二步:创建Circle类和Cylinder类,它们都定义在p2包中。
// Circle.java文件
package p2; // 创建p2包 import p1.*;
public class Circle implements Shape{ // 定义实现Shape接口的Circle类 … }
// Cylinder.java文件 package p2;
public class Cylinder extends Circle{ // 创建继承Circle类的Cylinder类 … }
第三步:创建主类,在其中的main()方法中创建对象,实现相应的功能。
// X5_3_6.java文件 package p3; import p2.*;
public class X5_3_6 { // 定义主类 public static void main(String[] args) { … } }
【参考程序】
// X5_3_6.java文件 package p3; import p2.*;
15
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库JAVA各章习题及答案 - 副本(3)在线全文阅读。
相关推荐: