Add lab6
This commit is contained in:
52
lab/lab6/Question7/Rectangle.java
Normal file
52
lab/lab6/Question7/Rectangle.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/27
|
||||
* Description: This is the Rectangle class.
|
||||
*/
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private double width;
|
||||
private double height;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param x the x coordinate of the center of the rectangle
|
||||
* @param y the y coordinate of the center of the rectangle
|
||||
* @param width the width of the rectangle
|
||||
* @param length the length of the rectangle
|
||||
*/
|
||||
public Rectangle(double x, double y, double width, double length) {
|
||||
super(x, y);
|
||||
this.width = width;
|
||||
this.height = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the area method
|
||||
*
|
||||
* @return double
|
||||
*/
|
||||
@Override
|
||||
public double area() {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getName() + " has area " + area();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test
|
||||
*/
|
||||
public static void testRectangle() {
|
||||
Rectangle r1 = new Rectangle(1.0, 2.0, 3.0, 4.0);
|
||||
System.out.println(r1.getX() == 1.0);
|
||||
System.out.println(r1.getY() == 2.0);
|
||||
System.out.println(r1.area() == 12.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user