使用ArrayList输出三角形的周长

时间:2014-04-02 19:54:36

标签: java class arraylist

我试图编写一个接受三角形不同边的程序,进行错误检查以确保它们有效,然后使用ArrayList输出它们。

我希望它看起来像这样...

"三角形t1的周长为24。"

"三角形t2的周长为30"

我认为ArrayList是我的主要问题(可能还有更多问题)。下面,我只是尽力实现ArrayList,但显然无法得到它。另外,我如何编写/更改我的toString函数,以便它还包含在main中创建的对象的名称(即t1,t2等)?

感谢您的期待!

import java.util.*;

public class Tri {

public static void main(String []args){

    Triangle t1 = new Triangle(7, 5, 4);
    Triangle t2 = new Triangle(9, 6, 1);

    System.out.println(t1.perimeter());
    System.out.println(t2.perimeter());

    ArrayList<Triangle>allTriangles = new ArrayList<Triangle>();

}

public static double totalPerimeter( ArrayList<Triangle>a ){

    System.out.println(Arrays.toString( a.toArray()));

    for( int i = 0; i < a.length; i++){

        System.out.println( a.perimeter[i]);

    }
}


class Triangle{

    public Triangle( double a, double b, double c ){

        this.sideA = a;
        this.sideB = b;
        this.sideB = c;

        if (checkSides() == true){}
    }

    public double getA(){
        return sideA;
    }

    public double getB(){
        return sideB;
    }

    public double getC(){
        return sideC;
    }

    public Triangle setA( double a ){

        sideA = a;
        return this;

    }

    public Triangle setB( double b ){

        sideB = b;
        return this;

    }
    public Triangle setC( double c ){

        sideC = c;
        return this;
    }

    public String toString(){

        return "Perimeter of triangle is " + perimeter;
    }

    public double perimeter(){

        if (checkSides() == true)

        perimeter = (sideA+sideB+sideC);
        return perimeter;

    }

private boolean checkSides(){

            if (!(sideA+sideB>sideC) && (sideA+sideC>sideB) && (sideB+sideC>sideA)){
                die("Not valid sides of triangle.");
                return false;
            }

            else return true;

        }

        public void die( String msg ){
            System.err.println( "\nFatal error: " + msg );
            System.exit( 1 );
        }

        private double perimeter;
        private double sideA;
        private double sideB;
        private double sideC;

}

3 个答案:

答案 0 :(得分:1)

totalPerimeter函数中:

  1. 由于a属于ArrayList类型,因此它没有length属性。然而 具有size()方法。参考:http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

  2. 由于a的类型为ArrayList,因此它没有perimeter数组字段。您是否意味着像a.get(i).perimeter()

答案 1 :(得分:1)

以下是您的代码:

        import java.util.*;

        public class Tri {

        public static void main(String []args){

            Triangle t1 = new Triangle(7, 5, 4);
            Triangle t2 = new Triangle(9, 6, 1);

            System.out.println(t1.perimeter());
            System.out.println(t2.perimeter());

            ArrayList<Triangle>allTriangles = new ArrayList<Triangle>();
            allTriangles.add(t1);
            allTriangles.add(t2);

            System.out.println(totalPerimeter(allTriangles));

        }

        public static double totalPerimeter( ArrayList<Triangle>a ){

            double tp = 0.0;

            for(Triangle t : a) {
                System.out.println( "peri : " +  t.perimeter());
                tp += t.perimeter();

            }

            return tp;

        //    for( int i = 0; i < a.length; i++){
        //
        //        System.out.println( a.perimeter[i]);
        //
        //    }
        }


        static class Triangle{

            public Triangle( double a, double b, double c ){

                this.sideA = a;
                this.sideB = b;
                this.sideB = c;

                if (checkSides() == true){}
            }

            public double getA(){
                return sideA;
            }

            public double getB(){
                return sideB;
            }

            public double getC(){
                return sideC;
            }

            public Triangle setA( double a ){

                sideA = a;
                return this;

            }

            public Triangle setB( double b ){

                sideB = b;
                return this;

            }
            public Triangle setC( double c ){

                sideC = c;
                return this;
            }

            public String toString(){

                return "Perimeter of triangle is " + perimeter;
            }

            public double perimeter(){

                if (checkSides() == true)

                perimeter = (sideA+sideB+sideC);
                return perimeter;

            }

        private boolean checkSides(){

                    if (!(sideA+sideB>sideC) && (sideA+sideC>sideB) && (sideB+sideC>sideA)){
                        die("Not valid sides of triangle.");
                        return false;
                    }

                    else return true;

                }

                public void die( String msg ){
                    System.err.println( "\nFatal error: " + msg );
                    System.exit( 1 );
                }

                private double perimeter;
                private double sideA;
                private double sideB;
                private double sideC;
        }
        }

答案 2 :(得分:1)

第一个问题:

  

我认为ArrayList是我的主要问题(可能还有更多   问题)。下面,我只是尽力实现ArrayList,但是   显然无法得到它。

好吧,您似乎错过了return语句,并且在totalPerimeter(...)方法中存在逻辑问题。您需要确保在变量中添加总周长,然后返回该变量。此外,ArrayList使用方法size()作为列表的长度而不是length。此外,您使用get(...)方法访问ArrayList对象。

public static double totalPerimeter( ArrayList<Triangle>a ){

    double total = 0.0; //give default value of 0

    for( int i = 0; i < a.size(); i++){

        total = total + a.get(i).perimeter(); //add up total

    }
    return total;//return it back!
}

第二个问题:

  

另外,我将如何编写/更改我的toString函数,以便它可以   还包含在main中创建的对象的名称(即t1,t2,   等)?

我认为你不能那样做。但是,您可以将String传递给构造函数:

private String name;

public Triangle( double a, double b, double c, String variableName){

    this.sideA = a;
    this.sideB = b;
    this.sideB = c;
    this.name = variableName;

    if (checkSides() == true){}
}

然后在main

Triangle t1 = new Triangle(7, 5, 4, "t1");
Triangle t2 = new Triangle(9, 6, 1, "t2");

然后,您可以更新toString(...)方法并访问变量name

注意:最后要注意的是,当您checkSides()并返回false时。您应throw Exception或向用户发出警告(例如打印出“警告:无效方”)。