2013-01-21 30 views
-1

下面我有一個小程序,我寫出了工作區域的形狀....面向對象的建議 - 我應該改變程序的運行方式嗎?

我的問題是這樣做的正確方法,一個朋友做了類似的,並有多個形狀,從主形狀繼承。 OOP?是我的,因爲我只會問一個形狀的區域,而不是更多?並且我將如何改變這個使它更加OO?

主要PROG /////

package areaprog; 

import java.util.Scanner; 
import java.util.InputMismatchException; 
public class Mainprog { 


public static void main (String [] args){ 

    //Area Menu Selection 
    System.out.println("What shape do you need to know the area of?\n" + 
    "1: Square?\n" + 
    "2: Rectangle?\n" + 
    "3: Triangle?\n" + 
    "4: Circle? \n" + 
    "5: Exit\n"  
    ); 

    //User input for menu 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Number: "); 

    //Menu syntax checking 
    while (!reader.hasNextDouble()) 
    { 
     System.out.println("Thats not a number you tool.\n"); 
     System.out.println("Now pick again\n" + 
       "1: Square?\n" + 
       "2: Rectangle?\n" + 
       "3: Triangle?\n" + 
       "4: Circle? \n" + 
       "5: Exit\n"  
       ); 

     reader.next(); //ask for next token  
    }    
     double input = reader.nextDouble(); 
     reader.nextLine(); 



    //Depending on user selection, depends on what method is called using switch. 
Scanner scan = new Scanner(System.in); 

    //Square selection and InputMismatch Exception 

try { 

    if (input == 1){ 
     System.out.println("What is a length of 1 side of the Square?\n"); 
      double s1 = scan.nextDouble(); 
      double SqAns = AreaCalculator.getSquareArea(s1); 
      System.out.println("The area of you square is: " + SqAns); 

        }  
    }   
catch (InputMismatchException e) 
    { 
    System.out.println("Why are you trying to be clever? use an interger"); 


    } 

    //Rectangle selection  
     if (input == 2){ 
     System.out.println("What is the width of your rectangle?.\n"); 
      double r1 = scan.nextDouble(); 
     System.out.println("What is the height of your rectangle?\n"); 
      double r2 = scan.nextDouble(); 
      double RecAns = AreaCalculator.getRectArea(r1, r2); 
     System.out.println("The area of your rectangle is: " + RecAns);  
     } 
    //Triangle selection 
    if (input == 3){ 
     System.out.println("What is the base length of the triangle?."); 
      double t1 = scan.nextDouble(); 
     System.out.println("What is the height of your triangle?"); 
      double t2 = scan.nextDouble(); 
      double TriAns = AreaCalculator.getTriArea(t1, t2); 
     System.out.println("The area of your triangle is " + TriAns); 
    } 
    //Circle selection 
    if (input == 4){ 
     System.out.println("What is the radius of your circle?."); 
      double c1 = scan.nextDouble(); 
      double CircAns = AreaCalculator.getCircleArea(c1); 
     System.out.println("The area of your circle is " + CircAns);  

    } 
    //Exit application 
    if (input == 5){ 
     System.out.println("Goodbye."); 

    } 


} 

    } 

AreaCalculator.java ////

package areaprog; 


public class AreaCalculator { 

public static double getRectArea(double width, double height) { 
    double aValue = width * height; 

    return aValue; 


} 

public static double getCircleArea(double radius){ 
    double PI = Math.PI; 
    double aValue = PI * Math.pow(radius, 2); 

    return aValue; 


} 

public static double getSquareArea(double side) { 
    double aValue = Math.pow(side, 2); 

    return aValue; 


} 

public static double getTriArea(double base , double height) { 
    double aValue = (base/2)* height; 

    return aValue; 


} 
} 
+0

你最好使用一個'枚舉'爲你的形狀。 – OldCurmudgeon

+0

@OldCurmudgeon爲什麼使用枚舉而不是類? –

+0

因爲它是一組類,它們都做類似的事情。例如。他們都需要一個'getArea'方法。 – OldCurmudgeon

回答

1

有多個類從單個基類或接口繼承絕對是一個更好的設計在這裏。使用類來封裝給定功能或對象(在這種情況下trianglesquare等。此外,當你有多個類共享一些功能,更好地提取出來作爲一個通用的接口來實現抽象的較好水平。

+0

對不起,即時通訊新編程沒有關係oop所以忍受着我:)但所有的公式都不一樣,所以我會從主類中演變成什麼? – tmcraig

+0

getArea方法,返回一個double。你將基礎實例和高級(對於三角形)傳遞給它的**構造函數**,因爲這將定義一個三角形(事實上,我認爲傳遞三個邊長是一個更好的選擇) –

+0

@tmcraig一個合同, getArea()'返回一個適合形狀的區域。這意味着您不需要檢查形狀類型,只需獲取其區域即可。 –

0

簡單的答案是使用這樣

interface Shape { 

double[] dimensions; 
double calcArea(); 
} 

和 '形' 的接口有你的形狀實現此接口。

class Circle implements Shape { 
... 
} 

,並實施不同的油杉()方法,爲每個形狀

在你的亞軍,你初始化一個圈,箱等...

當你需要面積,你不必在意它形狀實際上是在它後面,只需調用shape.calcArea()方法即可找到合適的形狀。

相關問題