Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Programming Assignment Unit 5

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming Assignment Unit 5

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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) {


this.name = name;
this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getId() {


return id;
}
5

public void setId(String id) {


this.id = id;
}

public List<Course> getCourses() {


return courses;
}

public void addCourse(Course course) {


this.courses.add(course);
}

public void setGrade(Course course, int grade) {


this.grades.put(course, grade);
}

public Map<Course, Integer> getGrades() {


return grades;
}
}
Course Class:
Now I am going to create another file name course.java 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) {


this.name = name;
this.code = code;
this.maxCapacity = maxCapacity;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getCode() {


return code;
}

public void setCode(String code) {


this.code = code;
}

public int getMaxCapacity() {


7

return maxCapacity;
}

public void setMaxCapacity(int maxCapacity) {


this.maxCapacity = 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 Collections.unmodifiableList. This
will prevent callers from modifying the internal lists directly.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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);
courses.add(course);
}

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


if (course.getMaxCapacity() > Course.numEnrolled) {
student.addCourse(course);
students.add(student);
Course.numEnrolled++;
return true;
}
return false;
}

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


student.setGrade(course, grade);
}

public static Course getCourseByCode(String courseCode) {


return courses.stream().filter(course ->
course.getCode().equals(courseCode)).findFirst().orElse(null);
}

public static Student getStudentById(String studentId2) {


return students.stream().filter(student ->
student.getId().equals(studentId2)).findFirst().orElse(null);
}

public static int calculateOverallGrade(Student student) {


int total = 0;
for (Course course : student.getCourses()) {
total += student.getGrades().get(course);
}
9

return total / student.getCourses().size();


}

public static List<Course> getCourses() {


return Collections.unmodifiableList(courses);
}

public static List<Student> getStudents() {


return Collections.unmodifiableList(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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {


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

System.out.print("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 = Integer.parseInt(reader.readLine()) ;

while (option != 6) {
switch (option) {
case 1:
System.out.print("Enter course name: ");
String name = reader.readLine();
System.out.print("Enter course code: ");
String code = reader.readLine();
System.out.print("Enter course max capacity: ");
int maxCapacity = Integer.parseInt(reader.readLine()) ;
CourseManagement.addCourse(name, code, maxCapacity);
System.out.println("Course added successfully");
break;
case 2:
System.out.print("Enter student name: ");
String studentName = reader.readLine();
System.out.print("Enter student id: ");
String studentId = reader.readLine();
Student student = new Student(studentName, studentId);
System.out.print("Enter course code: ");
String courseCode = reader.readLine();
Course course = CourseManagement.getCourseByCode(courseCode);
if (CourseManagement.enrollStudent(student, course))
System.out.println("Student enrolled successfully");
else
System.out.println("Student could not be enrolled, course is full");
break;
case 3:
System.out.print("Enter student id: ");
String studentId1 = reader.readLine();
Student student1 = CourseManagement.getStudentById(studentId1);
System.out.print("Enter course code: ");
String courseCode1 = reader.readLine();
Course course1 = CourseManagement.getCourseByCode(courseCode1);
if (CourseManagement.enrollStudent(student1, course1))
System.out.println("Course added to a student successfully");
else
System.out.println("Course could not be added, course is full");
break;
case 4:
System.out.print("Enter student id: ");
String studentId2 = reader.readLine();
11

Student student2 = CourseManagement.getStudentById(studentId2);


System.out.print("Enter course code: ");
String courseCode2 = reader.readLine();
Course course2 = CourseManagement.getCourseByCode(courseCode2);
System.out.print("Enter grade: ");
int grade = Integer.parseInt(reader.readLine()) ;
CourseManagement.assignGrade(student2, course2, grade);
System.out.println("Grade assigned successfully");
break;
case 5:
System.out.print("Enter student id: ");
String studentId3 = reader.readLine();
Student student3 = CourseManagement.getStudentById(studentId3);
System.out.println("Overall grade: " +
CourseManagement.calculateOverallGrade(student3));
break;
default:
System.out.println("Invalid option");
break;
}
System.out.print("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 = Integer.parseInt(reader.readLine()) ;
}
}
}
12

References
GeeksforGeeks. (2022, May 3). Java.io.BufferedReader class in Java. GeeksforGeeks.

https://www.geeksforgeeks.org/java-io-bufferedreader-class-java/

GeeksforGeeks. (2020, June 21). Java.Util.ArrayList.add() method in Java. GeeksforGeeks.

https://www.geeksforgeeks.org/java-util-arraylist-add-method-java/

Java ArrayList. (n.d.-a). https://www.w3schools.com/java/java_arraylist.asp

Java Constructors. (n.d.). https://www.w3schools.com/java/java_constructors.asp

Java HashMap. (n.d.). https://www.w3schools.com/java/java_hashmap.asp

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

https://www.geeksforgeeks.org/map-interface-java-examples/

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

https://www.geeksforgeeks.org/list-interface-java-examples/

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

https://www.geeksforgeeks.org/collections-class-in-java/

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

https://www.geeksforgeeks.org/inputstreamreader-class-in-java/

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

https://www.geeksforgeeks.org/handle-an-ioexception-in-java/

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

https://math.hws.edu/javanotes/c5/s1.html

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

https://math.hws.edu/javanotes/c5/s2.html

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

https://math.hws.edu/javanotes/c5/s3.html
13

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

https://math.hws.edu/javanotes/c5/s4.html

Javanotes 9, Section 5.6 -- this and super. (n.d.). https://math.hws.edu/javanotes/c5/s6.html

You might also like