The goal of these exercises is to practise these OOP concepts:
For the exercises below, we've provided the starter project above.
Inside the com.cbfacademy.cars package under the cars module, create a class called Car with the following members:
private String makeprivate String modelprivate String colourprivate int yearpublic Car(String make, String model, String colour, int year)- constructor that accepts parameters representing the make, model, colour and year of the carpublic String getMake()- returns the car makepublic String getModel()- returns the car modelpublic int getYear()- returns the year the car was madepublic String getColour()- returns the colour of the carpublic void setColour(String colour)- sets the colour of the carpublic String getDetails()- returns the car details, including the make, model, colour and year
To verify that your code works as expected, run the CarTest tests.
First, edit object-oriented-programming/cars/pom.xml to uncomment the <testExclude> element:
<testExclude>**/ShowroomTest.java</testExclude>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.
In your terminal, ensure that you are in the object-oriented-programming folder, then run the following command:
./mvnw --projects cars clean test -Dtest=CarTestIf you are using the Windows Command Prompt, use mvnw instead of ./mvnw for all commands.
Your implementation is correct when all tests pass.
Create a class called Showroom with the following members:
public List<Car> getCars()- returns a list containing the following threeCarobjects:- a blue Volvo V40 from 2012
- a red Porsche Panamera from 2009
- a grey Audi A3 from 2018
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.
To verify that your code works as expected, run the ShowroomTest tests.
Edit object-oriented-programming/cars/pom.xml and undo the changes to the <testExclude> element:
<!-- <testExclude>**/ShowroomTest.java</testExclude> -->In your terminal, run the following command:
./mvnw --projects cars clean test -Dtest=ShowroomTestYour implementation is correct when all tests pass.
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:
./mvnw -q --projects cars clean compile exec:java -Dexec.mainClass=com.cbfacademy.cars.AppFor any of the later exercises, simply set the appropriate project and package names.
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.
Inside the com.cbfacademy.accounts package under the accounts module, create a class called Account with the following members:
public Account(int accountNumber, double balance)- constructor that accepts parameters representing the new account number and starting balancepublic double getBalance()- returns the current account balancepublic int getAccountNumber()- returns the account numberpublic double deposit(double amount)- deposits funds to the account and returns the new balancepublic double withdraw(double requested)- withdraws funds from the account and returns the requested amount or0if the account has an insufficient balance
- This account doesn't have an overdraft facility.
- The balance of an account may only be modified through the
deposit()andwithdraw()methods. - Consider the necessary instance variables and the appropriate access modifiers to allow any sub-classes to access those values
To verify that your code works as expected, run the AccountTest tests.
Edit object-oriented-programming/accounts/pom.xml to uncomment the <testExclude> elements:
<testExclude>**/CurrentAccountTest.java</testExclude>
<testExclude>**/SavingsAccountTest.java</testExclude>In your terminal, run the following command:
./mvnw --projects accounts clean test -Dtest=AccountTestYour implementation is correct when all tests pass.
Using the Account class as a base class, create two derived classes:
SavingsAccount with the following members, in addition to the attributes of the Account class:
public SavingsAccount(int accountNumber, double balance, double interestRate)- constructor that accepts parameters representing the new account number, starting balance and interest ratepublic void applyInterest()applies interest to the accountpublic double getInterestRate()- returns the current interest ratepublic void setInterestRate()- sets the interest rate
CurrentAccount with the following members, in addition to the attributes of the Account class:
public CurrentAccount(int accountNumber, double balance, double overdraftLimit)- constructor that accepts parameters representing the new account number, starting balance and overdraft limitpublic double getOverdraftLimit()- returns the current overdraft limitpublic void setOverdraftLimit()- sets the overdraft limit
Ensure that you have overridden methods of the Account class where necessary in the derived classes.
To verify that your code works as expected, run the CurrentAccountTest and SavingsAccountTest tests.
Edit object-oriented-programming/accounts/pom.xml and undo the changes to the <testExclude> elements:
<!-- <testExclude>**/CurrentAccountTest.java</testExclude> -->
<!-- <testExclude>**/SavingsAccountTest.java</testExclude> -->In your terminal, run the following command:
./mvnw --projects accounts clean test -Dtest=CurrentAccountTest,SavingsAccountTestYour implementation is correct when all tests pass.
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:
- storing a internal list of accounts. Remember that accounts in the list could be instances of the
Accountclass, theSavingsAccountclass, or theCurrentAccountclass. - opening a new account, given a type and balance.
- getting a list of account numbers held by the bank
- getting an account, given an account number
- closing an account, given an account number
- displaying a report of all accounts held by the bank
- paying a dividend to all accounts held by the bank
- applying interest to all savings accounts
- contacting all current account holders if their account is in overdraft.
Update App.java to create a bank instance and then execute your bank's operations
There are no tests for the Bank class, so it's up to you how to implement it.
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.
The hierarchy will consist of a parent class Shape with three derived classes - Sphere, Rectangle, and Cylinder.
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).
Inside the com.cbfacademy.shapes package under the shapes module, create an abstract class Shape with the following members:
abstract double getArea()- returns the area of the shapeString getName()- returns the name of the shape
In the same package, create a concrete class Sphere that extends Shape:
Sphere(double radius)- constructordouble getArea()- overrides the base method. The value is given by the formula:4 * 𝛑 * (radius^2)(whereradius^2= radius2)
In the same package, create a concrete class Rectangle that extends Shape:
Rectangle(double length, double width)- constructordouble getArea()- overrides the base method. The value is given by the formula:length * width
In the same package, create a concrete class Cylinder that extends Shape:
Cylinder(double radius, double height)- constructordouble getArea()- overrides the base method. The value is given by the formula:2 * 𝛑 * radius * (height + radius).
Consider the appropriate visibility of all constructors, methods and instance variables
To verify that your code works as expected, run the ShapeTest tests.
Edit object-oriented-programming/shapes/pom.xml to uncomment the <testExclude> element:
<testExclude>**/PaintTest.java</testExclude>In your terminal, run the following command:
./mvnw --projects shapes clean test -Dtest=ShapeTestYour implementation is correct when all tests pass.
Inside the shapes package, create a class Paint with the following members:
Paint(double coverage)- constructor that accepts a parameter representing the number of square feet per gallon this paint can covergetAmount(Shape shape)- returns the amount of paint (measured in gallons) needed to paint a given shape
To verify that your code works as expected, run the PaintTest tests.
Edit object-oriented-programming/shapes/pom.xml and undo the changes to the <testExclude> element:
<!-- <testExclude>**/PaintTest.java</testExclude> -->In your terminal, run the following command:
./mvnw --projects shapes clean test -Dtest=PaintTestYour implementation is correct when all tests pass.
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.:
- a rectangular deck of length
35and width20 - a ball of radius
15 - a tank of radius
10and height30.
Consider the following ideas:
- print the amount of paint needed for each item
- print the overall amount of paint needed to the screen