java源碼——計算立體圖形的表面積和體積__原始碼

來源:互聯網
上載者:User

計算球,圓柱,圓錐的表面積和體積。

利用介面實現。

上代碼。


Contants.java

常量儲存類


package com.fuxuemingzhu.solidgraphics.contants;/** * <p> * Title: Contants * </p> * <p> * Description:常量類,存放程式裡用到的常量值 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @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;/** * <p> * Title: SolidGraphics * </p> * <p> * Description:立體圖形介面,所有立體圖形都要實現這個介面 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @date 2014年10月28日 下午1:52:22 */public interface SolidGraphics {/** * <p> * Title: caculateArea * </p> * <p> * Description:計算立體圖形的表面積 * </p> *  * @return 立體的表面積 *  */public float caculateArea();/** * <p> * Title: caculateVolume * </p> * <p> * Description:計算立體圖形的體積 * </p> *  * @return 立體的體積 *  */public float caculateVolume();}



Ball.java

球類


package com.fuxuemingzhu.solidgraphics.entity;import com.fuxuemingzhu.solidgraphics.base.SolidGraphics;import com.fuxuemingzhu.solidgraphics.contants.Contants;/** * <p> * Title: Ball * </p> * <p> * Description:球類 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @date 2014年10月28日 下午1:54:53 */public class Ball implements SolidGraphics {/** * r 球的半徑 */private float r;/** * <p> * Title: Ball * </p> * <p> * Description: 構造方法,根據球的半徑構造球 * </p> *  * @param r */public Ball(float r) {super();this.r = r;}/** * (非 Javadoc) * <p> * Title: caculateArea * </p> * <p> * Description:計算球的表面積 * </p> *  * @return 立體的表面積 * @see com.<span style="font-family: Arial, Helvetica, sans-serif;">fuxuemingzhu</span><span style="font-family: Arial, Helvetica, sans-serif;">.solidgraphics.base.SolidGraphics#caculateArea()</span> */public float caculateArea() {float area = (float) (4 * Contants.PI * Math.pow(r, 2));return area;}/** * (非 Javadoc) * <p> * Title: caculateVolume * </p> * <p> * Description:計算球的體積 * </p> *  * @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;/** * <p> * Title: Cylinder * </p> * <p> * Description:圓柱類 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @date 2014年10月28日 下午1:55:07 */public class Cylinder implements SolidGraphics {/** * r 圓柱的半徑 */private float r;/** * h 圓柱的高 */private float h;/** * <p> * Title: Cylinder * </p> * <p> * Description:構造方法,根據圓柱的半徑和高構造圓柱 * </p> *  * @param r * @param h */public Cylinder(float r, float h) {super();this.r = r;this.h = h;}/** * (非 Javadoc) * <p> * Title: caculateArea * </p> * <p> * Description:計算圓柱的表面積 * </p> *  * @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) * <p> * Title: caculateVolume * </p> * <p> * Description:計算圓柱的體積 * </p> *  * @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;/** * <p> * Title: Tapered * </p> * <p> * Description:圓錐類 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @date 2014年10月28日 下午1:55:18 */public class Tapered implements SolidGraphics {/** * r 圓錐的半徑 */private float r;/** * h 圓錐的高 */private float h;/** * <p> * Title: Tapered * </p> * <p> * Description:構造方法,根據圓錐的半徑和高構造圓錐 * </p> *  * @param r * @param h */public Tapered(float r, float h) {super();this.r = r;this.h = h;}/** * (非 Javadoc) * <p> * Title: caculateArea * </p> * <p> * Description: 計算圓錐的表面積 * </p> *  * @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) * <p> * Title: caculateVolume * </p> * <p> * Description: 計算圓錐的體積 * </p> *  * @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;/** * <p> * Title: Main * </p> * <p> * Description:主類,展示各立體的表面積,體積等資訊 * </p> *  * @author fuxuemingzhu *  * @email fuxuemingzhu@163.com *  * @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;/** * <p> * Title: main * </p> * <p> * Description:main()方法,程式的入口 * </p> *  * @param args *  */public static void main(String[] args) {// ///構造並展示球體showBall(8f);// //構造並展示圓柱體showCylinder(4f, 10f);// //構造並展示圓錐體showTapered(4f, 10f);}/** * <p> * Title: showBall * </p> * <p> * Description:構造並展示一個球體 * </p> *  * @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");}/** * <p> * Title: showCylinder * </p> * <p> * Description:構造並展示一個圓柱體 * </p> *  * @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");}/** * <p> * Title: showTapered * </p> * <p> * Description:構造並展示一個圓錐體 * </p> *  * @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");}}


附運行截圖。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.