java作业整理

题意:

1. 设计一个名为Trangle的类来扩展GeometricObject类,该类封装如下信息:

要求:(1) 三个名为side1side2side3double私有数据域表示三条边;

 (2) 一个无参数的构造方法;

      (3) 一个能创建带指定参数参数s1s2s3的构造方法;

      (4) 一个名为getPerimeter()的方法返回三角形的周长

      (5) 一个名为toString()的方法返回三角形的三条边信息。

      (6) 编写测试程序,创建一个边长为235,颜色为redTrangle类型对象t1,然后显示其周长。

public class Trangle extends GeometricObject1{
	private double side1;
	private double side2;
	private double side3;
	String color;
	public Trangle(){
		
	}
	
	public Trangle(double side1,double side2,double side3){
		this.side1 = side1;
this.side2 = side2;
		this.side3 = side3;
	}
   public Trangle(String color){
	   this.color = color;
   }
	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;
	}
    public double getPerimeter(){
    	return side1 + side2 + side3;
    }
    
	public String toString(){
		return "\n" + "Side2: " + side1 + "\n" + "Side2: " +
		side2 + "\n" + "Side3: " + side3 + "\n" ;
	}
}





public class TestTrangle {
       public static void main(String[] args){
    	   Trangle t1 = new Trangle(2,3,5);
    	   t1.color = "red";
    	   System.out.println("A trangle is: " + t1.toString());
    	   System.out.println("The perimeter is: " + t1.getPerimeter());
       }
}

你可能感兴趣的:(java作业整理)