Following is an example of a method in Java that has a variable length parameter
ID: 3545212 • Letter: F
Question
Following is an example of a method in Java that has a variable length parameter.
public static double largest(double . . . numList)
{
double max;
int index;
if (numList.length != 0)
{
max = list[0];
for (index = 1; index < numList.length; index++)
{
if (max < numList[index])
max = numList[index];
}
return max;
}
return 0.0;
}
a) Develop and test a similar method in NetBeans that uses a variable length parameter to add up
any number of values of type double,
public static double sum(double . . . numList)
b) Write a simple test program that calls function sum as follows:
sum(fnum1, fnum2, fnum3), where fnum1, etc. are initialized variables of type double
sum(num_list), where num_list is an array of doubles
Following is an example of a generic method in Java that prints a list containing any type elements.
public static <T> void print(T . . . list)
{
for (T elem : list)
System.out.print(elem +
Explanation / Answer
public class NewClass {
public static void main(String[] args) {
System.out.println(sum(3,6,8,9,10));//a) test for sum
double fnum1 = 39,fnum2=34, fnum3=9999.9;
System.out.println(" initialized variables as input "+sum(fnum1,fnum2,fnum3)); // b) test for sum(fnum1, fnum2, fnum3), where fnum1, etc. are initialized variables of type double
double[] num_list = {34,67,89,999.9,23.4,55.4};
System.out.println(" Array as input "+sum(num_list)); // b) test for sum(num_list), where num_list is an array of doubles
}
public static double sum(double ...numList) {
double sum =0.0;
if (numList.length != 0) {
for (int i = 0; i < numList.length; i++) {
sum += numList[i];
}
return sum;
}
return sum;
}
}
/*
*
*
*
*
* second part of question
*/
public class NewClass1 {
public static<T extends Number> double avg(T ...numList) {
double sum =0.0;
if (numList.length != 0) {
for (int i = 0; i < numList.length; i++) {
sum += numList[i].doubleValue();
}
return (sum/(double)numList.length);
}
return sum;
}
public static void main(String[] args) {
NewClass1 inttest = new NewClass1();
int num1 =5, num2 = 10, num3 = 14, num4=19;
double fnum1 =1.1, fnum2= 2.5,fnum3=5.8;
String name1="20", name2 = "35.5", name3="2.7",name4="5.4";
Double[] doubleList = {12.0,34.0,5.6,5.9,1.9};
Integer[] intList = {12,56,34,98,12};
double avg = 0.0;
avg = NewClass1.avg(num1,num2,num3,num4);
System.out.println("average of integers"+" "+ num1 + " "+num2+" "+num3+" "+ num4 + " : "+avg);
avg = NewClass1.avg(fnum1,fnum2,fnum3);
System.out.println("average of doubles"+" "+ fnum1 + " "+fnum2+" "+fnum3+" : "+avg);
avg = NewClass1.avg(doubleList);
System.out.println("average of double List : "+avg);
avg = NewClass1.avg(intList);
System.out.println("average of int List : "+avg);
// error can't send string type hence we restricted to type Number. It will through Runtime Exception : Uncompilable source code - Erroneous sym type: NewClass1.avg
// avg = NewClass1.avg(name1,name2,name3,name4);
// System.out.println("average of doubles"+" "+ fnum1 + " "+fnum2+" "+fnum3+" : "+avg);
}
}
// In Second part String array will through error. see comments in program
/*
*
*
*Third part of question
*
*
*
*/
class MyList<T> {
public <T extends Comparable<T>> T maximum(T ...input)
{
T max = input[0]; // assume x is initially the largest
for(int i=1;i<input.length;i++){
if(input[i].compareTo(max)>0){
max = input[i];
}
}
return max; // returns the largest object
}
}
public class NewClass2 {
public static void main(String[] args) {
Integer[] values = {10, 20, 30};
String[] names = {"hello","you","how","are"};
Double[] doublevalues = {100.0,34.0,78.9,99.4};
MyList<Integer> mylist = new MyList<Integer>();
System.out.println(" max of integers: "+mylist.maximum(values));
MyList<String> mystring = new MyList<String>();
System.out.println("max of string : "+ mystring.maximum(names));
MyList<Double> mydouble = new MyList<Double>();
System.out.println("Max of Doubles : "+mydouble.maximum(doublevalues));
}
}