Week 1
Basic Patterns
What is programming?
Speci
ficatio
n
Progr
am
A description of a
program’s goals
Code that
achieves the goals
P
rocess
Programming is the process of
converting a specification into a
program.
Patterns
MEMORY
2
What is the process?
Not a single process!
● Words to code table
● Key/framework approach
● Debugging/Testing
● Break it down, build it up
● Class diagrams
● Location tables
● + a bit of creativity and intuition[1]
[1] A classic text on creativity: “Lateral Thinking” by Edward De Bono (1970)
3
What is a pattern?
● A common solution to a common problem.
int totalRainfall = 0;
for (int i = 0; i < rainfall.length; i++)
{
totalRainfall += rainfall[i];
}
sum = 0;
for each item
{
sum += item;
}
Example program: The pattern: “sum”
4
The “sum” pattern
{
sum +=
}
● Words enclosed between
that need to be filled.
●
●
over a collection of items
●
the loop.
● x += y adds the amount y onto x.
Goal: Find the sum of a collection of items.
5
The “output” pattern
Goal: Show a value to the user.
● Pattern:
System.out.println(“
● e.g. show an age:
System.out.println(“age is “ + age);
● e.g. show a name:
System.out.println(“name is “ + name);
6
The “read” pattern
Goal: Read a value from the user.
● Pattern:
System.out.print(“
Or,
System.out.print(“
● e.g. read an age:
System.out.print(“Age: “);
int age = In.nextInt();
7
Read operations
● We provide you with class In which has 4 static methods:
● Copy this class into your project.
Then simply call the method on the class: In.nextInt()
public static int nextInt() Reads an integer
public static double nextDouble() Reads a double
public static char nextChar() Reads a single character
public static String nextLine() Reads a String
8
The “read loop” pattern
while (
{
}
Observations:
●
● always test for the “end of input”
value immediately after a
Goal: Read values until the user enters an “end of input” value.
9
The “array loop” pattern
for (int i = 0; i <
{
Goal: Loop over items in an array.
10
DEMO
11
DEMO
12
The “count” pattern
int count = 0;
count++;
int count = 0;
if (
count++;
Without guard: With guard:
Goal (without guard): Count the number of items in a collection.
Goal (with guard): Count the number of items that satisfy a condition.
13
x++ vs ++x
● Both x++ and ++x add 1 to x.
● x++ returns the value of x and THEN adds 1 to x.
● ++x adds 1 to x and THEN returns the value of x.
e.g.
int x = 7;
System.out.println(x++); // shows
7
x = 8
int x = 7;
System.out.println(++x); // shows
8
x = 8
14
DEMO
15
The “max” pattern
{
if (
max =
}
Key idea:
If this item is bigger than the max so far,
then make it the new max.
Goal: Find the maximum value in a collection of items
16
Process
17
Process: Words-to-code table
Specification: Read in card numbers until the user enters -1. Show the highest
card.
Enter card number: 8
Enter card number: 3
Enter card number: 12
Enter card number: 7
Enter card number: -1
The highest card is 12
18
Process: Words-to-code table
Break the specification down into pieces:
Read in the card numbers /
until the user enters -1 /
Show /
the highest card
Specification: Read in card numbers until the user enters -1. Show the highest
card.
19
Process: Words-to-code table
Words Code/pattern
Read in card numbers read
until the user enters -1 read loop
Show output
the highest card max
Arrange the words into a table:
20
Process: Words-to-code table
Words Code/pattern
Read in card numbers read
until the user enters -1 read loop
Show output
the highest card max
Translate the words to code:
21
DEMO
22