缺少关键点来计算三角形的面积

时间:2013-05-28 14:47:47

标签: java

import javabook.*;


class Triangle
{
    // DATA
    //............................................................

    //Private Variables
        private double theBase;
        private double theHeight;
        private double theArea;                                     //base and height = Area is enough calculation

    // CONSTRUCTORS
    //............................................................  // same name as the class and the file name
    public Triangle()                                               
    {
        this.theBase=0;
        this.theHeight=0;
        this.theArea=0;
    }

    public Triangle( OutputBox oBox, MainWindow mWindow )           
    {
        this.theBase=0;
        this.theHeight=0;                                           this.theArea=0;                                             //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’.
    }                                           


    public void calculateArea ()        
    {                                                                                              
        InputBox iBox = new InputBox(mWindow);
        this.theBase = iBox.getDouble("Please enter the length of the base of the triangle ");
        this.theHeight = iBox.getDouble("Please enter the height of the triangle");
        computeArea();
        oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea );
    }

    // METHODS - behaviours
    //............................................................
    public void computeArea()
    {
        this.theArea = (this.theBase / 2 ) * this.theHeight;        
                                                            }                                                               

    // METHODS - gets (accessors)  and sets (mutators)
    //............................................................

    //length
    public void setThebase(double base)
    {
           this.theBase= base;
    }

    public double getTheBase()
    {
           return(this.theBase);
    }

    //breath
    public void setTheHeight(double height)
    {
           this.theHeight= height;
    }

    public double getTheHeight()
    {
           return(this.theHeight);
    }

    //area
    public double getTheArea()
    {
           return(this.theArea);
    }
}

我在 this.theBase = iBox.getDouble(“请输入三角形底边的长度”)之上或之上缺少一些东西; 任何人都可以指出它取决于我的'我把它留了出来?因为我正在学习java并且一定会犯错误,所以请保持温和。

修复伪造{character:

后,这是修改后的错误消息
]./Triangle.java:42: cannot find symbol
symbol  : variable mWindow
location: class Triangle
        InputBox iBox = new InputBox(mWindow);
                                     ^
./Triangle.java:46: cannot find symbol
symbol  : variable oBox
location: class Triangle
        oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea );

更新了4个错误:

./Triangle.java:36: cannot find symbol
symbol  : variable oBox
location: class Triangle
        this.oBox = oBox;
            ^
./Triangle.java:37: cannot find symbol
symbol  : variable mWindow
location: class Triangle
        this.mWindow = mWindow;                                     //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’.
            ^
./Triangle.java:47: cannot find symbol
symbol  : variable mwindow
location: class Triangle
        InputBox iBox = new InputBox(mwindow);
                                     ^
./Triangle.java:51: cannot find symbol
symbol  : variable oBox
location: class Triangle
        oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea );
        ^
Note: App.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errors
            ^

1 个答案:

答案 0 :(得分:3)

第一个}之后的所有代码都不属于某个方法,因此......您需要创建一个代码。像这样:

  public Triangle( OutputBox oBox, MainWindow mWindow ) {
    this.theBase=0;
    this.theHeight=0
    this.theArea=0;                                         
  }
  public void calculateArea() {                                           
    InputBox iBox = new InputBox(mWindow);
    this.theBase = iBox.getDouble("Please enter the length of the base of the triangle ");
    this.theHeight = iBox.getDouble("Please enter the height of the triangle");
    computeArea();
    oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea  );
  }
}

然后,您可以在制作calculateArea()对象后致电Triangle

如果您有任何问题或疑问,请与我们联系。


关于您的其他错误,原因是您没有存储mWindowoBox的值。变化:

  public Triangle( OutputBox oBox, MainWindow mWindow )           
    {
        this.theBase=0;
        this.theHeight=0;
        this.theArea=0;
        //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’.
    }   

  public Triangle( OutputBox oBox, MainWindow mWindow )           
    {
        this.theBase=0;
        this.theHeight=0;
        this.theArea=0;
        this.oBox = oBox;
        this.mWindow = mWindow;
        //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’.
    }   

并使用您的私有变量添加oBoxmWindow

请注意,如果未使用上面的构造函数,您将收到运行时错误。


import javabook.*;


class Triangle
{
    // DATA
    //............................................................

    //Private Variables
        private double theBase;
        private double theHeight;
        private double theArea;                                     //base and height = Area is enough calculation
        private OutputBox oBox;
        private MainWindow mWindow;

    // CONSTRUCTORS
    //............................................................  // same name as the class and the file name

    public Triangle( OutputBox oBox, MainWindow mWindow )           
    {
        this.theBase=0;
        this.theHeight=0;
        this.theArea=0;                                             //This is a proof that the area of any triangle is 1/2 b x h’ of a base and height and the ‘area of a triangle is half of the base times the height’.
        this.oBox = oBox;
        this.mWindow = mWindow;
    }                                           


    public void calculateArea ()        
    {                                                                                              
        InputBox iBox = new InputBox(this.mWindow);
        this.theBase = iBox.getDouble("Please enter the length of the base of the triangle ");
        this.theHeight = iBox.getDouble("Please enter the height of the triangle");
        computeArea();
        this.oBox.println(" The area of a triangle of base : " + this.theBase+ " and height : " + this.theHeight +" is equal to : "+ this.theArea );
    }

    // METHODS - behaviours
    //............................................................
    public void computeArea()
    {
        this.theArea = (this.theBase / 2 ) * this.theHeight;        
                                                            }                                                               

    // METHODS - gets (accessors)  and sets (mutators)
    //............................................................

    //length
    public void setThebase(double base)
    {
           this.theBase = base;
    }

    public double getTheBase()
    {
           return(this.theBase);
    }

    //breath
    public void setTheHeight(double height)
    {
           this.theHeight= height;
    }

    public double getTheHeight()
    {
           return(this.theHeight);
    }

    //area
    public double getTheArea()
    {
           return(this.theArea);
    }
}