Skip to content

Instantly share code, notes, and snippets.

@StefanoFrazzetto
Created March 30, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanoFrazzetto/68b0ee884e87adfde92398e66a17c0b1 to your computer and use it in GitHub Desktop.
Save StefanoFrazzetto/68b0ee884e87adfde92398e66a17c0b1 to your computer and use it in GitHub Desktop.
Java - OOP - CSCU9A2
class ModuleNotFoundException extends Exception {
public ModuleNotFoundException(String errorMessage) {
super(errorMessage);
}
}
/**
* Write a description of class StudentAcademicHistory here.
*
* @author (your name)
* @version (a version number or a date)
*/
// add module: name and code
// mark module as completed
// student's information: name, id, etc.
// update mark for module
public class StudentAcademicHistory
{
private static final int MAX_MODULES = 10;
private String studentFullName;
private String studentId;
private Module[] modules = new Module[MAX_MODULES];
private int numberOfModules = 0;
// ["Java and GUI"] ["Networks"] ["..."]
// ["CSCU9A2" ] ["CSCU9AX" ] ["..."]
// [""] ["" ] ["..."]
// ^
//private String[] moduleNames = new String[MAX_MODULES];
//private String[] moduleCodes = new String[MAX_MODULES];
//private String[] moduleGrades = new String[MAX_MODULES];
//private boolean[] moduleCompleted = new boolean[MAX_MODULES];
public StudentAcademicHistory(String name, String id) {
studentFullName = name;
studentId = id;
}
public void addModule(String name, String code) {
modules[numberOfModules] = new Module(name, code);
numberOfModules++;
}
public void addModuleGrade(String code, String grade) throws ModuleNotFoundException {
for (int i = 0; i < numberOfModules; i++) {
Module currentModule = modules[i];
String currentModuleCode = currentModule.getCode();
if (code.equals(currentModuleCode)) {
currentModule.setGrade(grade);
// currentModule is a reference to the Module object in modules[i],
// so by calling this method here, the value of the object's attribute
// will be updated.
return;
}
}
// module not found
throw new ModuleNotFoundException(code);
}
public void printModulesInfo() {
for (int i = 0; i < numberOfModules; i++) {
System.out.println(modules[i]);
}
}
public static void main(String[] args) {
try {
StudentAcademicHistory sah = new StudentAcademicHistory("Jane Doe", "xyz");
sah.addModule("Java and GUI", "CSCU9A2");
sah.addModuleGrade("CSCU9A2", "9.5");
sah.printModulesInfo();
} catch (ModuleNotFoundException e) {
System.out.println(e);
}
}
}
/**
* Module contains the information about a module
* taken by a student.
* (This should be stored in a separate file called Module.java)
*/
class Module {
private String name;
private String code;
private String grade = "N/A";
private boolean completed = false;
public Module(String name, String code) {
this.name = name;
this.code = code;
}
public String getCode() {
return code;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String toString() {
return String.format("%s %s %s %b\n", name, code, grade, completed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment