In: Computer Science
20. Assume you have a Java Interface with a method has this signature:
public double min(double... grades);
Write the implementation of this method so that it returns the smallest of the grades. Show example on calling the method when you are done with the implementation.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package settest;
import java.util.Scanner;
/**
*
* @author pihu
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package settest;
/**
*
* @author pihu
*/
public interface min_interface {
public double min(double[] grades);
}
public class ImplInterface implements min_interface {
public double min(double[] grades)
{
double min;
min=grades[0];
for(int i=1;i<grades.length;i++)
{
if(min>grades[i])
{
min=grades[i];
}
}
return min;
}
public static void main(String args[])
{
System.out.println("Enter no of grades");
ImplInterface impInterface=new ImplInterface();
Scanner input=new Scanner(System.in);
int count=input.nextInt();
double grade[]=new double[count];
for(int i=0;i<count;i++)
{
System.out.println("Enter grade"+(i+1));
double element=input.nextDouble();
grade[i]=element;
}
System.out.println("Smallest Grades is "+impInterface.min(grade));
}
}