java源码——计算立体图形的表面积和体积

计算球,圆柱,圆锥的表面积和体积。

利用接口实现。

上代码。


Contants.java

常量存储类


package com.fuxuemingzhu.solidgraphics.contants;

/**
 * 

* Title: Contants *

*

* Description:常量类,存放程序里用到的常量值 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午2:18:43 */ public class Contants { /** * PI 圆周率 */ public static float PI = (float) Math.PI; }



SolidGraphics.java

立体图形接口,所有立体图形都要实现这个接口


package com.fuxuemingzhu.solidgraphics.base;

/**
 * 

* Title: SolidGraphics *

*

* Description:立体图形接口,所有立体图形都要实现这个接口 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午1:52:22 */ public interface SolidGraphics { /** *

* Title: caculateArea *

*

* Description:计算立体图形的表面积 *

* * @return 立体的表面积 * */ public float caculateArea(); /** *

* Title: caculateVolume *

*

* Description:计算立体图形的体积 *

* * @return 立体的体积 * */ public float caculateVolume(); }



Ball.java

球类


package com.fuxuemingzhu.solidgraphics.entity;

import com.fuxuemingzhu.solidgraphics.base.SolidGraphics;
import com.fuxuemingzhu.solidgraphics.contants.Contants;

/**
 * 

* Title: Ball *

*

* Description:球类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午1:54:53 */ public class Ball implements SolidGraphics { /** * r 球的半径 */ private float r; /** *

* Title: Ball *

*

* Description: 构造方法,根据球的半径构造球 *

* * @param r */ public Ball(float r) { super(); this.r = r; } /** * (非 Javadoc) *

* Title: caculateArea *

*

* Description:计算球的表面积 *

* * @return 立体的表面积 * @see com.fuxuemingzhu.solidgraphics.base.SolidGraphics#caculateArea() */ public float caculateArea() { float area = (float) (4 * Contants.PI * Math.pow(r, 2)); return area; } /** * (非 Javadoc) *

* Title: caculateVolume *

*

* Description:计算球的体积 *

* * @return 立体的体积 * @see com.caifugui.solidgraphics.base.SolidGraphics#caculateVolume() */ public float caculateVolume() { float volume = (float) ((4f / 3f) * Contants.PI * Math.pow(r, 3)); return volume; } /** * @return the r */ public float getR() { return r; } /** * @param r * the r to set */ public void setR(float r) { this.r = r; } }



Cylinder.java

圆柱类


package com.fuxuemingzhu.solidgraphics.entity;

import com.fuxuemingzhu.solidgraphics.base.SolidGraphics;
import com.fuxuemingzhu.solidgraphics.contants.Contants;

/**
 * 

* Title: Cylinder *

*

* Description:圆柱类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午1:55:07 */ public class Cylinder implements SolidGraphics { /** * r 圆柱的半径 */ private float r; /** * h 圆柱的高 */ private float h; /** *

* Title: Cylinder *

*

* Description:构造方法,根据圆柱的半径和高构造圆柱 *

* * @param r * @param h */ public Cylinder(float r, float h) { super(); this.r = r; this.h = h; } /** * (非 Javadoc) *

* Title: caculateArea *

*

* Description:计算圆柱的表面积 *

* * @return 立体的表面积 * @see com.fuxuemingzhu.solidgraphics.base.SolidGraphics#caculateArea() */ public float caculateArea() { float area = (float) (Contants.PI * Math.pow(r, 2) * 2 + 2 * Contants.PI * r * h); return area; } /** * (非 Javadoc) *

* Title: caculateVolume *

*

* Description:计算圆柱的体积 *

* * @return 立体的体积 * @see com.fuxuemingzhu.solidgraphics.base.SolidGraphics#caculateVolume() */ public float caculateVolume() { float volume = (float) (Contants.PI * Math.pow(r, 2) * h); return volume; } /** * @return the r */ public float getR() { return r; } /** * @param r * the r to set */ public void setR(float r) { this.r = r; } /** * @return the h */ public float getH() { return h; } /** * @param h * the h to set */ public void setH(float h) { this.h = h; } }



Tapered.java

圆锥类


package com.fuxuemingzhu.solidgraphics.entity;

