#PTAOOP617. 6-17 矩形

6-17 矩形

本站提交说明

此题属于 面向对象程序设计练习。请在练习页填写原题的填空或函数片段并提交;本题独立提交页接收拼接后的完整 Java 程序。

评测使用题面样例与本站补充测试,包含编译、输出及部分成员 API 检查。通过本站测试不代表通过 PTA 隐藏测试。

设计一个表示矩形的类Rectangle,这个类用一个表示坐标点的类Point的对象来表达它的左上角坐标,用一个表示尺寸的类Dimension的对象来表示它的大小。 你的程序要严格按照所给的类和函数的声明来实现。

函数接口定义:

/**
 * 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() {
		
	}
}

/**
 * Represents a rectangle, with a point at its top-left and a dimension.
 *
 */
class Rectangle {
	private Point topleft;
	private Dimension size;
	
	/**
	 * Creates a rectangle.
	 * @param topleft the coordinate of its top-left 
	 * @param size the dimension of its size
	 */
	public Rectangle(Point topleft, Dimension size) {
		
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 * The generated string as: "Rectangle at (x,y):width by height"
	 */
	public String toString() {
		
	}
	
	/**
	 * Moves the rectangle 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 rectangle 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 area of this rectangle.
	 * @return the area of this rectangle.
	 */
	public double area() {
		
	}
	
	/**
	 * Calculates the distance between this rectangle and r.
	 * @param r the other rectangle
	 * @return the distance between this rectangle and r.
	 */
	public double distance(Rectangle r) {
		
	}
}

裁判测试程序样例:

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));
		Rectangle r2 = new Rectangle(
			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(r2));
		in.close();
	}
}

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

输入样例:

0 0 100 100 20 20 2 2

输出样例:

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

本站测试模板说明

  • 题面 Main 使用 Scanner 但未写 import;完整示例补上导入。