This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
public class Circle extends Shape{
private double radius;
private Point center;
public Circle(double radius, double x, double y){
super("circle");
this.radius = radius;
center = new Point(x, y);
}
public double getRadius(){
return radius;
}
public Point getCenter(){
return center;
}
public void setRadius(double radius){
this.radius = radius;
}
public void setCenter(Point center){
this.center = center;
}
public double getArea(){
return radius*radius*3.1416;
}
public String toString(){
return "center=" + center.toString() + "; radius=" + radius;
}
}

View File

@@ -0,0 +1,24 @@
public class Point {
private double x, y;
public Point(double a, double b) {
setPoint(a, b);
}
public void setPoint(double a, double b) {
x = a;
y = b;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
}

View File

@@ -0,0 +1,40 @@
public class Rectangle extends Shape{
private Point topLeft;
private double width;
private double height;
public Rectangle(double width, double height, double x, double y) {
super("rectangle");
topLeft = new Point(x, y);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public Point getTopLeft() {
return topLeft;
}
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public String toString() {
return "top left=" + topLeft.toString() + "; width=" + width + "; height=" + height;
}
}

View File

@@ -0,0 +1,13 @@
public abstract class Shape {
protected String name;
public Shape(String n) {
name = n;
}
public abstract double getArea();
public String getName() {
return name;
}
}

View File

@@ -0,0 +1,12 @@
public class TestShape {
public static void main(String[] args) {
Shape[] myShapes = new Shape[3];
myShapes[0] = new Circle(10.5, 20, 25);
myShapes[1] = new Rectangle(30.5, 23.5, 15.5, 20.5);
myShapes[2] = new Circle(8, 9.5, 10.5);
for (int i = 0; i < myShapes.length; i++) {
System.out
.println(myShapes[i].getName() + "=" + myShapes[i].toString() + "; Area=" + myShapes[i].getArea());
}
}
}