0

PROGRAM:Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A string data field named color that specifies the color of a rectangle. Hypo-thetically, assume that all rectangles have the same color. The default color is white. A no- arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. The accessor and mutator methods for all three data fields. A method named getArea() that returns the area of this rectangle. A method named getPerimeter() that returns the perimeter.

Draw the UML diagram for the class. Implement the class. Write a test program that creates two Rectangle objects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the second object. Assign color red to all Rectangle objects. Display the properties of both objects and find their areas and perimeters.


CODE:

import java.io.*;
import java.util.*;

public class Rectangle
{
	public static void main (String args[])
	{
		Rectangle box1 = new Rectangle();
		Rectangle box2 = new Rectangle(4, 40);
		Rectangle box3 = new Rectangle(3.5, 35.9);

		String Color = "Red";

		box1.setColor(Color);
		box2.setColor(Color);
		box3.setColor(Color);

		box1.getColor();
		box2.getColor();
		box3.getColor();

		System.out.println("The perimeter of the first box is: " + box1.getPerimeter() + "\n");
		System.out.println("The perimeter of the second box is: " + box2.getPerimeter() + "\n");
		System.out.println("The perimeter of the third box is: " + box3.getPerimeter() + "\n");

		System.out.println("The area of the first box is: " + box1.getArea() + "\n");
		System.out.println("The area of the second box is: " + box2.getArea() + "\n");
		System.out.println("The area of the third box is: " + box3.getArea() + "\n");
	}

	private double height;
	private double width;
	private String color;

	public Rectangle()
	{
		height = 1;
		width = 1;
		color = "White";
	}


	public Rectangle (double height, double width, String color)
	{
		this.height = height;
		this.width = width;
		this.color = "White";
	}

	public void setHeight (double height)
	{
		this.height = height;
	}

	public double getHeight()
	{
		return this.height;
	}

	public void setWidth (double width)
	{
		this.width = width;
	}

	public double getWidth()
	{
		return this.width;
	}

	public void setColor (String color)
	{
		this.color = color;
	}

	public String getColor()
	{
		System.out.println("Color is: " + color + "\n");
		return this.color;
	}

	public double getPerimeter()
	{
		return 2 * (height + width);
	}

	public double getArea()
	{
		return height * width;
	}


}

ERROR:
C:\Users\Kamaldeep\Documents\Java programs\Summer Java\Rectangle.java:9: cannot find symbol
symbol : constructor Rectangle(int,int)
location: class Rectangle
Rectangle box2 = new Rectangle(4, 40);
^
C:\Users\Kamaldeep\Documents\Java programs\Summer Java\Rectangle.java:10: cannot find symbol
symbol : constructor Rectangle(double,double)
location: class Rectangle
Rectangle box3 = new Rectangle(3.5, 35.9);
^
2 errors

Tool completed with exit code 1

I'm not sure how to fix these two errors that i'm getting, what do i need to add? Could someone please help me?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by tong1
0

The error message shows:
(1) in line 9 you call a non-existing constructor of 2 arguments. As your teacher requests, you should define "a constructor that creates a rectangle with the specified width and height" only. You did not do so. Instead you have defined a constructor with 3 arguments:width,height, and color.
(1) Similarly in line 10 you call the non-existing constructor with 2 double parameters again.
What you should do is to define (supplement) the constructor that creates a rectangle with the specified width and height only for the class Rectangle.

Edited by tong1: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.