How to tackle a homework prgram that reads three sides for a triangle and computes the area if the input is valid?

前端 未结 3 373
半阙折子戏
半阙折子戏 2021-01-29 07:06

Here is my task:

Create a class named MyTriangle that contains the following two methods:

/** Return true if the sum of any two sides is *         


        
相关标签:
3条回答
  • 2021-01-29 07:52

    So you write the following: if the input is valid, show error message. Otherwise (else) compute area. You should just swap your if and else parts! Your program never calls the area() method if you are calling it with points for a valid triangle. Moreover, you never call the isValid() method. You assign true to the variable, and then check it in the next line, but the method that actually checks it has never been called.

    ALSO you need side variables for isValid(), but you only compute them in the area() method. You should compute them right after you get the points.

    0 讨论(0)
  • 2021-01-29 07:55

    When you call the area method (which you are not calling per @mashaned 's answer)

    area(side1, side2, side3);
    

    You are calling it with variables that have only been initialized and not set. side1, side2, and side3 do not have a value when you call area.

    You should either create a class for side variables so that you can pass in the x and y values like this:

    area(new Side(x1, y1), new Side(x2, y2), new Side(x3, y3));
    

    Or

    You should change the area method to accept x1, y1, x2, y2, x3, y3 instead of sides since you are computing sides in the area method like:

    public static double area(double x1, double y1, double x2, double y2, double x3, double y3) {
    

    Additionally, you are not doing anything with the area returned by the area method when you call it. I suggest something like:

    System.out.println("Area " + String.valueOf([INSERT VERSION OF AREA METHOD CALL OF YOUR CHOICE]));
    

    The first answer is also right about the isValid variable. You are not using the isValid() method for validation.

    You should use it in similar fashion to area in terms of what is passed in.

    An example class (roughly done) might be like the following:

    public class Side(){
            double x = 0;
            double y = 0;
    
            public Side(double x, double y){
                this.x = x;
                this.y = y;
            }
    
            public double getX(){
                return this.x;
            }
    
            public double getY(){
                return this.y;
            }
    
            public void setX(double x){
                this.x = x;
            }
    
            public void setY(double y){
                this.y = y;
            }
        }
    

    You may want to consider adding this method and removing the similar code from your area method:

    public void computeSides(double x1, double y1, double x2, double y2, double x3, double y3){
            side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
            side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
            side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
        }
    

    Then when you go to call isValid() or area() you just make sure you call computeSides() first and then the side variables will have values and both should work.

    0 讨论(0)
  • 2021-01-29 08:02

    You are just declaring a variable called "isValid" and setting it equal to true. You need to instead calculate the length of the sides before you check if the input is valid. Then you call the isValid function by calling

    isValid(side1, side2, side3);

    0 讨论(0)
提交回复
热议问题