Files
oop/lab/lab3/Question6/Student.java
2022-03-10 17:18:03 +08:00

245 lines
6.2 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/03
* Description: Student class
*/
/**
* This class is used to store student information
*/
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;
/**
* Whether the student is sleeping in the lab, default is false
*/
private boolean sleeping;
/**
* 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;
// std is not sleeping by default
this.sleeping = false;
}
/**
* 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;
}
/**
* Getter A public getter for the sleeping
*
* @return the sleeping of the student
*/
public boolean isSleeping() {
return sleeping;
}
/**
* Makes the student fall asleep. When a student fall asleep, the grade of the
* student decraeses by one letter grade. Any other grade will be set to 'F'.
*
* @return void
*/
public void goToSleep() {
sleeping = true;
// set student grade decrase by one letter,
// if the grade is not one of these, set it to 'F'
if (this.grade >= 'A' && this.grade < 'F') {
this.grade++;
} else {
this.grade = 'F';
}
}
/**
* Wake up the student. When a student wakes up, the grade of the student
* does not go up.
*
* @return void
*/
public void wakeUp() {
sleeping = false;
}
/**
* 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');
// check default sleeping status
System.out.println(s4.isSleeping() == false);
System.out.println(s5.isSleeping() == false);
System.out.println(s6.isSleeping() == false);
// make students fall asleep
s4.goToSleep();
s5.goToSleep();
s6.goToSleep();
// check sleeping status after fall asleep
System.out.println(s4.isSleeping() == true);
System.out.println(s5.isSleeping() == true);
System.out.println(s6.isSleeping() == true);
// check grade after fall asleep
System.out.println(s4.getGrade() == 'C');
System.out.println(s5.getGrade() == 'D');
System.out.println(s6.getGrade() == 'E');
// student s6 sleep several times
s6.goToSleep();
s6.goToSleep();
s6.goToSleep();
s6.goToSleep();
// check grade after sleep several times
System.out.println(s6.getGrade() == 'F');
}
}