/** Write a class Square whose constructor receives the length of the sides.Then supply methods to compute thearea and perimeter of the square.Also compute the length of the diagonal using the Pythagorean Theorem.Use the following class as a template
来源:学生作业帮助网 编辑:作业帮 时间:2024/10/05 11:14:32
/** Write a class Square whose constructor receives the length of the sides.Then supply methods to compute thearea and perimeter of the square.Also compute the length of the diagonal using the Pythagorean Theorem.Use the following class as a template
/** Write a class Square whose constructor receives the length of the sides.
Then supply methods to compute the
area and perimeter of the square.
Also compute the length of the diagonal using the Pythagorean Theorem.
Use the following class as a template your tester class
*/
public class Square {
\x05 public Square(double sideLength)
\x05 {
\x05\x05Square mySquare = new Square();
\x05 }
\x05 public double getArea()
\x05 {
\x05\x05double area = sideLength * sideLength;
\x05\x05return area;
\x05 }
\x05 public double getPerimeter()
\x05 {
\x05\x05double perimeter = 4 * sideLength;
\x05\x05return perimeter;
\x05 }
\x05 public double getDiagonalLength()
\x05 {
\x05\x05double diagonal = Math.sqrt(sideLength * sideLenght + sideLength * sideLength);
\x05\x05return diagonal;
\x05 }
}
Square mySquare = new Square();
error:constructor Square in class Square cannot be applied to given types;
double area = sideLength * sideLength;
error:cannot find symbol
double perimeter = 4 * sideLength;
error:cannot find symbol
double diagonal = Math.sqrt(sideLength * sideLenght + sideLength * sideLength);
error:cannot find symbol
anyone can help?
thx sooo much
我另外两个程序也都有 cannotfindsymbol的问题 到底应该怎样啊
/** Write a class Square whose constructor receives the length of the sides.Then supply methods to compute thearea and perimeter of the square.Also compute the length of the diagonal using the Pythagorean Theorem.Use the following class as a template
如果你的程序这么写,那么很明显你的后面的方法中使用的sideLength没有定义
你在外面加个double sideLength;就可以了.因为你在第一个构造方法中定义了double sideLength它只能在它那个方法里面用,所以你必须定义一个double sideLength为全局变量,后面的就不会报错了