黑马程序员技术交流社区
标题:
键盘输入任意数,计算面积与体积,周长
[打印本页]
作者:
薛大千
时间:
2015-4-11 22:26
标题:
键盘输入任意数,计算面积与体积,周长
/*
实现输入半径与高度求园的体积,底面面积与周长,
*/
import java.util.Scanner;
class Cylinder
{private double area,perimeter,volume;
private double height,ridus;
private double pi=3.14;
Cylinder(){}
Cylinder(double height,double ridus){
this.ridus=ridus;
this.height=height;
}
public double getRidus()//通过键盘输入获得半径的值
{
System.out.println("请输入圆柱的半径为:");
Scanner h= new Scanner(System.in);
ridus=h.nextDouble();
System.out.println("输入圆柱的半径为:"+ridus);
return ridus;}
public double getArea(){
area=pi*ridus*ridus;
return area;
}
public double getPerimeter(){
perimeter=2*pi*ridus;
return perimeter;}
public double getHeight()//通过键盘输入获得半径的值
{
System.out.println("请输入圆柱的高度为:");
Scanner f= new Scanner(System.in);
height=f.nextDouble();
System.out.println("输入圆柱的高度为:"+height);
return height;}
public double getVolume(){
volume=area*height;
return volume;
}
}
class Square extends Cylinder
{ Square(double height,double ridus){
super(height,ridus);
}
Square(){
}
}
class MapTest
{
public static void main(String[] args)
{ Square b=new Square();
Square a=new Square(b.getHeight(),b.getRidus());
Square c=new Square(b.getHeight(),b.getRidus());
System.out.println("圆柱的底面面积为"+a.getArea());
System.out.println("圆柱的底面周长为"+a.getPerimeter());
System.out.println("圆柱的体积为"+a.getVolume());
System.out.println("圆柱的底面面积为"+c.getArea());
System.out.println("圆柱的底面周长为"+c.getPerimeter());
System.out.println("圆柱的体积为"+c.getVolume());
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2