Skip to content
ExamSpine

Lesson 1.9 · Write it

Rectangle

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads the width and height of a rectangle, each a whole number on its own line, and prints two lines:

Area: 15
Perimeter: 16
  1. The first line is Area: and one space, then the width times the height.
  2. The second line is Perimeter: and one space, then twice the sum of width and height.
  3. The width is typed first, then the height.
  4. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert each line. In Java, use Integer.parseInt.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a 5 by 3 rectangle (rules 1 to 4)

    You type

    1. typed: 5
    2. typed: 3

    It prints

    Area: 15
    Perimeter: 16
  • a 1 by 1 square (rules 1 and 2)

    You type

    1. typed: 1
    2. typed: 1

    It prints

    Area: 1
    Perimeter: 4
solution.pySolution.java