Question
Java problem about inheritance help please Familiarize yourself with Java's Rectangle class in the java.awt package. Write a NicerRectangle class that extends the Rectangle class.
Java problem about inheritance help please
-
Familiarize yourself with Java's Rectangle class in the java.awt package.
-
Write a NicerRectangle class that extends the Rectangle class.
-
Do not add any instance variables.
-
Create 3 constructors. Each should make an appropriate call to another constructor either in the Rectangle class or in the NicerRectangle class.
-
A 4-parameter constructor that specifies the x, y, width, and height, in that order
-
A 2-parameter constructor that specifies the x and y values, and sets the width and height to 1
-
A no-parameter constructor that sets the x and y to zero, and the width and height to 1
-
-
Create getPerimeter and getArea methods that return the perimeter and area of the rectangle as int values. This method should call on the appropriate getters from the Rectangle class. Do not rely on the public instance variables.
-
Create a containsPoint method. The method has one Point parameter and returns true if the corresponding point is inside but not on the rectangle, and return false otherwise.
-
Create a containsSegment method. The method has two Point parameters representing the endpoints of a line segment. Return true if the corresponding line segment lies entirely inside but not on the rectangle, and return false otherwise.
-
Create a no-parameter void mutator named doubleSize that doubles the length and width of the rectangle. The x- and y-coordinates should not change.
-
Override the toString method so that the returned string contains all the information from the Rectangle toString, along with the perimeter and area of the NicerRectangle. See the sample output on the next page.
-
-
Do not submit a separate tester class. Instead, write the standard main() method inside the NicerRectangle class. In it, ask the user for a number of rectangles to be created. Then, create an array or ArrayList of randomly generated NicerRectangle objects (x, y, width, and height should all be random values in the range 1 to 20 inclusive). Print all of these objects, then grow all of the objects using your doubleSize method and then print them again. Finally, ask the user for the x- and y-coordinates of two points, and print a list of only the rectangles that contain the first point, all the rectangles that contain the second point, and all the rectangles that contain the segment formed by using those two points as endpoints.
-
Zip your one source code file, NicerRectangle.java, and upload the zip file to Canvas.
Here's a sample run of my tester (the main() method is inside the NicerRectangle class):
How many rectangles? 4
4 random rectangles...
NicerRectangle[x=6,y=7,width=4,height=5] Area=20, Perimeter=18
NicerRectangle[x=5,y=8,width=2,height=6] Area=12, Perimeter=16
NicerRectangle[x=6,y=9,width=9,height=9] Area=81, Perimeter=36
NicerRectangle[x=1,y=3,width=7,height=4] Area=28, Perimeter=22
after doubling sizes...
NicerRectangle[x=6,y=7,width=8,height=10] Area=80, Perimeter=36
NicerRectangle[x=5,y=8,width=4,height=12] Area=48, Perimeter=32
NicerRectangle[x=6,y=9,width=18,height=18] Area=324, Perimeter=72
NicerRectangle[x=1,y=3,width=14,height=8] Area=112, Perimeter=44
Enter x- and y-coordinates for start: 3 5
Enter x- and y-coordinates for end: 7 8
Here are the rectangles that contain point 3 5:
NicerRectangle[x=1,y=3,width=14,height=8] Area=112, Perimeter=44
Here are the rectangles that contain point 7 8:
NicerRectangle[x=6,y=7,width=8,height=10] Area=80, Perimeter=36
NicerRectangle[x=1,y=3,width=14,height=8] Area=112, Perimeter=44
Here are the rectangles that contain the segment from 3 5 to 7 8:
NicerRectangle[x=1,y=3,width=14,height=8] Area=112, Perimeter=44
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