CS代写 Week 1 Basic Patterns – cscodehelp代写

Week 1 Basic Patterns

What is programming?
Programming is the process of converting a specification into a program.
A description of a program¡¯s goals
Code that achieves the goals
Specification

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 Bono (1970)

What is a pattern?
¡ñ A common solution to a common problem.
Example program:
The pattern: ¡°sum¡±
int totalRainfall = 0;
for (int i = 0; i < rainfall.length; i++) {{ totalRainfall += rainfall[i]; sum += item; }} for each item The ¡°sum¡± pattern Goal: Find the sum of a collection of items. sum = 0;

sum += ;
¡ñ Words enclosed between are holes that need to be filled.
¡ñ is typically double or int
¡ñ is any loop
over a collection of items
¡ñ refers to the next item in
¡ñ x += y adds the amount y onto x.

The ¡°output¡± pattern
Goal: Show a value to the user. ¡ñ Pattern:
System.out.println(¡°

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();
(if has already been declared)

Read operations
¡ñ We provide you with class In which has 4 static methods:
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
¡ñ Copy this class into your project.
Then simply call the method on the class: In.nextInt()

The ¡°read loop¡± pattern
Goal: Read values until the user enters an ¡°end of input¡± value.

while ( != )


Observations:
¡ñ appears twice
¡ñ always test for the ¡°end of input¡±
value immediately after a .

The ¡°array loop¡± pattern
Goal: Loop over items in an array.
for (int i = 0; i < .length; i++)

The ¡°count¡± pattern
Goal (without guard): Count the number of items in a collection. Goal (with guard): Count the number of items that satisfy a condition.
Without guard:
int count = 0;

With guard:
int count = 0;

if ()

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.
int x = 7; int x = 7; System.out.println(x++); // shows System.out.println(++x); // shows 78

The ¡°max¡± pattern
Goal: Find the maximum value in a collection of items
max = ;

if ( > max)
max = ;
If this item is bigger than the max so far, then make it the new max.

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

Process: Words-to-code table
Specification: Read in card numbers until the user enters -1. Show the highest card.
Break the specification down into pieces: Read in the card numbers /
until the user enters -1 /
the highest card

Process: Words-to-code table
Arrange the words into a table:
Code/pattern
Read in card numbers
until the user enters -1
the highest card

Process: Words-to-code table
Translate the words to code:
Code/pattern
Read in card numbers
until the user enters -1
the highest card

Leave a Reply

Your email address will not be published. Required fields are marked *