/** * Obtains the user's course information. (Their course's grade and credit hours). * * @return array of Course to represent the students schedule */ static Course[] getInput(){ Console.Write("How many courses do you want to input for your GPA? "); int numCourses = 0; try{ numCourses = Int32.Parse(Console.ReadLine()); } catch(Exception e){ Console.WriteLine("Invalid input " + e.GetBaseException().ToString()); } Course[] courses = new Course[numCourses]; for (int i = 0; i < numCourses; i++) { try { Console.Write("Course " + i + "'s Credit Hours: "); int credits = Int32.Parse(Console.ReadLine()); Console.Write("Course " + i + "'s grade: "); string grade = Console.ReadLine(); courses[i] = new Course(credits, grade); Console.WriteLine(); } catch (Exception e){ Console.WriteLine("Invalid input " + e.GetBaseException().ToString() ); return null; } } return courses; }
/** * Parses through the students courses taking their grades and creidt hours * and calling getGradePoints to calculate their gpa. It then prints * the student's GPA. * * @param courses that student is taking */ static void calculateGPA(Course[] courses) { int totalCredits = 0; double points = 0; foreach (Course course in courses) { totalCredits += course.getCredits(); points += getGradePoints(course.getCredits(), course.getGrade()); //Console.WriteLine(course.ToString()); } Console.WriteLine("Your gpa this semester is: " + String.Format("{0:0.00}", (points / totalCredits)) ); }