-->
当前位置:首页 > 题库 > 正文内容

函数题:形状继承

Luz3年前 (2022-07-16)题库652
设计一个表示形状的类Shape,以及从Shape继承的表示矩形的类Rectangle和表示椭圆的类Ellipse。Shape类用一个表示坐标点的类Point的对象来表达它的左上角坐标,用一个表示尺寸的类Dimension的对象来表示它的大小。
你的程序要严格按照所给的类和函数的声明来实现。

### 函数接口定义:

Java
/**
* Represents a point in 2D, with x and y, like (x,y).
*/
class Point {
private int x;
private int y;

/**
* Creates a point with coordinate at (x,y)
* @param x the x coordinate
* @param y the y coordinate
*/
public Point(int x, int y) {

}

/* (non-Javadoc)
* @see java.lang.Object#toString()
* The generated string as: "(x,y)
*/
@Override
public String toString() {

}

/**
* Moves the point with dx and dy.
* @param dx the distance to be moved at x-axis
* @param dy the distance to be moved at y-axis
*/
public void move(int dx, int dy) {

}

/**
* Calculate the distance between this and p.
* @param p the other point.
* @return the distance between this and p.
*/
public double distance(Point p) {

}
}

/**
* A dimension in 2D, with width and height.
*/
class Dimension {
private int width;
private int height;

/**
* Creates a dimension with specified width and height.
* @param width the width of the dimension
* @param height the height of the dimension
*/
public Dimension(int width, int height) {

}

/* (non-Javadoc)
* @see java.lang.Object#toString()
* The generated string as: "width by height"
*/
@Override
public String toString() {

}

/**
* Resizes the dimension with scales at width and height.
* Although the scales are in double, the result should be integers as well.
* @param widthScale the scale at width
* @param heightScale the scale at height
*/
public void resize(double widthScale, double heightScale) {

}

/**
* Calculate the area of this dimension.
* @return the area of this dimension.
*/
public int area() {

}
}

class Shape {
private Point center;
private Dimension size;

/**
* Creates a shape.
* @param topleft the coordinate of its center
* @param size the dimension of its size
*/
public Shape(Point center, Dimension size) {
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return String.format("at %s:%s", center, size);
}

/**
* Moves the shape some distance.
* @param dx the distance to be moved at x-axis
* @param dy the distance to be moved at y-axis
*/
public void move(int dx, int dy) {
}

/**
* Resizes the shape at both width and height
* @param widthScale the scale at width
* @param heightScale the scale at height
*/
public void resize(double widthScale, double heightScale) {
}

/**
* Calculates the distance between this shape and r.
* @param r the other shape
* @return the distance between this shape and r.
*/
public double distance(Shape r) {
}

/**
* Calculates the area of this shape.
* @return the area of this shape.
*/
public double area() {
}
}

/**
* Represents a rectangle, with a point at its top-left and a dimension.
*
*/
class Rectangle extends Shape {
/**
* Creates a rectangle.
* @param topleft the coordinate of its center
* @param size the dimension of its size
*/
public Rectangle(Point center, Dimension size) {
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "Rectangle " + super.toString();
}
}

class Ellipse extends Shape {
/**
* Creates a ellipse.
* @param topleft the coordinate of its center
* @param size the dimension of its size
*/
public Ellipse(Point center, Dimension size) {
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "Ellipse " + super.toString();
}

/**
* Calculates the area of this ellipse.
* @return the area of this ellipse.
*/
@Override
public double area() {
}
}


### 裁判测试程序样例:
Java
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
int width = in.nextInt();
int height = in.nextInt();
Rectangle r = new Rectangle(
new Point(x,y), new Dimension(width, height));
Ellipse e = new Ellipse(
new Point(x,y), new Dimension(width, height));
int dx = in.nextInt();
int dy = in.nextInt();
r.move(dx, dy);
double widthScale = in.nextDouble();
double heightScale = in.nextDouble();
r.resize(widthScale, heightScale);
System.out.println(r);
System.out.printf("%.2f\n", r.area());
System.out.printf("%.2f\n", r.distance(e));
in.close();
}
}

/* 请在这里填写答案 */


### 输入样例:

in
0 0 100 100 20 20 2 2


### 输出样例:

out
Rectangle at (20,20):200 by 200
40000.00
28.28






答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。