java编程关于接口的程序
利用接口来计算并显示圆柱体和圆锥体的体积:(1)定义一个接口Volume,存放常量以及计算体积的方法calVolume();(2)分别定义圆柱体类Cylinder和圆锥体...
利用接口来计算并显示圆柱体和圆锥体的体积:(1)定义一个接口Volume,存放常量 以及计算体积的方法calVolume();(2)分别定义圆柱体类Cylinder和圆锥体Cone,设置必要的成员变量并实现接口中定义的方法(3)创建一个测试类VolumeTestDemo,实例化两个对象并计算、显示各自形状的体积。其中,圆柱体的体积计算公式为: ;圆锥体的体积计算公式为:1/3*πr^2h 。
展开
2个回答
展开全部
package com.shape;
public interface Volume {
double PI = 3.1415926;
//计算体积
public double calVolume();
//计算表面积
public double calArea();
}
package com.shape;
//圆柱体
public class Cylinder implements Volume {
private double Radius ;
private double Height;
public Cylinder(double Radius, double Height) {
this.Radius = Radius;
this.Height = Height;
}
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
Height = height;
}
@Override
public double calVolume() {
// TODO Auto-generated method stub
return PI * Radius * Radius * Height;
}
@Override
public double calArea() {
// TODO Auto-generated method stub
return PI * Radius * Radius * 2 + PI * Radius * Height * 2;
}
}
package com.shape;
//圆锥
public class Cone implements Volume {
private double Radius;
private double Height;
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
Height = height;
}
public Cone(double Radius, double Height) {
this.Radius = Radius;
this.Height = Height;
}
//圆锥体的母线的长
public double getConeLine() {
double Line = Radius * Radius + Height * Height;
return Math.sqrt(Line);
}
@Override
public double calVolume() {
// TODO Auto-generated method stub
return PI * Radius * Radius * Height / 3;
}
@Override
public double calArea() {
// TODO Auto-generated method stub
return PI * Radius * Radius + PI * Radius * getConeLine();
}
}
public class ShapeTestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cylinder cy = new Cylinder(3, 4);
System.out.println(cy.calVolume());
System.out.println(cy.calArea());
Cone cone = new Cone(3, 4);
System.out.println(cone.calVolume());
System.out.println(cone.getConeLine());
System.out.println(cone.calArea());
}
}
展开全部
package temp;
public class VolumeTestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
interface Volume{
final double pai = 3.1415;
//体积计算
public double calVolume();
}
//圆柱体类
class Cylinder implements Volume{
//底边半径
double r =0;
//高
double h =0;
public Cylinder(double r,double h){
this.r = r;
this.h = h;
}
//圆柱体体积计算
@Override
public double calVolume() {
// TODO Auto-generated method stub
return r*r*pai*h;
}
}
//圆锥类
class Cone implements Volume{
//底边半径
double r =0;
//高
double h =0;
public Cone(double r,double h){
this.r = r;
this.h = h;
}
@Override
public double calVolume() {
// TODO Auto-generated method stub
return 1/3*pai*r*r*h;
}
}
}
傲雪论坛为您解答,满意请采纳哦~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询