阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。 【说明】下面的程序先构造Point类,

10 查阅

阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。

【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。

public class Point

{

private double xCoordinate;

private double yCoordinate;

public Point 0 }

public Point(ouble x, double y)

{

xCoordinate = x;

yCoordinate = y;

}

public String toString()

{

return "( + Double.toString(Coordinate)+ ","

+ Double.toString(Coordinate) + ");

}

//other methods

}

public class Ball

{

(1); //中心点

private double radius; //半径

private String colour; ///颜色

public Ball() { }

public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法

{

center=(2);//调用类Point 中的构造方法

radius = r;

}

public Ball(double xValue, double yValue, double r, String c)

// 具有中心点、半径及颜色的构造方法

{

(3);//调用3个参数的构造方法

colour = c;

}

public String toString()

{

return "A ball with center" + center, toString() + ", radius"

+ Double.toString(radius) + ", colour" + colour;

}

//other methods

}

public class MovingBall. (4)

{

private double speed;

public MovingBall() { }

public MovingBall(double xValue, double yValue, double r, String e, double s)

{

(5);// 调用父类Ball中具有4个参数的构造方法

speed = s;

}

public String toString( )

{ return super, toString( ) + ", speed "+ Double.toString(speed); }

//other methods

}

public class Tester{

public static void main(String args[]){

MovingBall mb = new MovingBall(10,20,40,"green",25);

System.out.println(mb);

}

}

参考答案:

(1)private Point center(2)new Point (xValueyValue)(3)this(xValueyValuer)(4)extends Ball(5)super(xValueyValuerc)(1)private Point center(2)new Point (xValue,yValue)(3)this(xValue,yValue,r)(4)extends Ball(5)super(xValue,yValue,r,c) 解析:(1)private Point center

软考中级