|
| 1 | +# Object-Oriented Programming |
| 2 | + |
| 3 | +[][1] |
| 4 | +[][2] |
| 5 | +[][3] |
| 6 | + |
| 7 | +The goal of these exercises is to practise these OOP concepts: |
| 8 | +- [Classes & Objects](#pushpin-classes--objects) |
| 9 | +- [Inheritance](#pushpin-inheritance) |
| 10 | +- [Abstraction](#pushpin-abstraction) |
| 11 | + |
| 12 | +For the exercises below, we've provided the starter project above. |
| 13 | + |
| 14 | +## :pushpin: Classes & Objects |
| 15 | + |
| 16 | +### Part 1 |
| 17 | + |
| 18 | +Inside the `com.cbfacademy.cars` package under the `cars` module, create a class called `Car` with the following members: |
| 19 | +- `private String make` |
| 20 | +- `private String model` |
| 21 | +- `private String colour` |
| 22 | +- `private int year` |
| 23 | +- `public Car(String make, String model, String colour, int year)` - constructor that accepts parameters representing the make, model, colour and year of the car |
| 24 | +- `public String getMake()` - returns the car make |
| 25 | +- `public String getModel()` - returns the car model |
| 26 | +- `public int getYear()` - returns the year the car was made |
| 27 | +- `public String getColour()` - returns the colour of the car |
| 28 | +- `public void setColour()` - sets the colour of the car |
| 29 | +- `public String getDetails()` - returns the car details, including the make, model, colour and year |
| 30 | + |
| 31 | +#### :white_check_mark: Verify Your Implementation |
| 32 | + |
| 33 | +To verify that your code works as expected, run the `CarTest` tests. |
| 34 | + |
| 35 | +First, edit `object-oriented-programming/cars/pom.xml` to uncomment the `<testExclude>` element: |
| 36 | + |
| 37 | +```xml |
| 38 | + <testExclude>**/ShowroomTest.java</testExclude> |
| 39 | +``` |
| 40 | + |
| 41 | +#### :information_source: Notes |
| 42 | +The `<testExclude>` element excludes the `ShowroomTest` class from being compiled until you've progressed to the next step, as a compiler error would be thrown otherwise. |
| 43 | + |
| 44 | +In your terminal, ensure that you are in the `object-oriented-programming` folder, then run the following command: |
| 45 | + |
| 46 | +```shell |
| 47 | +./mvnw --projects cars clean test -Dtest=CarTest |
| 48 | +``` |
| 49 | + |
| 50 | +If you are using the Windows Command Prompt, use `mvnw` instead of `./mvnw` for all commands. |
| 51 | + |
| 52 | +Your implementation is correct when all tests pass. |
| 53 | + |
| 54 | +### Part 2 |
| 55 | + |
| 56 | +Create a class called `Showroom` with the following members: |
| 57 | +- `public List<Car> getCars()` - returns a list containing the following three `Car` objects: |
| 58 | + - a blue Volvo V40 from 2012 |
| 59 | + - a red Porsche Panamera from 2009 |
| 60 | + - a grey Audi A3 from 2018 |
| 61 | + |
| 62 | +In the `main` method of the `App` class, instantiate a `Showroom` object, call `getCars()` and print the details for each `Car` instance in the list. |
| 63 | + |
| 64 | +#### :white_check_mark: Verify Your Implementation |
| 65 | + |
| 66 | +To verify that your code works as expected, run the `ShowroomTest` tests. |
| 67 | + |
| 68 | +Edit `object-oriented-programming/cars/pom.xml` and undo the changes to the `<testExclude>` element: |
| 69 | + |
| 70 | +```xml |
| 71 | + <!-- <testExclude>**/ShowroomTest.java</testExclude> --> |
| 72 | +``` |
| 73 | + |
| 74 | +In your terminal, run the following command: |
| 75 | + |
| 76 | +```shell |
| 77 | +./mvnw --projects cars clean test -Dtest=ShowroomTest |
| 78 | +``` |
| 79 | + |
| 80 | +Your implementation is correct when all tests pass. |
| 81 | + |
| 82 | +#### :information_source: Notes |
| 83 | +If you want to experiment with the provided application in the App.java file, you can run a command in this format `./mvnw -q --projects [project name] clean compile exec:java -Dexec.mainClass=[package].[class]` from the terminal: |
| 84 | + |
| 85 | +```shell |
| 86 | +./mvnw -q --projects cars clean compile exec:java -Dexec.mainClass=com.cbfacademy.cars.App |
| 87 | +``` |
| 88 | + |
| 89 | +For any of the later exercises, simply set the appropriate project and package names. |
| 90 | + |
| 91 | +Notice that, unlike the test command, we use the `-q` flag to suppress the output of the Maven build as we don't need to see the generated informational messages if we're just running our own application code. |
| 92 | + |
| 93 | +## :pushpin: Inheritance |
| 94 | + |
| 95 | +### Part 1 |
| 96 | + |
| 97 | +Inside the `com.cbfacademy.accounts` package under the `accounts` module, create a class called `Account` with the following members: |
| 98 | +- `public Account(int accountNumber, double balance)` - constructor that accepts parameters representing the new account number and starting balance |
| 99 | +- `public double getBalance()` - returns the current account balance |
| 100 | +- `public int getAccountNumber()` - returns the account number |
| 101 | +- `public double deposit(double amount)` - deposits funds to the account and returns the new balance |
| 102 | +- `public double withdraw(double requested)` - withdraws funds from the account and returns the requested amount or `0` if the account has an insufficient balance |
| 103 | + |
| 104 | +#### :information_source: Notes |
| 105 | +- This account doesn't have an overdraft facility. |
| 106 | +- The balance of an account may only be modified through the `deposit()` and `withdraw()` methods. |
| 107 | +- Consider the necessary instance variables and the appropriate access modifiers to allow any sub-classes to access those values |
| 108 | + |
| 109 | +#### :white_check_mark: Verify Your Implementation |
| 110 | + |
| 111 | +To verify that your code works as expected, run the `AccountTest` tests. |
| 112 | + |
| 113 | +Edit `object-oriented-programming/accounts/pom.xml` to uncomment the `<testExclude>` elements: |
| 114 | + |
| 115 | +```xml |
| 116 | + <testExclude>**/CurrentAccountTest.java</testExclude> |
| 117 | + <testExclude>**/SavingsAccountTest.java</testExclude> |
| 118 | +``` |
| 119 | + |
| 120 | +In your terminal, run the following command: |
| 121 | + |
| 122 | +```shell |
| 123 | +./mvnw --projects accounts clean test -Dtest=AccountTest |
| 124 | +``` |
| 125 | + |
| 126 | +Your implementation is correct when all tests pass. |
| 127 | + |
| 128 | +### Part 2 |
| 129 | + |
| 130 | +Using the `Account` class as a base class, create two derived classes: |
| 131 | + |
| 132 | +`SavingsAccount` with the following members, in addition to the attributes of the `Account` class: |
| 133 | +- `public SavingsAccount(int accountNumber, double balance, double interestRate)` - constructor that accepts parameters representing the new account number, starting balance and interest rate |
| 134 | +- `public void applyInterest()` applies interest to the account |
| 135 | +- `public double getInterestRate()` - returns the current interest rate |
| 136 | +- `public void setInterestRate()` - sets the interest rate |
| 137 | + |
| 138 | +`CurrentAccount` with the following members, in addition to the attributes of the `Account` class: |
| 139 | +- `public CurrentAccount(int accountNumber, double balance, double overdraftLimit)` - constructor that accepts parameters representing the new account number, starting balance and overdraft limit |
| 140 | +- `public double getOverdraftLimit()` - returns the current overdraft limit |
| 141 | +- `public void setOverdraftLimit()` - sets the overdraft limit |
| 142 | + |
| 143 | +#### :information_source: Notes |
| 144 | +Ensure that you have overridden methods of the `Account` class where necessary in the derived classes. |
| 145 | + |
| 146 | +#### :white_check_mark: Verify Your Implementation |
| 147 | + |
| 148 | +To verify that your code works as expected, run the `CurrentAccountTest` and `SavingsAccountTest` tests. |
| 149 | + |
| 150 | +Edit `object-oriented-programming/accounts/pom.xml` and undo the changes to the `<testExclude>` elements: |
| 151 | + |
| 152 | +```xml |
| 153 | + <!-- <testExclude>**/CurrentAccountTest.java</testExclude> --> |
| 154 | + <!-- <testExclude>**/SavingsAccountTest.java</testExclude> --> |
| 155 | +``` |
| 156 | + |
| 157 | +In your terminal, run the following command: |
| 158 | + |
| 159 | +```shell |
| 160 | +./mvnw --projects accounts clean test -Dtest=CurrentAccountTest,SavingsAccountTest |
| 161 | +``` |
| 162 | + |
| 163 | +Your implementation is correct when all tests pass. |
| 164 | + |
| 165 | +### :rocket: Stretch Goal |
| 166 | + |
| 167 | +If you have extra time in the session, or wish to experiment further, create a `Bank` class to manage accounts. Consider the following ideas — you may choose to implement some, all or come up with your own: |
| 168 | +- storing a internal list of accounts. Remember that accounts in the list could be instances of the `Account` class, the `SavingsAccount` class, or the `CurrentAccount` class. |
| 169 | +- opening a new account, given a type and balance. |
| 170 | +- getting a list of account numbers held by the bank |
| 171 | +- getting an account, given an account number |
| 172 | +- closing an account, given an account number |
| 173 | +- displaying a report of all accounts held by the bank |
| 174 | +- paying a dividend to all accounts held by the bank |
| 175 | +- applying interest to all savings accounts |
| 176 | +- contacting all current account holders if their account is in overdraft. |
| 177 | + |
| 178 | +Update `App.java` to create a bank instance and then execute your bank's operations |
| 179 | + |
| 180 | +#### :information_source: Notes |
| 181 | +There are no tests for the `Bank` class, so it's up to you how to implement it. |
| 182 | + |
| 183 | +## :pushpin: Abstraction |
| 184 | + |
| 185 | +In this exercise you will develop a class hierarchy of shapes and write a program that computes the amount of paint needed to paint different objects. |
| 186 | +The hierarchy will consist of a parent class `Shape` with three derived classes - `Sphere`, `Rectangle`, and `Cylinder`. |
| 187 | + |
| 188 | +For the purposes of this exercise, the only attribute a shape will have is a `name` and the method of interest will be one that computes the area of the shape (surface area in the case of three-dimensional shapes). |
| 189 | + |
| 190 | +### Part 1 |
| 191 | + |
| 192 | +Inside the `com.cbfacademy.shapes` package under the `shapes` module, create an abstract class `Shape` with the following members: |
| 193 | +- `abstract double getArea()` - returns the area of the shape |
| 194 | +- `String getName()` - returns the name of the shape |
| 195 | + |
| 196 | +In the same package, create a concrete class `Sphere` that extends `Shape`: |
| 197 | +- `Sphere(double radius)` - constructor |
| 198 | +- `double getArea()` - overrides the base method. The value is given by the formula (`4` * `𝛑` * (`radius`<sup>2</sup>)) |
| 199 | + |
| 200 | +In the same package, create a concrete class `Rectangle` that extends `Shape`: |
| 201 | +- `Rectangle(double length, double width)` - constructor |
| 202 | +- `double getArea()` - overrides the base method. The value is given by the formula (`length` * `width`) |
| 203 | + |
| 204 | +In the same package, create a concrete class `Cylinder` that extends `Shape`: |
| 205 | +- `Cylinder(double radius, double height)` - constructor |
| 206 | +- `double getArea()` - overrides the base method. The value is given by the formula (`height` * `𝛑` * (`radius`<sup>2</sup>)). |
| 207 | + |
| 208 | +#### :information_source: Notes |
| 209 | +Consider the appropriate visibility of all constructors, methods and instance variables |
| 210 | + |
| 211 | +#### :white_check_mark: Verify Your Implementation |
| 212 | + |
| 213 | +To verify that your code works as expected, run the `ShapeTest` tests. |
| 214 | + |
| 215 | +Edit `object-oriented-programming/shapes/pom.xml` to uncomment the `<testExclude>` element: |
| 216 | + |
| 217 | +```xml |
| 218 | + <testExclude>**/PaintTest.java</testExclude> |
| 219 | +``` |
| 220 | + |
| 221 | +In your terminal, run the following command: |
| 222 | + |
| 223 | +```shell |
| 224 | +./mvnw --projects shapes clean test -Dtest=ShapeTest |
| 225 | +``` |
| 226 | + |
| 227 | +Your implementation is correct when all tests pass. |
| 228 | + |
| 229 | +### Part 2 |
| 230 | + |
| 231 | +Inside the `shapes` package, create a class `Paint` with the following members: |
| 232 | +- `Paint(double coverage)` - constructor that accepts a parameter representing the number of square feet per gallon this paint can cover |
| 233 | +- `getAmount(Shape shape)` - returns the amount of paint (measured in gallons) needed to paint a given shape |
| 234 | + |
| 235 | +#### :white_check_mark: Verify Your Implementation |
| 236 | + |
| 237 | +To verify that your code works as expected, run the `PaintTest` tests. |
| 238 | + |
| 239 | +Edit `object-oriented-programming/shapes/pom.xml` and undo the changes to the `<testExclude>` element: |
| 240 | + |
| 241 | +```xml |
| 242 | + <!-- <testExclude>**/PaintTest.java</testExclude> --> |
| 243 | +``` |
| 244 | + |
| 245 | +In your terminal, run the following command: |
| 246 | + |
| 247 | +```shell |
| 248 | +./mvnw --projects shapes clean test -Dtest=PaintTest |
| 249 | +``` |
| 250 | + |
| 251 | +Your implementation is correct when all tests pass. |
| 252 | + |
| 253 | +### :rocket: Stretch Goal |
| 254 | + |
| 255 | +If you have extra time in the session, or wish to experiment further, create a **program** in the same package that computes the amount of paint needed to paint various shapes, e.g.: |
| 256 | +- a rectangular deck of length `35` and width `20` |
| 257 | +- a ball of radius `15` |
| 258 | +- a tank of radius `10` and height `30`. |
| 259 | + |
| 260 | +Consider the following ideas: |
| 261 | +- print the amount of paint needed for each item |
| 262 | +- print the overall amount of paint needed to the screen |
| 263 | + |
| 264 | +[1]: https://docs.oracle.com/javase/21/docs/api/index.html |
| 265 | +[2]: https://junit.org/junit5/ |
| 266 | +[3]: https://maven.apache.org/ |
0 commit comments