Programming Assignment Unit 5
Programming Assignment Unit 5
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 Student() {
}
public Course() {
}
return maxCapacity;
}
private CourseManagement() {
throw new IllegalStateException("Utility class");
}
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
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
References
GeeksforGeeks. (2022, May 3). Java.io.BufferedReader class in Java. GeeksforGeeks.
https://www.geeksforgeeks.org/java-io-bufferedreader-class-java/
https://www.geeksforgeeks.org/java-util-arraylist-add-method-java/
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/
https://www.geeksforgeeks.org/collections-class-in-java/
https://www.geeksforgeeks.org/inputstreamreader-class-in-java/
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
https://math.hws.edu/javanotes/c5/s2.html
https://math.hws.edu/javanotes/c5/s3.html
13
https://math.hws.edu/javanotes/c5/s4.html