From 6bf672bd5bf9c610074295964383fbd5bb4bffee Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 17 Mar 2022 18:18:06 +0800 Subject: [PATCH] Lab 4 --- lab/lab4/Question1/Start.java | 5 ++ lab/lab4/Question1/Student.java | 82 +++++++++++++++++++++++++++++ lab/lab4/Question2/Start.java | 30 +++++++++++ lab/lab4/Question2/Student.java | 82 +++++++++++++++++++++++++++++ lab/lab4/Question3/Chicken.java | 92 +++++++++++++++++++++++++++++++++ lab/lab4/Question3/Start.java | 32 ++++++++++++ lab/lab4/Question3/Student.java | 82 +++++++++++++++++++++++++++++ lab/lab4/Question4/Chicken.java | 92 +++++++++++++++++++++++++++++++++ lab/lab4/Question4/Start.java | 52 +++++++++++++++++++ lab/lab4/Question4/Student.java | 82 +++++++++++++++++++++++++++++ 10 files changed, 631 insertions(+) create mode 100644 lab/lab4/Question1/Start.java create mode 100644 lab/lab4/Question1/Student.java create mode 100644 lab/lab4/Question2/Start.java create mode 100644 lab/lab4/Question2/Student.java create mode 100644 lab/lab4/Question3/Chicken.java create mode 100644 lab/lab4/Question3/Start.java create mode 100644 lab/lab4/Question3/Student.java create mode 100644 lab/lab4/Question4/Chicken.java create mode 100644 lab/lab4/Question4/Start.java create mode 100644 lab/lab4/Question4/Student.java diff --git a/lab/lab4/Question1/Start.java b/lab/lab4/Question1/Start.java new file mode 100644 index 0000000..402ffc0 --- /dev/null +++ b/lab/lab4/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Student.testStudent(); + } +} diff --git a/lab/lab4/Question1/Student.java b/lab/lab4/Question1/Student.java new file mode 100644 index 0000000..3b910f6 --- /dev/null +++ b/lab/lab4/Question1/Student.java @@ -0,0 +1,82 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-10 + * Description: Student class + */ + +public class Student { + + /* + * Student's ID number + */ + private int ID; + + /* + * whether the student is currently sleeping or not, default is false + */ + private boolean sleeping; + + /** + * Constructor. Initialize the ID number and sleeping status as false. + * + * @param ID, the ID number of the student + */ + public Student(int ID) { + this.ID = ID; + this.sleeping = false; + } + + /** + * Getter of the ID number. + * + * @return the ID number of the student + */ + public int getID() { + return this.ID; + } + + /** + * Identify whether the student is sleeping or not. + * + * @return true if the student is sleeping, false otherwise + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Make the student fall in sleep. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Walkup the student. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Test student class. + */ + public static void testStudent() { + Student s = new Student(1234567890); + + System.out.println(s.getID() == 1234567890); + System.out.println(s.isSleeping() == false); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + } +} diff --git a/lab/lab4/Question2/Start.java b/lab/lab4/Question2/Start.java new file mode 100644 index 0000000..86e3ed5 --- /dev/null +++ b/lab/lab4/Question2/Start.java @@ -0,0 +1,30 @@ +/* + * Explain: + * The check method is static because it is do not need to create an Start object. + */ + +public class Start { + public static void main(String[] args) { + Student.testStudent(); + + Student stu = new Student(123); + System.out.println(check(stu) == "need coffee"); + stu.fallAsleep(); + System.out.println(check(stu) == "sweet dreams"); + } + + /** + * check student sleeping. If student is sleeping, return "sweet dreams", if + * not, return "need coffee". + * + * @param stu. Student object + * @return String. + */ + public static String check(Student stu) { + if (stu.isSleeping()) { + return "sweet dreams"; + } else { + return "need coffee"; + } + } +} diff --git a/lab/lab4/Question2/Student.java b/lab/lab4/Question2/Student.java new file mode 100644 index 0000000..3b910f6 --- /dev/null +++ b/lab/lab4/Question2/Student.java @@ -0,0 +1,82 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-10 + * Description: Student class + */ + +public class Student { + + /* + * Student's ID number + */ + private int ID; + + /* + * whether the student is currently sleeping or not, default is false + */ + private boolean sleeping; + + /** + * Constructor. Initialize the ID number and sleeping status as false. + * + * @param ID, the ID number of the student + */ + public Student(int ID) { + this.ID = ID; + this.sleeping = false; + } + + /** + * Getter of the ID number. + * + * @return the ID number of the student + */ + public int getID() { + return this.ID; + } + + /** + * Identify whether the student is sleeping or not. + * + * @return true if the student is sleeping, false otherwise + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Make the student fall in sleep. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Walkup the student. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Test student class. + */ + public static void testStudent() { + Student s = new Student(1234567890); + + System.out.println(s.getID() == 1234567890); + System.out.println(s.isSleeping() == false); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + } +} diff --git a/lab/lab4/Question3/Chicken.java b/lab/lab4/Question3/Chicken.java new file mode 100644 index 0000000..a382da3 --- /dev/null +++ b/lab/lab4/Question3/Chicken.java @@ -0,0 +1,92 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-17 + * Description: This is the Chicken class. + */ + +public class Chicken { + /* + * The weight instance variable indicates the current weight of the chiken. + */ + private double weight; + + /* + * The sleeping instance variable indicates whether the chicken is currently + * sleeping or not. + * It is intially sleeping. + */ + private boolean sleeping; + + /** + * Constructor: Chicken(double weight) + * + * @param weight + * The weight of the chicken. + * The weight must be greater than 0. If the weight is less than + * 0, the weight is set to 0. + */ + public Chicken(double weight) { + if (weight < 0) { + this.weight = 0; + } else { + this.weight = weight; + } + + // initially the chicken is sleeping + this.sleeping = true; + } + + /** + * Getter method: getWeight() + * + * @return The weight of the chicken. + */ + public double getWeight() { + return this.weight; + } + + /** + * Getter method: isSleeping() + * + * @return true if the chicken is sleeping, false otherwise. + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Makes the chicken fall asleep. Or do nothing if the chicken is already + * sleeping. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Makes the chicken wake up. Or do nothing if the chicken is already awake. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Tests function. + */ + public static void testChicken() { + Chicken c = new Chicken(2.3); + System.out.println(c.getWeight() == 2.3); + System.out.println(c.isSleeping() == true); + c.wakeUp(); + System.out.println(c.isSleeping() == false); + c.wakeUp(); // should do nothing because the chicken is already awake + System.out.println(c.isSleeping() == false); + c.fallAsleep(); + System.out.println(c.isSleeping() == true); + c.fallAsleep(); // should do nothing because the chicken is already sleeping + System.out.println(c.isSleeping() == true); + } +} diff --git a/lab/lab4/Question3/Start.java b/lab/lab4/Question3/Start.java new file mode 100644 index 0000000..833c306 --- /dev/null +++ b/lab/lab4/Question3/Start.java @@ -0,0 +1,32 @@ +/* + * Explain: + * The check method is static because it is do not need to create an Start object. + */ + +public class Start { + public static void main(String[] args) { + Student.testStudent(); + + Student stu = new Student(123); + System.out.println(check(stu) == "need coffee"); + stu.fallAsleep(); + System.out.println(check(stu) == "sweet dreams"); + + Chicken.testChicken(); + } + + /** + * check student sleeping. If student is sleeping, return "sweet dreams", if + * not, return "need coffee". + * + * @param stu. Student object + * @return String. + */ + public static String check(Student stu) { + if (stu.isSleeping()) { + return "sweet dreams"; + } else { + return "need coffee"; + } + } +} diff --git a/lab/lab4/Question3/Student.java b/lab/lab4/Question3/Student.java new file mode 100644 index 0000000..3b910f6 --- /dev/null +++ b/lab/lab4/Question3/Student.java @@ -0,0 +1,82 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-10 + * Description: Student class + */ + +public class Student { + + /* + * Student's ID number + */ + private int ID; + + /* + * whether the student is currently sleeping or not, default is false + */ + private boolean sleeping; + + /** + * Constructor. Initialize the ID number and sleeping status as false. + * + * @param ID, the ID number of the student + */ + public Student(int ID) { + this.ID = ID; + this.sleeping = false; + } + + /** + * Getter of the ID number. + * + * @return the ID number of the student + */ + public int getID() { + return this.ID; + } + + /** + * Identify whether the student is sleeping or not. + * + * @return true if the student is sleeping, false otherwise + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Make the student fall in sleep. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Walkup the student. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Test student class. + */ + public static void testStudent() { + Student s = new Student(1234567890); + + System.out.println(s.getID() == 1234567890); + System.out.println(s.isSleeping() == false); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + } +} diff --git a/lab/lab4/Question4/Chicken.java b/lab/lab4/Question4/Chicken.java new file mode 100644 index 0000000..a382da3 --- /dev/null +++ b/lab/lab4/Question4/Chicken.java @@ -0,0 +1,92 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-17 + * Description: This is the Chicken class. + */ + +public class Chicken { + /* + * The weight instance variable indicates the current weight of the chiken. + */ + private double weight; + + /* + * The sleeping instance variable indicates whether the chicken is currently + * sleeping or not. + * It is intially sleeping. + */ + private boolean sleeping; + + /** + * Constructor: Chicken(double weight) + * + * @param weight + * The weight of the chicken. + * The weight must be greater than 0. If the weight is less than + * 0, the weight is set to 0. + */ + public Chicken(double weight) { + if (weight < 0) { + this.weight = 0; + } else { + this.weight = weight; + } + + // initially the chicken is sleeping + this.sleeping = true; + } + + /** + * Getter method: getWeight() + * + * @return The weight of the chicken. + */ + public double getWeight() { + return this.weight; + } + + /** + * Getter method: isSleeping() + * + * @return true if the chicken is sleeping, false otherwise. + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Makes the chicken fall asleep. Or do nothing if the chicken is already + * sleeping. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Makes the chicken wake up. Or do nothing if the chicken is already awake. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Tests function. + */ + public static void testChicken() { + Chicken c = new Chicken(2.3); + System.out.println(c.getWeight() == 2.3); + System.out.println(c.isSleeping() == true); + c.wakeUp(); + System.out.println(c.isSleeping() == false); + c.wakeUp(); // should do nothing because the chicken is already awake + System.out.println(c.isSleeping() == false); + c.fallAsleep(); + System.out.println(c.isSleeping() == true); + c.fallAsleep(); // should do nothing because the chicken is already sleeping + System.out.println(c.isSleeping() == true); + } +} diff --git a/lab/lab4/Question4/Start.java b/lab/lab4/Question4/Start.java new file mode 100644 index 0000000..f8b9d5c --- /dev/null +++ b/lab/lab4/Question4/Start.java @@ -0,0 +1,52 @@ +/* + * Explain: + * The check method is static because it is do not need to create an Start object. + */ + +public class Start { + public static void main(String[] args) { + Student.testStudent(); + + Student stu = new Student(123); + System.out.println(check(stu) == "need coffee"); + stu.fallAsleep(); + System.out.println(check(stu) == "sweet dreams"); + + Chicken.testChicken(); + + Chicken chick = new Chicken(123); + System.out.println(check(chick) == "sweet dreams"); + chick.wakeUp(); + System.out.println(check(chick) == "need coffee"); + } + + /** + * check student sleeping. If student is sleeping, return "sweet dreams", if + * not, return "need coffee". + * + * @param stu. Student object + * @return String. + */ + public static String check(Student stu) { + if (stu.isSleeping()) { + return "sweet dreams"; + } else { + return "need coffee"; + } + } + + /** + * check chicken sleeping. If chicken is sleeping, return "sweet dreams", if + * not, return "need coffee". + * + * @param chk. Chicken object + * @return String. + */ + public static String check(Chicken chk) { + if (chk.isSleeping()) { + return "sweet dreams"; + } else { + return "need coffee"; + } + } +} diff --git a/lab/lab4/Question4/Student.java b/lab/lab4/Question4/Student.java new file mode 100644 index 0000000..3b910f6 --- /dev/null +++ b/lab/lab4/Question4/Student.java @@ -0,0 +1,82 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-03-10 + * Description: Student class + */ + +public class Student { + + /* + * Student's ID number + */ + private int ID; + + /* + * whether the student is currently sleeping or not, default is false + */ + private boolean sleeping; + + /** + * Constructor. Initialize the ID number and sleeping status as false. + * + * @param ID, the ID number of the student + */ + public Student(int ID) { + this.ID = ID; + this.sleeping = false; + } + + /** + * Getter of the ID number. + * + * @return the ID number of the student + */ + public int getID() { + return this.ID; + } + + /** + * Identify whether the student is sleeping or not. + * + * @return true if the student is sleeping, false otherwise + */ + public boolean isSleeping() { + return this.sleeping; + } + + /** + * Make the student fall in sleep. + * + * @return void + */ + public void fallAsleep() { + this.sleeping = true; + } + + /** + * Walkup the student. + * + * @return void + */ + public void wakeUp() { + this.sleeping = false; + } + + /** + * Test student class. + */ + public static void testStudent() { + Student s = new Student(1234567890); + + System.out.println(s.getID() == 1234567890); + System.out.println(s.isSleeping() == false); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.fallAsleep(); + System.out.println(s.isSleeping() == true); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + s.wakeUp(); + System.out.println(s.isSleeping() == false); + } +}