17软工2班宁一健 图形类 JAVA第三次作业

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/xixihahah574/article/details/101449647

以下是画图类作业第(1)问的代码

/**

  • 抽象图形类
  • @author 宁一健

*/

public abstract class Figure {
private String brushColor;
private boolean fill;
public Figure(){

}
public Figure(String brushColor,boolean fill){
	this.brushColor=brushColor;
	this.fill = fill;
}

public String getBrushColor() {
	return brushColor;
}
public void setBrushColor(String brushColor) {
	this.brushColor = brushColor;
}
public boolean isFill() {
	
	return fill;
}
public void setFill(boolean fill) {
	this.fill = fill;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract String toString();

}
/**

  • 圆类
  • @author 宁一健

*/

public class Circle extends Figure{
private double radius;
public Circle(){

}
public Circle(String brushColor,boolean fill,double radius){
	super(brushColor, fill);
	this.radius=radius;
}

public double getArea() {
	double area = 3.14*radius*radius;
	return area;
}

public double getPerimeter() {
	double perimerter = 3.14*2*radius;
	return perimerter;
}
@Override
public String toString() {
	if(isFill()==true){
		return "这个圆是"+getBrushColor()+"、"+"有填充"+"、"+"半径为"+radius+"的圆形";
	}else{
		return "这个圆是"+getBrushColor()+"、"+"无填充"+"、"+"半径为"+radius+"的圆形";
	}
}

}
/**

  • 正方形类
  • @author 宁一健

*/

public class Square extends Figure{
private double length;
public Square() {

}
public Square(String brushColor,boolean fill,double length){
	super(brushColor, fill);
	this.length = length;
}

public double getArea() {
	double area = length*length;
	return area;
	
}

public double getPerimeter() {
	double perimerter = 4*length;
	return perimerter;
	
}

public String toString() {
	if(isFill()==true){
		return "这个正方形是"+getBrushColor()+"、"+"有填充"+"、"+"边长为"+length+"的正方形";
	}else{
		return "这个正方形是"+getBrushColor()+"、"+"无填充"+"、"+"边长为"+length+"的正方形";
	}
}

}
/**

  • 长方形类
  • @author 宁一健

*/

public class Rectangle extends Figure{
private double width;
private double height;
public Rectangle(){

}
public Rectangle(String brushColor,boolean fill,double width,double height){
	super(brushColor, fill);
	this.width=width;
	this.height=height;
	
}
public double getArea() {
	double area = width*height;
	return area;
	
}

public double getPerimeter() {
	double perimerter = 2*(width+height);
	return perimerter;
	
}

public String toString() {
	if(isFill()==true){
		return "这个长方形是"+getBrushColor()+"、"+"有填充"+"、"+"宽为"+width+"、"+"高为"+height+"的正方形";
	}else{
		return "这个长方形是"+getBrushColor()+"、"+"无填充"+"、"+"宽为"+width+"、"+"高为"+height+"的正方形";
	}
}

}
/**

  • 画板类测试
  • @author 宁一健

*/

public class DrawingBoard {

public static void main(String[] args) {
	Figure circle = new Circle("绿色", true, 3.6);
	System.out.println("圆的面积为"+circle.getArea()+"  "+"圆的周长为"+circle.getPerimeter()+"  "+circle.toString());
	Figure square = new Square("蓝色", false, 5);
	System.out.println("正方形的面积为"+square.getArea()+"  "+"正方形的周长为"+square.getPerimeter()+"  "+square.toString());
	Figure rectangle = new Rectangle("黄色", true, 5, 6);
	System.out.println("长方形的面积为"+rectangle.getArea()+"  "+"长方形的周长为"+rectangle.getPerimeter()+"  "+rectangle.toString());
}

}
以下是第(2)问的代码
/**
设计Comparable接口,接口中设计compareTo()方法,
用来比较对象。此方法的返回值类型设计为int类型。
此方法接收一个参数,参数类型为图形类。
*/

public interface Comparable {

public int compareTo(Figure figure);

}
/**

  • 在图形类中实现compareTo()方法,用来比较两个图形的面积大小
  • @author 宁一健

*/

public abstract class Figure implements Comparable{
private String brushColor;
private boolean fill;
public Figure(){

}
public Figure(String brushColor,boolean fill){
	this.brushColor=brushColor;
	this.fill = fill;
}

public String getBrushColor() {
	return brushColor;
}
public void setBrushColor(String brushColor) {
	this.brushColor = brushColor;
}
public boolean isFill() {
	
	return fill;
}
public void setFill(boolean fill) {
	this.fill = fill;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract String toString();
public int compareTo(Figure figure){
	if(figure instanceof Figure){
		Figure f = (Figure)figure;
		if(this.getArea()>f.getArea()){
			return 1;
		}else if(this.getArea()<f.getArea()){
			return -1;
		}else {
			return 0;
		}
	}
	return 0;
	
	
	
}

}
/**

  • 圆类
  • @author 宁一健

*/

public class Circle extends Figure{
private double radius;
public Circle(){

}
public Circle(String brushColor,boolean fill,double radius){
	super(brushColor, fill);
	this.radius=radius;
}

public double getArea() {
	double area = 3.14*radius*radius;
	return area;
}

public double getPerimeter() {
	double perimerter = 3.14*2*radius;
	return perimerter;
}
@Override
public String toString() {
	if(isFill()==true){
		return "这个圆是"+getBrushColor()+"、"+"有填充"+"、"+"半径为"+radius+"的圆形";
	}else{
		return "这个圆是"+getBrushColor()+"、"+"无填充"+"、"+"半径为"+radius+"的圆形";
	}
}

}
/**

  • 正方形类
  • @author 宁一健

*/

public class Square extends Figure{
private double length;
public Square() {

}
public Square(String brushColor,boolean fill,double length){
	super(brushColor, fill);
	this.length = length;
}

public double getArea() {
	double area = length*length;
	return area;
	
}

public double getPerimeter() {
	double perimerter = 4*length;
	return perimerter;
	
}

public String toString() {
	if(isFill()==true){
		return "这个正方形是"+getBrushColor()+"、"+"有填充"+"、"+"边长为"+length+"的正方形";
	}else{
		return "这个正方形是"+getBrushColor()+"、"+"无填充"+"、"+"边长为"+length+"的正方形";
	}
}

}
/**

  • 长方形类
  • @author 宁一健

*/

public class Rectangle extends Figure{
private double width;
private double height;
public Rectangle(){

}
public Rectangle(String brushColor,boolean fill,double width,double height){
	super(brushColor, fill);
	this.width=width;
	this.height=height;
	
}
public double getArea() {
	double area = width*height;
	return area;
	
}

public double getPerimeter() {
	double perimerter = 2*(width+height);
	return perimerter;
	
}

public String toString() {
	if(isFill()==true){
		return "这个长方形是"+getBrushColor()+"、"+"有填充"+"、"+"宽为"+width+"、"+"高为"+height+"的正方形";
	}else{
		return "这个长方形是"+getBrushColor()+"、"+"无填充"+"、"+"宽为"+width+"、"+"高为"+height+"的正方形";
	}
}

}
/**

  • 画板类测试
  • 在测试类中,创建图形类的数组,数组中存放多个圆形、正方形、长方形对象,
  • 使用compareTo()方法找出数组中面积最大的图形。
  • @author 宁一健

*/

public class DrawingBoard {

public static void main(String[] args) {
	Figure[] figures = new Figure[]{new Circle("绿色", true, 10),new Circle("绿色", true, 3),
			new Square("蓝色", false, 5.6),new Square("蓝色", false, 9),
			new Rectangle("黑色", true, 5, 6),new Rectangle("黑色", true, 25, 6)};
	Figure max = figures[0];
	for(int i=0;i<figures.length;i++){
		int compareTo = figures[i].compareTo(max);
		if(compareTo>0){
			max=figures[i];
		}
		
	}
	System.out.println("这些图形类中最大的面积为"+max.getArea()+" "+max);
}

}

猜你喜欢

转载自blog.csdn.net/xixihahah574/article/details/101449647
今日推荐