Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.)

  1. 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.

    1. What type of instance method would rotate be, an accessor or mutator?

    2. 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.

  2. 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.

    1. What type of instance method would largerThan be, an accessor or mutator?
    2. 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.
  3. 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.

    1. Explain what problems are present in the code fragment that prevent it from compiling.
    2. 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

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

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

Design a health and safety policy.

Answered: 1 week ago