Given this class:
Which two changes would encapsulate this class and ensure that the area field is always equal to length * height whenever the Rectangle class is used?
(Choose two.)
Given this class:
Which two changes would encapsulate this class and ensure that the area field is always equal to length * height whenever the Rectangle class is used?
(Choose two.)
To ensure encapsulation and that the area field is always equal to length multiplied by height, two main changes are required: calling the setArea method at the end of the setHeight method and at the end of the setLength method. This makes sure that whenever either height or length is modified, the area is recalculated immediately, maintaining consistency.
The answer is AC, to do what is asked by the question, you must put the method at the end of the set method of each one.
The goal here is to ensure that the area field is always updated correctly whenever length or height is modified. Encapsulation involves restricting direct access to fields and ensuring that they are modified through controlled methods. Given the provided options, here are the changes that would encapsulate the class and ensure that the area field is always equal to length * height: Call the setArea method at the end of the setHeight method. This ensures that whenever the height is set, the area is recalculated. Call the setArea method at the end of the setLength method. Similarly, this ensures that whenever the length is set, the area is recalculated. So, the correct choices are A and C:
The answer is AC public class Rectangle { private double length; private double height; private double area; public void setLength(double length) { this.length = length; setArea(); } public void setHeight(double height) { this.height = height; setArea(); } public void setArea() { this.area = length * height; } public static void main(String[] args) { Rectangle a = new Rectangle(); a.setHeight(2); a.setLength(3); a.setArea(); System.out.println(a.area); } }