课程名称:
任课教师: 学号: 姓名:
------------------------------------------------------------------------------------------------------------------------ public static void main(String[] args) { String s,s1=\ char c; s= \ for (int i=0;i
abstract class Shape { abstract double area(); public void print() { System.out.println(\ } }
class Rectangle extends Shape { int width, length; public Rectangle(int width,int length){ this.width=width; this.length=length; } public double area() { return width * length; } public void print(){ super.print(); System.out.println(\ } }
class Circle extends Shape{ double r; public Circle(double r){ this.r=r;
本题13页,本页为第11页 教务处试题编号:
课程名称:
任课教师: 学号: 姓名:
------------------------------------------------------------------------------------------------------------------------ } public double area() { return 3* r*r; } public void print(){ super.print(); System.out.println(\ } }
public class TestShape { public static void main(String args[]) { Shape s1 = new Rectangle(3, 4); System.out.println(s1.area()); s1.print(); Shape s2=new Circle(3.0); System.out.println(s2.area()); s2.print(); } }
8.
class ObjectCreation {
TestClass testClass = new TestClass(\ static TestClass testClass2 = new TestClass(\
public ObjectCreation(String name) { } }
class TestClass {
public TestClass(String name) { } }
public class ChildObjectCreation extends ObjectCreation{
本题13页,本页为第12页 教务处试题编号:
System.out.println(\
System.out.println(\
课程名称:
任课教师: 学号: 姓名:
------------------------------------------------------------------------------------------------------------------------ static TestClass testClass3 = new TestClass(\ TestClass testClass4=new TestClass(\
public ChildObjectCreation(String name) { }
public static void main(String[] args) { } }
六、程序设计题 1.
(本小题7分)编写一个完整的Java程序使用复数类验证两个复数 5+17i和4+12i 相加产生一个新的复数 9+29i 。复数类 Complex必须满足如下要求:
(1) 复数类Complex 的属性有: RealPart : int型,代表复数的实部 ImaginPart : int 型,代表复数的虚部 (2) 复数类Complex 的方法有:
Complex( ) : 构造函数,将复数的实部和虚部都置0
Complex( int r , int i ) : 构造函数,形参 r 为实部的初值, i为虚部的初值。
Complex complexAdd(a) : 将当前复数对象与形参相加,所得的结果仍是一个复数,并将它返回给此方法的调用者。
String ToString( ) : 把当前复数对象的实部、虚部组合成a+bi 的字符串形式,其中 a 和 b分别为实部和虚部的数值。 2.
(本小题4分)按以下要求编写程序。
ChildObjectCreation cd=new ChildObjectCreation(\super(name);
System.out.println(\
(1) 设计并实现一个 Rectangle类,它包括width和 height两个数据域。 (2) 在Rectangle中提供两个方法分别计算矩形的周长和面积。 (3) 编程测试程序利用Rectangl类输出一个矩形的周长和面积。
本题13页,本页为第13页 教务处试题编号: