Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13
Last time Design before code Computers are stupid, you are smart Variable = a name for a place to store a value Activity diagram elements Pseudo code 2
Next time If you want to take notes, get/print the slides before you come to class BRING YOUR CLICKERS 3
4 Programming is more than writing code Programming – representing a general solution to problem set in a way a machine can use, which consists of 1.Design of an Algorithm a step by step solution to a problem that completes in finite time 2.Coding – translate an algorithm into a program 3.Testing – ensure the correctness of the program 4
5 Job 1: Design developing a solution to the problem If you can’t solve the problem, you can’t write the code Use graphical representations (UML) to solve 5
6 Programs are like airplanes Airplanes On the ground (still) – Static – Easy to know what it looks like (structure) In the air – Dynamic – Flexes with wind, gravity, pilot controls, physics Programs Code (not running) – Static – Easy to know what it looks like (structure) Executing (a process) – Dynamic – Changes with input data, user control, system, rules of languages
OO Programming Break the problem into entities (space, board, game) Model the entities with classes Declare and use objects of the classes (9 spaces) 7 entity classes boundary class
8 OO Programming Code Programs are composed (written by humans) – Structural Code – Action Code – Comments – because a meaningful program is revised at a later date
Structural Code Establishes the organizational structure of programs and classes Designed using UML class diagrams 9
Action Code Implements algorithms that actually do things Mostly written inside methods Designed using flow charts / activity diagrams 10
11 UML is used to design UML = “unified modeling language” includes several diagram types, including – Class diagrams represents the structure of the program – Activity diagrams represents the activity of the program
12 UML Class Diagram Robot X Y direction forward() turn() Name of the class Values (instance variables) Actions (methods)
13 Class and Object Class is the pattern (cookie cutter) – Structure: defines the Values (instance variables) Actions (methods) – Structure: the programmer writes a class (code not executing) Object is the instance (cookie) – Action – Each object has its own copy of instance variables – Each can do the actions defined in the class in a running program
14 Big Picture NetBeans Project program Package – a collection of classes Class – a name for a collection of values (attributes) and a collection of methods Method – the actions in a class Statements – the individual actions of a method
15 Big Picture Package Robot X Y direction forward() turn() Room length width Obstacle length width RProgram Robot Room Obstacle1 Obstacle2 Obstacle3 Begin() Each class is a file on the computer Class Method
Activity Diagram Elements ElementSymbol Start Stop Statements Branching Repetition Subprograms Order of activities 16
17 Loops: top-tested 17 Loop condition checked BEFORE the loop body Loop may execute 0 times
18 Loops: bottom-tested 18 Loop condition checked AFTER loop body execution Loop will execute AT LEAST once
19 Write a program that … Simulates the movement of a robot in a room. The robot can do these things: move forward one step (one foot), turn to the right (90°). In the room are some number of rectangular obstacles. If the robot runs into one of these objects, it can’t move forward. …
20 Paper airplanes! Step 0: partner Step 1: write the instructions for making a paper airplane on a piece of paper Step 2: give the instructions to your partner Step 3: do exactly as the instructions you received say to do (just as a computer would) Step 4: test 20
21 Find (and fix) design problems before they become code problems Play the computer – Simulate the program you have designed by following the design you wrote – Follow the design you wrote, not the one in your head Tracing / Desk-checking 21
22 Problems from Chapter 4 Write a program to read a list of nonnegative integers and to display the largest integer, the smallest integer, and the average of all integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest, and average values. The average should be a value of type double so that it is computed with a fractional part. 22
23 Summary Problem solving does not involve code We’ve discussed (and practiced) the basic problem solving concepts and skills Don’t panic if this didn’t all make sense … it’s only week 2 23