This commit is contained in:
2022-03-10 17:18:03 +08:00
commit d6c5f57935
17 changed files with 1138 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/03
* Description: Student class
*/
public class Student {
/**
* Student's ID number
*/
private int ID;
/**
* Student's name
*/
private String name;
/**
* A single character representing the grade of the student
*/
private char grade;
/**
* Constructor A public constructor with one parameter
*
* @param id the ID of the student, only accept positive integer if the id is
* not positive, it will be set to 0
*
* @param name the name of the student.
*/
public Student(int ID, String name) {
// if the id is not positive, set it to 0
if (ID > 0) {
this.ID = ID;
} else {
this.ID = 0;
}
// set the name
this.name = name;
// set the grade to default 'A'
this.grade = 'A';
}
/**
* Getter A public getter for the ID.
*
* @return the ID of the student
*/
public int getID() {
return ID;
}
/**
* Getter A public getter for the name.
*
* @return the name of the student
*/
public String getName() {
return name;
}
/**
* Setter A public setter for the name.
*
* @param name the name of the student
*
* @return void
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter A public getter for the grade.
*
* @return the grade of the student
*/
public char getGrade() {
return grade;
}
/**
* Setter A public setter for the grade.
*
* @param grade the grade of the student
*
* @return void
*/
public void setGrade(char grade) {
this.grade = grade;
}
/**
* Test method A public method to test the class
*
* @return void
*/
public static void testStudent() {
// create students
Student s1 = new Student(-1, "Walter");
Student s2 = new Student(0, "Yongyuan");
Student s3 = new Student(1, "Chen");
// print true for id
System.out.println(s1.getID() == 0);
System.out.println(s2.getID() == 0);
System.out.println(s3.getID() == 1);
// print true for name
System.out.println(s1.getName().equals("Walter"));
System.out.println(s2.getName().equals("Yongyuan"));
System.out.println(s3.getName().equals("Chen"));
// change the name
s1.setName("Walter Wang");
s2.setName("Yongyuan Wang");
s3.setName("Chen Wang");
// print true for name
System.out.println(s1.getName().equals("Walter Wang"));
System.out.println(s2.getName().equals("Yongyuan Wang"));
System.out.println(s3.getName().equals("Chen Wang"));
// check default grade
System.out.println(s1.getGrade() == 'A');
System.out.println(s2.getGrade() == 'A');
System.out.println(s3.getGrade() == 'A');
// set grade
s1.setGrade('B');
s2.setGrade('C');
s3.setGrade('D');
// check grade
System.out.println(s1.getGrade() == 'B');
System.out.println(s2.getGrade() == 'C');
System.out.println(s3.getGrade() == 'D');
}
}