首页 技术 正文
技术 2022年11月15日
0 收藏 915 点赞 3,172 浏览 7756 个字

计算任意三角形,正方形,正五边形,圆形的周长和面积。

利用类的继承实现。

将计算结果进行输出。

不多说,贴码。

Contants.java

常量存储类

<pre name="code" class="java">package com.fuxuemingzhu.graphs.contants;/**
* <p>
* Title: Contants
* </p>
* <p>
* Description:常量类,存放程序里用到的常量值
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月28日 下午2:20:15
*/
public class Contants {
/**
* PI 圆周率
*/
public static float PI = (float) Math.PI;/**
* MULTIPLES 正五边形求面积时,边长平方的比值
*/
public static float MULTIPLES = 1.72f;}

Circle.java

圆类

package com.fuxuemingzhu.graphs.entity;import com.fuxuemingzhu.graphs.contants.Contants;
import com.fuxuemingzhu.graphs.graphs.Graphs;/**
* <p>
* Title: Circle
* </p>
* <p>
* Description:圆形类
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月27日 下午11:49:53
*/
public class Circle extends Graphs {/**
* 圆的半径
*/
private float r;/**
* <p>
* Title: Circle
* </p>
* <p>
* Description:构造方法,根据圆的半径构造圆
* </p>
*
* @param r
*/
public Circle(float r) {
super();
this.r = r;
}/**
* (非 Javadoc)
* <p>
* Title: calculateLenth
* </p>
* <p>
* Description:计算圆的周长
* </p>
*
* @return 图形周长
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth()
*/
public float calculateLenth() {
// 圆的周长公式
float lenth = (float) (2 * Contants.PI * r);
return lenth;
}/**
* (非 Javadoc)
* <p>
* Title: calculateSize
* </p>
* <p>
* Description:计算圆的面积
* </p>
*
* @return 图形面积
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateSize()
*/
public float calculateSize() {
// 圆的面积公式
float size = (float) (Contants.PI * Math.pow(r, 2));
return size;
}/**
* @return the r
*/
public float getR() {
return r;
}/**
* @param r
* the r to set
*/
public void setR(float r) {
this.r = r;
}}

Pentagon.java

正五边形

package com.fuxuemingzhu.graphs.entity;import com.fuxuemingzhu.graphs.contants.Contants;
import com.fuxuemingzhu.graphs.graphs.Graphs;/**
* <p>
* Title: Pentagon
* </p>
* <p>
* Description:正五边形类
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月27日 下午11:51:00
*/
public class Pentagon extends Graphs {/**
* 正五边形边长
*/
private float a;/**
* <p>
* Title: Pentagon
* </p>
* <p>
* Description:构造方法,根据正五边形的边长构造五边形
* </p>
*
* @param a
*/
public Pentagon(float a) {
super();
this.a = a;
}/**
* (非 Javadoc)
* <p>
* Title: calculateLenth
* </p>
* <p>
* Description:计算正五边形的周长
* </p>
*
* @return 图形周长
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth()
*/
public float calculateLenth() {
/* 正五边形的周长公式 */
float lenth = 5 * a;
return lenth;
}/**
* (非 Javadoc)
* <p>
* Title: calculateSize
* </p>
* <p>
* Description:计算正五边形的面积
* </p>
*
* @return 图形面积
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateSize()
*/
public float calculateSize() {
// 正五边形的面积公式
float size = (float) (Contants.MULTIPLES * Math.pow(a, 2));
return size;
}/**
* @return the a
*/
public float getA() {
return a;
}/**
* @param a
* the a to set
*/
public void setA(float a) {
this.a = a;
}}

Square.java

正方形类

