Add: assignment 2 close #2

This commit is contained in:
2022-04-03 17:23:02 +08:00
parent 80ae76b597
commit 5e9fc07694
13 changed files with 448 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,9 @@
public class Start {
public static void main(String[] args) {
Sport.testSport();
Tennis.testTennis();
Running.testRunning();
Marathon.testMarathon();
HalfMarathon.testHalfMarathon();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}