Describing how to solve problems
By the end of this topic you should:
Unfortunately programming doesn’t do anything for you on its own.
Every piece of functionality on every computer - from the operating system all the way up to this page being drawn from HTML have all been enabled by someone writing that functionality.
Programming or software development is essentially writing the general solution to a problem in a strongly structured and limited set of keywords.
So programming doesn’t solve your problems for you, it enables you to:
Whiteboard exercise:
Imagine a person who has no idea how anything works, but can carry out basic instructions.
How would you tell them how to make a cup of tea?
An example solution could be as simple as:
Our solution includes phrases like "fill the cup" and "Remove the teabag".
How does someone "fill a cup"?
These instructions never mention a spoon, which would be quite painful!
In how much detail do we need to go to adequately describe our solution?
The answer is contextual
We can safely assume the person knows what pouring is, but not that they would need to turn on a kettle
In software we have a different context, the difference between what basic functions are provided and what is not.
Before we dive into Java, we're first going to learn the standard toolbox in most languages.
We are going to use Pseudocode to learn these tools, and then transfer our learning into Java.
Pseudocode is an almost-bridge between normal English statements and a fully functional programming language.
It’s a way of expressing the logic you want to achieve without having to go straight into writing code.
It doesn’t have a set syntax (defined structure) but does have the key components of most programming languages.
In order to describe solutions to problems, most programming languages provide the following functionality at a minimum:
Variables are for storing values we can refer to and update in our program.
set variableName to variableValue
set myAge to 24
set legalDrinkingAge to 18
Collections of things are very common in programming, so we can represent those too
set destinations to ["Rome", "Tokyo", "New York"]
set oddNumbers to [1, 3, 5, 7, 9]
if-statements give us conditional execution in our code.
This enables us to change the program's behavior based on true/false assertions.
if (some condition)
do X
As a flowchart, this looks like:
This program checks if a person's age is of legal drinking age in the UK.
set myAge to 24
set legalDrinkingAge to 18
if (myAge is greater than or equal to legalDrinkingAge)
I can drink alcohol
An 'else' clause can be added, which is done if the condition is false:
if (some condition)
do X
else
do Y
As a flowchart, this looks like:
The previous example has been extended to describe what people who cannot legally drink can do.
set myAge to 24
set legalDrinkingAge to 18
if (myAge is greater than or equal to legalDrinkingAge)
I can drink alcohol
else
I can only drink soft drinks
Conditions can be chained, if you need more than 2 paths:
if (condtion 1)
do X
else if (condition 2)
do Y
else
do Z
As a flowchart, this looks like:
This program checks the day of the week, and prints a message depending on what day.
set dayOfWeek to the current day of the week
if (dayOfWeek is Saturday)
it is the beginning of the weekend
else if (dayOfWeek is Sunday)
it is the last day of the weekend
else
it is not the weekend
Write pseudocode for the following:
If Captain Obvious is happy, all customers get a 15% discount. Otherwise, no discounts are available.
With our existing tools, we can do things to a piece of data
What if we want to do the same thing to everything in a collection?
We can achieve this by iterating through our list using for-loops
For-loops enable us to do some operations multiple times, there are 2 varieties:
Essentially "Do something for each thing in some collection".
for (each item in a collection)
do something
This program takes a list of destination names and turns them into links for a destination landing page.
(e.g. https://uk.hotels.com/search.do?q-destination=Rome)
set destinations to ["Rome", "Tokyo", "New York"]
for (each destination in destinations)
create a link to its destination landing page
This example as a flowchart:
Write pseudocode for the following:
A hotel wants confirmation of all the bookings over the next week. Print each booking number from the list thisWeekBookings.
Sometimes you aren't iterating over a list, but you instead want to iterate over a range of numbers.
For example, I don't need a list if I want to count from 1 to 100, I don't want to store a list of these numbers just in case.
for with counter gives you a counter you can use for whatever you want. You can:
for (counter from lowerBound to upperBound)
do something
or with a different increment/step
for (counter from lowerBound to upperBound, step stepValue)
do something
This would print out the 2 times table.
for (counter from 2 to 20, step 2)
print counter
Write pseudocode for the following:
A hotel wants to run a special offer on their $150 room, but isn't sure what percent discount to apply. Calculate the amount of money saved for each value from 1 to 50 percent.
On rare occasions you will need to something an unknown number of times, for example:
While loops let us do something a potentially infinite number of times
The loop stops when the condition becomes true
while (some condition is true)
do something
A concrete example that copies a file from one file to another, line by line
set file1 to the file to copy
set file2 to a new file
while (file1 has more lines)
set currentLine to next line of file1
write currentLine to file2
The previous example as a flowchart:
Write pseudocode for the following:
Hotels.com is running a giveaway competition and wants to give away as many prizes as possible with a $500,000 budget. Each prize costs $33.29. Keep giving away prizes (taking away from the budget) until this budget has been used up.
For example, to decide whether to take out an umbrella today, I’d say
If it’s going to rain then I’d take my umbrella.
Which I can write in a more structured way as:
if (it is going to rain)
bring my umbrella
else
leave it at home
Or to find someone’s name in an unsorted list, I’d say
Go through the list until I see the name I’m looking for
This is a bit harder to translate to pseudocode, but essentially I’m saying
I would check each item in the list, until I find the name or I’ve checked the entire list
Which I can now write as:
for (each name in the list)
if (that name matches the one I'm looking for)
the name is in the list
else
continue through the list
if (I go through the whole list without finding the name)
the name is not in the list
Or using a variable, as:
set isInTheList to false
for (each name in the list)
if (that name matches the one I'm looking for)
set isInTheList to true
else
continue through the list
if (isInTheList is true)
the name is in the list
else
the name is not in the list
The provided solutions are only example solutions, there is no single answer.
The more strict you are in using pseudocode, the closer your solution is to a functioning program.
Write a program that decides whether I should wear a jacket, and whether I should also take an umbrella out today.
Give your answer in pseudocode.
set currentWeather to the current weather
if (currentWeather is cold)
take jacket
if (currentWeather is rainy)
take umbrella
Notice that we haven't said how to get the current weather. Pseudocode's job is to let us focus on our own logic, and worry about this later.
We also store the current weather in a variable, so we can refer back to it in a consistent way.
Print every number beween 1 and 100, then only print numbers that are a multiple of 5.
Give your answer in pseudocode.
for (counter from 0 to 100, step 5)
print counter
Using a step of 5 means our counter goes up in increments of 5. We need no extra logic to print multiples of 5.
Given a box of red, green and blue marbles, sort them all into smaller boxes by colour.
Give your answer in pseudocode.
set redBox to empty
set greenBox to empty
set blueBox to empty
for (each marble in mixedBox)
if (marble is red)
put marble in redBox
if (marble is green)
put marble in greenBox
if (marble is blue)
put marble in blueBox
Add up the numbers from 1 to 10.
Give your answer in pseudocode.
Extension:Print the running total as you're adding up.
set total to 0
for (counter from 1 to 10)
total = total + counter
print total
We start with a counter at zero. We then begin the loop which runs 10 times, the value of counter increasing by 1 each time.
So the first run, counter is 1 and total is set to 1, second run counter is 2 and total is set to 1 + 2, third run counter is 3 and total is set to 3 + 3, ...
set total to 0
for (counter from 1 to 10)
total = total + counter
print total
print total
We now print the total each time we go through the loop body, which will print the in-progress subtotals.
Thinking back, how would you make a cup of tea?
Give your answer in pseudocode.
get a cup and teabag
place teabag in cup
fill kettle
turn on kettle
wait until kettle is boiled
if (recipient wants milk)
3/4 fill cup using kettle
fill remainder of cup with milk
else
fill cup using kettle
if (recipient wants sugar)
add sugar
serve cup