// The "Names" class. // an example illustrating the use of arrays import hsa.Console;// for input and output public class Names { static Console c = new Console(); //static final int number = 5;//constant value representing how many items to store static int number = 0;// how many names are to be processed void show(String[] s, int[] g)//s is an array of strings and g is an array of integers { int i = 0; for (i=1; i<=number; i=i+1) { c.println(s[i] +" " + g[i]);//print the ith name } } double getAverage(int[] g) //requires an integer array as parameter { int i = 0;// use as a counter int sum = 0;//hold the current sum for (i = 1; i <= number; i=i+1) { sum = sum + g[i]; } return (double)(sum)/(double)number; } public static void main (String[] args) { //String[ ] names = new String[50];//declaration of an array that can hold up to 50 names String[] names;//declare an array whose length is obtained from the user. //int[ ] grades = new int[50];// declaration of an array that can hold up to 50 integers int[] grades;// declare an array whoselength is obtained from the user. int i = 0;//an integer used for counting c.println("How many names and grades do you have?"); number = c.readInt(); names = new String[number+1];//sets up the array and allows starting at index 1. grades = new int[number+1]; Names member = new Names();// a class member for calling methods double average = 0.0;// variable to hold the average of the grades c.println(" Please enter "+number + " names, one per line, and their grades");// alert user to what will happen for (i = 1; i <= number; i=i+1)//do the loop body 10 times { c.println("Next name?");//ask the user for another name names[i]=c.readLine();//get a name and place it in the array at the ith entry position c.println("Next grade?"); grades[i] = c.readInt(); //c.println(names[i]);//show the names in the array } average = member.getAverage(grades);//call the getAverage method to find the average grade c.println();// skip a line c.println(" Here are the names and grades");//tell what will happen member.show(names, grades);//call the method that displays the names c.println(); c.println(" The average grade is " + average); } // main method } // Names class