Question
I need help solving my code. This is what I am building this -> Java program that allows teachers to input student grades for various
I need help solving my code. This is what I am building this -> Java program that allows teachers to input student grades for various assignments and exams, and store the data in a 2-dimensional array, and perform calculations to determine the overall course grade for each student. The program includes functionality for reading in data from a text file, such as a roster of students in the course, and for saving the calculated grades to a text file for record-keeping purposes.The program uses encapsulation to hide the implementation details of certain functions, such as the calculation of the overall course grade, from the user. It uses inheritance to extend a base "Student" class with more specialized classes for different types of students (e.g. undergraduate, graduate) . The program utilizes abstract classes and interfaces to define common behavior that should be implemented by different classes in the hierarchy, such as a "CalculateGrade" abstract method that all student classes must implement. Finally, the program uses polymorphism to create objects from these classes and call their methods in a flexible and dynamic way. The program also includes try/catch blocks to handle potential errors that may occur during input and processing, such as invalid data being entered or a file not being found. Loops and if-statements are used to control the flow of the program and to enable the user to perform different actions, such as viewing or editing a student's grades, or generating reports on the class as a whole This is my code right now: im getting a bunch of errors can someone fix it pls
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class MyProgram { public static void main(String[] args) { Student[] students = null; try { students = readStudentsFromFile("roster.txt"); } catch (FileNotFoundException e) { System.out.println("Error: File not found"); return; } Scanner input = new Scanner(System.in); int choice; do { System.out.println("1. View student grades"); System.out.println("2. Edit student grades"); System.out.println("3. Generate report"); System.out.println("4. Exit"); choice = input.nextInt(); switch (choice) { case 1: viewStudentGrades(students); break; case 2: editStudentGrades(students); break; case 3: generateReport(students); break; case 4: break; default: System.out.println("Invalid choice. Please try again."); } } while (choice != 4); saveStudentsToFile(students, "grades.txt"); } public static Student[] readStudentsFromFile(String filename) throws FileNotFoundException { Scanner fileInput = new Scanner(new File(filename)); int numStudents = fileInput.nextInt(); Student[] students = new Student[numStudents]; fileInput.nextLine(); // consume newline character for (int i = 0; i < numStudents; i++) { String line = fileInput.nextLine(); String[] parts = line.split(","); String name = parts[0]; int[] grades = new int[parts.length - 1]; for (int j = 1; j < parts.length; j++) { grades[j - 1] = Integer.parseInt(parts[j]); } students[i] = new Student(name, grades); } return students; } public static void saveStudentsToFile(Student[] students, String filename) { try (PrintWriter fileOutput = new PrintWriter(new File(filename))) { fileOutput.println(students.length); for (Student student : students) { fileOutput.print(student.getName() + ","); int[] grades = student.getGrades(); for (int grade : grades) { fileOutput.print(grade + ","); } fileOutput.println(); } } catch (FileNotFoundException e) { System.out.println("Error: Unable to save file"); } } public static void viewStudentGrades(Student[] students) { Scanner input = new Scanner(System.in); System.out.print("Enter student name: "); String name = input.nextLine; for (Student student : students) { if (student.getName().equalsIgnoreCase(name)) { int[] grades = student.getGrades(); for (int i = 0; i < grades.length; i++) { System.out.println("Assignment " + (i + 1) + ": " + grades[i]); } System.out.println("Overall grade: " + student.calculateGrade()); return; } } System.out.println("Error: Student not found"); } public static void editStudentGrades(Student[] students) { Scanner input = new Scanner(System.in); System.out.print("Enter student name: "); String name = input.nextLine(); for (Student student : students) { if (student.getName().equalsIgnoreCase(name)) { int[] grades = student.getGrades(); for (int i = 0; i < grades.length; i++) { System.out.print("Enter grade for assignment " + (i + 1) + ": "); int grade = input.nextInt(); student.setGrade(i, grade); } System.out.println("Grades updated successfully"); return; } } System.out.println("Error: Student not found"); } public static void generateReport(Student[] students) { double totalGrade = 0; for (Student student : students) { totalGrade += student.calculateGrade(); } double averageGrade = totalGrade / students.length; System.out.println("Average class grade: " + averageGrade); } } abstract class Student { private String name; private int[] grades; public Student(String name, int[] grades) { this.name = name; this.grades = grades; } public String getName() { return name; } public int[] getGrades() { return grades; } public void setGrade(int index, int grade) { grades[index] = grade; } public abstract double calculateGrade(); } class UndergraduateStudent extends Student { public UndergraduateStudent(String name, int[] grades) { super(name, grades); } @Override public double calculateGrade() { double total = 0; for (int grade : grades) { total += grade; } return total / grades.length; } } class GraduateStudent extends Student { private double researchScore; public GraduateStudent(String name, int[] grades, double researchScore) { super(name, grades); this.researchScore = researchScore; } public double getResearchScore() { return researchScore; } public void setResearchScore(double researchScore) { this.researchScore = researchScore; } @Override public double calculateGrade() { double total = 0; for (int grade : grades) { total += grade; } total += researchScore; return total / (grades.length + 1); } } |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started