THINK FIRST·CODE LATER

← All labs

Floor plan: total area through an abstract class

Problem

An architect's plan lists rooms of different shapes. Write an abstract class Room with a name, the abstract method getArea(), a concrete method paintNeeded(double litresPerSquareMetre) and a toString() that uses getArea(); then three subclasses:

  • RectangularRoom (width, length);
  • RoundRoom (radius);
  • LShapedRoom (width, length, and the width and length of the corner cut out of the rectangle).

FloorPlan reads the number of rooms, then lines rect name w l, round name r or lshape name w l cutW cutL, stores them in a Room[], and uses two static methods, totalArea(Room[]) and largest(Room[]). For the input

4
rect Kitchen 3 4
round Tower 2
lshape Living 6 5 2 2
rect Hall 1.5 6

the program prints

Kitchen      12.00 m2
Tower        12.57 m2
Living       26.00 m2
Hall          9.00 m2
Total area: 59.57 m2
Largest room: Living
Floor paint at 0.25 litre per m2: 14.9 litres

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l15-8-data-1.txt

4
rect Kitchen 3 4
round Tower 2
lshape Living 6 5 2 2
rect Hall 1.5 6
What your program must print for l15-8-data-1.txt
Kitchen      12.00 m2
Tower        12.57 m2
Living       26.00 m2
Hall          9.00 m2
Total area: 59.57 m2
Largest room: Living
Floor paint at 0.25 litre per m2: 14.9 litres

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l15-8-data-1.txt

Windows PowerShell does not understand <:

Get-Content l15-8-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.