I am making a Java program about a simple student management system. Here's the summary of how the code works:
Student class
- Represents a student with attributes such as the name and grade
- Provides methods to access and modify the student's name and grade.
Classroom class
- Manages a classroom of students.
- Stores a list of students and a maximum number of students.
- Provides methods to add a student, remove a student by name or index, and retrieve all students. Removal of a student will occur if they have the same name as another student.
- Includes methods to find the student with the highest grade and calculate the class average grade.
ClassroomStudentMain class
- Create an instance of the Classroom class with a limit of 5 students.
- Adds five students to the classroom with their respective names and grades.
- Prints the list of all students and their respective grades.
- Finds and prints the name and grade of the student with the highest grade.
- Calculates and prints the average grade of the class.
I already made an iteration of the code where the 3 classes are in one Java file named "ClassroomStudentMain.java". Here's the implementation of the Java file I am pertaining to.
import java.util.ArrayList;
import java.util.List;
class Classroom {
private int studentLimit;
private List<Student> students;
public Classroom(int studentLimit) {
this.studentLimit = studentLimit;
this.students = new ArrayList<>();
}
public boolean addStudent(Student student) {
if (students.size() < studentLimit) {
students.add(student);
return true;
}
return false;
}
public boolean removeStudentByName(String name) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equals(name)) {
students.remove(i);
return true;
}
}
return false;
}
public boolean removeStudentByIndex(int index) {
if (index >= 0 && index < students.size()) {
students.remove(index);
return true;
}
return false;
}
public List<Student> getAllStudents() {
return students;
}
public Student getStudentWithHighestGrade() {
Student highestGradeStudent = null;
double highestGrade = -1;
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getGrade() > highestGrade) {
highestGrade = student.getGrade();
highestGradeStudent = student;
}
}
return highestGradeStudent;
}
public double getClassAverage() {
if (students.isEmpty()) {
return 0;
}
double sum = 0;
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
sum += student.getGrade();
}
return sum / students.size();
}
}
class Student {
private String name;
private double grade;
public Student(String name, double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
public class ClassroomStudentsMain {
public static void main(String[] args) {
Classroom classroom = new Classroom(5);
classroom.addStudent(new Student("John", 80));
classroom.addStudent(new Student("Alice", 90));
classroom.addStudent(new Student("Bob", 75));
classroom.addStudent(new Student("Emily", 95));
classroom.addStudent(new Student("Tom", 85));
System.out.println("All Students: ");
List<Student> students = classroom.getAllStudents();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
System.out.println(student.getName() + " - Grade: " + student.getGrade());
}
System.out.println("\nHighest Grade Student: ");
Student highestGradeStudent = classroom.getStudentWithHighestGrade();
System.out.println(highestGradeStudent.getName() + " - Grade: " + highestGradeStudent.getGrade());
System.out.println("\nClass Average Grade: " + classroom.getClassAverage());
}
}
This iteration of the code works properly and it generated the class files necessary. Now I am trying to make an iteration where the 3 classes: Classroom, Student, and ClassroomStudentsMain are seperate Java files. I already made a folder for the 2 classes: ClassroomVer2, and StudentVer2, named "students_class". I implemented the codes for the classes the exact same way as I implemented them in the first iteration of the code above. Let me insert the respective codes for the 2 classes I am pertaining to.
This is for the StudentVer2 class. "StudentVer2.java"
package students_class;
public class StudentVer2 {
private String name;
private double grade;
public StudentVer2(String name, double grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
This is for the ClassroomVer2 class. "ClassroomVer2.java"
package students_class;
import java.util.ArrayList;
import java.util.List;
public class ClassroomVer2 {
private int studentLimit;
private List<StudentVer2> students;
public ClassroomVer2(int studentLimit) {
this.studentLimit = studentLimit;
this.students = new ArrayList<>();
}
public boolean addStudent(StudentVer2 student) {
if (students.size() < studentLimit) {
students.add(student);
return true;
}
return false;
}
public boolean removeStudentByName(String name) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equals(name)) {
students.remove(i);
return true;
}
}
return false;
}
public boolean removeStudentByIndex(int index) {
if (index >= 0 && index < students.size()) {
students.remove(index);
return true;
}
return false;
}
public List<StudentVer2> getAllStudents() {
return students;
}
public StudentVer2 getStudentWithHighestGrade() {
StudentVer2 highestGradeStudent = null;
double highestGrade = -1;
for (int i = 0; i < students.size(); i++) {
StudentVer2 student = students.get(i);
if (student.getGrade() > highestGrade) {
highestGrade = student.getGrade();
highestGradeStudent = student;
}
}
return highestGradeStudent;
}
public double getClassAverage() {
if (students.isEmpty()) {
return 0;
}
double sum = 0;
for (int i = 0; i < students.size(); i++) {
StudentVer2 student = students.get(i);
sum += student.getGrade();
}
return sum / students.size();
}
}
The StudentVer2.java file compiled without any issues and generated a class file of its own. However, when I tried to compile and generate the class file for the ClassroomVer2.java file, it showed these errors, here are the errors that popped up in VS Code.
ClassroomVer2.java:8: error: cannot find symbol private List students; ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:15: error: cannot find symbol public boolean addStudent(StudentVer2 student) { ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:41: error: cannot find symbol public List getAllStudents() { ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:45: error: cannot find symbol public StudentVer2 getStudentWithHighestGrade() { ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:46: error: cannot find symbol StudentVer2 highestGradeStudent = null; ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:50: error: cannot find symbol StudentVer2 student = students.get(i); ^ symbol: class StudentVer2 location: class ClassroomVer2
ClassroomVer2.java:66: error: cannot find symbol StudentVer2 student = students.get(i); ^ symbol: class StudentVer2 location: class ClassroomVer2 7 errors
How can I resolve this?
I attempted to generate the class file for ClassroomVer2.java in the Command Prompt. However, I got the exact same error "Cannot find symbol". I also tried renaming the folder, where the StudentVer2.java and ClassroomVer2.java files are located, a couple of times as well. Still no luck. ;(