Lab 05: Mini Problems
Learning Objectives
Students should
- be able to organize program into packages
- be able to create and use nested classes
Initializing
To get the files, run the following command from your PE node.
We recommend creating a new directory called mini
to store all your lab mini problems.
1 |
|
The files will only be available on Wednesday, 9 October 2024.
Lab 04 Extension
Parts of this problem is an extension to mini problems of Lab 04. The solution to the mini problems of Lab 4 can be found in the accompanying directory.
In Ex 5, you will need to copy your solution instead of having the accompanying solution.
Mini Problem 1
We have created an immutable point in Lab 04, the partial code is shown below. While it may not fully solve Lab 04, it is sufficient for our purpose.
Point.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Task 1: Packaging
We will now create a package called shapes
.
To do this, follow the steps below.
Please revise the bash commands to do this.
- Create a directory called
shapes
. - Copy
Point.java
intoshapes
. - Add the line
package shapes;
at the top ofPoint.java
.- All classes in the package
shapes
must begin withpackage shapes;
.
- All classes in the package
- Add the line
import shapes.Point;
at the top ofTask1.java
.- Note that you should NOT use
import shapes.*;
. Instead, you should import the needed classes individually to avoid namespace polution.
- Note that you should NOT use
At the end, you should see something like the following directory structure.
The base directory is called mini1
.
All directory ends with /
as part of their names.
1 2 3 4 5 |
|
Now we can check if this works.
We will test with Task1.java
only.
- Compile
Task1.java
withjavac Task1.java
.- Notice that
Point.java
inside the directoryshapes
will also be compiled.
- Notice that
- Run
Task1.java
withjava Task1
.- You should get all ok.
Task 2: Adding Shapes
Given points, we can now create shapes.
We have created Circle
before, so let us recreate it inside the shapes
package.
Remember that all classes in the package shapes
must begin with package shapes;
.
Before we specify the requirement for Circle
, we first remove the keyword public
from the constructor of class Point
.
This will prevent us from creating a point directly from outside of the package.
That is because beside the public
and private
modifier, we have the following modifiers.
Without any modifier, the class Point
can only be used by code in the same class or in the same package.
Modifier | Class | Package | Subclass | Others |
---|---|---|---|---|
public |
Y | Y | Y | Y |
protected |
Y | Y | Y | N |
no modifier | Y | Y | N | N |
private |
Y | N | N | N |
Circle.java Now we can create Circle
inside the package shapes
.
Design your class in the following way.
- It is a
public
class with twoprivate
fields. The first field is aPoint
indicating the center point of the circle. The second field is the radius of the circle asint
. - It has a
public
constructor that accepts two parameters. The first parameter is the center point and the second parameter is the radius of the circle. - The
public
methodtoString
prints the circle asCircle @ <center> with radius <radius>
.<center>
is the string representation of its center point and<radius>
is the string representation of its radius. - The
protected
methodgetRadius
returns the radius of the circle. - Add the method
public void moveTo(int x, int y)
in the classCircle
to move the center point of the circle. However, instead of invokingPoint::moveTo
, try creatingnew Point(x, y)
directly in theCircle
class.
You can check your implementation with Task2.java
.
MyCircle.java Now we can create ColoredCircle
outside the package shapes
.
Design your class in the following way.
- It is a
public
class extendingCircle
with no fields. - It has a
public
constructor that accepts one parameters: the radius of the circle. The center point is always at (0, 0). - It has a single
public
methodisSmallerThan(int radius)
to compare the radius with the input parameterradius
. The method returns true if the radius is strictly smaller thanradius
.
You can check your implementation with Task3.java
.
Mini Problem 2
We have created a generic pair in Lab 04, the partial code is shown below. While it may not fully solve Lab 04, it is sufficient for our purpose. Although we are not extending pair, you may use it as an inspiration.
Pair.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Task 1: Abstract Boolean Condition
We will now create a new class that is an abstraction of an boolean condition.
We call this class Bool<T>
.
Your current task is to implement the abstract class Bool<T>
with the following specification.
- It is a generic abstract class with one type parameter
T
. - It has a single
private
field of typeT
. - It has a
private
constructor that accepts a value of typeT
. - It has a single concrete method
T getVal()
that returns the field of typeT
. - It has two
public
abstract
methods.Bool<T> doThen(T val)
.Bool<T> doElse(T val)
.
This class will have a factory method based on the second task below.
Task 2: Nested Class
We will now create two static nested classes.
True The class True<T>
extends Bool<T>
.
It overrides the two abstract methods as follows.
Bool<T> doThen(T val)
: returns a new instance ofTrue<T>
such that theprotected
field of typeT
inherited fromBool<T>
is set to the given valueval
.Bool<T> doElse(T val)
: returns itself without change.
False The class False<T>
extends Bool<T>
.
It overrides the two abstract methods as follows.
Bool<T> doThen(T val)
: returns itself without change.Bool<T> doElse(T val)
: returns a new instance ofFalse<T>
such that theprotected
field of typeT
inherited fromBool<T>
is set to the given valueval
.
Factory Method Implement the factory method test(boolean cond)
that takes in a boolean condition and returns Bool<T>
such that
- if
cond
istrue
, it returns a newTrue<T>
with initial value ofnull
. - if
cond
isfalse
, it returns a newFalse<T>
with initial value ofnull
.
See the sample usage below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Look Ma, no if-then-else statement!
You can check your implementation with Task1.java
.