program gives me an error at the RectangleOne.calculateArea() and RectangleTwo.c
ID: 3627730 • Letter: P
Question
program gives me an error at the RectangleOne.calculateArea() and RectangleTwo.calculateArea()public class Rectangle
{
private double length;
private double width;
Program gives me an error at the MyRectangleClassProgram when it gets to
public void setLength( double length )
{
this.length = length;
}
public void setWidth( double width )
{
this.width = width;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double calculatePerimeter( double length, double width )
{
return 2 * length + 2 * width;
}
public double calculateArea( double length, double width )
{
return length * width;
}
} // end of Rectangle class
public class MyRectangleClassProgam
{
public static void main( String[] args )
{
Rectangle rectangleOne = new Rectangle();
Rectangle rectangleTwo = new Rectangle();
rectangleOne.setLength( 4.0 );
rectangleOne.setWidth( 6.0 );
System.out.printf( "Area is %.2f and Perimeter is %.2f ",
rectangleOne.calulateArea(), rectangleOne.calculatePerimeter() );
rectangleTwo.setLength( 9.0 );
rectangleTwo.setLength( 7.0 );
System.out.printf ( "Area is %.2f and Perimeter is %.2f ",
rectangleTwo.calculateArea, rectangleTwo.calculatePerimeter() );
System.exit(0);
}
}
Explanation / Answer
You had a few errors, including spelling mistakes (easy fixes), etc. Also, you defined the calculateArea method to have parameters of length and width, but you called it with no parameters. I have fixed your code and attached it below. public class Rectangle { private double length; private double width; public void setLength( double length ) { this.length = length; } public void setWidth( double width ) { this.width = width; } public double getLength() { return length; } public double getWidth() { return width; } public double calculatePerimeter() { return 2 * length + 2 * width; } public double calculateArea() { return length * width; } } public class MyRectangleClassProgam { public static void main( String[] args ) { Rectangle rectangleOne = new Rectangle(); Rectangle rectangleTwo = new Rectangle(); rectangleOne.setLength( 4.0 ); rectangleOne.setWidth( 6.0 ); System.out.printf( "Area is %.2f and Perimeter is %.2f ", rectangleOne.calculateArea(), rectangleOne.calculatePerimeter() ); rectangleTwo.setLength( 9.0 ); rectangleTwo.setWidth( 7.0 ); System.out.printf ( "Area is %.2f and Perimeter is %.2f ", rectangleTwo.calculateArea(), rectangleTwo.calculatePerimeter() ); System.exit(0); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.