import com.fuxuemingzhu.solidgraphics.base.SolidGraphics;
import com.fuxuemingzhu.solidgraphics.contants.Contants;

/**
 * 

* Title: Tapered *

*

* Description:圆锥类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午1:55:18 */ public class Tapered implements SolidGraphics { /** * r 圆锥的半径 */ private float r; /** * h 圆锥的高 */ private float h; /** *

* Title: Tapered *

*

* Description:构造方法,根据圆锥的半径和高构造圆锥 *

* * @param r * @param h */ public Tapered(float r, float h) { super(); this.r = r; this.h = h; } /** * (非 Javadoc) *

* Title: caculateArea *

*

* Description: 计算圆锥的表面积 *

* * @return 立体的表面积 * @see com.fuxuemingzhu.solidgraphics.base.SolidGraphics#caculateArea() */ public float caculateArea() { float area = (float) (0.5f * Math.sqrt(Math.pow(r, 2) + Math.pow(h, 2)) * 2 * Contants.PI * r + Contants.PI * Math.pow(r, 2)); return area; } /** * (非 Javadoc) *

* Title: caculateVolume *

*

* Description: 计算圆锥的体积 *

* * @return 立体的体积 * @see com.fuxuemingzhu.solidgraphics.base.SolidGraphics#caculateVolume() */ public float caculateVolume() { float volume = (float) ((1f / 3f) * Contants.PI * Math.pow(r, 2) * h); return volume; } /** * @return the r */ public float getR() { return r; } /** * @param r * the r to set */ public void setR(float r) { this.r = r; } /** * @return the h */ public float getH() { return h; } /** * @param h * the h to set */ public void setH(float h) { this.h = h; } }


Main.java

主类,展示各立体的表面积,体积等信息


package com.fuxuemingzhu.solidgraphics.main;

import com.fuxuemingzhu.solidgraphics.entity.Ball;
import com.fuxuemingzhu.solidgraphics.entity.Cylinder;
import com.fuxuemingzhu.solidgraphics.entity.Tapered;

/**
 * 

* Title: Main *

*

* Description:主类,展示各立体的表面积,体积等信息 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 下午3:46:57 */ public class Main { /** * ball 声明一个球体 */ private static Ball ball; /** * cylinder 声明一个圆柱体 */ private static Cylinder cylinder; /** * tapered 声明一个圆锥体 */ private static Tapered tapered; /** *

* Title: main *

*

* Description:main()方法,程序的入口 *

* * @param args * */ public static void main(String[] args) { // ///构造并展示球体 showBall(8f); // //构造并展示圆柱体 showCylinder(4f, 10f); // //构造并展示圆锥体 showTapered(4f, 10f); } /** *

* Title: showBall *

*

* Description:构造并展示一个球体 *

* * @param r * */ private static void showBall(float r) { // //构造一个球体 ball = new Ball(r); // //展示球体 System.out.println("输入的球的半径是:" + ball.getR()); System.out.println("此球的表面积是:" + ball.caculateArea()); System.out.println("此球的体积是:" + ball.caculateVolume() + "\n"); } /** *

* Title: showCylinder *

*

* Description:构造并展示一个圆柱体 *

* * @param r * @param h * */ private static void showCylinder(float r, float h) { // /构造一个圆柱体 cylinder = new Cylinder(r, h); // /展示圆柱体 System.out.println("输入的圆柱的底面圆半径是:" + cylinder.getR() + ",高为:" + cylinder.getH()); System.out.println("此圆柱的表面积是:" + cylinder.caculateArea()); System.out.println("此圆柱的体积是:" + cylinder.caculateVolume() + "\n"); } /** *

* Title: showTapered *

*

* Description:构造并展示一个圆锥体 *

* * @param r * @param h * */ private static void showTapered(float r, float h) { // ///构造一个圆锥体 tapered = new Tapered(r, h); // /展示圆锥体 System.out.println("输入的圆锥的底面圆半径是:" + tapered.getR() + ",高为:" + tapered.getH()); System.out.println("此圆锥的表面积是:" + tapered.caculateArea()); System.out.println("此圆锥的体积是:" + tapered.caculateVolume() + "\n"); } }


附运行截图。


java源码——计算立体图形的表面积和体积_第1张图片


你可能感兴趣的:(源代码)