Analyzable Interface Modify the CourseGrades class you created in Programming Ch
ID: 3819061 • Letter: A
Question
Analyzable Interface Modify the CourseGrades class you created in Programming Challenge 5 so it implements the following interface: public interface Analyzable {double getAverage(); GradedActivity getHighest(); GradedActivity getLowest();} The getAverage method should return the average of the numeric scores in the grades array. The getHighest method should return a reference to the element of the grades array that has the highest numeric score. The getLowest method should return a reference to the element of the grades array that has the lowest numeric score. Demonstrate the new methods in a complete program.Explanation / Answer
// there are four methods in this class for performing four different tasks
//1. take input array elements .
//2. show average of the array elements.
//3. show heighest element of the array.
//4. show lowest element of the array.
//tyere is a user interface come on command line when the program will run and shows above options .
//the necessary task is the first one to fill the array first.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class GradeActivity implements Analyzable{
double arr[] = new double[5];
int i = 0;
void grade() throws NumberFormatException, IOException{
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0 ; i < arr.length ; i++){
System.out.print( i+1+" enter element: ");
double a = Double.parseDouble(scanner.readLine());
arr[i] = a;
}
i++;
}
@Override
public double getAverage() {
int count =0;
double avg,add = 0;
for(int i = 0 ; i < arr.length ; i++){
count = i+1;
add = add + arr[i];
}
avg = add / count;
System.out.println(avg);
return avg;
}
@Override
public GradeActivity getHighest() {
double high = arr[0],add = 0;
for(int i = 0 ; i < arr.length ; i++){
if(high <arr[i]){
high = arr[i];
}
}
System.out.println(high);
return null;
}
@Override
public GradeActivity getLowest() {
double high = arr[0],add = 0;
for(int i = 0 ; i < arr.length ; i++){
if(high > arr[i]){
high = arr[i];
}
}
System.out.println(high);
return null;
}
public static void main(String str[]){
Scanner scanner = new Scanner(System.in);
char ch;
try {
GradeActivity ga = new GradeActivity();
do{
System.out.println("select operation.");
System.out.println("1 : fill array please.");
System.out.println("2 : Average.");
System.out.println("3 : Highest.");
System.out.println("4 : Lowest.");
int choice = scanner.nextInt();
switch(choice){
case 1 :
ga.grade();
break;
case 2 :
ga.getAverage();
break;
case 3:
ga.getHighest();
break;
case 4 :
ga.getLowest();
break;
default :System.out.println("wrong entry :");
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
ch = scanner.next().charAt(0);
}while(ch=='Y' || ch=='y');
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.