public class RectangleObjects { public static void main(String[] args) { //Create a rectangle with width of 4 and height of 40 SimpleRectangle rectangle1 = new SimpleRectangle(); System.out.println("The width of the rectangle is " + rectangle1.width + ", height is " + rectangle1.height + ", area is " + rectangle1.getArea() + ", and perimeter is " + rectangle1.getPerimeter()); //Create a rectangle with width of 3.5 and height of 35.9 SimpleRectangle rectangle2 = new SimpleRectangle(3.5, 35.9); System.out.println("The width of the rectangle is " + rectangle2.width + ", height is " + rectangle2.height + ", area is " + rectangle2.getArea() + ", and perimeter is " + rectangle2.getPerimeter()); } } class SimpleRectangle { double width; double height; //Construct a rectangle with width of 4 and height of 40 SimpleRectangle() { width = 4; height = 40; } SimpleRectangle(double newWidth, double newHeight) { width = newWidth; height = newHeight; } //Return the area for rectangle objects double getArea() { return 2 * width * 2 * height; } //Return the perimeter for rectangle objects double getPerimeter() { return 2 * width + 2 * height; } //Set a new width and height for this rectangle void setWidth(double newWidth, double newHeight) { width = newWidth; height = newHeight; } }
Output:
The width of the rectangle1 is 4.0, height is 40.0, area is 160.0, and perimeter is 88.0
The width of the rectangle2 is 3.5, height is 35.9, area is 125.64999999999999, and perimeter is 78.8
Can someone show me how to limit the output to 2 decimal places for 125.64999999999999 to 125.65?
--- Update ---
Never mind. I got everything fixed. Please close and/or delete this post, thank you.