Online learning is a hot topic in these days. Your company just won a state tender for an e-learning tool that helps high school students learn maths.
Your team has just started planning a geometry module. Hierarchical object-oriented modelling seemed a natural choice here: after all, every square is a rectangle, isn't it? So you came up with a class hierarchy for six basic shapes, and your job will be to implement the core features (like area and perimeter) of these shape types.
You also need to add a simple console UI to list and add shapes to a shape repository. This will be replaced with some GUI later. Oh, and don't forget to contribute to the emerging documentation, so please draw a UML class diagram as well!
- draw UML class diagrams
- use inheritance
- override methods
- when static context (the
statickeyword) can be useful - hiding (the
newkeyword) vs overriding (theoverridekeyword) - use
sealed - string representation of objects
-
Implement the constructors and the required methods in shape subclasses. The shapes should be immutable which means that their state (stored in attributes) cannot be changed.
- All attributes are immutable
- All attributes are private, and only those methods are not private which are used by other classes
- All shape subclasses have an overridden version for calculating the corresponding perimeter and area
- All shape subclasses have their custom
ToString()representation. The representation includes the name of the shape (like"Rectangle") and the names and values of its base attributes (in this case"r=...").
-
Implement
ShapeCollectionto manage a list of shapes. The collection can consist of any subtype ofShape.- It is possible to add shapes to the collection via the
AddShapemethod. - Method
GetShapesTable()returns a printable table with all the shapes in the collection and their parameter values. The table looks like the example inexample_table.txt. - Method
GetLargestShapeByPerimeter()returns the shape instance with the largest perimeter in the collection - Method
GetLargestShapeByArea()returns the shape instance with the largest area in the collection
- It is possible to add shapes to the collection via the
-
Show a main menu upon starting the program that provides access to basic operations on a shape collection.
- By choosing the "Add new shape" option, the user can select from a list of existing shapes (like circle) and after that she needs to specify the required parameters (like radius).
- By choosing the "Show all shapes" option the table view provided by the shape collection is displayed to the user
- By choosing the "Show shape with the largest perimeter" option displays the shape with largest perimeter in the collection
- By choosing the "Show shape with the largest area" option displays the shape with largest area in the collection
- By choosing the "Show formulas" option the user can select from a list of existing shapes (like circle) and after that the stored formulas for that shape class (like
Circle area formula: π×r×r, Circle perimeter formula: 2×π×rwill be displayed. - The user can exit the program by selecting "Exit"
-
Create a class diagram based on the existing project skeleton structure and keep extending it when extending the code. Use PlantUML to generate it.
- The PlantUML source file named
uml.txtand a rendered diagram calleduml.pngis updated in the repository. - Relation indicators on the diagram represent the actual state of code (like inheritance or composition between classes).
- All fields are indicated on the diagram
- All fields are indicated on the diagram
- The PlantUML source file named
None
- Remember that polymorphism enables you to reduce the amount of code to write. Use it to avoid data and logic redundancy.
- As a rule of thumb: a field should be a class member
(static) if all the instances share the same value, and
a method can be static if no instances are needed to perform its task
e.g. no need to store state between invocations.
- For example, the radius of a circle (
r) is a field of a specific circle instance, but the written formula of its perimeter ("2×π×r") is the same for all circles so this string should be stored only once in a class field. - Correspondingly, the method which calculates the actual area
is an instance method (since it needs the value of
r), while the method returning the formula can be static (since it only needs access to a static field).
- For example, the radius of a circle (
- There are multiple ways to use PlantUML, but the easiest one is to use the online editor linked below. You can generate an image by clicking on "View as PNG" under the code box.