java面向对象之图形面积、周长及比较
在画图软件中,可以画出不同大小或颜色的圆形、矩形等几何图形。几何图形之间有许多共同的特征,如它们可以是用某种颜色画出来的,可以是填充的或者不填充的。此外还有些不同的特征,比如,圆形都有半径,可以根据半径计算圆形的面积和周长,矩形都有宽和高,可以根据宽高来计算矩形的面积和周长。
1、编写Java程序。
(1)使用继承机制,分别设计实现抽象基类图形类,派生类圆形类、正方形类、长方形类,要求:
①抽象图形类中有方法获取图形面积、获取图形周长等;
②使用构造方法为其成员属性赋初值;
③在每个派生类中都重写toString()方法,返回所有属性的信息;
④根据文字描述合理设计类的其他成员属性和方法。
(2)设计实现画板类,要求:
分别求三个对象的面积和周长,并将每个对象的所有属性信息打印到控制台。
2、基于上题背景,设计实现以下程序:
(1)设计Comparable接口,接口中设计compareTo()方法,用来比较对象。此方法的返回值类型设计为int类型。此方法接收一个参数,参数类型为图形类。
(2)在图形类中实现compareTo()方法,用来比较两个图形的面积大小。
(3)在测试类中,创建图形类的数组,数组中存放多个圆形、正方形、长方形对象,使用compareTo()方法找出数组中面积最大的图形。
提示:比较对象时,可以将compareTo()方法的返回值设置为1、0、-1,来代表比较结果,当前对象大、两者相等或者当前对象小。再次提醒,此题涉及的代码全部展示在此题的题号下,与上一题相独立。
package com.circular;
/**
* 图形比较面积的接口
*
* @author mopeiwen
* @version 2019年9月25日 下午6:31:48
*/
public interface Comparable {
/**
* 和另一个图形比较面积
* @param graphical 图形类
* @return 返回1、0、-1(大于为1,相等为0,小于为-1)
*/
public int compareTo(Graphical graphical);
}
package com.circular;
/**
* 抽象图形类
*
* @author anneli
* @date 2019年9月25日 下午4:44:54
*
*/
public abstract class Graphical implements Comparable {
private String Color;// 颜色
private String isFill;// 是否填充
public Graphical(String color, String isFill) {
this.Color = color;
this.isFill = isFill;
}
public String getColor() {
return Color;
}
public void setColor(String color) {
Color = color;
}
public String isFill() {
return isFill;
}
public void setFill(String isFill) {
this.isFill = isFill;
}
public Graphical() {
}
@Override
public String toString() {
return "图形属性 [Color=" + Color + ", isFill=" + isFill + "]";
}
/**
* 重写比较面积的方法
*/
public int compareTo(Graphical graphical) {
if (this.getFigureArea() >= graphical.getFigureArea()) {
if (this.getFigureArea() == graphical.getFigureArea()) {
return 0;
} else {
return 1;
}
} else {
return -1;
}
}
/**
* 获得图形面积
*/
public abstract double getFigureArea();
/**
* 获得图形周长
*/
public abstract double getFigureCirs();
}
package com.circular;
/**
* 圆形类
*
* @author anneli
* @date 2019年9月25日 下午5:19:43
*
*/
public class Circular extends Graphical {
private double Radius;
public Circular() {
}
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public Circular(String color, String isFill, double radius) {
super(color, isFill);
this.Radius = radius;
}
@Override
public double getFigureArea() {
System.out.println("圆形面积:" + Radius * Radius * Math.PI);
return Radius * Radius * Math.PI;
}
@Override
public double getFigureCirs() {
System.out.println("圆形面积:" + Radius * 2.0 * Math.PI);
return Radius * 2.0 * Math.PI;
}
@Override
public String toString() {
return "Circular [Radius=" + Radius + ", getColor()=" + getColor() + ", isFill()=" + isFill() + "]";
}
}
package com.circular;
/**
* 长方形类
*
* @author anneli
* @date 2019年9月25日 下午5:20:06
*
*/
public class Rectangle extends Graphical {
private double Width;
private double 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;
}
public Rectangle() {
}
public Rectangle(String color, String isFill, double width, double height) {
super(color, isFill);
this.Width = width;
this.Height = height;
}
@Override
public double getFigureArea() {
System.out.println("长方形面积:" + Width * Height);
return Width * Height;
}
@Override
public double getFigureCirs() {
System.out.println("长方形周长:" + (2 * Width + 2 * Height));
return (2 * Width + 2 * Height);
}
@Override
public String toString() {
return "Rectangle [Width=" + Width + ", Height=" + Height + ", getColor()=" + getColor() + ", isFill()="
+ isFill() + "]";
}
}
package com.circular;
/**
* 正方形类
*
* @author anneli
* @date 2019年9月25日 下午5:20:17
*
*/
public class Square extends Graphical {
private double Width;
public double getWidth() {
return Width;
}
public void setWidth(double width) {
Width = width;
}
public Square() {
}
public Square(String color, String isFill, double width) {
super(color, isFill);
this.Width = width;
}
@Override
public double getFigureArea() {
System.out.println("正方形面积:" + Width * Width);
return Width * Width;
}
@Override
public double getFigureCirs() {
System.out.println("正方形周长:" + 4.0 * Width);
return 4.0 * Width;
}
@Override
public String toString() {
return "Square [Width=" + Width + ", getColor()=" + getColor() + ", isFill()=" + isFill() + "]";
}
}
package com.circular;
/**
* 画板类 生成图形并打印
*
* @author anneli
* @date 2019年9月25日 下午5:20:31
*
*/
public class Drawing {
public static void main(String[] args) {
Circular circular = new Circular("绿色", "填充", 3.0);// 圆形
Rectangle rectangle = new Rectangle("红色", "无填充", 10.0, 5.0);// 长方形
Square square = new Square("黄色", "无填充", 4.0);// 正方形
circular.getFigureArea();
circular.getFigureCirs();
rectangle.getFigureArea();
rectangle.getFigureCirs();
square.getFigureArea();
square.getFigureCirs();
System.out.println(circular);
System.out.println(rectangle);
System.out.println(square);
}
}
package com.circular;
/**
* 类的描述
*
* @author mopeiwen
* @version 2019年9月25日 下午6:58:46
*/
public class Main {
public static void main(String[] args) {
Graphical[] graphicals = new Graphical[10];
graphicals[0] = new Circular("红色","填充",3.0);
graphicals[1] = new Circular("红色","填充",4.0);
graphicals[2] = new Circular("红色","填充",5.0);
graphicals[3] = new Rectangle("绿色","填充",3.0,4.0);
graphicals[4] = new Rectangle("绿色","填充",2.0,6.0);
graphicals[5] = new Rectangle("绿色","填充",3.0,5.0);
graphicals[6] = new Square("蓝色","填充",1.0);
graphicals[7] = new Square("蓝色","填充",2.0);
graphicals[8] = new Square("蓝色","填充",3.0);
graphicals[9] = new Square("蓝色","填充",4.0);
Graphical graTest = null;
for (int i = 0; i < graphicals.length; i++) {
if (i==0) {
graTest = graphicals[i];
}
else{
if (graTest.compareTo(graphicals[i])==-1) {
graTest = graphicals[i];
}
}
}
System.out.println(graTest.getFigureArea());
}
}