Question

In: Computer Science

In the notation, <T extends Number>, the Number class A. Specifies both an upper and lower...

In the notation, <T extends Number>, the Number class

A. Specifies both an upper and lower bound for the type T

B. Specifies a lower bound for the parameter type T

C. Specifies an upper bound for the parameter type T

D. None of the above

Solutions

Expert Solution

Answer c) Specifies an upper bound for the parameter type T

<T extends Number> is used to Specifies an upper bound for the parameter type T

Reason:

Upper Bounded
You can use an upper bounded to relax the restrictions on a variable. For example, say you want to write a method that works on List<Integer>, List<Double>, and List<Number>; you can achieve this by using an upper bounded wildcard.

To declare an upper-bounded wildcard, use the wildcard character ('?'), followed by the extends keyword, followed by its upper bound. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).

To write the method that works on lists of Number and the subtypes of Number, such as Integer, Double, and Float, you would specify List<? extends Number>. The term List<Number> is more restrictive than List<? extends Number> because the former matches a list of type Number only, whereas the latter matches a list of type Number or any of its subclasses.

Consider the following process method:

public static void process(List<? extends Foo> list) { /* ... */ }
The upper bounded wildcard, <? extends Foo>, where Foo is any type, matches Foo and any subtype of Foo. The process method can access the list elements as type Foo:

public static void process(List<? extends Foo> list) {
for (Foo elem : list) {
// ...
}
}
In the foreach clause, the elem variable iterates over each element in the list. Any method defined in the Foo class can now be used on elem.

The sumOfList method returns the sum of the numbers in a list:

public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
The following code, using a list of Integer objects, prints sum = 6.0:

List<Integer> li = Arrays.asList(1, 2, 3);
System.out.println("sum = " + sumOfList(li));
A list of Double values can use the same sumOfList method. The following code prints sum = 7.0:

List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
System.out.println("sum = " + sumOfList(ld));


Related Solutions

the midpoint of a class is the sum of its lower and upper limits divided by...
the midpoint of a class is the sum of its lower and upper limits divided by two
Coefficients Standard Error t-Stat    P-value Lower 95% Upper 95% Lower 95.0% Upper 95.0% Intercept Calories...
Coefficients Standard Error t-Stat    P-value Lower 95% Upper 95% Lower 95.0% Upper 95.0% Intercept Calories Calories (x) Fat Content (y) Predicted y value Residual 140 7 5.75 1.25 160 8 6.77 1.23 70 2 2.18 -0.18 120 3.5 4.73 -1.23 170 16 7.28 8.72 290 12 13.4 -1.4 210 17 9.32 7.68 190 12 8.3 3.7 210 1.5 9.32 -7.82 130 3.5 5.24 -1.74 150 4.5 6.26 -1.76 160 12 6.77 5.23 150 7 6.26 0.74 140 6 5.75...
Describe the major social classes in Canada: upper class, middle class, working-class, and lower class. Consider...
Describe the major social classes in Canada: upper class, middle class, working-class, and lower class. Consider the size of each class, as well as the income levels, typical schooling, and type of work that characterize people at each level.
Give asymptotic upper and lower bounds for T(n). Assume that T(n) is constant for n <=...
Give asymptotic upper and lower bounds for T(n). Assume that T(n) is constant for n <= 2. Make your bounds as tight as possible, and justify your answers. T(n) = T(n-2) + n^2
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
Identify the lower class limits, upper class limits, class width, class midpoints, and class boundaries for the given frequency distribution.
Identify the lower class limits, upper class limits, class width, class midpoints, and class boundaries for the given frequency distribution. Also identify the number of individuals included in the summary. Identify the lower class limits. Identify the upper class limits.Identify the class width.Identify the class midpoints.
Prove the upper and lower bound of T(n) = T(n/3) + T(2n/3) + O(n)
Prove the upper and lower bound of T(n) = T(n/3) + T(2n/3) + O(n)
Give upper and lower bounds for T(n) in the following recurrence: T(n) = 3T(n/4) + n
Give upper and lower bounds for T(n) in the following recurrence: T(n) = 3T(n/4) + n
Identify the lower class limits, upper class limits, Blood Platelet Count of class width, class midpoints, and class boundaries for the given frequency distribution
Identify the lower class limits, upper class limits, Blood Platelet Count of class width, class midpoints, and class boundaries for the given frequency distribution. Also identify the number of individuals included in the summaryIdentify the lower class limits.Identify the upper class limits.Identify the class width. Identify the class midpoints.Identify the class boundaries.Identify the number of individuals included in the summary.
Lower Class Limit Upper Class Limit Frequency 60 64 3 65 69 7 70 74 3...
Lower Class Limit Upper Class Limit Frequency 60 64 3 65 69 7 70 74 3 75 79 7 80 84 7 85 89 11 90 94 10 95 99 9 100 104 12 (a) What is the 11th percentile for the following Grouped Frequency Data Table? (b) What is the 4th decile for the following Grouped Frequency Data Table?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT