Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. (Rectangle) Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following methods: public Rectangle(int x, int

1. (Rectangle) Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following methods:

public Rectangle(int x, int y, int width, int height)

Constructs a new rectangle whose top-left corner is specified by the given coordinates and with the given width and height. If width or height is negative, issue an error stating Illegal Argument and return a Rectangle object with the X and Y coordinates given and a 0 width and height.

public int getHeight()

Returns this rectangle's height.

public int getWidth()

Returns this rectangle's width.

public int getX()

Returns this rectangle's x-coordinate.

public int getY()

Returns this rectangle's y-coordinate.

public String toString()

Returns a string representation of this rectangle, such as

"Rectangle[x=1,y=2,width=3,height=4]"

2. Add the following accessor methods to your Rectangle class from the previous exercises:

public boolean contains(int x, int y)

public boolean contains(Point p)

Returns whether the given Point or coordinates lie inside the bounds of this Rectangle. The edges are included; for example, a rectangle with [x=2,y=5,width=8,height=10] will return true for any point from (2, 5) through (10, 15) inclusive.

For the Point class, use the class from Lab 8. Add accessor methods getX() and getY() to the Point class which are used to return the X and Y coordinates, respectively.

3. Add a method to Rectangle called center with the following signature:

public DoublePoint center();

This returns the center point of the Rectangle. The type of the return is a DoublePoint object. This is a special Point object that uses double values for its coordinates instead of integers.

4. Add a method to Rectangle called diagonalLength with the following signature that returns the length (a double) of the diagonal from the upper left corner of the rectangle to the lower right.

public double diagonalLength()

5. The class Circle is defined as follows:

public class Circle {

public Point center;

public double radius;

public Circle(Point center, double radius) {

this.center = center;

this.radius = radius;

}

}

Add another contains method that determines if a Circle is entirely contained within the Rectangle object. The signature is:

public boolean contains(Circle c);

I did number 1, and 2 but I'm not sure.

Also I have no ideas for number 3, 4, and 5

Please help me!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions