Skip to content
Snippets Groups Projects
Commit 960146f2 authored by Korbinian Riedhammer's avatar Korbinian Riedhammer
Browse files

Updated wording

parent a09db4ce
Branches
No related tags found
No related merge requests found
_This is an assignment to the class [Programmieren 3](https://hsro-inf-prg3.github.io) at the [University of Applied Sciences Rosenheim](http://www.fh-rosenheim.de)._
# Assignment 5: Generic bounds
# Assignment 5: Generics and Bounds
This assignment covers an advanced topic of generics: bounds on type parameters and wildcards.
Bounds describe type constraints of generic classes or methods that allow to balance type safety with flexibility of use.
In this assignment we want to deal with generic bounds.
Generic bounds are a great possibility to describe constraints of generic classes or methods.
## Setup
......@@ -12,33 +13,41 @@ Generic bounds are a great possibility to describe constraints of generic classe
3. Import the project to your IDE (remember the guide in assignment 1)
4. Validate your environment by running the tests from your IntelliJ and by running `gradle test` on the command line.
## Part 1
First we want to create the model for this assigment:
## Groundwork
First, create the model for this assigment:
![Model spec](./assets/class-spec-1.svg)
1. Create the `enum` _PlantColor_
2. Implement the `abstract` class _Plant_
3. Implement the child classes _Flower_ and _Shrub_<br>
_Remarks:_ A shrub is always green. A flower mustn't be green but any other color is possible (handle a wrong value in the constructor)
4. Create tests to ensure that your model classes are correct (e.g. green flowers aren't possible)
_Remarks:_ A shrub is always green; a flower may be any color but green (handle a wrong value in the constructor)
4. Create tests to ensure that your model classes are correct (e.g. green flowers won't work)
## Part 2
## Basic Bounds
Now that we've a model we want to implement a _PlantBed_ which takes care of the plants.
Implement a `PlantBed` which manages a list of plants (use `SimpleList<T>`).
![PlantBed spec](./assets/class-spec-2.svg)
Implement the class _PlantBed_ as given in the UML. The _PlantBed_ may contain any subclass of _Plant_ but nothing else! (Think of the concept of bounds when you declare the generic!)
A `PlantBed` may contain any subclass of `Plant` but nothing else!
Use appropriate bounds when declaring the generic class.
_Remarks:_ The method `getPlantsByColor` is very easy to implement if you think of the `filter` method of the `SimpleList`! Remember to create tests for nearly every line you code!
_Remarks:_ The method `getPlantsByColor` is very easy to implement if you think of the `filter` method of the `SimpleList`!
Remember to create tests for nearly every line you code!
## Part 3
## Bounds on Wildcards
Last but not least we'll look at the PECS (**P**roducer **E**extends **C**onsumer **S**uper) principle.
![Utils spec](./assets/class-spec-3.svg)
1. Modify the _map_ method in the _SimpleList_ interface according to the PECS principle
2. Implement the utility method _splitBedByColor_ in a utility class _PlantBedUtility_ <br>_Side note:_ why should the class _PlantBedUtility_ be `abstract` and have a `private` constructor?<br>_Remark:_ the given UML for the utility method does not include any generic bound but you **need** some (PECS!) because the compilere won't let you implement the method as described in the UML!
1. Modify the `map` method of the `SimpleList` interface according to the PECS principle
2. Implement the utility method `splitBedByColor` in a utility class `PlantBedUtility`.
Why should this class be declared `abstract` and have a `private` constructor?
_Remark:_ the above UML diagram for the utility method does not include any bounds but you **need** some (PECS!) -- the compiler is unable to capture the types if you implement the method strictly as described in the UML!
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment