Programming Assignment Unit 5
Department of Computer Science, University of the People
CS 1102: Programming 1
Kwok Wing Cheung
October 13, 2023
Here is a java program of a university management system to add course, enrol a student
to a course, give the student a grade to a specific course and get the overall grade of the student:
Student Class:
import [Link];
import [Link];
public class Student {
private String name;
private int id;
private List<Course> enrolledCourses;
private List<Double> grades; // Assuming each index corresponds to the grade for the
corresponding course
public Student(String name, int id) {
[Link] = name;
[Link] = id;
[Link] = new ArrayList<>();
[Link] = new ArrayList<>();
}
// Getter and Setter methods for name and id
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
// Getter method for enrolledCourses
public List<Course> getEnrolledCourses() {
return enrolledCourses;
}
// Method to enroll students in courses
public void enrollInCourse(Course course) {
[Link](course);
[Link](0.0); // Initialize the grade for the new course to 0
}
// Method to assign grades to students
public void assignGrade(Course course, double grade) {
int index = [Link](course);
if (index != -1) {
[Link](index, grade);
} else {
[Link]("Student is not enrolled in the specified course.");
}
}
// Method to get overall grade
public double calculateOverallGrade() {
if ([Link]()) {
return 0.0; // Handle the case when the student is not enrolled in any courses
}
double totalGrades = 0.00;
for (double grade : grades) {
totalGrades += grade;
}
return totalGrades / (double) [Link]();
}
}
Course Class:
import [Link];
import [Link];
public class Course {
private String courseCode; // to store course code
private String courseName; // to store name of the course
private int maxCapacity; // to store the maximum number of students can enroll
private static int totalEnrolledStudents = 0; // to store the total number of students
of that course, the base result is 0
private List<Student> enrolledStudents;
// instance of Course class
public Course(String courseCode, String courseName, int maxCapacity) {
[Link] = courseCode;
[Link] = courseName;
[Link] = maxCapacity;
[Link] = new ArrayList<>();
}
// Getter methods for each attribute
public String getCourseCode() {
return courseCode;
}
public String getCourseName() {
return courseName;
}
public int getmaxCapacity() {
return maxCapacity;
}
public static int getTotalEnrolledStudents() {
return totalEnrolledStudents;
}
// Method to enroll students in the course
public void enrollStudent(Student student) {
if ([Link]() < maxCapacity) {
[Link](student);
totalEnrolledStudents++;
[Link](this);
} else {
[Link]("Course is full. Cannot enroll more students.");
}
}
}
CourseManagement Class:
import [Link];
import [Link];
public class CourseManagement {
private static List<Course> courses = new ArrayList<>(); // to store all the courses
private static List<Student> students = new ArrayList<>(); // to store a list of all students
// to add a course to the course list
public static void addCourse(String courseCode, String courseName, int maxCapacity) {
Course course = new Course(courseCode, courseName, maxCapacity);
[Link](course);
}
// getter methods to get student and course data
public static List<Course> getCourses() {
return courses;
}
public static List<Student> getStudents() {
return students;
}
// to enroll a student to a specific course
public static void enrollStudent(Student student, Course course) {
[Link](student);
[Link](student);
}
// to assign a grade for a specific course to a student
public static void assignGrade(Student student, Course course, double grade) {
[Link](course, grade);
}
// to calculate the overall grade of the student
public static double calculateOverallGrade(Student student) {
return [Link]();
}
}
Administrative Interface (The main command line program):
import [Link];
public class AdministratorInterface {
private static Scanner scanner = new Scanner([Link]);
public static void main(String[] args) {
while (true) {
[Link]("\nAdministrator Menu:");
[Link]("1. Add a new course");
[Link]("2. Enroll a student");
[Link]("3. Assign a grade to a student");
[Link]("4. Calculate overall grade for a student");
[Link]("5. Exit");
int choice = [Link]();
switch (choice) {
case 1:
addCourse();
break;
case 2:
enrollStudent();
break;
case 3:
assignGrade();
break;
case 4:
calculateOverallGrade();
break;
case 5:
[Link]("Exiting the program. Goodbye!");
[Link](0);
default:
[Link]("Invalid choice. Please try again.");
}
}
}
// Method to add a new course
private static void addCourse() {
[Link]("Enter course code: ");
String courseCode = [Link]();
// Consume the newline character
[Link]();
[Link]("Enter course name: ");
String courseName = [Link]();
[Link]("Enter maximum capacity: ");
int maxCapacity = [Link]();
[Link](courseCode, courseName, maxCapacity);
[Link]("Course added successfully!");
}
// Method to enroll a student
private static void enrollStudent() {
// Get student details
[Link]("Enter student name: ");
String studentName = [Link]();
[Link]();
[Link]("Enter student ID: ");
int studentId = [Link]();
Student student = new Student(studentName, studentId);
// Get course details
[Link]("Enter course code to enroll the student: ");
String courseCode = [Link]();
Course course = findCourse(courseCode);
if (course != null) {
[Link](student, course);
[Link]("Student enrolled successfully!");
} else {
[Link]("Course not found.");
}
}
// Method to assign a grade to a student
private static void assignGrade() {
// Get student details
[Link]("Enter student ID: ");
int studentId = [Link]();
Student student = findStudent(studentId);
if (student != null) {
// Get course details
[Link]("Enter course code: ");
String courseCode = [Link]();
Course course = findCourse(courseCode);
if (course != null) {
// Get grade
[Link]("Enter grade for the student: ");
double grade = [Link]();
[Link](student, course, grade);
[Link]("Grade assigned successfully!");
} else {
[Link]("Course not found.");
}
} else {
[Link]("Student not found.");
}
}
// Method to calculate overall grade for a student
private static void calculateOverallGrade() {
// Get student details
[Link]("Enter student ID: ");
int studentId = [Link]();
Student student = findStudent(studentId);
if (student != null) {
double overallGrade = [Link](student);
[Link]("Overall grade for the student: " + overallGrade);
} else {
[Link]("Student not found.");
}
}
// Helper method to find a course by its code
private static Course findCourse(String courseCode) {
for (Course course : [Link]()) {
if ([Link]().equals(courseCode)) {
return course;
}
}
return null;
}
// Helper method to find a student by their ID
private static Student findStudent(int studentId) {
for (Student student : [Link]()) {
if ([Link]() == studentId) {
return student;
}
}
return null;
}
}
Here is a sample output for the program above: