You are here: Home / Topics / Program to explain Generic class with Wildcard in Java

Program to explain Generic class with Wildcard in Java

Filed under: Java on 2024-04-13 09:06:42

// Program to explain Generic class with Wildcard.

class  GenericStats<T extends Number> 
{
T[ ]  nums;
   
GenericStats(T[ ]  o) 
{
 nums = o;
}

double average() 
{
 double sum = 0.0;
 for(int i=0; i < nums.length; i++)
 {
  sum += nums[ i ].doubleValue();
 }
 return sum / nums.length;
}

boolean sameAvg(GenericStats<?> ob) 
{
 if(average() == ob.average()) 
  return true;

 return false;
}
}  
 
public class  GenericTest12 
{
public static void main(String args[ ]) 
{
 Integer inums[ ] = { 1, 2, 3, 4, 5 }; 
 GenericStats<Integer> iob = new GenericStats<Integer>(inums);
 double v = iob.average();
 System.out.println("iob average is " + v);
 
 Double dnums[ ] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
 GenericStats<Double> dob = new GenericStats<Double>(dnums);
 double w = dob.average();
 System.out.println("dob average is " + w); 
 
 Float fnums[ ] = { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F }; 
 GenericStats<Float> fob = new GenericStats<Float>(fnums);
 double x = fob.average(); 
 System.out.println("fob average is " + x);
 
 System.out.print("Averages of iob and dob ");
 if(iob.sameAvg(dob))
 {
  System.out.println("are the same."); 
 }
 else
 {
  System.out.println("differ."); 
 }
 
 System.out.print("Averages of iob and fob ");
 if(iob.sameAvg(fob))
 {
  System.out.println("are the same."); 
 }
 else
 {
  System.out.println("differ.");
 }
}
}


Output:

iob average is 3.0
dob average is 3.3
fob average is 3.0
Averages of iob and dob differ.
Averages of iob and fob are the same.

About Author:
J
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.