X462: Find Largest Shape

For the question below, assume the following implementation of the Measurable interface and Square and Circle classes:

x
 
1
public class Shape {
2
3
}
4
5
public class Circle extends Shape
6
{
7
   private double radius;
8
9
10
   public Circle(double newRadius)
11
   {
12
      radius = newRadius;
13
   } // end constructor
14
15
   public double getRadius() {
16
       return radius;
17
   }
18
19
20
   public double getCircumference()
21
   {
22
      return 2.0 * Math.PI * radius;
23
   } // end getCircumference
24
25
   public double getPerimeter() // Measurable interface
26
   {
27
      return getCircumference();
28
   } // end getPerimeter
29
30
   public double getArea()      // Measurable interface
31
   {
32
      return Math.PI * radius * radius;
33
   } // end getArea
34
35
} // end Circle
36
37
public class Square extends Shape  {
38
39
    private double side;
40
41
    public Square(double len) {
42
        side = len;
43
    }
44
45
    public double getSide() {
46
        return side;
47
    }
48
49
    public double getPerimeter() {
50
        return 4 * side;
51
    }
52
53
    public double getArea() {
54
        return side * side;
55
    }
56
57
}

Write a method that takes in two variables: one of type Square, one of type Circle. This code should assign the object with the larger area the variable max defined below, then return max. The two shapes will never have the same Area.

Your Answer:

xxxxxxxxxx
6
 
1
public Shape getLarger(Square sq, Circle cir) {
2
  Shape max = sq;
3
  
4
  return max;
5
}
6
Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.