Respuesta :
Answer:
//program in java.
import java.io.*;
class Averager {
public static int sum;
public static int count;
//constructor to initialize values
Averager(){
sum = 0;
count = 0;
}
//Method to return Sum
public static int getSum(){
return sum;
}
//Method to add the value passed in parameter
public static void add(int num){
sum=sum+num;
count=count+1;
}
//Method to return Count
public static int getCount(){
return count;
}
//Method to return Average
public static double getAverage(){
//type casting is used
return (double) sum / (double) count;
}
//User Driven Input method to run the program
public static void main (String[] args)throws IOException {
//Using BufferedReader class for reading input
InputStreamReader x = new InputStreamReader(System.in);
BufferedReader inp = new BufferedReader(x);
int n;
do{
System.out.println("Enter your choice");
System.out.println("1.getSum()");
System.out.println("2.add()");
System.out.println("3.getCount()");
System.out.println("4.getAverage()");
System.out.println("5.Exit");
n=Integer.parseInt(inp.readLine());
switch(n)
{
case 1 : int s = getSum();
System.out.println("The Sum is " + s);
break;
case 2 : System.out.println("Enter the number to add");
int num;
num=Integer.parseInt(inp.readLine());
add(num);
break;
case 3 : int c = getCount();
System.out.println("The Count is " + c);
break;
case 4 : double avg = getAverage();
System.out.println("The Average is " + avg);
break;
default : break;
};
}while(n!=5);
System.out.println("Exiting");
}
}
Explanation :
Average() constructor is used for initialization. Method getSum() will return the value of sum. Method getCount() will return the count and method getAverage() will return the average. The average will be of double type. User driver input is designed to call the methods as and when required.
Input:
Enter your choice
1.getSum()
2.add()
3.getCount()
4.getAverage()
5.Exit
1
Output:
The Sum is 0