Hey yall,
This is my first Java Programming course and I'm a little stuck on the arrays section. Our assignment is to create multiple single and multidimensional arrays and also create multiple methods. The following is the assignment:
1.A method that accepts an array of doubles and returns the highest double number in the array.(Highest Grade)
2. A method that accepts an array of doubles and returns the lowest double number in the array.(Lowest Grade)
3. A method that accepts an array of doubles and returns the average of the numbers in the array as a double.(Average Grade)
4. A method that accepts a double number and returns a String letter grade.
5. A method that has no input but returns a double array of student grades.
6. A method that accepts an integer for input and returns the name equivalent for numbers 1 – 10 and adds the correct two letter extension to numbers after that. This should be used when asking for the student's name and grades like:
"Please enter the first student's name: " and "Please enter the first grade: " ... second grade: ...
7. Use a String Array to store the student names along with a parallel, double[][] multidimensional array to store all of the student grades
The flow of the program should look like this:
1.Ask for number of students
a. Collect number of students
2. Create student name array and multidimensional grades array
3. Collect each student’s information
a.Ask for first student’s name and assign it to students[]
student[i] = input.next();
b. Call the method that collects student grades and assign it to the grades array
grades[i] = collectGrades();
4. Do this for each student (loop back to 3)
5. When all of the information is collected into the parallel arrays, for each student:
a. System.out.print() a line that has the proper header
b. For each student output the information collected in the proper format using the other required methods.
The problem I'm having is getting it to loop back. I can collect the number of students, the name of that first student, the grades for that student and it will give the output - but I'm confused on how to get it to loop back to ask for the name and the grades again. I'm also confused on how to pass the data from grades to the multidimensional array. Attached is my code:
Thanks so much for your help in advance!
import java.util.*; public class Midterm { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the number of students: "); int numofStudents = input.nextInt(); // define arrays String[] studentName = new String[numofStudents]; double[][] scores = new double[numofStudents][]; // for loop for student's names for (int n = 0;n < 1; n++){ System.out.println("Enter the " + numName(n+1) + " student's name(s): "); studentName[n] = input.next(); }// end for //call collectGrades double grades[] = collectGrades(); //output System.out.println("Student Name\tHigh Score\tLow Score\tAverage\tGrade"); System.out.println("----------------------------------------------------------"); for(int s = 0;s < numofStudents;s++) { System.out.println(studentName[0 + s] + "\t\t" +highGrade(grades) + "\t\t" +lowGrade(grades) + "\t\t\t" +(averageGrade(grades))); } } public static double[] collectGrades() { Scanner input = new Scanner(System.in); System.out.println("How many grades will you be entering" + "\n for this student?"); int numGrades = input.nextInt(); double grades[] = new double[numGrades]; //collect grades for(int g = 0;g < grades.length; g++) { System.out.println("Please enter the " +numName(g+1) + " grade:"); grades[g] = input.nextDouble(); } // end for loop return grades; } // end collectGrades public static double highGrade (double arrayIn[]) { Arrays.sort(arrayIn); return arrayIn[arrayIn.length -1]; } // end highGrade public static double lowGrade (double arrayIn[]) { Arrays.sort(arrayIn); return arrayIn[0]; } // end highGrade public static double averageGrade(double arrayIn[]) { double avgGrade = 0.0; for(int i = 0;i < arrayIn.length;i++) { avgGrade += arrayIn[i]; } // end for return avgGrade/arrayIn.length; } // end averageGrade public static String numName(int intIn) { String numName = ""; switch(intIn) { case 1: numName = "first"; break; case 2: numName = "second"; break; case 3: numName = "third"; break; case 4: numName = "fourth"; break; case 5: numName = "fifth"; break; case 6: numName = "sixth"; break; case 7: numName = "seventh"; break; case 8: numName = "eight"; break; case 9: numName = "ninth"; break; case 10: numName = "tenth"; break; default: numName = intIn + (intIn<20?"th" : (intIn%10==1?"st": (intIn%10==2?"nd":(intIn%10==3?"rd":"rh")))); } //end Switch return numName; } } // end public class Midterm