java源码——计算不同图形的周长和面积

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

利用类的继承实现。

将计算结果进行输出。

不多说,贴码。


Contants.java  

常量存储类

package com.fuxuemingzhu.graphs.contants;

/**
 * 

* Title: Contants *

*

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

* * @author fuxuemingzhu * * @email [email protected] * * @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;

/**
 * 

* Title: Circle *

*

* Description:圆形类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月27日 下午11:49:53 */ public class Circle extends Graphs { /** * 圆的半径 */ private float r; /** *

* Title: Circle *

*

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

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

* Title: calculateLenth *

*

* Description:计算圆的周长 *

* * @return 图形周长 * @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth() */ public float calculateLenth() { // 圆的周长公式 float lenth = (float) (2 * Contants.PI * r); return lenth; } /** * (非 Javadoc) *

* Title: calculateSize *

*

* Description:计算圆的面积 *

* * @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;

/**
 * 

* Title: Pentagon *

*

* Description:正五边形类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月27日 下午11:51:00 */ public class Pentagon extends Graphs { /** * 正五边形边长 */ private float a; /** *

* Title: Pentagon *

*

* Description:构造方法,根据正五边形的边长构造五边形 *

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

* Title: calculateLenth *

*

* Description:计算正五边形的周长 *

* * @return 图形周长 * @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth() */ public float calculateLenth() { /* 正五边形的周长公式 */ float lenth = 5 * a; return lenth; } /** * (非 Javadoc) *

* Title: calculateSize *

*

* Description:计算正五边形的面积 *

* * @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;

/**
 * 

* Title: Square *

*

* Description:正方形类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月27日 下午11:48:49 */ public class Square extends Graphs { /** * a 正方形的边长 */ private float a; /** *

* Title: Square *

*

* Description:构造方法,根据正方形的边长构造正方形 *

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

* Title: calculateLenth *

*

* Description:计算正方形的周长 *

* * @return 图形周长 * @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth() */ public float calculateLenth() { // 正方形的周长公式 float lenth = 4 * a; return lenth; } /** * (非 Javadoc) *

* Title: calculateSize *

*

* Description:计算正方形的面积 *

* * @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;

/**
 * 

* Title: Triangle *

*

* Description: 三角形类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月27日 下午11:47:38 */ public class Triangle extends Graphs { /** * a 三角形的第一条边 */ private float a; /** * b 三角形的第二条边 */ private float b; /** * c 三角形的第三条边 */ private float c; /** *

* Title: Triangle *

*

* Description:正三角形的的构造器 *

* * @param a */ public Triangle(float a) { super(); this.a = a; this.b = a; this.c = a; } /** *

* Title: *

*

* Description:一般三角形的构造方法,传入三边的边长 *

* * @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) *

* Title: calculateLenth *

*

* Description:计算三角形的周长 *

* * @return 图形周长 * @see com.fuxuemingzhu.graphs.graphs.Graphs#calculateLenth() */ public float calculateLenth() { // 三角形的周长公式 float lenth = a + b + c; return lenth; } /** * (非 Javadoc) *

* Title: calculateSize *

*

* Description:计算三角形的面积 *

* * @return 图形面积 * @see com.fuxuemingzhu.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;

/**
 * 

* Title: Graphs *

*

* Description:抽象类,是所有图形类的基类 *

* * @author fuxuemingzhu * * @email [email protected] * * @date 2014年10月28日 上午12:04:28 */ public abstract class Graphs { /** *

* Title: calculateLenth *

*

* Description:抽象方法,计算图形的周长 *

* * @return 图形周长 * */ public abstract float calculateLenth(); /** *

* Title: calculateSize *

*

* Description:抽象方法,计算图形的面积 *

* * @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;

/**
 * 

* Title: Main *

*

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

* * @author fuxuemingzhu * * @email [email protected] * * @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; /** *

* Title: main *

*

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

* * @param args * */ public static void main(String[] args) { // ///构造错误的三角形 showTriangle(4f, 5f, 10f); // //构造正确的三角形 showTriangle(6f, 8f, 9f); // ///构造正方形 showSquare(8f); // //构造圆 showCircle(8f); // //构造正五边形 showPentagon(8f); } /** *

* Title: showTriangle *

*

* Description:判断输入的数据能否构成三角形,如果可以则构造出三角形,并且展示;否则,系统返回错误。 *

* * @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"); } /** *

* Title: showSquare *

*

* Description:根据输入的边长构造出正方形,并且展示正方形的周长和面积 *

* * @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"); } /** *

* Title: showCircle *

*

* Description:根据输入的半径构造出圆,并且展示圆的周长和面积 *

* * @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"); } /** *

* Title: showPentagon *

*

* Description:根据输入的边长构造出正五边形,并且展示正五边形的周长和面积 *

* * @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"); } }

附运行截图。


java源码——计算不同图形的周长和面积_第1张图片


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