實驗四 (1):定義一個形狀類(Shape)方法:計算周長,計算面積


(1)定義一個形狀類(Shape)方法:計算周長,計算面積
子類:
矩形類(Rectangle) :額外的方法:differ() 計算長寬差
圓形類(Circle)
三角形類(Triangle)
正方形類(Square) 矩形的子類
生成幾個不同的形狀對象,放在一個Shape類型的數組里,分別求每個形狀的周長和面積。如果形狀對象是一個矩形,且不是正方形,則計算長寬差。

  1 package com.tiger.practice;
  2 
  3  class Shape {
  4     
  5     public double length(){
  6         return 0;
  7     }
  8     
  9     public double area() {
 10         return 0;
 11     }
 12 }
 13  
 14  class Rectangle extends Shape {
 15 
 16         private double width;
 17         private double height;
 18 
 19 
 20         public Rectangle(double height,double width) {
 21             this.height=height;
 22             this.width = width;
 23         }
 24 
 25         @Override
 26         public double length() {
 27             // TODO Auto-generated method stub
 28             return 2*(width+height);
 29         }
 30 
 31         @Override
 32         public double area() {
 33             // TODO Auto-generated method stub
 34             return width*height;
 35         }
 36 
 37         public double differ() {
 38             return Math.abs(height-width);
 39         }
 40 
 41     }
 42  
 43  class Square extends Rectangle {
 44     
 45      public double edge;
 46  
 47      public Square(double edge) {
 48          super(edge,edge);
 49          setEdge(edge);
 50          // TODO Auto-generated constructor stub
 51      }
 52 
 53      public double getEdge() {
 54          return edge;
 55      }
 56  
 57      public void setEdge(double edge) {
 58          this.edge = edge;
 59      }  
 60  }
 61  
 62  class Triangle extends Shape {
 63         private double a;
 64         private double b;
 65         private double c;
 66         
 67         public Triangle(double a,double b,double c) {
 68             this.a= a;
 69             this.b = b;
 70             this.c = c;
 71         }
 72 
 73         @Override
 74         public double length() {
 75             // TODO Auto-generated method stub
 76             return a+b+c;
 77         }
 78 
 79         @Override
 80         public double area() {
 81             double p= (a+b+c)/2;
 82             // TODO Auto-generated method stub
 83             return Math.sqrt(p*(p-a)*(p-b)*(p-c));
 84         }
 85     }
 86  
 87  class circle extends Shape{
 88      private double r;
 89      public circle(double r){
 90          this.r = r;
 91      }
 92      public double length(){
 93          return 3.1415*2*r;
 94      }
 95      public double area(){
 96          return 3.1415*r*r;
 97      }
 98      
 99  }
100 
101 public class shapes {
102 
103     /**
104      * @param args
105      */
106     public static void main(String[] args) {
107         // TODO Auto-generated method stub
108         Shape[] shapes= {
109                 new Rectangle(5,10),
110                 new circle(5),
111                 new Square(3),
112                 new Triangle(3, 4, 5)
113             };
114         for(int i=0;i<shapes.length;i++) {
115             double length=shapes[i].length();
116             double area=shapes[i].area();
117             if(shapes[i]instanceof circle) {
118                 System.out.println("Shape("+(i+1)+"): "+
119                         "length"+String.format("%.4f", length)+",area"
120                         +String.format("%.4f", area));
121             }
122             else {
123                         System.out.println("Shape("+(i+1)+"): "+
124                             "length"+length+",area"+area);
125                     if(shapes[i]instanceof Rectangle) {
126                         Rectangle r=(Rectangle)shapes[i];
127                         System.out.println("長寬差是:   "+r.differ());
128                     }
129             }
130         }
131     }
132 
133 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 30.編寫一個Shape類,具有屬性:周長和面積; 定義其子類三角形和矩形,分別具有求周長的方法。 定義主類E,在其main方法中創建三角形和矩形類的對象, 並賦給Shape類的對象a、b,使用對象a、b來測試其特性。 編寫一個Shape類,具有屬性:周長和面積; 定義其子類三角形和矩形,分別具有求周長的方法。 定義主類E,在其main方法中創建三角形和矩形類的對象, 並賦給Shape類的對象a、b,使用對象a、b來測試其特性。 java計算圓的面積和周長 編寫一個Java程序,計算半徑為3.0的圓周長和面積並輸出結果。把圓周率π定義為常量,半徑定義為變量,然后進行計算並輸出結果。 初遇C#:一個簡單的小程序(圓形周長,面積計算器) 按要求編寫一個Java應用程序: (1)定義一個類,描述一個矩形,包含有長、寬兩種屬性,和計算面積方法。 (2)編寫一個類,繼承自矩形類,同時該類描述長方體,具有長、寬、高屬性, 和計算體積的方法。 (3)編寫一個測試類,對以上兩個類進行測試,創建一個長方體,定義其長、 寬、高,輸出其底面積和體積。 /*練習—02|2. 輸入一個圓半徑(r)當r>=0時,計算並輸出圓的面積和周長,否則,輸出提示信息。*/ 作業: 1.8(圓的面積和周長)編寫程序,使用以下的公式計算並顯示半徑為5.5的圓的面積和周長。 實驗3-8 輸出三角形面積和周長 (15分) 25.按要求編寫一個Java應用程序: (1)編寫一個矩形類Rect,包含: 兩個屬性:矩形的寬width;矩形的高height。 兩個構造方法: 1.一個帶有兩個參數的構造方法,用於將width和height屬性初化; 2.一個不帶參數的構造方法,將矩形初始化為寬和高都為10。 兩個方法: 求矩形面積的方法area() 求矩形周長的方法perimeter() (2)通過繼承Rect類編寫一個具有
 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM