0% found this document useful (0 votes)
25 views13 pages

Course Enrollment & Grade System Code

This document outlines a programming assignment for creating a Course Enrollment and Grade Management System, detailing the implementation of classes for Student and Course, along with a CourseManagement utility class. It includes methods for adding courses, enrolling students, assigning grades, and calculating overall grades, as well as a main program to handle user input. The document also provides references for Java concepts used in the implementation.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

Course Enrollment & Grade System Code

This document outlines a programming assignment for creating a Course Enrollment and Grade Management System, detailing the implementation of classes for Student and Course, along with a CourseManagement utility class. It includes methods for adding courses, enrolling students, assigning grades, and calculating overall grades, as well as a main program to handle user input. The document also provides references for Java concepts used in the implementation.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1

Programming Assignment Unit 5: Course Enrollment & Grade Management System

Department of Computer Science, University of People

CS 1102-01 Programming 1- AY2024-t4


16 May, 2024
2

Programming Assignment Unit 5: Course Enrollment & Grade Management System


In this programming assignment I am going to create a program which is going to ask
from user for input like add new course, enroll new student, add course to a student, assign
grade, calculate overall grade and exit. The sources are provided in the references page.
3

Student Class:
First of all I am going to import all the packages which is going to be used in my student
calls first will be ArrayList, second will be HashMap, third will be List, fourth will be List, and
fifth will be Map.
ArrayList will be used to create a resizable list to store information while List will be
used to implement ArrayList so we can use list as a type for readability. Map will be used same
as the List so we can implement HashMap as a type for readability, and HashMap will be used to
create key-value pair data structure.
Now the name of the class will be the name of the file student (I will not go into the
details why it will be like this). Public means this class can be accessed from other parts of the
code. Next I am going to declare to variable which will be private String name and private String
id. One will store student name while other will store student id. Now I am going to declare
Arraylist and HashMap both as private and with final keyword because I don’t want user to make
changes in it.
Now I am going to define a class with the public keyword because I want it to be
accessed from the other parts of the code. Next step I am going to define properties of a student
object (some people use the word attributes for it). I am going to use private keyword because I
want to control access to these properties or attributes. First I will define name, id, and next I am
going to create a private ArrayList with final keyword because I don’t want it to be reassigned by
the user. Next I am going to create a HashMap I am will also use final keyword because of the
same reason.
Next I am going to create constructors with public key word and name of it will be
Student() it will be an empty constructor. It will be useful when name and id don’t need initial
values. After that I am going to create another constructor with same name it will take student
name and id as arguments (parameters) and it will initialize the student’s information during
object creation.
Now is the time to write code for getter and setter methods, with these methods I can
provide controlled access and modification of the student information. Getter will get name id
courses and grades from the user and setter methods will set name, id, course, and grades. Which
will allow me to update the values of variables with provided arguments. This whole code will
store student’s name, id, and enrolled courses in a list, and corresponding grades in a map.
Code:
import [Link];
import [Link];
import [Link];
import [Link];
4

public class Student {


private String name;
private String id;

private final List<Course> courses = new ArrayList<>();

private final Map<Course, Integer> grades = new HashMap<>();

public Student() {
}

public Student(String name, String id) {


[Link] = name;
[Link] = id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public String getId() {


return id;
}
5

public void setId(String id) {


[Link] = id;
}

public List<Course> getCourses() {


return courses;
}

public void addCourse(Course course) {


[Link](course);
}

public void setGrade(Course course, int grade) {


[Link](course, grade);
}

public Map<Course, Integer> getGrades() {


return grades;
}
}
Course Class:
Now I am going to create another file name [Link] in this I am going to create a
public class name course. I want to access this class from other parts of the code that’s why I
used public keyword. In this class I am going to declare name, code (it is actually course code)
and maxCapacity everything as private because I want to restrict direct access to them. I am also
going to use another variable which will be static and protected. Static means this variable
belongs to the class itself and not to individual objects. There will be only one copy shared by all
course objects, and protected means access will be allowed from the subclasses and the same
package. The name of the variable will be numEnrolled this variable will likely to keep track of
the total number of the students enrolled across all courses.
6

Next I am going to create constructors. First I am going to create a empty constructor


with public keyword. I am making it empty because it is useful when information isn’t provided
during object creation. Next I am going to create another constructor which will take Student
name, code and max capacity. This will initialize values with information got from the user to
the object during object creation.
Now I will write getter methods to get data from the user and save those values to create
a new object after that I will use setters to access those values and modify them in needed.
Code:
public class Course {
private String name;
private String code;
private int maxCapacity;

protected static int numEnrolled = 0;

public Course() {
}

public Course(String name, String code, int maxCapacity) {


[Link] = name;
[Link] = code;
[Link] = maxCapacity;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public String getCode() {


return code;
}

public void setCode(String code) {


[Link] = code;
}

public int getMaxCapacity() {


7

return maxCapacity;
}

public void setMaxCapacity(int maxCapacity) {


[Link] = maxCapacity;
}
}
Course Management:
Now I am going to create a file name course management. In this I am going to create a
class same name as file name. Then I am going to import ArrayList to create resizable lists to
store courses and students. Collections to provide utility methods for working with collections
like lists, and lastly List to represent a list data structure used for readability.
Next I am going to declare a private constructor CourseManagment() which throws and
IllegalStateException errof if someone tries to create an instance of CoruseManagement. This
suggest it’s intended as static utility class, not meant to be instantiated. Now I am going to create
lists with private static and final keywords one will be for courses and the other will be for
students.
Now I am going to write static methods which will provide functionalities for managing
courses, students, and enrollments. They are all static methods, meaning they can be called
directly on the class without needing an object, addCoruse will take name of student, code
(course code) and maxCapacity as arguments and adds a new course with provided information.
enrollStudent will take student and course as arguments and it will attempt to enroll a student in
a course. It will check the capacity and updates both student and course information if
successful.
assignGrade will take student course and grade as arguments and assign grade to a
student for specific course. getCoruseByCode will take courseCode and finds a course by its
code using streams (potentially more efficient for large lists) and returns the course object or null
if not found. getStudentById will take student id as argument and finds student by their id using
streams and returns the student object or null if not found.
calculateOverallGrade will take student as argument and it calculates the overall grade
for a student by averaging grades from all enrolled courses. These methods provide access to the
course and student list but return unmodifiable versions using [Link]. This
will prevent callers from modifying the internal lists directly.
Code:
import [Link];
import [Link];
import [Link];

public class CourseManagement {


8

private CourseManagement() {
throw new IllegalStateException("Utility class");
}

private static final List<Course> courses = new ArrayList<>();

private static final List<Student> students = new ArrayList<>();

public static void addCourse(String name, String code, int maxCapacity) {


Course course = new Course(name, code, maxCapacity);
[Link](course);
}

public static boolean enrollStudent(Student student, Course course) {


if ([Link]() > [Link]) {
[Link](course);
[Link](student);
[Link]++;
return true;
}
return false;
}

public static void assignGrade(Student student, Course course, int grade) {


[Link](course, grade);
}

public static Course getCourseByCode(String courseCode) {


return [Link]().filter(course ->
[Link]().equals(courseCode)).findFirst().orElse(null);
}

public static Student getStudentById(String studentId2) {


return [Link]().filter(student ->
[Link]().equals(studentId2)).findFirst().orElse(null);
}

public static int calculateOverallGrade(Student student) {


int total = 0;
for (Course course : [Link]()) {
total += [Link]().get(course);
}
9

return total / [Link]().size();


}

public static List<Course> getCourses() {


return [Link](courses);
}

public static List<Student> getStudents() {


return [Link](students);
}
}
Main Program:
Everything I did before was create parts of the program and now it’s time to put it
together. I am going to create file name Main and a class with the same name. It this I am going
to import BufferedReader which is used to read text input from the user line by line.
InputStreamReader which Bridges between character streams and byte streams it is often used
with BufferedReader. I am also going to import IOException for handling potential input / output
exceptions.
Now I am going to write main method with special keywords throws IOException to
throw an IOExpection to indicate potential issues with user input. Now I am going to create a
BufferedReader object to read user input from the console / terminal. After that I am going to
print menu on the console / terminal, then I will run while loop which will keep asking options
until the user enters 6 to exit the loop.
Inside the loop I am using switch statements to handles different options based on the
user’s input 1 for adding new course, 2 for enrolling new student, 3 for adding course to a
student, 3 for assign a grade, 5 for calculate overall grade, and default for handling invalid
options. After each case is executed the menu is reprinted and the loop continues until the user
exits the program.
Code:
import [Link];
import [Link];
import [Link];

public class Main {


public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader([Link]));

[Link]("1. Add new course\n2. Enroll new student\n3. Add course to a student\n4.
Assign grade\n5. Calculate overall grade\n6. Exit\nEnter option: ");
10

int option = [Link]([Link]()) ;

while (option != 6) {
switch (option) {
case 1:
[Link]("Enter course name: ");
String name = [Link]();
[Link]("Enter course code: ");
String code = [Link]();
[Link]("Enter course max capacity: ");
int maxCapacity = [Link]([Link]()) ;
[Link](name, code, maxCapacity);
[Link]("Course added successfully");
break;
case 2:
[Link]("Enter student name: ");
String studentName = [Link]();
[Link]("Enter student id: ");
String studentId = [Link]();
Student student = new Student(studentName, studentId);
[Link]("Enter course code: ");
String courseCode = [Link]();
Course course = [Link](courseCode);
if ([Link](student, course))
[Link]("Student enrolled successfully");
else
[Link]("Student could not be enrolled, course is full");
break;
case 3:
[Link]("Enter student id: ");
String studentId1 = [Link]();
Student student1 = [Link](studentId1);
[Link]("Enter course code: ");
String courseCode1 = [Link]();
Course course1 = [Link](courseCode1);
if ([Link](student1, course1))
[Link]("Course added to a student successfully");
else
[Link]("Course could not be added, course is full");
break;
case 4:
[Link]("Enter student id: ");
String studentId2 = [Link]();
11

Student student2 = [Link](studentId2);


[Link]("Enter course code: ");
String courseCode2 = [Link]();
Course course2 = [Link](courseCode2);
[Link]("Enter grade: ");
int grade = [Link]([Link]()) ;
[Link](student2, course2, grade);
[Link]("Grade assigned successfully");
break;
case 5:
[Link]("Enter student id: ");
String studentId3 = [Link]();
Student student3 = [Link](studentId3);
[Link]("Overall grade: " +
[Link](student3));
break;
default:
[Link]("Invalid option");
break;
}
[Link]("1. Add new course\n2. Enroll new student\n3. Add course to a student\
n4. Assign grade\n5. Calculate overall grade\n6. Exit\nEnter option: ");
option = [Link]([Link]()) ;
}
}
}
12

References
GeeksforGeeks. (2022, May 3). [Link] class in Java. GeeksforGeeks.

[Link]

GeeksforGeeks. (2020, June 21). [Link]() method in Java. GeeksforGeeks.

[Link]

Java ArrayList. (n.d.-a). [Link]

Java Constructors. (n.d.). [Link]

Java HashMap. (n.d.). [Link]

GeeksforGeeks. (2024, May 8). Map Interface in Java. GeeksforGeeks.

[Link]

GeeksforGeeks. (2024a, March 11). List Interface in Java with Examples. GeeksforGeeks.

[Link]

GeeksforGeeks. (2023, May 7). Collections class in Java. GeeksforGeeks.

[Link]

GeeksforGeeks. (2022a, February 21). InputStreamReader class in Java. GeeksforGeeks.

[Link]

GeeksforGeeks. (2024a, February 15). How to handle an IOException in Java? GeeksforGeeks.

[Link]

Javanotes 9, Section 5.1 -- Objects, instance methods, and instance variables. (n.d.).

[Link]

Javanotes 9, Section 5.2 -- Constructors and Object Initialization. (n.d.).

[Link]

Javanotes 9, Section 5.3 -- Programming with Objects. (n.d.).

[Link]
13

Javanotes 9, Section 5.4 -- Programming example: card, hand, deck. (n.d.).

[Link]

Javanotes 9, Section 5.6 -- this and super. (n.d.). [Link]

Common questions

Powered by AI

The system ensures data encapsulation and controlled access to private variables in the Student and Course classes through the use of private access modifiers and public getter and setter methods. This design pattern allows controlled exposure of object attributes, so that changes to properties like name, id, or grades are mediated through methods, maintaining encapsulation and allowing validation or transformation of data when modifying these properties .

Using Collections.unmodifiableList in the CourseManagement class is important because it returns an unmodifiable view of the specified list, preventing external modification. This ensures that the internal state of the course and student lists cannot be altered directly by other parts of the program, thereby preserving data integrity and encapsulation within the class .

In the Student class, ArrayList is used to store the enrolled courses as it provides a resizable array that is necessary for dynamically managing a list of courses each student has. The HashMap, on the other hand, stores grades associated with each Course object. It uses key-value pairs where a course object is a key and the corresponding grade is a value. Both structures are essential because they allow for efficient management of course and grade data, offering flexibility in storage and retrieval .

The static variable numEnrolled in the Course class serves to keep a global count of the number of students enrolled across all courses. This use of a static variable ensures that the enrollment count is shared and updated uniformly across all instances of the Course class, allowing centralized tracking of enrollment statistics which is critical for managing maximum course capacity efficiently .

The Main program uses BufferedReader in conjunction with InputStreamReader to read lines of user input from the console. BufferedReader is appropriate here because it efficiently reads text from an input character stream, buffering characters to provide fast and flexible input management. This is ideal for command-line applications where user input needs to be continuously read and processed, ensuring smooth interaction in the application loop .

The method enrollStudent in the CourseManagement class effectively handles capacity management by checking if the course's maxCapacity is greater than the static numEnrolled count before adding a student. This condition ensures that a new student can only be enrolled if the course is not already full, thereby maintaining course capacity constraints and preventing over-enrollment .

The main program employs a throws declaration for IOException in the main method to handle potential input/output issues. While this captures unexpected I/O errors, the exception handling could be refined by implementing more granular try-catch blocks around specific I/O operations to handle exceptional cases immediately, offering better error recovery and user feedback rather than exiting the program on a fault .

The CourseManagement class prevents creating instances by having a private constructor that throws an IllegalStateException if instantiated. This design signifies that the class is meant to serve as a utility class with static methods, highlighting its role as a service-oriented class that manages the operations related to courses and students without needing object instantiation .

The 'addCourse' method in the CourseManagement class is responsible for adding new courses to the course list by creating Course objects with specified name, code, and maxCapacity parameters. It is structured as a static method, allowing it to be invoked directly without creating an instance of CourseManagement, supporting the utility nature of the class and enabling centralized course management functionality .

The CourseManagement class utilizes streams, specifically with the getCourseByCode and getStudentById methods, to filter and retrieve objects based on specific criteria. By using streams, the class benefits from concise and readable code, potential performance improvements through internal iterations, and the ability to easily leverage parallel processing. These features make streams an efficient and modern approach to handling collections in complex applications .

You might also like