6-50 矩形分数 10

设计一个表示矩形的类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

代码如下;

/**
 * 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) {
        this.x = x;
        this.y = y;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     * The generated string as: "(x,y)
     */
    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
    
    /**
     * 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) {
        this.x += dx;
        this.y += 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) {
        return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));
    }
}

/**
 * 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) {
        this.width = width;
        this.height = height;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     * The generated string as: "width by height"
     */
    @Override
    public String toString() {
        return width + " by " + height;
    }
    
    /**
     * 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) {
        this.width = (int) (this.width * widthScale);
        this.height = (int) (this.height * heightScale);
    }
    
    /**
     * Calculate the area of this dimension.
     * @return the area of this dimension.
     */
    public int area() {
        return this.height * this.width;
    }
}

/**
 * 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) {
        this.topleft = topleft;
        this.size = size;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     * The generated string as: "Rectangle at (x,y):width by height"
     */
    public String toString() {
        return "Rectangle at " + this.topleft + ":" + this.size;
    }
    
    /**
     * 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) {
        this.topleft.move(dx, 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) {
        this.size.resize(widthScale, heightScale);
    }
    
    /**
     * Calculates the area of this rectangle.
     * @return the area of this rectangle.
     */
    public double area() {
        return this.size.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) {
        return this.topleft.distance(r.topleft);
    }
}

这里就说明一下第一个测试点

 卡了一会

最后发现是在Dimension的revise函数

一定要保证最后的结果是int类型,得把他们乘完之后的结果再进行强制转换

一开始我是这样写的

 

结果第一个测试点就过不了,最后发现得还得是把他们乘积的结果再强转,才通过 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值