Multiple Choice Question

Quiz Guidelines

 

QID: 
85
Tags: 
Question: 

Consider the following two classes (in the same package but defined in different source files):

public class Square {
      double side = 0;
      double area;
         public Square(double length){ this.side = length; }
        public double getSide() { return side; }
        public void setSide(double side) { this.side = side; }
       double getArea() { return area; }
}
public class TestClass {
      public static void main(String[] args) throws Exception {
      Square sq = new Square(10.0);
      sq.area = sq.getSide()*sq.getSide();
     System.out.println(sq.getArea());
       }   }

You are assigned the task of refactoring the Square class to make it better in terms of encapsulation. What changes will you make to this class?

Select 2 options

A. Make setSide() method private. 
B. Make getArea() method private. 
C. Make side and area fields private. 
D. Make the side field private and remove the area field. 
E. Change getArea method to:
         public double getArea(){ return side*side; }
F. Add a setArea() method.

Q&A Set: