Tim Cooke

Exercises for Programmers (Python): 7 of 57 - Area of a Rectangular Room

2nd September 2020

Exercise 7 in book Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian P. Hogan.

Create a program that calculates the area of a room. Prompt the user for the length and width in feet. Then display the area in both square feet and square meters.

Example Output

What is the length of the room in feet? 25
What is the width of the room in feet? 20
You entered dimensions of 15 feet by 20 feet.
The area is
300 square feet
27.871 square meters

This one was straight forward for me as it can be solved quite tidily with an Object. Create an object providing the length and width values at construction time then it's easy to have accessor functions that provide the area in square feet and square meters. Of course starting with a couple of tests.

import unittest

from exercises.Ex07_area_of_a_rectangular_room.rectangular_room import RectangularRoom

class TestRectangularRoom(unittest.TestCase):

    def test_given15FeetBy20Feet_thenArea300SquareFeet(self):
        rectangularRoom = RectangularRoom(15, 20)
        self.assertEqual(rectangularRoom.areaFeet(), 300)

    def test_given15FeetBy20Feet_thenArea27point871SquareMeters(self):
        rectangularRoom = RectangularRoom(15, 20)
        self.assertEqual(rectangularRoom.areaMeters(), 27.871)

Writing tests first is a nice way to approach a problem because it drives the design. In this case I have made a clear expectation that a RectangularRoom class is required and how I expect it to take the length and width values. At this point I realise I don't know how to write a constructor for a Python class nor how to represent class variables, so some research is required and of course it turns out to be simple enough.

class RectangularRoom:
    lengthFeet = 0.0
    widthFeet = 0.0

    def __init__(self, length, width):
        self.lengthFeet = length
        self.widthFeet = width

Then a couple of functions are required to provide the area of the room in square feet and square meters. Nothing particularly tricky here other than having to figure out how to round floating point numbers to 3 decimal places.


    def areaFeet(self):
        return self.lengthFeet * self.widthFeet

    def areaMeters(self):
        return round(self.areaFeet() * self.SQUARE_FEET_TO_SQUARE_METER_CONVERSION, 3)

The script functionality with user input is nothing new from the previous examples, just read in some data, process it, and respond appropriately.

if __name__ == "__main__":
    length = input("What is the length of the room in feet? ")
    width = input("What is the width of the room in feet? ")
    print("You entered dimensions of " + length + " feet by " + width + " feet.")
    print("The area is")
    room = RectangularRoom(float(length), float(width))
    print(str(room.areaFeet()) + " square feet")
    print(str(room.areaMeters()) + " square meters.")
$ python3 exercises/Ex07_area_of_a_rectangular_room/rectangular_room.py 
What is the length of the room in feet? 15
What is the width of the room in feet? 20
You entered dimensions of 15 feet by 20 feet.
The area is
300.0 square feet
27.871 square meters.

Learning review