99 lines
2.3 KiB
Java
99 lines
2.3 KiB
Java
/*
|
|
* 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;
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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 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"));
|
|
}
|
|
}
|