init
This commit is contained in:
166
lab/lab3/Question5/Student.java
Normal file
166
lab/lab3/Question5/Student.java
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* 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) {
|
||||
this(ID, name, 'A');
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor A public constructor with three parameters
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @param grade the grade of the student, if the grade is not one of these, it
|
||||
* will be set to 'A'
|
||||
*/
|
||||
public Student(int ID, String name, char grade) {
|
||||
// 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
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
|
||||
// create students with grade different from A
|
||||
Student s4 = new Student(2, "Chen", 'B');
|
||||
Student s5 = new Student(3, "Yongyuan", 'C');
|
||||
Student s6 = new Student(4, "Walter", 'D');
|
||||
|
||||
// print true for grade
|
||||
System.out.println(s4.getGrade() == 'B');
|
||||
System.out.println(s5.getGrade() == 'C');
|
||||
System.out.println(s6.getGrade() == 'D');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user