函数题:sdut-fun-oop-矩形
设计一个表示矩形的类Rectangle,这个类用一个表示坐标点的类Point的对象来表达它的左上角坐标,用一个表示尺寸的类Dimension的对象来表示它的大小。 要严格按照所给的类和函数的声明来实现。
### 函数接口定义:
/**
* 2D平面中带的点,有x和y坐标,如:(x,y)。
*/
class Point {
int x;
int y;
/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
}
/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
}
/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
}
/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
}
}
/**
* 二维尺寸,有宽度和高度。
*/
class Dimension {
int width;
int height;
/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
}
/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
}
/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的,但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
}
/**
* @return 计算这个Dimension的面积.
*/
public int area() {
}
}
/**
* 表示一个矩形,在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;
/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
}
/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
}
/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
}
/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
}
/**
* @return 这个矩形的面积。
*/
public double area() {
}
/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
}
}
### 裁判测试程序样例:
import java.util.Scanner;
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();
}
}
/* 请在这里填写答案 */
### 输入样例:
in
0 0 100 100 20 20 2 2
### 输出样例:
out
Rectangle at (20,20):200 by 200
40000.00
28.28
答案:若无答案欢迎评论
/**
* 2D平面中带的点,有x和y坐标,如:(x,y)。
*/
class Point {
int x;
int y;
/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
this.x=x;
this.y=y;
}
/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
return "("+x+","+y+")";
}
/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
this.x+=dx;
this.y+=dy;
}
/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
}
}
/**
* 二维尺寸,有宽度和高度。
*/
class Dimension {
int width;
int height;
/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
this.width=width;
this.height=height;
}
/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
return this.width+" by "+this.height;
}
/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的,但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
this.width*=widthScale;
this.height*=heightScale;
}
/**
* @return 计算这个Dimension的面积.
*/
public int area() {
return width*height;
}
}
/**
* 表示一个矩形,在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;
/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
this.topleft=topleft;
this.size=size;
}
/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
return "Rectangle at ("+topleft.x+","+topleft.y+"):"+size.width+" by "+size.height;
}
/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
topleft.move(dx, dy);
}
/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}
/**
* @return 这个矩形的面积。
*/
public double area() {
return size.area();
}
/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}
### 函数接口定义:
/**
* 2D平面中带的点,有x和y坐标,如:(x,y)。
*/
class Point {
int x;
int y;
/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
}
/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
}
/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
}
/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
}
}
/**
* 二维尺寸,有宽度和高度。
*/
class Dimension {
int width;
int height;
/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
}
/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
}
/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的,但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
}
/**
* @return 计算这个Dimension的面积.
*/
public int area() {
}
}
/**
* 表示一个矩形,在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;
/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
}
/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
}
/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
}
/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
}
/**
* @return 这个矩形的面积。
*/
public double area() {
}
/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
}
}
### 裁判测试程序样例:
import java.util.Scanner;
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();
}
}
/* 请在这里填写答案 */
### 输入样例:
in
0 0 100 100 20 20 2 2
### 输出样例:
out
Rectangle at (20,20):200 by 200
40000.00
28.28
答案:若无答案欢迎评论
/**
* 2D平面中带的点,有x和y坐标,如:(x,y)。
*/
class Point {
int x;
int y;
/**
* C创建一个坐标为(x,y)的点
* @param x x坐标
* @param y y坐标
*/
public Point(int x, int y) {
this.x=x;
this.y=y;
}
/*
* 生成的字符串为:(x,y)
*/
@Override
public String toString() {
return "("+x+","+y+")";
}
/**
* 用dx和dy移动点。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
this.x+=dx;
this.y+=dy;
}
/**
* @param p 另外一个点
* @return 计算这个点和p点之间的距离。
*/
public double distance(Point p) {
return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
}
}
/**
* 二维尺寸,有宽度和高度。
*/
class Dimension {
int width;
int height;
/**
* 创建具有指定宽度和高度的Dimension。
* @param width——Dimension的宽度
* @param height——Dimension的高度
*/
public Dimension(int width, int height) {
this.width=width;
this.height=height;
}
/*
* 生成的字符串为:"width by height"
*/
@Override
public String toString() {
return this.width+" by "+this.height;
}
/**
* R按宽度和高度的比例调整尺寸。
* 虽然标度是双精度的,但结果应该是整数。
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
this.width*=widthScale;
this.height*=heightScale;
}
/**
* @return 计算这个Dimension的面积.
*/
public int area() {
return width*height;
}
}
/**
* 表示一个矩形,在其左上角有一个点Point和一个维度Dimension.
*
*/
class Rectangle {
Point topleft;
Dimension size;
/**
* 创建一个矩形。
* @param topleft 左上角的坐标
* @param size 它的尺寸
*/
public Rectangle(Point topleft, Dimension size) {
this.topleft=topleft;
this.size=size;
}
/*
* 生成的字符串如下: "Rectangle at (x,y):width by height"
*/
public String toString() {
return "Rectangle at ("+topleft.x+","+topleft.y+"):"+size.width+" by "+size.height;
}
/**
* 将矩形移动一段距离。
* @param dx 在x轴上移动的距离
* @param dy 在y轴上移动的距离
*/
public void move(int dx, int dy) {
topleft.move(dx, dy);
}
/**
* 调整矩形的宽度和高度
* @param widthScale 宽度比例尺
* @param heightScale 高度比例尺
*/
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}
/**
* @return 这个矩形的面积。
*/
public double area() {
return size.area();
}
/**
* 计算矩形左上角顶点与矩形r左上角点之间的距离。
* @param r 另一个矩形
* @return 这个矩形和r之间的距离。
*/
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}