Worksheet 1


Input, Calculation and Variables

Short Assessed Programming Exercise 1, Input, Calculation and Variables

To pass this exercise you must demonstrate that you are able to do the following in solving the problem

❑ EXPLAIN HOW YOUR PROGRAM WORKS

❑ Write a program that uses input instructions

❑ Write a program that uses variables

❑ Write a program that uses arithmetic expressions

❑ Write a program that makes simple use of comments – at least the author’s name and date and explanation of what the program does overall

When your program works, explain it to a student, test it and check if it ticks all the boxes, If you are having problems or don’t understand the requirements, ask for help.


Student Loan Calculation

A Student Loan company is providing an online calculator for students to work out how much they owe. They first input how much they owed at the start of the year, then how much they paid off that year. You may assume these are in whole numbers of pounds. It then adds 6.3% interest to the total at the end of the year and prints out the new total. The final answer should be given to two decimal places (rounded). HINT: You can do this by using the built-in round() function.

An example run of the program is as follows (numbers in bold are typed in by the user):


Amount of loan at start of year? 1000

Amount paid off this year? 5

The new amount owed is (in pounds): 1057.69


Another example run:

Amount of loan at start of year? 1000

Amount paid off this year? 6

The new amount owed is (in pounds): 1056.62



GRADING:

Your program will be marked taking into account the next boxes

❑ The program runs correctly for all input (not just the examples above) including extreme values. [15 marks]

❑ The program should clearly indicate what should be typed when the user is needed to enter values (including indicating values not acceptable). [15 marks]

This program is now more complicated. You cannot just run it once and see if it works as what it does depends on the values input. You will need to run it lots of times – enough to convince yourself it always works. What about extreme values – very big or very small? Zero is always a good value to test for. For this exercise, if the program crashes when bad values are entered that is ok as long as the user was warned in some way not to type those values beforehand. Check if the correct answer is given. Check carefully there are no missing spaces or punctuation in the output. Inspecting your code by eye, including checking if all variables are given a value before the value is used, is always a good idea. Check the comments to make sense.


Making Decisions

Short Assessed Programming Exercise 2: Making Decisions

To pass this exercise you must demonstrate that you are able to do the following in solving the problem

❑ EXPLAIN HOW YOUR PROGRAM WORKS

❑ Write a program that uses if-then-else constructs.

❑ Write a program that includes useful comments, at least one per method saying what it does.

❑ Write a program that uses indentation in a way that makes its structure clear.

When your program works, explain it to a student, test it and check it ticks all the boxes, If you are having problems or don’t understand the requirements, ask for help.


Body Age

Write a program that estimates a person’s body age - whether they are ageing faster or slower than their actual years - as a measure of how fit and healthy they are. This simplified test is based on the person inputting their actual age in years, their heart rate (beats in a minute) and how far they can stretch forward when sitting on the floor (to the nearest cm). This is the distance between where their fingers fall when back straight and where they fall when stretching forward without bending knees. The calculation is done as follows. Take their actual age and add or subtract years to it as follows:

• Heart rate of less than 60, subtract 4

• Heart rate of 60-64, subtract 2.

• Heart rate of 65-72, add 1.

• Heart rate of 73 or more, add 2.

Next modify the resulting age based on the distance they can stretch: • Stretch less than 25cm: add 3

• Stretch 25-36cm: add 2.

• Stretch 37cm, add 0.

• Stretch 38-40cm, subtract 2.

• Stretch more than 40cm, subtract 3

An example run of the program (words in bold are typed in by the user):


What is your age? 55

What is your resting heart rate? 66

How far can you stretch? 37

Your body’s age is 56

Another example run:

What is your age? 21

What is your resting heart rate? 61 How far can you stretch? 42

Your body’s age is 16



GRADING:

Your program will be marked taking into account the next boxes

❑ The program runs correctly for all input. [10 marks]

❑ All branches of the IF-THEN-ELSE constructs work [20 marks] ❑ The program deals with input it doesn’t know about [10 marks]

