When I get run my Rectangle.java, I can get the inputted length and width in the Rectangle, but when I attempt to calculate the area/perimeter with a getter I get a zero as the result
I attempted to add and remove the setters, put in the getter/setter methods within the getArea and getPerimeter, but nothing seems to work
//Code provided by the teacher as a template
Rectangle temp = new Rectangle();
temp.print();
System.out.println();
temp.setLength(2.5);
temp.setWidth(3.0);
//Consider how your rectangle will change after setting the length and width to specific values.
temp.print();
System.out.println();
Rectangle r = new Rectangle(3.5,2);
r.print();
//My Class
public class Rectangle
{
private static double length;
private static double width;
private static double perimeter;
private static double area;
public Rectangle(double length, double width)
{
setLength(length);
setWidth(width);
}
public Rectangle()
{
}
public static double getLength()
{
return length;
}
public static void setLength(double length)
{
Rectangle.length = length;
}
public static double getWidth()
{
return width;
}
public static void setWidth(double width)
{
Rectangle.width = width;
}
public static double getPerimeter(double length, double width)
{
return 2*width+2*length;
}
public static double getArea(double length, double width)
{
area= getLength()*getWidth();
return length*width;
}
public static String print()
{
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
}
No error messages.
Output:
This rectangle has a length of 0.0 and a width of 0.0 The area of the rectangle is: 0.0 The perimeter of the rectangle is: 0.0
This rectangle has a length of 2.5 and a width of 3.0 The area of the rectangle is: 0.0 The perimeter of the rectangle is: 0.0
This rectangle has a length of 3.5 and a width of 2.0 The area of the rectangle is: 0.0 The perimeter of the rectangle is: 0.0
You need to change the print method and call perimeter and area method so that those variables get the required values
public static String print()
{
getPerimeter(length,width);
getArea(length,width);
String Rectangle = new String();
System.out.println("This rectangle has a length of "+length+" and a width of "+width);
System.out.println("The area of the rectangle is: "+ area);
System.out.println("The perimeter of the rectangle is: "+ perimeter);
return Rectangle;
}
I would suggest not to use static key words as the values will not be bound to the object