Skip to content

Commit 1e64323

Browse files
committed
feat(init): set up Java OOP exercises
- Add Maven project configuration with parent pom.xml - Create three exercise modules: accounts, cars, and shapes - Set up test structure for each module - Add Maven wrapper and .gitignore - Include README with project documentation
0 parents  commit 1e64323

File tree

23 files changed

+1543
-0
lines changed

23 files changed

+1543
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/
8+
*.iws
9+
*.iml
10+
*.ipr
11+
12+
### Eclipse ###
13+
.apt_generated
14+
.classpath
15+
.factorypath
16+
.project
17+
.settings
18+
.springBeans
19+
.sts4-cache
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
33+
34+
### Mac OS ###
35+
.DS_Store

.mvn/wrapper/maven-wrapper.jar

61.1 KB
Binary file not shown.

.mvn/wrapper/maven-wrapper.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.1/apache-maven-3.9.1-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar

README.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# Object-Oriented Programming
2+
3+
[![Java Language](https://img.shields.io/badge/PLATFORM-OpenJDK-3A75B0.svg?style=for-the-badge)][1]
4+
[![JUnit5 Testing Framework](https://img.shields.io/badge/testing%20framework-JUnit5-26A162.svg?style=for-the-badge)][2]
5+
[![Maven Dependency Manager](https://img.shields.io/badge/dependency%20manager-Maven-AA215A.svg?style=for-the-badge)][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/

accounts/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>object-oriented-programming</artifactId>
6+
<groupId>com.cbfacademy</groupId>
7+
<version>1.0.0</version>
8+
</parent>
9+
10+
<groupId>com.cbfacademy</groupId>
11+
<artifactId>accounts</artifactId>
12+
<version>1.0.0</version>
13+
14+
<name>accounts</name>
15+
16+
<build>
17+
<pluginManagement>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<configuration>
23+
<testExcludes>
24+
<!-- <testExclude>**/BankTest.java</testExclude> -->
25+
<!-- <testExclude>**/CurrentAccountTest.java</testExclude> -->
26+
<!-- <testExclude>**/SavingsAccountTest.java</testExclude> -->
27+
</testExcludes>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</pluginManagement>
32+
</build>
33+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cbfacademy;
2+
3+
public class App {
4+
public static void main(String[] args) {
5+
System.out.println("Hello World!");
6+
}
7+
}

accounts/src/main/java/com/cbfacademy/accounts/.gitkeep

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.cbfacademy;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.hamcrest.CoreMatchers.notNullValue;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
10+
@DisplayName(value = "Basic Test Suite")
11+
public class AppTest {
12+
13+
@Test
14+
@DisplayName("creates the app")
15+
public void createsAnApp() {
16+
final App app = new App();
17+
18+
assertThat(app, is(notNullValue()));
19+
}
20+
}

0 commit comments

Comments
 (0)