package com.fuxuemingzhu.graphs.entity;import com.fuxuemingzhu.graphs.graphs.Graphs;/**
* <p>
* Title: Square
* </p>
* <p>
* Description:正方形类
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月27日 下午11:48:49
*/
public class Square extends Graphs {/**
* a 正方形的边长
*/
private float a;/**
* <p>
* Title: Square
* </p>
* <p>
* Description:构造方法,根据正方形的边长构造正方形
* </p>
*
* @param a
*/
public Square(float a) {
super();
this.a = a;
}/**
* (非 Javadoc)
* <p>
* Title: calculateLenth
* </p>
* <p>
* Description:计算正方形的周长
* </p>
*
* @return 图形周长
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth()
*/
public float calculateLenth() {
// 正方形的周长公式
float lenth = 4 * a;
return lenth;
}/**
* (非 Javadoc)
* <p>
* Title: calculateSize
* </p>
* <p>
* Description:计算正方形的面积
* </p>
*
* @return 图形面积
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateSize()
*/
public float calculateSize() {
// 正方形的面积公式
float size = (float) Math.pow(a, 2);
return size;
}/**
* @return the a
*/
public float getA() {
return a;
}/**
* @param a
* the a to set
*/
public void setA(float a) {
this.a = a;
}}

Triangle.java 

三角形类

package com.fuxuemingzhu.graphs.entity;import com.fuxuemingzhu.graphs.graphs.Graphs;/**
* <p>
* Title: Triangle
* </p>
* <p>
* Description: 三角形类
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月27日 下午11:47:38
*/
public class Triangle extends Graphs {/**
* a 三角形的第一条边
*/
private float a;
/**
* b 三角形的第二条边
*/
private float b;
/**
* c 三角形的第三条边
*/
private float c;/**
* <p>
* Title: Triangle
* </p>
* <p>
* Description:正三角形的的构造器
* </p>
*
* @param a
*/
public Triangle(float a) {
super();
this.a = a;
this.b = a;
this.c = a;
}/**
* <p>
* Title:
* </p>
* <p>
* Description:一般三角形的构造方法,传入三边的边长
* </p>
*
* @param a
* @param b
* @param c
*/
public Triangle(float a, float b, float c) {
super();
this.a = a;
this.b = b;
this.c = c;
}/**
* (非 Javadoc)
* <p>
* Title: calculateLenth
* </p>
* <p>
* Description:计算三角形的周长
* </p>
*
* @return 图形周长
* @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth()
*/
public float calculateLenth() {
// 三角形的周长公式
float lenth = a + b + c;
return lenth;
}/**
* (非 Javadoc)
* <p>
* Title: calculateSize
* </p>
* <p>
* Description:计算三角形的面积
* </p>
*
* @return 图形面积
* @see com.<span style="font-family: Arial, Helvetica, sans-serif;">fuxuemingzhu</span>.graphs.graphs.Graphs#calculateSize()
*/
public float calculateSize() {
// 三角形的面积公式(海伦公式)
float p = (a + b + c) / 2;float size = (float) Math.sqrt(p * (p - a) * (p - b) * (p - c));
return size;
}/**
* @return the a
*/
public float getA() {
return a;
}/**
* @param a
* the a to set
*/
public void setA(float a) {
this.a = a;
}/**
* @return the b
*/
public float getB() {
return b;
}/**
* @param b
* the b to set
*/
public void setB(float b) {
this.b = b;
}/**
* @return the c
*/
public float getC() {
return c;
}/**
* @param c
* the c to set
*/
public void setC(float c) {
this.c = c;
}}

Graphs.java

抽象类,是所有图形类的基类

package com.fuxuemingzhu.graphs.graphs;/**
* <p>
* Title: Graphs
* </p>
* <p>
* Description:抽象类,是所有图形类的基类
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月28日 上午12:04:28
*/
public abstract class Graphs {/**
* <p>
* Title: calculateLenth
* </p>
* <p>
* Description:抽象方法,计算图形的周长
* </p>
*
* @return 图形周长
*
*/
public abstract float calculateLenth();/**
* <p>
* Title: calculateSize
* </p>
* <p>
* Description:抽象方法,计算图形的面积
* </p>
*
* @return 图形面积
*
*/
public abstract float calculateSize();}

