Skip to content
ExamSpine

Lesson 6.5 · Write it

Mark checker

A whole programPython or Java10 tests, 7 of them hidden

On a phone? Put the lines in order instead

A teacher types test marks, one per line, and types end after the last one. Write a program that checks each line before using it, rejects the ones that cannot be marks, and adds up the rest.

  1. A line is a mark when it is a whole number from 0 to 100. Add each mark to the total and count it.
  2. For every other line, print Rejected: followed by the line, and carry on with the next line.
  3. When the line is exactly end, stop reading. Then print Accepted: followed by the number of marks, and on the next line Total: followed by their total.
  4. A line that is a whole number is from -1000 to 1000, written with digits and maybe a minus sign in front. Any other line is a word, a decimal such as 55.5, or empty. There are at most 30 lines before end.

In Python, convert each line with int and catch ValueError. In Java, convert it with Integer.parseInt and catch NumberFormatException.

Your program is run several times. Only what it prints is checked, and spaces at the end of a line are ignored.

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 10 times: the 3 examples below, and 7 hidden ones that check the rules above. Only what it prints is checked.

  • two marks, both kept (rules 1 and 3)

    You type

    1. typed: 70
    2. typed: 85
    3. typed: end

    It prints

    Accepted: 2
    Total: 155
  • a word, and a whole number that is too big, are both rejected (rule 2)

    You type

    1. typed: 70
    2. typed: absent
    3. typed: 150
    4. typed: end

    It prints

    Rejected: absent
    Rejected: 150
    Accepted: 1
    Total: 70
  • 0 and 100 are marks; -1 and 101, just outside them, are not (rules 1 and 2)

    You type

    1. typed: 0
    2. typed: 100
    3. typed: -1
    4. typed: 101
    5. typed: end

    It prints

    Rejected: -1
    Rejected: 101
    Accepted: 2
    Total: 100
solution.pySolution.java