Shape.java接口代碼
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括曲阜網(wǎng)站建設(shè)、曲阜網(wǎng)站制作、曲阜網(wǎng)頁制作以及曲阜網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,曲阜網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到曲阜省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圓類代碼
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
? this.radius = radius;
}
@Override
public double area() {
? return PI * this.radius * this.radius;
}
public double perimeter() {
? return 2 * PI * this.radius;
}
}
Cylinder.java圓柱體類代碼
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
? super(radius);
? this.height = height;
}
public double area() {
? return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
? return super.area() * this.height;
}
}
X5_3_6.java主類代碼
public class X5_3_6 {
public static void main(String[] args) {
? Circle cir1 = new Circle(5);
? System.out.println("圓的面積為:" + cir1.area());
? System.out.println("圓的周長為:" + cir1.perimeter());
? Cylinder cy1 = new Cylinder(10, 15);
? System.out.println("圓柱體的表面積為:" + cy1.area());
? System.out.println("圓柱體的體積為:" + cy1.volume());
}
}
上面是我寫的代碼,下圖是執(zhí)行結(jié)果,麻煩看一下,是否可以。
import?java.util.Scanner;
interface?JSolidFigure?{
//表面積
void?SurfaceArea();
//體積
void?Volume();
}
//圓柱
class?Cylinder?implements?JSolidFigure{
@Override
public?void?SurfaceArea()?{
Scanner?sc=new?Scanner(System.in);
System.out.println("請輸入半徑:");
double?r=sc.nextDouble();
System.out.println("請輸入高:");
double?h=sc.nextDouble();
double?surfaceArea=2*Math.PI*r*r+2*Math.PI*r*h;
System.out.println("表面積:"+surfaceArea);
}
@Override
public?void?Volume()?{
Scanner?sc=new?Scanner(System.in);
System.out.println("請輸入半徑:");
double?r=sc.nextDouble();
System.out.println("請輸入高:");
double?h=sc.nextDouble();
double?volume=Math.PI*r*r*h;
System.out.println("體積:"+volume);
}
}
//圓錐
class?Cone?implements?JSolidFigure{
@Override
public?void?SurfaceArea()?{
}
@Override
public?void?Volume()?{
}
}
//球
class?Ball?implements?JSolidFigure{
@Override
public?void?SurfaceArea()?{
}
@Override
public?void?Volume()?{
}
}
//長方
class?Rectangular?implements?JSolidFigure{
@Override
public?void?SurfaceArea()?{
}
@Override
public?void?Volume()?{
}
}
//測試
public?class?SolidFigure{
public?static?void?main(String[]?args)?{
//多態(tài)
JSolidFigure?jsf=new?Cylinder();
System.out.println("下面求圓柱表面積+++++++++++++++++++++++++++++++");
jsf.SurfaceArea();
System.out.println("下面求圓柱體積+++++++++++++++++++++++++++++++++");
jsf.Volume();
}
}
圓柱體體積公式:V=πr2h。
java中的Math類中提供了π常量:
public static final double PI = 3.14159265358979323846;可以直接使用。
求平方,可以使用Math.pow(r, 2)方法完成,也可以寫成r*r。
指定圓柱的半徑和高度,即可求出體積。
public class Cylinder {
// 半徑(考慮精度問題,使用double)
private double radius;
private double height;
public Cylinder(double radius, double height) {
super();
this.radius = radius;
this.height = height;
}
// 獲取當(dāng)前圓柱對象的體積
public double getVolume() {
return getVolume(radius, height);
}
// 提供計(jì)算圓柱體積的通用方法。
public static double getVolume(double radius, double height) {
return Math.PI / 2 * Math.pow(radius, 2) * height;
}
public double getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public static void main(String[] args) {
// radius = 1.5, height = 5;
// 1、
Cylinder cylinder = new Cylinder(1.5f, 5);
System.out.println(cylinder.getVolume());
// 2、
System.out.println(Cylinder.getVolume(1.5f, 5));
}
}
package Area;
// 圓
public class circle {
// 半徑
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
// 面積計(jì)算
public double getArea(double r){
this.r=r;
return r*r*3.14;
}
}
package Area;
// 圓柱體
public class circularCylinder extends circle{
// 高度
private double h;
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
// 體積
public void getValumn(double r,double h){
this.h=h;
System.out.println(this.getArea(r)*h);
}
}
package Area;
import java.util.Scanner;
// 主程序
public class testArea {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
circle c = new circle();
circularCylinder cc = new circularCylinder();
System.out.println("請輸入圓的半徑:");
// 手動(dòng)設(shè)置圓半徑
c.setR(scanner.nextDouble());
System.out.println("請輸入圓柱體高度:");
// 設(shè)置圓柱體的高
cc.setH(scanner.nextDouble());
System.out.print("圓面積為:"+c.getArea(c.getR()));
System.out.print("相應(yīng)的圓柱體體積為:");
cc.getValumn(c.getR(),cc.getH());
}
}
class Cylinder extends Circle
{
double PI=super.getπ();
double r=super.getR();
int h=5;
//定義圓柱體的高
public double getArea()
//求圓柱體表面積
{
return 2*PI*r*r+2*PI*r*h;
}
public double getSize2()
//求 圓柱體體積
{
return PI*r*r*h;
}
}