Jodi is new to Java generics. She has proposed the following method. Will it work? If not, what has to be changed to make it work?

```
1 // method to compute total perimeter of a collection of Shapes
2 public double totalPerimeter( Collection c ) {
3 double total = 0.0;
4
5 for( Shape s : c )
6 total = total + s.getPerimeter();
7 return total;
8 }

```

Not quite. One line 2, the parameter to the method is of type Collection, which would allow the passing of objects other than Shapes. The compiler is going to use the default upper bound for ‘?’, which is Object, and consequently will complain about the use of s and c in the for loop header “Shape s : c”. This is easily solved by changing the header to Collection c , or better still, Collection c

Computer Science & Information Technology

You might also like to view...

What is the correct output from the following program?


A.
B. A = 8 B = 2
C. A = 3 B = 5
D. A = 4 B = 6
E. None of the above is correct

Computer Science & Information Technology

Predefined formulas in Excel are called functions and can be accessed using the Insert Function button.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology