17 workers soft 2 graphics class JAVA class Ningyi Jian third time jobs

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xixihahah574/article/details/101449647

Paint job category are the following (1) Q code

/**

  • Abstract graphics classes
  • @author Ningyi Jian

*/

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();

}
/**

  • Round classes
  • @author Ningyi Jian

*/

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+"的圆形";
	}
}

}
/**

  • Square class
  • @author Ningyi Jian

*/

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+"的正方形";
	}
}

}
/**

  • Rectangle class
  • @author Ningyi Jian

*/

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+"的正方形";
	}
}

}
/**

  • Sketchpad class test
  • @author Ningyi Jian

*/

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());
}

}
The following is the first (2) Q code
/ **
design Comparable interface, interface design the compareTo () method,
for comparison. This method is designed as a return value of type int.
This method takes a parameter of type of pattern class.
* /

public interface Comparable {

public int compareTo(Figure figure);

}
/**

  • CompareTo implemented in the graphical class () method is used to compare two graphic area size
  • @author Ningyi Jian

*/

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;
	
	
	
}

}
/**

  • Round classes
  • @author Ningyi Jian

*/

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+"的圆形";
	}
}

}
/**

  • Square class
  • @author Ningyi Jian

*/

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+"的正方形";
	}
}

}
/**

  • Rectangle class
  • @author Ningyi Jian

*/

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+"的正方形";
	}
}

}
/**

  • Sketchpad class test
  • In the test class, create an array pattern class, stored in a plurality of circular array, square, rectangular objects,
  • Use compareTo () method to find the maximum graphics array area.
  • @author Ningyi Jian

*/

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);
}

}

Guess you like

Origin blog.csdn.net/xixihahah574/article/details/101449647