java源码——计算立体图形的表面积和体积
计算球,圆柱,圆锥的表面积和体积。
利用接口实现。
上代码。
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");
}
}
附运行截图。
java源码——计算立体图形的表面积和体积的更多相关文章
- java源码——计算不同图形的周长和面积
计算任意三角形,正方形,正五边形,圆形的周长和面积. 利用类的继承实现. 将计算结果进行输出. 不多说,贴码. Contants.java 常量存储类 <pre name="code& ...
- java源码——计算大于一个数的最小素数
根据输入数字,计算大于一个数的最小素数. 话不多说,直接贴码. package com.fuxuemingzhu.countprime.main; import java.util.Scanner; ...
- Java练习 SDUT-3337_计算长方体、四棱锥的表面积和体积
计算长方体.四棱锥的表面积和体积 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 计算如下立体图形的表面积和体积. 从图 ...
- Tika结合Tesseract-OCR 实现光学汉字识别(简体、宋体的识别率百分之百)—附Java源码、测试数据和训练集下载地址
OCR(Optical character recognition) —— 光学字符识别,是图像处理的一个重要分支,中文的识别具有一定挑战性,特别是手写体和草书的识别,是重要和热门的科学研究方向.可 ...
- 解密随机数生成器(二)——从java源码看线性同余算法
Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...
- Java源码解读(一)——HashMap
HashMap作为常用的一种数据结构,阅读源码去了解其底层的实现是十分有必要的.在这里也分享自己阅读源码遇到的困难以及自己的思考. HashMap的源码介绍已经有许许多多的博客,这里只记录了一些我看源 ...
- 24点扑克牌游戏——(含java源码)(GUI实现)
给出四个数字,要求,在其间添加运算符和括号,使得计算结果等于24. 括号的放置即为决定哪几个数先进行计算.所以,我们先确定首先进行计算的两个相邻的数,计算完成后,就相当于剩下三个数字,仍需要在它们之间 ...
- 从Java源码到Java字节码
Java最主流的源码编译器,javac,基本上不对代码做优化,只会做少量由Java语言规范要求或推荐的优化:也不做任何混淆,包括名字混淆或控制流混淆这些都不做.这使得javac生成的代码能很好的维持与 ...
- kafka对消费者分配分区规则(Java源码)
在上一篇 kafka topic消息分配partition规则(Java源码) 我们对生产者产生的消息分配partition规则进行了分析,那么本章我们来看看消费者是怎么样分配partition的. ...
随机推荐
- pcm-pcie 解析
简介 pcm 全称为 Performance Counter Monitor,该项目是针对 intel 平台处理器的资源利用率进行监控的工具.在现代 Intel 处理器已经提供了监视处理器内部性能事件 ...
- Linux非root安装Python3以及解决SSL问题
说明 接上一篇. [Linux]非root安装Python3及其包管理 上一篇虽然成功安装了Python3及一些常用的模块,但因为一直装不上SSL模块,导致一些包无法安装,尝试了不少方法都失败了(网上 ...
- 谈谈AI
由AI大作业想到的 近几年<人工智能导论>的大作业是编写一个博弈程序,这类程序的典型框架就是α-β剪枝算法,像著名的打败了国际象棋大师卡斯帕罗的深蓝,就是这么干的,一些中国象棋程序也是这样 ...
- Excel-给出指定数值的日期 date()
DATE函数 函数名称:DATE 主要功能:给出指定数值的日期. 使用格式:DATE(year,month,day) 参数说明:year为指定的年份数值(小于9999):month为指定的月份数值(可 ...
- keybd_event模拟键盘按键,mouse_event怎么用
从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...
- A Child's History of England.48
A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...
- HTTP 之 options预请求
一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...
- Linux的小知识
1. top 命令可以在Linux下查看任务管理器和当前进程使用资源情况. 2. Ctrl+c 即可退出,然后使用 kill+进程号 命令可杀死指定进程 3.在Linux的 /etc/rc.local ...
- soapui pro 5.1.2 的破解方法
Protection-4.6,和scz.key这两个文件能破解5.1.2的SoapUI 的Pro版本,mac 和 windows均可.1.拷贝Protection-4.6.jar到soapui安装的l ...
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...