From 5e9fc076940d8c6ce7a934996ba7e11d16b4ab85 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 3 Apr 2022 17:23:02 +0800 Subject: [PATCH] Add: assignment 2 close #2 --- assignment2/Question1/HalfMarathon.java | 24 ++++++++++++ assignment2/Question1/Marathon.java | 43 +++++++++++++++++++++ assignment2/Question1/Running.java | 50 +++++++++++++++++++++++++ assignment2/Question1/Sport.java | 46 +++++++++++++++++++++++ assignment2/Question1/Start.java | 9 +++++ assignment2/Question1/Tennis.java | 33 ++++++++++++++++ assignment2/Question2/HalfMarathon.java | 24 ++++++++++++ assignment2/Question2/Marathon.java | 43 +++++++++++++++++++++ assignment2/Question2/Running.java | 50 +++++++++++++++++++++++++ assignment2/Question2/Sport.java | 46 +++++++++++++++++++++++ assignment2/Question2/SportsCourt.java | 37 ++++++++++++++++++ assignment2/Question2/Start.java | 10 +++++ assignment2/Question2/Tennis.java | 33 ++++++++++++++++ 13 files changed, 448 insertions(+) create mode 100644 assignment2/Question1/HalfMarathon.java create mode 100644 assignment2/Question1/Marathon.java create mode 100644 assignment2/Question1/Running.java create mode 100644 assignment2/Question1/Sport.java create mode 100644 assignment2/Question1/Start.java create mode 100644 assignment2/Question1/Tennis.java create mode 100644 assignment2/Question2/HalfMarathon.java create mode 100644 assignment2/Question2/Marathon.java create mode 100644 assignment2/Question2/Running.java create mode 100644 assignment2/Question2/Sport.java create mode 100644 assignment2/Question2/SportsCourt.java create mode 100644 assignment2/Question2/Start.java create mode 100644 assignment2/Question2/Tennis.java diff --git a/assignment2/Question1/HalfMarathon.java b/assignment2/Question1/HalfMarathon.java new file mode 100644 index 0000000..ca28cbb --- /dev/null +++ b/assignment2/Question1/HalfMarathon.java @@ -0,0 +1,24 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class HalfMarathon extends Marathon { + /** + * Constructor. Half marathon always has player number 99 and length of 21.0975 + */ + public HalfMarathon() { + super(99, 21.0975); + } + + /** + * Test. + */ + public static void testHalfMarathon() { + HalfMarathon hm = new HalfMarathon(); + System.out.println(hm.getLength() == 21.0975); + System.out.println(hm.getPlayerNumber() == 99); + System.out.println(hm.isFun() == false); + } +} diff --git a/assignment2/Question1/Marathon.java b/assignment2/Question1/Marathon.java new file mode 100644 index 0000000..29d3102 --- /dev/null +++ b/assignment2/Question1/Marathon.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class Marathon extends Running { + /** + * Constructor. + * + * @param playerNumber + * the player number + * @param length + * the length of the marathon + */ + public Marathon(int playerNumber, double length) { + super(playerNumber, length); + } + + /** + * Constructor. Always set the length to 42.195 + * + * @param playerNumber + */ + public Marathon(int playerNumber) { + this(playerNumber, 42.195); + } + + /** + * Test. + */ + public static void testMarathon() { + Marathon m1 = new Marathon(1, 39.39); + System.out.println(m1.getLength() == 39.39); + System.out.println(m1.getPlayerNumber() == 1); + System.out.println(m1.isFun() == false); + + Marathon m2 = new Marathon(2); + System.out.println(m2.getLength() == 42.195); + System.out.println(m2.getPlayerNumber() == 2); + System.out.println(m2.isFun() == false); + } +} diff --git a/assignment2/Question1/Running.java b/assignment2/Question1/Running.java new file mode 100644 index 0000000..5b1b77e --- /dev/null +++ b/assignment2/Question1/Running.java @@ -0,0 +1,50 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class Running extends Sport { + /** + * The length of the running. + */ + private double length; + + /** + * Constructor. + * + * @param playerNumber the player number of the running. + * @param length the length of the running + */ + public Running(int playerNumber, double length) { + super(playerNumber); + this.length = length; + } + + /** + * Getter for the length of the running. + */ + public double getLength() { + return length; + } + + /** + * Running is not fun. + * + * @return false + */ + @Override + public boolean isFun() { + return false; + } + + /** + * Test. + */ + public static void testRunning() { + Running r1 = new Running(1, 1.5); + System.out.println(r1.getPlayerNumber() == 1); + System.out.println(r1.getLength() == 1.5); + System.out.println(r1.isFun() == false); + } +} diff --git a/assignment2/Question1/Sport.java b/assignment2/Question1/Sport.java new file mode 100644 index 0000000..ac50875 --- /dev/null +++ b/assignment2/Question1/Sport.java @@ -0,0 +1,46 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Sport class. + */ + +public class Sport { + private int playerNumber; + + /** + * Constructor. + * + * @param playerNumber + */ + public Sport(int playerNumber) { + this.playerNumber = playerNumber; + } + + /** + * Getter for playerNumber. + * + * @return playerNumber + */ + public int getPlayerNumber() { + return playerNumber; + } + + /** + * Whether the sport is fun or not. Some sport are fun and some sports are not + * fun. By default the sport is not fun. + * + * @return true if the sport is fun, false if the sport is not fun. + */ + public boolean isFun() { + return false; + } + + /** + * Test. + */ + public static void testSport() { + Sport sport = new Sport(10); + System.out.println(sport.getPlayerNumber() == 10); + System.out.println(sport.isFun() == false); + } +} diff --git a/assignment2/Question1/Start.java b/assignment2/Question1/Start.java new file mode 100644 index 0000000..f384976 --- /dev/null +++ b/assignment2/Question1/Start.java @@ -0,0 +1,9 @@ +public class Start { + public static void main(String[] args) { + Sport.testSport(); + Tennis.testTennis(); + Running.testRunning(); + Marathon.testMarathon(); + HalfMarathon.testHalfMarathon(); + } +} diff --git a/assignment2/Question1/Tennis.java b/assignment2/Question1/Tennis.java new file mode 100644 index 0000000..47b5088 --- /dev/null +++ b/assignment2/Question1/Tennis.java @@ -0,0 +1,33 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Tennis class which is the subclass of the Sport class. + */ + +public class Tennis extends Sport { + /** + * Constructor. Tennis has 2 player. + */ + public Tennis() { + super(2); + } + + /** + * Tennis is fun. + * + * @return true + */ + @Override + public boolean isFun() { + return true; + } + + /** + * Test. + */ + public static void testTennis() { + Tennis t = new Tennis(); + System.out.println(t.getPlayerNumber() == 2); + System.out.println(t.isFun() == true); + } +} diff --git a/assignment2/Question2/HalfMarathon.java b/assignment2/Question2/HalfMarathon.java new file mode 100644 index 0000000..ca28cbb --- /dev/null +++ b/assignment2/Question2/HalfMarathon.java @@ -0,0 +1,24 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class HalfMarathon extends Marathon { + /** + * Constructor. Half marathon always has player number 99 and length of 21.0975 + */ + public HalfMarathon() { + super(99, 21.0975); + } + + /** + * Test. + */ + public static void testHalfMarathon() { + HalfMarathon hm = new HalfMarathon(); + System.out.println(hm.getLength() == 21.0975); + System.out.println(hm.getPlayerNumber() == 99); + System.out.println(hm.isFun() == false); + } +} diff --git a/assignment2/Question2/Marathon.java b/assignment2/Question2/Marathon.java new file mode 100644 index 0000000..29d3102 --- /dev/null +++ b/assignment2/Question2/Marathon.java @@ -0,0 +1,43 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class Marathon extends Running { + /** + * Constructor. + * + * @param playerNumber + * the player number + * @param length + * the length of the marathon + */ + public Marathon(int playerNumber, double length) { + super(playerNumber, length); + } + + /** + * Constructor. Always set the length to 42.195 + * + * @param playerNumber + */ + public Marathon(int playerNumber) { + this(playerNumber, 42.195); + } + + /** + * Test. + */ + public static void testMarathon() { + Marathon m1 = new Marathon(1, 39.39); + System.out.println(m1.getLength() == 39.39); + System.out.println(m1.getPlayerNumber() == 1); + System.out.println(m1.isFun() == false); + + Marathon m2 = new Marathon(2); + System.out.println(m2.getLength() == 42.195); + System.out.println(m2.getPlayerNumber() == 2); + System.out.println(m2.isFun() == false); + } +} diff --git a/assignment2/Question2/Running.java b/assignment2/Question2/Running.java new file mode 100644 index 0000000..5b1b77e --- /dev/null +++ b/assignment2/Question2/Running.java @@ -0,0 +1,50 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Running class which is the subclass of the Sport class + */ + +public class Running extends Sport { + /** + * The length of the running. + */ + private double length; + + /** + * Constructor. + * + * @param playerNumber the player number of the running. + * @param length the length of the running + */ + public Running(int playerNumber, double length) { + super(playerNumber); + this.length = length; + } + + /** + * Getter for the length of the running. + */ + public double getLength() { + return length; + } + + /** + * Running is not fun. + * + * @return false + */ + @Override + public boolean isFun() { + return false; + } + + /** + * Test. + */ + public static void testRunning() { + Running r1 = new Running(1, 1.5); + System.out.println(r1.getPlayerNumber() == 1); + System.out.println(r1.getLength() == 1.5); + System.out.println(r1.isFun() == false); + } +} diff --git a/assignment2/Question2/Sport.java b/assignment2/Question2/Sport.java new file mode 100644 index 0000000..ac50875 --- /dev/null +++ b/assignment2/Question2/Sport.java @@ -0,0 +1,46 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Sport class. + */ + +public class Sport { + private int playerNumber; + + /** + * Constructor. + * + * @param playerNumber + */ + public Sport(int playerNumber) { + this.playerNumber = playerNumber; + } + + /** + * Getter for playerNumber. + * + * @return playerNumber + */ + public int getPlayerNumber() { + return playerNumber; + } + + /** + * Whether the sport is fun or not. Some sport are fun and some sports are not + * fun. By default the sport is not fun. + * + * @return true if the sport is fun, false if the sport is not fun. + */ + public boolean isFun() { + return false; + } + + /** + * Test. + */ + public static void testSport() { + Sport sport = new Sport(10); + System.out.println(sport.getPlayerNumber() == 10); + System.out.println(sport.isFun() == false); + } +} diff --git a/assignment2/Question2/SportsCourt.java b/assignment2/Question2/SportsCourt.java new file mode 100644 index 0000000..03954e5 --- /dev/null +++ b/assignment2/Question2/SportsCourt.java @@ -0,0 +1,37 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the class of SportsCourt. + */ + +public class SportsCourt { + private Sport sport; + + /** + * Constructor. + * + * @param sport + */ + public SportsCourt(Sport sport) { + this.sport = sport; + } + + /** + * Getter of sport. + * + * @return sport + */ + public Sport playSport() { + return sport; + } + + /** + * Test. + */ + public static void testSportsCourt() { + HalfMarathon hm1 = new HalfMarathon(); + SportsCourt sc1 = new SportsCourt(hm1); + HalfMarathon hm2 = (HalfMarathon) sc1.playSport(); + System.out.println(sc1.playSport() == hm2); + } +} diff --git a/assignment2/Question2/Start.java b/assignment2/Question2/Start.java new file mode 100644 index 0000000..95c4990 --- /dev/null +++ b/assignment2/Question2/Start.java @@ -0,0 +1,10 @@ +public class Start { + public static void main(String[] args) { + Sport.testSport(); + Tennis.testTennis(); + Running.testRunning(); + Marathon.testMarathon(); + HalfMarathon.testHalfMarathon(); + SportsCourt.testSportsCourt(); + } +} diff --git a/assignment2/Question2/Tennis.java b/assignment2/Question2/Tennis.java new file mode 100644 index 0000000..47b5088 --- /dev/null +++ b/assignment2/Question2/Tennis.java @@ -0,0 +1,33 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022/03/04 + * Description: This is the Tennis class which is the subclass of the Sport class. + */ + +public class Tennis extends Sport { + /** + * Constructor. Tennis has 2 player. + */ + public Tennis() { + super(2); + } + + /** + * Tennis is fun. + * + * @return true + */ + @Override + public boolean isFun() { + return true; + } + + /** + * Test. + */ + public static void testTennis() { + Tennis t = new Tennis(); + System.out.println(t.getPlayerNumber() == 2); + System.out.println(t.isFun() == true); + } +}