Question
Problem 4: Our Rectangle class revisited individual-only Recall the Rectangle class that we defined in lecture. (The relevant version of this class is available here.)
Problem 4: Our Rectangle class revisited
individual-only
Recall the Rectangle class that we defined in lecture. (The relevant version of this class is available here.)
-
Consider a potential non-static method named rotate that would rotate a Rectangleobject 90 degrees by swapping its width and height values. For example, if a Rectangles dimensions are 10 x 30, then calling the rotate method would change its dimensions to 30 x 10. Because the method only needs to change the internals of the called object, it doesnt need to and should not! return a value.
-
What type of instance method would rotate be, an accessor or mutator?
-
Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method.
-
-
Now consider a potential non-static method named largerThan that takes anotherRectangle object and determines if the called Rectangle object (i.e., the one on which the method is invoked) has a larger area than the other Rectangle object returningtrue if the called object has a larger area and false otherwise.
- What type of instance method would largerThan be, an accessor or mutator?
- Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method.
-
Consider the following client code i.e., code from another class that uses a Rectangleobject:
Rectangle r1 = new Rectangle(60, 80); System.out.println("r1's height is: " + r1.height); r1.width = r1.width + 20; System.out.println(r1); // print the new dimensions
Because our Rectangle class employs appropriate encapsulation, this code fragment will not compile.
- Explain what problems are present in the code fragment that prevent it from compiling.
- Rewrite the fragment to eliminate those problems while maintaining the intended functionality of the original version. Dont make any unnecessary changes.
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