Area 02 · Object-oriented programming
Shape area report
Read a list of shapes, then report each area, the total and the largest, without the report loop knowing which kind of shape it holds.
- Spec no.
- HO-03
- Level
- Warm-up
- Time box
- 45 min
- Reference
- Java · C#
- Grading
- Self-assessed · rubric is ours, not DICT's
01 Coverage
| Area | Sub-skills practised | Role |
|---|---|---|
| 01 Data manipulation | Validation, ArrayList handling | Also |
| 02 Object-oriented programming | Abstraction, Encapsulation, Inheritance, Polymorphism | Primary |
Sign up to start coding practice
Read the task freely. An account is required before editing and running a solution. Code runs also need an internet connection.
Sign up or sign in02 Requirements
- Abstract class Shape with abstract area() and describe().
- Circle(r), Rectangle(w, h) and Square(side). Square extends Rectangle and overrides only describe().
- Fields are private or protected, set in the constructor. Every dimension must be greater than zero; the constructor rejects anything else.
- Read lines until end: circle r, rect w h or square s. For an unknown shape, a wrong number of values, a non-number or a non-positive size, print Skipped: <line>.
- Keep all shapes in one list of Shape. Print describe() and the area to two decimals for each, then Total area and Largest.
03 Input and output
- Lines of circle r | rect w h | square s
- end to finish
- Skipped lines as they are read
- Circle(r=2.00) area=12.57 style lines
- Total area: x.xx and Largest: <describe()>
circle 2
rect 3 4
triangle 3
square 5
circle -1
endSkipped: triangle 3
Skipped: circle -1
Circle(r=2.00) area=12.57
Rectangle(3.00 x 4.00) area=12.00
Square(5.00) area=25.00
Total area: 49.57
Largest: Square(5.00)Match the sample exactly first, then write at least two test inputs of your own.
04 Edge cases to test
- triangle 3 (unknown shape)
- circle -1 and circle 0
- rect 3 (missing value)
- An empty list before end: print the total only
05 Starter outline and hints
abstract Shape { area(); describe(); static positive(v) }Circle, Rectangle(protected w, h), Square extends Rectangleparse(line) → Shape or null; catch bad numbersfor each shape: print; total += area; track largest
Hint 1
Throwing an exception from the constructor keeps invalid objects from ever existing.
Hint 2
Use Math.PI, not 3.14, or your circle area will be off in the second decimal.
06 Practice rubric
| Criterion | Sub-skills | Look for | Pts |
|---|---|---|---|
| Abstraction and inheritance | AbstractionInheritance | Abstract base; Square reuses Rectangle's area(). | 30 |
| Polymorphism | Polymorphism | The report loop has no type checks or casts. | 25 |
| Encapsulation | Encapsulation | No public mutable fields; validation inside constructors. | 20 |
| Input handling | Validation | Every bad line is skipped with a message; nothing crashes. | 15 |
| Output | ArrayList handling | Two-decimal format and the right largest shape. | 10 |
| Total | 100 | ||
Our own weighting for practice. It is not the DICT scoring scheme.
07 Self-check
Tick only what you have verified by running your own program.
Loading saved checks…
08 Reference solution
Both versions compiled and reproduced the sample output on Java 21 and .NET 8 when this site was built. Reference solutions are in Java and C#. The 2023 FAQ copy also lists C, C++ and Visual Basic 6.0 / .NET; the same structure carries over, but you will need your language's own file and database APIs.
Try the whole brief first. The reference is one correct approach, not the only one, and reading it early removes most of the practice value.
09 Exam-day habits
- Work without internet search to simulate exam conditions.
- Type the sample input yourself and compare your output line by line before you look at the reference.