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.
- A line is a mark when it is a whole number from 0 to 100. Add each mark to the total and count it.
- For every other line, print
Rejected:followed by the line, and carry on with the next line. - When the line is exactly
end, stop reading. Then printAccepted:followed by the number of marks, and on the next lineTotal:followed by their total. - 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 beforeend.
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
- typed: 70
- typed: 85
- 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
- typed: 70
- typed: absent
- typed: 150
- 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
- typed: 0
- typed: 100
- typed: -1
- typed: 101
- typed: end
It prints
Rejected: -1 Rejected: 101 Accepted: 2 Total: 100