java案例_面向对象编程_Stool
生活随笔
收集整理的這篇文章主要介紹了
java案例_面向对象编程_Stool
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個小型家具廠主要生產小凳子,形狀由上中下三個圓柱體組成,如下圖所示。參照圖定義類Circle,Cylinder, Stool,要求屬性必須私有,根據類的數據成員定義類的構造方法(參數為數值類型》。定義測試類Program和主方法,測試計算2件凳子和3個圓柱體需要多少公斤油漆和立方米木料,假設園柱體的半徑和高度均以米為單位,每平方米需要油滾О.1公斤,每立方米體積需要1.2倍的木料。
項目結構:
運行結果:
源代碼:
package com.serein.stool_opp_bata;public class Program {public static void main(String[] args) {Cylinder cylinder1 = new Stool(0.5,0.1);Cylinder cylinder2 = new Stool(0.2,0.4);Cylinder cylinder3 = new Stool(0.8,0.2);Stool stool4 = new Stool(); //無參構造凳子對象,以便后續調用凳子對象專有求面積體積方法if (cylinder1 instanceof Stool) {Stool stool1 = (Stool) cylinder1;System.out.println("第1個圓柱所需要的油漆是:" + stool1.getArea() + "公斤");System.out.println("第1個圓柱所需要的木料是:" + stool1.getVolume() + "立方米");}if (cylinder2 instanceof Stool) {Stool stool2 = (Stool) cylinder2;System.out.println("第2個圓柱所需要的油漆是:" + stool2.getArea() + "公斤");System.out.println("第2個圓柱所需要的木料是:" + stool2.getVolume() + "立方米");}if (cylinder3 instanceof Stool) {Stool stool3 = (Stool) cylinder3;System.out.println("第3個圓柱所需要的油漆是:" + stool3.getArea() + "公斤");System.out.println("第3個圓柱所需要的木料是:" + stool3.getVolume() + "立方米");}System.out.println("----------------------------");System.out.println("2個凳子需要的油漆是:" + stool4.getStoolArea(0.5, 0.1, 0.2, 0.4, 0.8, 0.2) *2 + "公斤");System.out.println("2個凳子需要的木料是:" + stool4.getStoolVolume(0.5, 0.1, 0.2, 0.4, 0.8, 0.2) *2 + "立方米");} } package com.serein.stool_opp_bata;public class Circle {private double radius;public final static double PI = 3.14159;public double getCircumference(){return 2*PI*radius;}public double getArea(){return PI*radius*radius;}public Circle() {}public Circle(double radius) {this.radius = radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;} } package com.serein.stool_opp_bata;public class Cylinder extends Circle {private double height;@Overridepublic double getArea() {return super.getArea()*2 + getCircumference()*height;}public double getVolume(){return super.getArea()*height;}public Cylinder() {}public Cylinder(double height) {this.height = height;}public Cylinder(double radius, double height) {super(radius);this.height = height;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;} } package com.serein.stool_opp_bata;public class Stool extends Cylinder{@Overridepublic double getArea(){return super.getArea() * 0.1;}@Overridepublic double getVolume(){return super.getVolume() * 1.2;}public double getStoolVolume(double topRadius, double topHeight,double middleRadius, double middleHeight,double bottomRadius, double bottomHeight) {return (PI * topRadius * topRadius * topHeight + PI * middleRadius * middleRadius * middleHeight+ PI * bottomRadius * bottomRadius * bottomHeight) * 1.2;}/***計算凳子表面積時,必須記得圓柱體拼接,會有4個中間圓柱底面被遮住,不能單純相加*/public double getStoolArea(double topRadius, double topHeight,double middleRadius, double middleHeight,double bottomRadius, double bottomHeight){return (PI * topRadius *topRadius *2 + 2 * PI *topRadius * topHeight+ 2 * PI *middleRadius * middleHeight+ PI * bottomRadius *bottomRadius *2 + 2 * PI *bottomRadius * bottomHeight- PI * middleRadius *middleRadius *2) * 0.1;}public Stool() {}public Stool(double height) {super(height);}public Stool(double radius, double height) {super(radius, height);}}感謝大家觀看!歡迎點贊評論轉發哦!
總結
以上是生活随笔為你收集整理的java案例_面向对象编程_Stool的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACdream 之ACfun 题解
- 下一篇: c语言的字符串不是存放,在C语言中,字符