類的組合與繼承——作業

LHelka發表於2020-10-28
  1. 設計類,分別完成:
    定義一個點類;
    繼承點類,定義一個圓類,計算其面積、周長;
    繼承圓類,定義一個圓柱類,計算其表面積、體積;
    設計一個包含主方法的類,對設計的3個類進行測試。
package Draft_1;

public class Text {
	public static void main(String[] args) {
		Point p1 = new Point();
	    Point p2 = new Circle(1);
	    Point p3 = new Cylinder(4,10);
	    Circle cir1 = new Circle(3);
	    System.out.println("圓的周長:"+cir1.Perimeter());
	    System.out.println("圓的面積:"+cir1.Area());
	    Cylinder cyl1 = new Cylinder(2,5);
	    System.out.println("圓柱的表面積:"+cyl1.Area());
	    System.out.println("圓柱的體積:"+cyl1.Volume());
	}
	
}

class Point {
	
	private double x,y;
	
	public Point() {
		x = 0;
		y = 0;
		//System.out.println("Point");
	}
	
}

class Circle extends Point {
	
	private double r;
	
	public Circle(double r) {
		super();
		this.r = r;
		//System.out.println("Circle");
	}
	
	public double Perimeter() {
		return 2*3.14*r;
	}
	
	public double Area() {
		return 3.14*r*r;
	}
	
}

class Cylinder extends Circle {
	
	private double h;

	public Cylinder(double r,double h) {
		super(r);
		this.h = h;
		//System.out.println("Cylinder");
	}
	
	public double Area() {
		return super.Perimeter()*h+super.Area()*2;
	}
	
	public double Volume() {
		return super.Area()*h;
	}
	
}
  1. 定義一個矩形類,計算其面積、周長;
package Draft_1;

public class Text {
	public static void main(String[] args) {
		Rectangle r1 = new Rectangle(2,3);
		System.out.println("長方形的長:"+r1.Get_a());
		System.out.println("長方形的寬:"+r1.Get_b());
		System.out.println("長方形的周長:"+r1.Perimeter());
		System.out.println("長方形的面積:"+r1.Area());
		System.out.println();
		Cuboid c1 = new Cuboid(3,4,5);
		System.out.println("長方體的面積:"+c1.Area());
		System.out.println("長方體的體積:"+c1.Volume());
	}
	
}

class Rectangle {
	
	private double a,b;
	
	public Rectangle(double a,double b) {
		this.a = a;
		this.b = b;
	}
	
	public double Get_a() {
		return a;
	}
	
	public double Get_b() {
		return b;
	}
	public double Perimeter() {
		return 2*a+2*b;
	}
	
	public double Area() {
		return a*b;
	}
	
}

class Cuboid extends Rectangle {
	
	private double c;
	
	public Cuboid(double a,double b,double c) {
		super(a,b);
		this.c = c;
	}
	
	public double Area() {
		return 2*super.Area()+2*super.Get_a()*c+2*super.Get_b()*c;
	}
	
	public double Volume() {
		return super.Get_a()*super.Get_b()*c;
	}
	
}
  1. 定義一個點類,然後利用點類再定義一個三角形類,最後定義主類,計算並輸出三角形的面積、周長等有關資訊。
package Draft_1;

public class Text {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3,4,5);
		System.out.println("三角形的周長:"+t1.Perimeter());
		System.out.println("三角形的面積:"+t1.Area());
	}
}

class Point {
	
	private double a,b;
	
	public Point() {
		a = 0;
		b = 0;
	}
	
}

class Triangle extends Point {
	
	private double a,b,c,p;
	
	public Triangle(double a,double b,double c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
		p = Perimeter()/2;
	}
	
	public double Perimeter() {
		return a+b+c;
	}
	
	public double Area() {
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}
	
}
  1. 已知一個點類,再定義一個直線類,如何定義與實現呢?
package Draft_1;

public class Text {
	public static void main(String[] args) {
		Straight s1 = new Straight();
	}
}

class Point {
	
	private double a,b;
	
	public Point() {
		a = 0;
		b = 0;
	}
	
}

class Straight {
	
	private double a,b;
	
	public Straight() {
		a = 0;
		b = 0;
	}

}
  1. 設有Shape類,Rectangle類,Cube類,Circle類,這些類之間的繼承關係如下圖所示;
    要求:(1)定義這五個類,至少包含下列表(下頁)的功能;
    (2)設計一個主方法,其功能是求n個圖形(是表中圖形個數的任意組合)的面積之和,執行介面如圖。
在這裡插入程式碼片

相關文章