From d6c5f579355ce17e6e235264dd4489ce778f70a8 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 10 Mar 2022 17:18:03 +0800 Subject: [PATCH] init --- .gitignore | 1 + assignment1/Question1/AutoRobot.java | 183 +++++++++++++++++++ lab/lab2/Question1/SumAndAverage.java | 69 +++++++ lab/lab2/Question2/TimeTable.java | 65 +++++++ lab/lab2/Question3/TestPalindrome.java | 43 +++++ lab/lab3/Question1/Start.java | 5 + lab/lab3/Question1/Student.java | 47 +++++ lab/lab3/Question2/Start.java | 5 + lab/lab3/Question2/Student.java | 51 ++++++ lab/lab3/Question3/Start.java | 5 + lab/lab3/Question3/Student.java | 98 ++++++++++ lab/lab3/Question4/Start.java | 5 + lab/lab3/Question4/Student.java | 141 ++++++++++++++ lab/lab3/Question5/Start.java | 5 + lab/lab3/Question5/Student.java | 166 +++++++++++++++++ lab/lab3/Question6/Start.java | 5 + lab/lab3/Question6/Student.java | 244 +++++++++++++++++++++++++ 17 files changed, 1138 insertions(+) create mode 100644 .gitignore create mode 100644 assignment1/Question1/AutoRobot.java create mode 100644 lab/lab2/Question1/SumAndAverage.java create mode 100644 lab/lab2/Question2/TimeTable.java create mode 100644 lab/lab2/Question3/TestPalindrome.java create mode 100644 lab/lab3/Question1/Start.java create mode 100644 lab/lab3/Question1/Student.java create mode 100644 lab/lab3/Question2/Start.java create mode 100644 lab/lab3/Question2/Student.java create mode 100644 lab/lab3/Question3/Start.java create mode 100644 lab/lab3/Question3/Student.java create mode 100644 lab/lab3/Question4/Start.java create mode 100644 lab/lab3/Question4/Student.java create mode 100644 lab/lab3/Question5/Start.java create mode 100644 lab/lab3/Question5/Student.java create mode 100644 lab/lab3/Question6/Start.java create mode 100644 lab/lab3/Question6/Student.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/assignment1/Question1/AutoRobot.java b/assignment1/Question1/AutoRobot.java new file mode 100644 index 0000000..b648cc4 --- /dev/null +++ b/assignment1/Question1/AutoRobot.java @@ -0,0 +1,183 @@ +/* + * Authro: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/09 + * Description: This is the main class of AutoRobot. + */ + +public class AutoRobot { + /** + * current speed of the robot. + */ + private double currentSpeed; + + /** + * name of the robot + */ + private String name; + + /** + * Constructor for objects of class AutoRobot. Default name is "xyz". + * Initial speed is 0. + */ + public AutoRobot() { + // initialise instance variables + this.currentSpeed = 0; + this.name = "xyz"; + } + + /** + * Getter method for name. + * + * @return name of the robot. + */ + public String getName() { + return this.name; + } + + /** + * Getter method for current speed. + * + * @return current speed of the robot. + */ + public double getCurrentSpeed() { + return this.currentSpeed; + } + + /** + * Increase the speed of the robot by 5. The speed exceed 20 will + * print "Dangerous" message and do nothing. + * + * @return current speed of the robot. + */ + public void accelerate() { + if (this.currentSpeed >= 20) { + System.out.println("Dangerous! Please stay within speed limit."); + return; + } + + this.currentSpeed += 5; + } + + /** + * Decrase the speed of the robot by 5. The speed can not be less than 0. + * + * @return current speed of the robot. + */ + public void brake() { + if (this.currentSpeed - 5 <= 0) { + this.currentSpeed = 0; + return; + } + + this.currentSpeed -= 5; + } + + /** + * Print out current status. + * + * @return void. + */ + public void printStatus() { + System.out.println(String.format("Hello there, my name is %s and my current speed is %.1f", + this.name, this.currentSpeed)); + } + + /** + * Setter method for name. + * + * @param name name of the robot. + * @return void. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Static test the class AutoRobot. + * + * @return void. + */ + public static void testAutoRobot() { + AutoRobot robot = new AutoRobot(); + + // print current status + robot.printStatus(); + + // print true for default value + System.out.println(robot.getName().equals("xyz")); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 0)); + System.out.println(robot.getCurrentSpeed() == 0); + + // change name and print current status + robot.setName("ABC"); + + // print true for name + System.out.println(robot.getName().equals("ABC")); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 5)); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 10)); + + // brake and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 5)); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 10)); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 15)); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 20)); + + // accelerate and print true for current speed + robot.accelerate(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 20)); + + // break and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 15)); + + // brake and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 10)); + + // brake and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 5)); + + // brake and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 0)); + + // brake and print true for current speed + robot.brake(); + System.out.println(doubleEquals(robot.getCurrentSpeed(), 0)); + + // print current status + robot.printStatus(); + } + + public static void main(String[] args) { + testAutoRobot(); + } + + /** + * Compare double value. + * + * @param a double value. + * @param b double value. + * @return true if a and b are equal. + */ + public static boolean doubleEquals(double a, double b) { + return Math.abs(a - b) < 0.00001; + } +} diff --git a/lab/lab2/Question1/SumAndAverage.java b/lab/lab2/Question1/SumAndAverage.java new file mode 100644 index 0000000..084668e --- /dev/null +++ b/lab/lab2/Question1/SumAndAverage.java @@ -0,0 +1,69 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-02 + * Assignment: #1 Question 1 + * Description: This program is used to calculate the sum and average of the numbers. + * Input: The numbers + * Output: The sum and average of the numbers + * Version: 1.0 + */ +public class SumAndAverage { + public static void main(String[] args) { + int sum = 0; // Store the accumulated sum init to 0 + double average; // average in double + int lowerbound = 1; // The lowerbound to sum + int upperbound = 100; // The upperbound to sum + + // Use a for-loop to sum from lowerbound to upperbound + for (int number = lowerbound; number <= upperbound; number++) { + // add only odds number + if (number % 2 != 0) { + sum += number; + } + } + + // Compute average in double. Beware that int/int produces int. + average = (double) sum / (upperbound - lowerbound + 1); + + // Print sum and average + System.out.println("The sum is " + sum); + System.out.println("The average is " + average); + + // 6.1 while loop + System.out.println("---- While-loop:"); + sum = 0; + int number = lowerbound; + while (number <= upperbound) { + // only add number that is divisible by 7 + if (number % 7 == 0) { + sum += number; + } + number++; + } + + // Compute average in double. Beware that int/int produces int. + average = (double) sum / (upperbound - lowerbound + 1); + + // Print sum and average + System.out.println("The sum is " + sum); + System.out.println("The average is " + average); + + // 6.2 do-while + System.out.println("---- Do-while:"); + sum = 0; // Reset sum + number = lowerbound; + do { + // sum of squares of number + sum += number * number; + number++; + } while (number <= upperbound); + + // Compute average in double. Beware that int/int produces int. + average = (double) sum / (upperbound - lowerbound + 1); + + // Print sum and average + System.out.println("The sum is " + sum); + System.out.println("The average is " + average); + + } +} diff --git a/lab/lab2/Question2/TimeTable.java b/lab/lab2/Question2/TimeTable.java new file mode 100644 index 0000000..6e0671b --- /dev/null +++ b/lab/lab2/Question2/TimeTable.java @@ -0,0 +1,65 @@ +/* + * Authro: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-02 + * Description: A table of multiplication table of 1 to 9. + * Using nested for loop to print the table. + * Assignemnt: #2 Question2 + * Output: + +* | 1 2 3 4 5 6 7 8 9 +--------------------------------------- +1 | 1 2 3 4 5 6 7 8 9 +2 | 2 4 6 8 10 12 14 16 18 +3 | 3 6 9 12 15 18 21 24 27 +4 | 4 8 12 16 20 24 28 32 36 +5 | 5 10 15 20 25 30 35 40 45 +6 | 6 12 18 24 30 36 42 48 54 +7 | 7 14 21 28 35 42 49 56 63 +8 | 8 16 24 32 40 48 56 64 72 +9 | 9 18 27 36 45 54 63 72 81 + + */ + +public class TimeTable { + public static void printHeader() { + System.out.println("* | 1 2 3 4 5 6 7 8 9"); + System.out.println("---------------------------------------"); + } + public static void printLineBegin(int i) { + System.out.print(i + " |"); + } + public static int spaceBefore(int i) { + // format the number to be printed + String s = Integer.toString(i); + // calculate the number of spaces before the number + int len = s.length(); + // return the number of spaces + int space = 4 - len; + return space; + } + public static void main(String[] args) { + // print the two lines header + printHeader(); + + // loop for main content + for (int i = 1; i <= 9; i++) { + + printLineBegin(i); + + // loop for each number in the line + for (int j = 1; j <= 9; j++) { + int number = i * j; + + // print the space before the number + for (int k = 0; k < spaceBefore(number); k++) { + System.out.print(" "); + } + + // print the number + System.out.print(number); + } + // print the end of the line + System.out.println(); + } + } +} diff --git a/lab/lab2/Question3/TestPalindrome.java b/lab/lab2/Question3/TestPalindrome.java new file mode 100644 index 0000000..3cb4252 --- /dev/null +++ b/lab/lab2/Question3/TestPalindrome.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-02 + * Description: Test whether a sentence is a palindrome, iguore the case punctuation and spaces. + * Assignment: #2 Question 3 + */ + +import java.util.Scanner; + +public class TestPalindrome { + public static void main(String[] args) { + System.out.print("Enter a String: "); + Scanner in = new Scanner(System.in); + + // read a line of input from the user + String inStr = in.nextLine(); + + // close the scanner object + in.close(); + + // convert the string to lower case + String str = inStr.toLowerCase(); + + // remove all the punctuation using Character.isLetter() + // str = str.replaceAll("[^a-zA-Z0-9]", ""); + String str2 = str; + str = ""; + for (int i = 0; i < str2.length(); i++) { + if (Character.isLetter(str2.charAt(i))) { + str += str2.charAt(i); + } + } + + // reverse the string + String reverse = new StringBuffer(str).reverse().toString(); + + // compare the two strings + // if they are the same, the string is a palindrome + // otherwise, it is not + System.out.format("\"%s\" is %s a palindrome.\n", inStr, + str.equals(reverse) ? "" : "not "); + } +} diff --git a/lab/lab3/Question1/Start.java b/lab/lab3/Question1/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question1/Student.java b/lab/lab3/Question1/Student.java new file mode 100644 index 0000000..c658eb1 --- /dev/null +++ b/lab/lab3/Question1/Student.java @@ -0,0 +1,47 @@ +/* + * 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; + + /** + * 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 + */ + public Student(int ID) { + this.ID = ID; + } + + /** + * Getter A public getter for the ID. + * + * @return the ID of the student + */ + public int getID() { + return ID; + } + + /** + * Test method A public method to test the class + * + * @return void + */ + public static void testStudent() { + Student s1 = new Student(-39); + Student s2 = new Student(114514); + Student s3 = new Student(0); + + // print true + System.out.println(s1.getID() == -39); + System.out.println(s2.getID() == 114514); + System.out.println(s3.getID() == 0); + } +} diff --git a/lab/lab3/Question2/Start.java b/lab/lab3/Question2/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question2/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question2/Student.java b/lab/lab3/Question2/Student.java new file mode 100644 index 0000000..c20e9b1 --- /dev/null +++ b/lab/lab3/Question2/Student.java @@ -0,0 +1,51 @@ +/* + * 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; + + /** + * 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 + */ + public Student(int ID) { + if (ID > 0) { + this.ID = ID; + } else { + this.ID = 0; + } + } + + /** + * Getter A public getter for the ID. + * + * @return the ID of the student + */ + public int getID() { + return ID; + } + + /** + * Test method A public method to test the class + * + * @return void + */ + public static void testStudent() { + Student s1 = new Student(-39); + Student s2 = new Student(114514); + Student s3 = new Student(0); + + // print true + System.out.println(s1.getID() == 0); + System.out.println(s2.getID() == 114514); + System.out.println(s3.getID() == 0); + } +} diff --git a/lab/lab3/Question3/Start.java b/lab/lab3/Question3/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question3/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question3/Student.java b/lab/lab3/Question3/Student.java new file mode 100644 index 0000000..32879bb --- /dev/null +++ b/lab/lab3/Question3/Student.java @@ -0,0 +1,98 @@ +/* + * 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")); + } +} diff --git a/lab/lab3/Question4/Start.java b/lab/lab3/Question4/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question4/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question4/Student.java b/lab/lab3/Question4/Student.java new file mode 100644 index 0000000..49d0954 --- /dev/null +++ b/lab/lab3/Question4/Student.java @@ -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'); + } +} diff --git a/lab/lab3/Question5/Start.java b/lab/lab3/Question5/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question5/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question5/Student.java b/lab/lab3/Question5/Student.java new file mode 100644 index 0000000..a3e1c39 --- /dev/null +++ b/lab/lab3/Question5/Student.java @@ -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'); + } +} diff --git a/lab/lab3/Question6/Start.java b/lab/lab3/Question6/Start.java new file mode 100644 index 0000000..6e3343e --- /dev/null +++ b/lab/lab3/Question6/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab3/Question6/Student.java b/lab/lab3/Question6/Student.java new file mode 100644 index 0000000..23deed1 --- /dev/null +++ b/lab/lab3/Question6/Student.java @@ -0,0 +1,244 @@ +/* + * 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'); + } +}