Java课后题——(三角形类)设计一个名为triangle的类来扩展geometricobject类,该类包括

2021-10-28 00:00:00 角形 该类 课后

设计一个名为triangle的类来扩展geometricobject类,该类包括

  • 三个名为side1,side2,side3的double数据域表示这个三角形的三条边
  • 一个无参构造方法创建默认三角形
  • 一个能创建带指定size的三角形构造方法
  • 所有三个数据域的访问器
  • 是否填充
  • 面积,周长,颜色 GeometricObject类如下:
    abstract class GeometricObject{
    
    	private boolean filled;
    	private String color;
    	private java.util.Date dateCreated;
    
    	protected GeometricObject() {
    		this(false,"white");
    		dateCreated = new java.util.Date();
    	}
    
    	protected GeometricObject(boolean filled, String color) {
    		//super();
    		this.filled = filled;
    		this.color = color;
    		dateCreated = new java.util.Date();
    	}
    
    	public boolean isFilled() {
    		return filled;
    	}
    
    	public void setFilled(boolean filled) {
    		this.filled = filled;
    	}
    
    	public String getColor() {
    		return color;
    	}
    
    	public void setColor(String color) {
    		this.color = color;
    	}
    
    	public java.util.Date getDateCreated() {
    		return dateCreated;
    	}
    
    	public void setDateCreated(java.util.Date dateCreated) {
    		this.dateCreated = dateCreated;
    	}
    
    	@Override
    	public String toString() {
    		return "created on"+dateCreated+"\ncolor"+color+"and filled:"+filled;
    	}
    
    	public abstract double getArea();
    
    	public abstract double getPerimater();
    }
    

    Triangle类如下:

  • public class TriangleFromGeometricObject extends GeometricObject{
    
    	private double side1;
    	private double side2;
    	private double side3;
    	
    	public TriangleFromGeometricObject() {
    		super();
    	}
    	
    	public TriangleFromGeometricObject(boolean filled, String color,double side1,double side2,double side3) {
    		super(filled, color);
    		this.side1 = side1;
    		this.side2 = side2;
    		this.side3 = side3;
    	}
    
    	public double getSide1() {
    		return side1;
    	}
    
    	public void setSide1(double side1) {
    		this.side1 = side1;
    	}
    
    	public double getSide2() {
    		return side2;
    	}
    
    	public void setSide2(double side2) {
    		this.side2 = side2;
    	}
    
    	public double getSide3() {
    		return side3;
    	}
    
    	public void setSide3(double side3) {
    		this.side3 = side3;
    	}
    
    	@Override
    	public double getArea() {
    		double area = Math.sqrt((getPerimater()/2)*(getPerimater()/2-side1)*(getPerimater()/2-side2)*(getPerimater()/2-side3));
    		return area;
    	}
    
    	@Override
    	public double getPerimater() {
    		return side1 + side2 + side3;
    	}
    
    	@Override
    	public String toString() {
    		return "TriangleFromGeometricObject [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + ", toString()="
    				+ super.toString() + "]";
    	}
    }

     

  • 测试类:

  • public class TriangleCombine {
    
    	public static void main(String[] args) {
    		
    		java.util.Scanner input = new java.util.Scanner(System.in);
    		
    		System.out.println("请输入是否填充:");
    		boolean filled = input.nextBoolean();
    		
    		System.out.println("请输入颜色:");
    		String colorFFF = input.nextLine();
    		String color = input.nextLine();
    		
    		System.out.println("请输入三角形的三条边:");
    		double side1 = input.nextDouble();
    		double side2 = input.nextDouble();
    		double side3 = input.nextDouble();
    		
    		GeometricObject triangle = new TriangleFromGeometricObject(filled,color,side1,side2,side3);
    	
    		System.out.println("filled="+triangle.isFilled());
    		System.out.println("color="+triangle.getColor());
    		System.out.println("dateCrated="+triangle.getDateCreated());
    		System.out.println("side1="+side1+"\nside2="+side2+"\nside3="+side3);
    		System.out.println("getArea="+triangle.getArea());
    		System.out.println("getPerimater="+triangle.getPerimater());
    		System.out.println("toString="+triangle.toString());
    	
    	
    	
    	}
    
    }
    

    java初学者,若有不足请大家指出!谢谢!

    原文作者:改改改不完的Bug......
    原文地址: https://blog.csdn.net/weixin_44040023/article/details/90522528
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章