圆和圆锥

 
 
定义一个圆类Circle(在定义时要求用到private、static、final、this()、this.):该类有2个属性,分别是圆周率PI(取值3.14),半径radius。该类有构造方法Circle()用来将圆的radius初始化为缺省值1,构造方法Circle(double radius)用来将圆的radius初始化为参数的值;设置半径的setRadius(double radius)和获取半径的getRadius();方法perimeter()用来求出对象的周长并返回周长;方法area()用来求出对象的面积并返回面积;方法show()用来输出圆对象的详细信息,输出格式为独立的一行“圆半径=XXXX.XX,周长=xxxx.xx,面积=xxxx.xx”(小数点后面保留2位)。再定义一个从Circle类派生出来的Cone(圆锥体)(类定义过程中要求用到private、super()、super.):里面增加高度height属性;提供一个无参的构造方法将圆锥的半径和高度都默认为1,一个带2个参数的构造方法用来在创建指定半径和高度的圆锥对象;设置高度的setHeight(double height)和获取高度的getHeight()方法;方法surfaceArea()用来求圆锥的表面积;方法volume()用来求圆锥的体积;重写父类的show()方法以两个独立的行“圆锥底面圆半径=XXXX.XX,周长=xxxx.xx,面积=xxxx.xx”和“圆锥高=xxxx.xx,表面积=xxxx.xx,体积=xxxx.xx”显示圆锥的详细信息。在演示类Main中:创建一个半径为3.0的圆c,让它调用show()显示其信息,然后再输出一个空行;创建一个默认的圆锥cone1和一个指定底半径和高度(具体数据从键盘输入)的圆锥cone2,分别显示它们的详细信息,两个圆锥详细信息之间也加一个空行;显示一个空行;然后再把圆锥的对象引用cone2赋值给Circle类的c变量,显示一个独立行“现在c引用的对象如下:”,然后再调用c.show(),看显示的是圆锥的详细信息还是圆锥底面的圆的详细信息。
 
输入描述
 
 
圆锥cone2的半径和高度
 
输出描述
 
 
圆c的详细 信息圆锥cone1的详细信息圆锥cone2的详细信息上转型对象c的详细信息上述输出中的标点全为英文状态的
 
输入样例
 
 
5.0 6.0
 
输出样例
 
 
圆半径=3.00,周长=18.84,面积=28.26圆锥底面圆半径=1.00,周长=6.28,面积=3.14圆锥高=1.00,表面积=7.58,体积=1.05圆锥底面圆半径=5.00,周长=31.40,面积=78.50圆锥高=6.00,表面积=201.12,体积=157.00现在c引用的对象如下:圆锥底面圆半径=5.00,周长=31.40,面积=78.50

圆锥高=6.00,表面积=201.12,体积=157.00

import java.util.*;

import org.omg.CORBA.SystemException;
class Circle
{
	public double radius;
	public double height;
	public static double pi=3.14;
	public Circle()
	{
		radius=1;
		height=1;
	}
	public Circle(double radius,double height)
	{
		this.radius=radius;
		this.height=height;
	}
	public void setRadius(double radius)
	{
		this.radius=radius;
	}
	public void setheight(double height)
	{
		this.height=height;
	}
	public double getRaius()
	{
		return radius;
	}
	public double getheight()
	{
		return height;
	}
	public double perimeter()
	{
		return 3*pi*radius;
	}
	public double area()
	{
		return pi*radius*radius;
	}
	public void show(double radius,double height) 
	{
		double sur=pi*radius*radius;
		double l=Math.sqrt(height*height+radius*radius);
	System.out.printf("圆半径=%.2f,周长=%.2f,面积=%.2f\n\n",radius,2*pi*radius,sur);
	}
	public void show1(double radius,double height)
	{
		double sur=pi*radius*radius;
		double l=Math.sqrt(height*height+radius*radius);
		System.out.printf("圆锥高=%.2f,表面积=%.2f,体积=%.2f\n",height,sur+pi*radius*l,sur*height*1/3);
	}
	public void show2(double radius,double height) 
	{
		double sur=pi*radius*radius;
		double l=Math.sqrt(height*height+radius*radius);
	System.out.printf("圆锥底面圆半径=%.2f,周长=%.2f,面积=%.2f\n",radius,2*pi*radius,sur);
	}
}
class Cone extends Circle
{
	public double height;
	public void Cone()
	{
		super.show2(super.radius, super.height);
		super.show1(super.radius, super.height);
		System.out.printf("\n");
	}
	public void Cone(double height,double radius) 
	{
		super.setRadius(radius);
		super.setheight(height);
		super.show2(super.getRaius(),super.getheight());
		super.show1(super.getRaius(),super.getheight());
		System.out.printf("\n");
	}
	public void show()
	{
		System.out.printf("现在c引用的对象如下:\n");
	}
}
public class Main {

	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		double x,y;
		x=in.nextDouble();
		y=in.nextDouble();
		Circle st=new Circle();
		st.show(3,y);
		Cone sd=new Cone();
		Cone st1=new Cone();
		sd.Cone();
		st1.Cone(y,x);
		st1.show();
		st1.Cone(y,x);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值