update,
This commit is contained in:
34
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Circle.java
Normal file
34
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
24
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Point.java
Normal file
24
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Point.java
Normal 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 + "]";
|
||||
}
|
||||
}
|
40
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Rectangle.java
Normal file
40
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Rectangle.java
Normal 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;
|
||||
}
|
||||
}
|
13
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Shape.java
Normal file
13
_resources/it114105/itp4510/Lab01/Lab1.2/q3/Shape.java
Normal 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;
|
||||
}
|
||||
}
|
12
_resources/it114105/itp4510/Lab01/Lab1.2/q3/TestShape.java
Normal file
12
_resources/it114105/itp4510/Lab01/Lab1.2/q3/TestShape.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user