Decision statements add a new level of difficulty for testing. You need to make sure every line works, but when you run the code some of the lines are not executed – as in any if statement either the then case or the else case is executed not both. You must test the program by running it lots of times with different values, choosing values that will definitely test all the lines of code (so test all branches) between them. Also remember to check carefully the output is right (spaces, punctuation, etc).


Worksheet 3 & 4


Worksheet 3

While Loops

To pass this exercise you must demonstrate that you are able to do the following in solving the problem

❑ EXPLAIN HOW YOUR PROGRAM WORKS

❑ Write a program that correctly uses while loops

❑ Include simple comments – at least the actual author’s name and date


Paralympic Swimming relay

Paralympic swimming relay competitions involve competitors of different disability levels making up a team. For those with physical disabilities, each swimmer’s disability is rated on a scale of 1-10 and the total points rating for the 4 swimmers must be no greater than a given number of points. For example, a legal relay team for a 34-point freestyle relay may consist of two S8 swimmers and two S9 swimmers (9 + 9 + 8 + 8 = 34), or an S10 swimmer and three S8 swimmers (10 + 8 + 8 + 8 = 34). Write a program that uses a loop to work out the total disability point score for a single team, and check that it is legal, for a given point score relay.

HINT: Have a variable that keeps a running total.

The program must be split in to functions that take arguments and return results in a sensible way. One function should take the swimmer’s number (1-4) as argument and ask for and return the number the user inputs (that swimmer’s disability category). Another function should calculate the legality of the team, when given as an argument the total of the four disability classes and the maximum total allowed.

The following is an example run of the program


What is the classification (maximum points) of this relay event? 34

What is the disability category of Swimmer 1? 10

What is the disability category of Swimmer 2? 8

What is the disability category of Swimmer 3? 8

What is the disability category of Swimmer 4? 8

That team has 34 points so is legal


The following is another example run of the program


What is the classification (maximum points) of this relay event? 32

What is the disability category of Swimmer 1? 9

What is the disability category of Swimmer 2? 9

What is the disability category of Swimmer 3? 8

What is the disability category of Swimmer 4? 7

That team has 33 points so is NOT legal


GRADING:

Your program will be marked taking into account the next boxes

❑ The program runs correctly. [10 marks]

❑ Running totals are correct at all points through the loop [20 marks]

❑ The program uses a while loop construct and does the correct number of iterations [20 marks]

As with the if statements, loops execute code depending on a test. You need to check that the loop does execute exactly the right number of times every time. Make sure loops can’t run forever for any input (including input the programmer didn’t think of the user ever entering such as empty strings, zero and negative numbers. You can include print statements in the code, so that when you run it, you get some feedback on what the code is doing. Perhaps “This is the ith time through this loop”, etc..


Worksheet 4

To pass this exercise you must demonstrate that you are able to do the following in solving the problem

❑ EXPLAIN HOW YOUR PROGRAM WORKS

❑ Write a program that correctly uses while loops

❑ Include simple comments – at least the actual author’s name and date

When your program works, explain it to a student, test it and check if it ticks all the boxes, If you are having problems or don’t understand the requirements, ask for help.


Late Trains

Write a program that is to be used to count the number of times trains are late departing from a particular station. It should use a while loop to repeatedly ask for the user to give the number of minutes late that the train that just departed was. It should stop asking when any negative number is entered. It should then give the number of trains that were on time (those that were no minutes late) out of the total and the number of minutes the worst train was late.

For example, one run might be as follows.


How many minutes late was the train? 2

How many minutes late was the train? 0

How many minutes late was the train? 0

How many minutes late was the train? 13

How many minutes late was the train? 1

How many minutes late was the train? 0

How many minutes late was the train? -1

3 out of 6 trains were on time.


The worst train was 13 minutes late.


GRADING:

Your program will be marked taking into account the next boxes

❑ The program runs correctly. [10 marks]

❑ The program stops asking the user if a negative number is entered. [20 marks]

❑ It prints EXACTLY the right thing (reread the above description and make sure you didn’t miss anything) [50 marks]

❑ There are comments in the code to understand what the program does [20 marks]