Main.java

主类,展示各图形的周长,面积等信息

package com.fuxuemingzhu.graphs.main;import com.fuxuemingzhu.graphs.entity.Circle;
import com.fuxuemingzhu.graphs.entity.Pentagon;
import com.fuxuemingzhu.graphs.entity.Square;
import com.fuxuemingzhu.graphs.entity.Triangle;/**
* <p>
* Title: Main
* </p>
* <p>
* Description:主类,展示各图形的周长,面积等信息
* </p>
*
* @author fuxuemingzhu
*
* @email fuxuemingzhu@163.com
*
* @date 2014年10月27日 下午11:51:51
*/
public class Main {/**
* triangle 声明一个三角形
*/
private static Triangle triangle;/**
* square 声明一个正方形
*/
private static Square square;/**
* circle 声明一个圆
*/
private static Circle circle;/**
* pentagon 声明一个正五边形
*/
private static Pentagon pentagon;/**
* <p>
* Title: main
* </p>
* <p>
* Description:main()方法,程序的入口
* </p>
*
* @param args
*
*/
public static void main(String[] args) {// ///构造错误的三角形
showTriangle(4f, 5f, 10f);// //构造正确的三角形
showTriangle(6f, 8f, 9f);// ///构造正方形
showSquare(8f);// //构造圆
showCircle(8f);// //构造正五边形
showPentagon(8f);}/**
* <p>
* Title: showTriangle
* </p>
* <p>
* Description:判断输入的数据能否构成三角形,如果可以则构造出三角形,并且展示;否则,系统返回错误。
* </p>
*
* @param a
* @param b
* @param c
*
*/
private static void showTriangle(float a, float b, float c) {
float p = (a + b + c) / 2;
// //判断能否构造成三角形
if ((p - a) <= 0 || (p - b) <= 0 || (p - c) <= 0) {
System.out.println("输入的三角形的三边长为:" + a + "," + b + "," + c);
System.err.println("错误!输入的三边无法构成三角形!\n");
return;
}
// ///构造三角形
triangle = new Triangle(a, b, c);
System.out.println("输入的三角形的三边长分别是:" + triangle.getA() + ","
+ triangle.getB() + "," + triangle.getC());
System.out.println("此三角形的周长是:" + triangle.calculateLenth());
System.out.println("此三角形的面积是:" + triangle.calculateSize() + "\n");
}/**
* <p>
* Title: showSquare
* </p>
* <p>
* Description:根据输入的边长构造出正方形,并且展示正方形的周长和面积
* </p>
*
* @param a
*
*/
private static void showSquare(float a) {
// /构造正方形
square = new Square(a);
// //展示
System.out.println("输入的正方形的边长是:" + square.getA());
System.out.println("此正方形的周长是:" + square.calculateLenth());
System.out.println("此正方形的面积是:" + square.calculateSize() + "\n");
}/**
* <p>
* Title: showCircle
* </p>
* <p>
* Description:根据输入的半径构造出圆,并且展示圆的周长和面积
* </p>
*
* @param r
*
*/
private static void showCircle(float r) {
// //构造圆
circle = new Circle(r);
// //展示
System.out.println("输入的圆的半径是:" + circle.getR());
System.out.println("此圆的周长是:" + circle.calculateLenth());
System.out.println("圆的面积是:" + circle.calculateSize() + "\n");
}/**
* <p>
* Title: showPentagon
* </p>
* <p>
* Description:根据输入的边长构造出正五边形,并且展示正五边形的周长和面积
* </p>
*
* @param a
*
*/
private static void showPentagon(float a) {
// /构造正五边形
pentagon = new Pentagon(a);
// //展示
System.out.println("输入的正五边形的边长是:" + pentagon.getA());
System.out.println("此正五边形的周长是:" + pentagon.calculateLenth());
System.out.println("此正五边形的面积是:" + pentagon.calculateSize() + "\n");
}
}

附运行截图。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906