Question
Let us assume you are writing the Rectangle class in Java: public class Rectangle { private final double cornerX; private final double cornerY; private final
Let us assume you are writing the Rectangle class in Java:
public class Rectangle { private final double cornerX; private final double cornerY; private final double width; private final double height;
public Rectangle(double cornerX, double cornerY, double width, double height) { this.cornerX = cornerX; this.cornerY = cornerY; this.width = width; this.height = height; }
/** Outputs a rectangle as a string * The string is [cornerX, cornerY, width, height] * */ public String toString() { String s = "[" + cornerX + "," + cornerY + "," + width + "," + height + "]"; return s; } }
You want to plan ahead, so that others can use Rectangle. You consider that someone may create an instance of Rectangle as follows: Rectangle r = new Rectangle(10, 5, 15, 7); You now consider that they may wish to find the width of r. What is the best thing you can do to your code to allow this?
1:
You should add a public method called getWidth()
2:
Nothing needs to be done. They should simply use r.width
3:
You should make width a public variable.
4:
Nothing. The variable is private because no one should need to know what its value is.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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