类的组合与继承【作业】

  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个图形(是表中图形个数的任意组合)的面积之和,运行界面如图。
在这里插入代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值