Java source--Calculate the surface area and volume of three-dimensional graphics source code

Source: Internet
Author: User
Tags pow

Calculate the surface area and volume of the ball, cylinder, cone.

Using interface implementations.

On the code.


Contants.java

Constant Storage class


Package com.fuxuemingzhu.solidgraphics.contants;

/**
 * <p>
 * title:contants
 * </p> *
 <p>
 * Description: constant class, storing constant values used in the program
 * </p> * * * * * 
 @author fuxuemingzhu * * @email fuxuemingzhu@163.com * * 
 @date October 28, 2014 PM 2:18:43
/public class Contants {

	/**
	 * PI pi */public
	static float PI = (float) Math. PI;

}



Solidgraphics.java

Three-dimensional graphics interface, all stereo graphics to implement this interface


Package com.fuxuemingzhu.solidgraphics.base;

/**
 * <p>
 * title:solidgraphics
 * </p> *
 <p>
 * Description: Stereo Graphics interface, All stereo graphics are to implement this interface
 * </p>
 * * 
 @author Fuxuemingzhu
 * * * 
 @email fuxuemingzhu@163.com
 * 
 * @date October 28, 2014 afternoon 1:52:22/Public
interface Solidgraphics {

	/**
	 * <p>
	 * Title:caculatearea
	 * </p>
	 * <p>
	 * Description: Calculating the surface area of three-dimensional graphics
	 * </p>
	 * 
	 * @return three-dimensional surface area
	 * * * 
	 /public
	float Caculatearea ();

	/**
	 * <p>
	 * Title:caculatevolume
	 * </p> *
	 <p>
	 * Description: Calculate the volume of three-dimensional graphics
	 * </p> * * 
	 @return Stereo Volume
	 * 
	 /public
	float Caculatevolume ();

}



Ball.java

Ball


Package com.fuxuemingzhu.solidgraphics.entity;
Import Com.fuxuemingzhu.solidgraphics.base.SolidGraphics;

Import com.fuxuemingzhu.solidgraphics.contants.Contants; 
 /** * <p> * title:ball * </p> * <p> * Description: Ball * </p> * * @author Fuxuemingzhu *
	 * @email fuxuemingzhu@163.com * * @date October 28, 2014 PM 1:54:53 * * Public class Ball implements Solidgraphics {/**

	* R Ball Radius * * Private float R; /** * <p> * title:ball * </p> * <p> * Description: Construction method, according to the radius of the ball to construct the ball * </p> * * @pa
		RAM R */public Ball (float R) {super ();
	THIS.R = R; /** * (Non-Javadoc) * <p> * Title:caculatearea * </p> * <p> * Description: Calculate the surface area of the sphere * ;/p> * * @return three-dimensional surface area * @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; /** * (Non-Javadoc) * <p> * title:caculatevolume * </p> * <p> * Description: Calculate the volume of the ball * &L  t;/p> * * @return Stereo Volume * @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

Cylindrical series


Package com.fuxuemingzhu.solidgraphics.entity;
Import Com.fuxuemingzhu.solidgraphics.base.SolidGraphics;

Import com.fuxuemingzhu.solidgraphics.contants.Contants; /** * <p> * title:cylinder * </p> * <p> * Description: Cylindrical type * </p> * * @author Fuxuemingzh  U * * * * * @email fuxuemingzhu@163.com * * @date October 28, 2014 PM 1:55:07/public class Cylinder implements Solidgraphics

	{/** * R cylinder's RADIUS * * Private float R;

	/** * H Cylinder High * * private float H; 
	 /** * <p> * title:cylinder * </p> * <p> * Description: Construction method, based on the radius of the cylinder and the high tectonic column * </p> *
		* @param R * @param h */public Cylinder (float R, float h) {super ();
		THIS.R = R;
	This.h = h; /** * (Non-Javadoc) * <p> * Title:caculatearea * </p> * <p> * Description: Calculate the surface area of a cylinder * &L t;/p> * * @return three-dimensional surface area * @see Com.fuxuemingzhu.solidgraphics.base.solidgraphics#caculatearea ()/Public FL Oat Caculatearea () {float ARea = (float) (Contants.pi * MATH.POW (R, 2) * 2 + 2 * CONTANTS.PI * R * H);
	return area; /** * (Non-Javadoc) * <p> * title:caculatevolume * </p> * <p> * Description: Calculating Cylinder Volume * & lt;/p> * * @return Stereo Volume * @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 * To set */public void SetH (float h) {this.h = h;
 }

}



Tapered.java

Cone type


Package com.fuxuemingzhu.solidgraphics.entity;
Import Com.fuxuemingzhu.solidgraphics.base.SolidGraphics;

Import com.fuxuemingzhu.solidgraphics.contants.Contants; 
 /** * <p> * title:tapered * </p> * <p> * Description: Cone type * </p> * * @author Fuxuemingzhu 

	* * * * * @email fuxuemingzhu@163.com * * @date October 28, 2014 PM 1:55:18/public class tapered implements Solidgraphics {

	/** * R Cone RADIUS * * Private float R;

	/** * H Cone High * * private float H; 
	 /** * <p> * title:tapered * </p> * <p> * Description: Construction method, according to the radius of the cone and the high structural cone * </p> *
		* @param R * @param h */public tapered (float R, float h) {super ();
		THIS.R = R;
	This.h = h; /** * (Non-Javadoc) * <p> * Title:caculatearea * </p> * <p> * Description: Calculate the surface area of the cone * & lt;/p> * * @return three-dimensional surface area * @see Com.fuxuemingzhu.solidgraphics.base.solidgraphics#caculatearea ()/public F Loat 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; /** * (Non-Javadoc) * <p> * title:caculatevolume * </p> * <p> * Description: Calculating the volume of a cone *  </p> * @return Stereo Volume * @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 * To set */public void SetH (float h) {this.h = h;
 }

}


Main.java

Main class, showing the three-dimensional surface area, volume and other information


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: Main class, showing the three-dimensional surface area, volume and other information * </p> * * @author Fuxuemingzhu * * * * * * @email fuxuemingzhu@163.com * * @date October 28, 2014 afternoon 3:46:57/public class Main {/** * ball

	Declare a sphere */private static Ball Ball;

	/** * Cylinder declares a cylindrical body/private static cylinder cylinder;

	/** * Tapered Declare a cone/private static tapered tapered; /** * <p> * title:main * </p> * <p> * Description:main () method, program entry * </p> * * @par

		AM args * */public static void main (string[] args) {/////constructs and displays the Sphere Showball (8f);

		Construct and display cylindrical showcylinder (4f, 10f);

	Construct and display cone showtapered (4f, 10f); /** * <p> * title:showball * </p> * <p> * DeScription: Construct and present a sphere * </p> * * @param r */private static void Showball (float r) {////construct a sphere BA

		ll = new Ball (r);
		Display Sphere System.out.println ("The radius of the input ball is:" + BALL.GETR ());
		System.out.println ("The surface area of the ball is:" + Ball.caculatearea ());

	System.out.println ("The volume of this ball is:" + ball.caculatevolume () + "\ n"); 
	 /** * <p> * title:showcylinder * </p> * <p> * Description: Construct and display a cylinder * </p> * * @param R * @param h * */private static void Showcylinder (float R, float h) {///construct a cylinder cylinder = new C

		Ylinder (R, h);
		/Show Cylinder SYSTEM.OUT.PRINTLN ("input cylinder's bottom circle radius is:" + cylinder.getr () + ", High for:" + Cylinder.geth ());
		System.out.println ("The surface area of this cylinder is:" + Cylinder.caculatearea ());

	System.out.println ("The volume of this cylinder is:" + cylinder.caculatevolume () + "\ n"); 
	 /** * <p> * title:showtapered * </p> * <p> * Description: Construction and presentation of a cone * </p> * * @param R * @param h * * * * private static void ShowtaPered (float R, float h) {/////constructs a cone tapered = new Tapered (R, h);
		/Show cone System.out.println ("input cone's bottom circle radius is:" + tapered.getr () + ", High for:" + Tapered.geth ());
		System.out.println ("The surface area of this cone is:" + Tapered.caculatearea ());
	System.out.println ("The volume of this cone is:" + tapered.caculatevolume () + "\ n");
 }
}


Attach a screenshot of the operation.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.