Skip to content
ExamSpine

Lesson 1.12

Capstone: several inputs, several results

Part 1: Programs that talkReading, about 3 min1 exercise in this lesson

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Planning a program with several inputs and several results

By the end of this you will be able to plan a program that reads several values and prints several results, using only what you already know.

This lesson has no new idea. It puts the last eleven lessons together. A bigger program is easier to write when you plan it in four steps, in this order (steps 1 and 2 often share a line):

  1. Read: put each line into its own variable, in the order the lines are typed.
  2. Convert: decide what each line is: text, a whole number or a decimal. Turn the number lines into numbers.
  3. Compute: work out each result and keep it in its own variable.
  4. Print: show each result on its own line, with its label.

The four steps in one program

This program reads the name of a garden plot, its length and width in whole metres, and the price of one metre of fence. It prints the area, the length of fence needed, and what the fence costs.

name = input()
length = int(input())
width = int(input())
price = float(input())
area = length * width
fence = 2 * (length + width)
cost = fence * price
print("Plot: " + name)
print("Area: " + str(area))
print("Fence: " + str(fence))
print("Cost: " + str(cost))
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String name = scanner.nextLine();
    int length = Integer.parseInt(
        scanner.nextLine());
    int width = Integer.parseInt(
        scanner.nextLine());
    double price = Double.parseDouble(
        scanner.nextLine());
    int area = length * width;
    int fence = 2 * (length + width);
    double cost = fence * price;
    System.out.println("Plot: " + name);
    System.out.println("Area: " + area);
    System.out.println("Fence: " + fence);
    System.out.println("Cost: " + cost);
}

If the user types Herbs, 4, 3 and 2.5, one per line, the program prints:

Plot: Herbs
Area: 12
Fence: 14
Cost: 35.0

Look at how the program is laid out. All the reading and converting comes first. Then each result gets its own line of arithmetic. The printing comes last. The brackets in the fence line make the addition happen before the multiplication. The name stays text, because nothing is worked out from it.

Plan before you type

Before you write any code, list the inputs and the results on paper:

  • For each input: what is it called, and is it text, a whole number or a decimal?
  • For each result: which inputs does it use, and what arithmetic?

Then write the program one step at a time, and run it after each step. If a result could have a long tail of decimal places, round it the way the exercise tells you, and check whether a later result is worked out from the rounded value.

A common mistake: forgetting to convert. A line you never converted is still text, so + joins it to the next one instead of adding, and the program may still run and print something that looks almost right.

Now try it yourself on the questions below.

Exercise 1.12.11.12.1 · Write itPay slip

Write a program that reads a name (text) on the first line, the hours worked (a whole number) on the second line, and the hourly rate (a decimal number) on the third line, then prints a pay slip of four lines:

Name: Ada
Gross: 112.5
Tax: 11.25
Net: 101.25
  1. Gross pay is the hours times the rate.
  2. Tax is 10 percent of the gross, rounded to at most 2 decimal places.
  3. Net pay is the gross minus the tax, rounded to at most 2 decimal places.
  4. Each line starts with its label, a colon and one space, then the value.
  5. The name is printed exactly as typed.
  6. 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 for hours and float for the rate; round with round(value, 2). In Java, use Integer.parseInt for hours and Double.parseDouble for the rate; round with Math.round(value * 100.0) / 100.0.

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 full pay slip (rules 1 to 6)

    You type

    1. typed: Ada
    2. typed: 15
    3. typed: 7.50

    It prints

    Name: Ada
    Gross: 112.5
    Tax: 11.25
    Net: 101.25
  • zero hours (rules 1 to 4)

    You type

    1. typed: Ben
    2. typed: 0
    3. typed: 10.00

    It prints

    Name: Ben
    Gross: 0.0
    Tax: 0.0
    Net: 0.0
solution.pySolution.java