Lab 4
This commit is contained in:
5
lab/lab4/Question1/Start.java
Normal file
5
lab/lab4/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Student.testStudent();
|
||||
}
|
||||
}
|
||||
82
lab/lab4/Question1/Student.java
Normal file
82
lab/lab4/Question1/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
30
lab/lab4/Question2/Start.java
Normal file
30
lab/lab4/Question2/Start.java
Normal file
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
82
lab/lab4/Question2/Student.java
Normal file
82
lab/lab4/Question2/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
92
lab/lab4/Question3/Chicken.java
Normal file
92
lab/lab4/Question3/Chicken.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
32
lab/lab4/Question3/Start.java
Normal file
32
lab/lab4/Question3/Start.java
Normal file
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
82
lab/lab4/Question3/Student.java
Normal file
82
lab/lab4/Question3/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
92
lab/lab4/Question4/Chicken.java
Normal file
92
lab/lab4/Question4/Chicken.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
52
lab/lab4/Question4/Start.java
Normal file
52
lab/lab4/Question4/Start.java
Normal file
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
82
lab/lab4/Question4/Student.java
Normal file
82
lab/lab4/Question4/Student.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user