-1

下面我有一个我写的小程序来计算形状的面积......

我的问题是这样做的正确方法,一个朋友做了类似的事情并且有多个从主要形状继承的形状。哎呀?我的可以吗,因为我只会问形状的面积而不会更多?我将如何改变它以使其更加面向对象?

主程序 /////

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.");

        }


    }

}

面积计算器.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;


    }
}
4

2 回答 2

1

从单个基类或接口继承多个类绝对是一个更好的设计。使用类来封装给定的功能或对象(在这种情况下triangle等)square。此外,当您有多个共享某些功能的类时,最好将其提取为通用接口以实现更好的抽象级别。

于 2013-01-21T16:34:33.473 回答
0

简单的答案是使用这样的“形状”界面

interface Shape {

double[] dimensions;
double calcArea();
}

并让你所有的形状都实现这个接口。

class Circle implements Shape {
...
}

并为每个形状实现不同的 calcArea() 方法

在你的跑步者中,你初始化一个圆圈、盒子等......

当你需要 area 时,你不必关心它后面实际上是哪个形状,只需调用 shape.calcArea() 方法,它就会找到正确的那个。

于 2013-01-21T16:40:13.587 回答