package com.graphicalcalculation;
//圆形类
public class Circular extends Graph {
public Circular() {
}
private double Radius;
public Circular(double radius) {
this.Radius = radius;
}
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
@Override
public double getGraphicCircumFerence() {
return 2 * Radius * Math.PI;
}
@Override
public double getGraphicArea() {
return Radius * Radius * Math.PI;
}
public String toString() {
return "圆的属性:Radius=" + Radius + "";
}
}
package com.graphicalcalculation;
//抽象基类图形类
public abstract class Graph implements Comparable {
public Graph() {
}
public abstract double getGraphicCircumFerence();
public abstract double getGraphicArea();
public int compareTo(Graph graph) {
int b = 0;
if (this.getGraphicArea() > graph.getGraphicArea()) {
return b=1;
}
else if (this.getGraphicArea() == graph.getGraphicArea()) {
return b=0;
}
else if (this.getGraphicArea() < graph.getGraphicArea()) {
return b=-1;
}
return b;
}
}
package com.graphicalcalculation;
//长方形类
public class Rectangle extends Circular {
public Rectangle() {
}
private double Width;
private double Height;
public Rectangle(double width, double height) {
this.Width = width;
this.Height = height;
}
public double getWidth() {
return Width;
}
public void setWidth(double width) {
Width = width;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
Height = height;
}
@Override
public double getGraphicCircumFerence() {
return 2 * Height + 2 * Width;
}
@Override
public double getGraphicArea() {
return Height * Height;
}
public String toString() {
return "长方形的属性:Width=" + Width + ",Height=" + Height + "";
}
}
package com.graphicalcalculation;
//正方形类
public class Square extends Graph {
public Square() {
}
private double a;
public Square(double a) {
this.a = a;
}
public double getRadius() {
return a;
}
public void setRadius(double a) {
a = a;
}
@Override
public double getGraphicCircumFerence() {
return 4 * a;
}
@Override
public double getGraphicArea() {
return a * a;
}
public String toString() {
return "正方形的属性: a=" + a + "";
}
}
package com.graphicalcalculation;
import java.io.IOException;
import java.nio.CharBuffer;
@SuppressWarnings(“unused”)
public class Art {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10.0, 5.0);//实例化长方形
Circular circular = new Circular(3.0);//实例化圆形
Square square = new Square(4.0);//实例化正方形
rectangle.getGraphicCircumFerence();
rectangle.getGraphicArea();
circular.getGraphicCircumFerence();
circular.getGraphicArea();
square.getGraphicCircumFerence();
square.getGraphicArea();
System.out.println(rectangle);
System.out.println(circular);
System.out.println(square);
}
}
package com.graphicalcalculation;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// 实例化长方形
Rectangle rectangle = new Rectangle(10.0, 5.0);
// 实例化圆形
Circular circular = new Circular(3.0);
// 实例化正方形
Square square = new Square(4.0);
System.out.println("长方形的周长:" + rectangle.getGraphicCircumFerence());
System.out.println("长方形的面积:" + rectangle.getGraphicArea());
System.out.println("圆的周长:" + circular.getGraphicCircumFerence());
System.out.println("圆的面积:" + circular.getGraphicArea());
System.out.println("正方形的周长:" + square.getGraphicCircumFerence());
System.out.println("正方形的面积:" + square.getGraphicArea());
}
}