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

Programming Assignment Unit 5

This document describes a university management system with classes for Students, Courses, and CourseManagement. The system allows adding courses, enrolling students in courses, assigning grades, and calculating overall grades. An administrative interface provides a menu to interact with the system.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Programming Assignment Unit 5

This document describes a university management system with classes for Students, Courses, and CourseManagement. The system allows adding courses, enrolling students in courses, assigning grades, and calculating overall grades. An administrative interface provides a menu to interact with the system.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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 java.util.ArrayList;
import java.util.List;

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) {


this.name = name;
this.id = id;
this.enrolledCourses = new ArrayList<>();
this.grades = new ArrayList<>();
}

// Getter and Setter methods for name and id


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

// Getter method for enrolledCourses


public List<Course> getEnrolledCourses() {
return enrolledCourses;
}

// Method to enroll students in courses


public void enrollInCourse(Course course) {
enrolledCourses.add(course);
grades.add(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 = enrolledCourses.indexOf(course);
if (index != -1) {
grades.set(index, grade);
} else {
System.out.println("Student is not enrolled in the specified course.");
}
}

// Method to get overall grade


public double calculateOverallGrade() {
if (enrolledCourses.isEmpty()) {
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) enrolledCourses.size();


}
}

Course Class:
import java.util.ArrayList;
import java.util.List;

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) {
this.courseCode = courseCode;
this.courseName = courseName;
this.maxCapacity = maxCapacity;
this.enrolledStudents = 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 (enrolledStudents.size() < maxCapacity) {
enrolledStudents.add(student);
totalEnrolledStudents++;
student.enrollInCourse(this);
} else {
System.out.println("Course is full. Cannot enroll more students.");
}
}
}
CourseManagement Class:
import java.util.ArrayList;
import java.util.List;

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

// to assign a grade for a specific course to a student


public static void assignGrade(Student student, Course course, double grade) {
student.assignGrade(course, grade);
}

// to calculate the overall grade of the student


public static double calculateOverallGrade(Student student) {
return student.calculateOverallGrade();
}
}
Administrative Interface (The main command line program):
import java.util.Scanner;

public class AdministratorInterface {


private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {


while (true) {
System.out.println("\nAdministrator Menu:");
System.out.println("1. Add a new course");
System.out.println("2. Enroll a student");
System.out.println("3. Assign a grade to a student");
System.out.println("4. Calculate overall grade for a student");
System.out.println("5. Exit");

int choice = scanner.nextInt();

switch (choice) {
case 1:
addCourse();
break;
case 2:
enrollStudent();
break;
case 3:
assignGrade();
break;
case 4:
calculateOverallGrade();
break;
case 5:
System.out.println("Exiting the program. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
// Method to add a new course
private static void addCourse() {
System.out.print("Enter course code: ");
String courseCode = scanner.next();

// Consume the newline character


scanner.nextLine();

System.out.print("Enter course name: ");


String courseName = scanner.nextLine();

System.out.print("Enter maximum capacity: ");


int maxCapacity = scanner.nextInt();

CourseManagement.addCourse(courseCode, courseName, maxCapacity);


System.out.println("Course added successfully!");
}

// Method to enroll a student


private static void enrollStudent() {
// Get student details
System.out.print("Enter student name: ");
String studentName = scanner.next();

scanner.nextLine();

System.out.print("Enter student ID: ");


int studentId = scanner.nextInt();

Student student = new Student(studentName, studentId);

// Get course details


System.out.print("Enter course code to enroll the student: ");
String courseCode = scanner.next();

Course course = findCourse(courseCode);

if (course != null) {
CourseManagement.enrollStudent(student, course);
System.out.println("Student enrolled successfully!");
} else {
System.out.println("Course not found.");
}
}

// Method to assign a grade to a student


private static void assignGrade() {
// Get student details
System.out.print("Enter student ID: ");
int studentId = scanner.nextInt();

Student student = findStudent(studentId);

if (student != null) {
// Get course details
System.out.print("Enter course code: ");
String courseCode = scanner.next();

Course course = findCourse(courseCode);

if (course != null) {
// Get grade
System.out.print("Enter grade for the student: ");
double grade = scanner.nextDouble();

CourseManagement.assignGrade(student, course, grade);


System.out.println("Grade assigned successfully!");
} else {
System.out.println("Course not found.");
}
} else {
System.out.println("Student not found.");
}
}

// Method to calculate overall grade for a student


private static void calculateOverallGrade() {
// Get student details
System.out.print("Enter student ID: ");
int studentId = scanner.nextInt();

Student student = findStudent(studentId);


if (student != null) {
double overallGrade = CourseManagement.calculateOverallGrade(student);
System.out.println("Overall grade for the student: " + overallGrade);
} else {
System.out.println("Student not found.");
}
}

// Helper method to find a course by its code


private static Course findCourse(String courseCode) {
for (Course course : CourseManagement.getCourses()) {
if (course.getCourseCode().equals(courseCode)) {
return course;
}
}
return null;
}

// Helper method to find a student by their ID


private static Student findStudent(int studentId) {
for (Student student : CourseManagement.getStudents()) {
if (student.getId() == studentId) {
return student;
}
}
return null;
}
}
Here is a sample output for the program above:

You might also like