Question
Purpose is for you to create objects and call methods from the Java library. You will have to use the online API documentation to do
Purpose is for you to create objects and call methods from the Java library. You will have to use the online API documentation to do this.
Working with the Java library Rectangle class
You will create and use objects from the Java library Rectangle class. You should have the online API documentation open as you do this.
(The Hints section below will get you started in BlueJ.)
Write a Java program to do the following:
Create some Rectangle objects
create a Rectangle named rectangle1 with location (0, 0) and width = 10, height = 20
create another Rectangle named rectangle2 with location (5, 10) and width = 20, height = 10
print each rectangle nicely labelled, like this:
System.out.println("rectangle1 is: " + rectangle1.toString()); note that the Rectangle method toString() does not draw the rectangle, it simply returns the location and dimensions in text form e.g. output is:
rectangle1 is: java.awt.Rectangle[x=0,y=0,width=10,height=20]
Intersecting rectangles
The Rectangle method intersection() computes the intersection of two rectangles that is, the rectangle that would be formed by two overlapping rectangles if they were drawn.
Check the online API documentation to find that this method creates and returns a new Rectangle object resulting from the intersection of the this, invoking object Rectangle with the Rectangle specified as the parameter. Example of the method call:
Rectangle r3 = r1.intersection(r2);
in your program, create and print a new Rectangle object resulting from the intersection of rectangle1 and rectangle2
Empty rectangles
The definition of an empty rectangle is a little odd this is considered to be a rectangle with width or height <= 0.
create anotherRectangle, that does not overlap with rectangle1 e.g. give it location (100, 200) and width = 10, height = 10
create and print a new Rectangle object resulting from the intersection of rectangle1 and anotherRectangle
notice that when the 2 Rectangle objects do not overlap, intersection() still creates and returns a new rectangle. Its just that this non-existent, virtual rectangle is empty
use the API docs to find a method to confirm that this final Rectangle is empty. Print the output of this method call, nicely labelled
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started