giovedì 28 marzo 2013

Q51


Given:
public class Circle {
       double radius;
       public double area;
       public Circle(double r) {
             radius = r;
       }
       public double getRadius() {
             return radius;
       }
       public void setRadius(double r) {
             radius = r;
       }
       public double getArea() {
             return /* ??? */;
       }
}
 
class App {
       public static void main(String[] args) {
             Circle c1 = new Circle(17.4);
             c1.area = Math.PI * c1.getRadius() * c1.getRadius();
       }
}

This class is poorly encapsulated. You need to change the circle class to compute and return the area instead.
What three modifications are necessary to ensure that the class is being properly encapsulated?
A.
Change the access modifier of the setRadius () method to private
B.
Change the getArea () method
public double getArea () { return area; }
C.
When the radius is set in the Circle constructor and the setRadius () method, recomputed the area and store it into the area field
D.
Change the getRadius () method:
public double getRadius () {
area = Math.PI * radius * radius;
return radius;
}
 Risposta
A Non c'è necessità di avere setRadius publico perchè il raggio viene impostato dal metodo Circle.
B Bisogna restituire l'area nel getArea
C Quando cambia il raggio deve cambiare anche l'area.

Nessun commento:

Posta un commento