0

I am making a Java program about a simple student management system. Here's the summary of how the code works:

  1. 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.
  2. 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.
  3. 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. ;(

A_Pag5329
  • 1
  • 2
  • 1
    `javac *.java` -- use a proper build tool. Maven is popular. – Elliott Frisch Jun 09 '23 at 12:54
  • 1
    please, put the relevant, all of the relevant and only the relevant code in your question. Don't post information as links to images, but as formatted text. – Stultuske Jun 09 '23 at 12:55
  • 1
    @SamuelMarchant underscore is valid in package name. – Mar-Z Jun 09 '23 at 13:11
  • @Mar-Z , thanks. – Samuel Marchant Jun 09 '23 at 13:20
  • @A_Pag5329 while calling the compiler you should be in the root directory of the project and add the package name before the file name. Like: javac students_class/ClassroomVer2.java – Mar-Z Jun 09 '23 at 13:26
  • Try import StudentVer2; statement in ClassroomVer2 . It could be a minor compiler glitch . Don't forget package location names and declarations at tops of file should match. I think with packages declared at top you must actually commit "import" statements for any file used whether in sane folder or others! If you had no package declarations it could then be implicit. implicit (no declarations) implies everything will be in the one folder.With package statement declare import of everything used. With java API they are in different packages so only those require declaring in implicit. – Samuel Marchant Jun 09 '23 at 13:48
  • Put all the classes into the `students_class` package. Classes in the default package haven't worked completely since JDK 1.4 twenty years ago. – user207421 Jul 14 '23 at 07:02

3 Answers3

0

I think the issue you're facing is due to the complilation process not being able to find the symbol (class) 'StudentVer2' when compiling 'ClassroomVer2.java '. This happens because the compiler needs to know the location of the 'StudentVer2' class file in order to compile 'ClassroomVer2.java'.

0

You need to make certain that public class ClassroomVer2 and public class StudentVer2 are both in the students_class package. If StudentVer2 is in a different package, then put the following in the ClassroomVer2 class.

import <other parent packages>.StudentVer2;

I tried both options and in each case had no unresolved symbol errors.

WJS
  • 36,363
  • 4
  • 24
  • 39
0

Try import StudentVer2; statement in ClassroomVer2. I think with packages declared at top you must actually commit "import" statements for any file used whether in sane folder or others! If you had no package declarations it could then be implicit. implicit (no declarations) implies everything will be in the one folder.

With package statement declare import of everything used. With java API they are in different packages so only those require declaring in implicit.

Don't forget package location names and declarations at tops of file should match. Also conventional package systems the files start to be in the 3rd folder deep, and the first package folder named is conventionally one of /com /org or. /net

Samuel Marchant
  • 331
  • 2
  • 6