Skip to content
ExamSpine

Lesson 1.12 · Write it

Pay slip

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

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