lecture 12: final review tami meredith. programming requires 1. identification of the problem...

27
CSCI1226 Introduction to Computing Science and Programming Lecture 12: Final Review Tami Meredith

Upload: buck-abraham-gordon

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

CSCI1226Introduction to Computing Science

and Programming

Lecture 12: Final Review Tami Meredith

Page 2: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Programming Requires1. Identification of the problem

Understanding it, identifying correct solutions

2. Solving the problem:A. Selecting the data structures, and

Data Management is the key here

B. Identifying an algorithmControl Flow is the basis of algorithms

3. Coding the solution4. Testing, debugging, verifying the solution

Page 3: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Step Number 1UNDERSTAND THE PROBLEM

It is impossible to solve the problem if you don't fully understand it!

Know all the details. Know what the solution will look like.

Read everything completely before you begin.

Page 4: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

What is a Program?Programs = Data + Algorithms

a. Data: the nouns, the things we manipulateb. Algorithms: the verbs, the actions we perform

A program manipulates the input (data) to produce the required output (data)

Page 5: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Step Number 2SOLVE THE PROBLEM

Half the task is figuring out a way to find the solution you desire.

Don't worry about format or Java – Just write down some steps that will find the solution!

Paper is cheap – write down your ideas.There is more than one way to do most things

when programming.

Page 6: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Step Number 3LET JAVA DO THE WORK

Think about all the methods you know! Can you use one to do part of the job or make

the job easier?Will sorting the data help?Can you convert it to an easier to use format?

E.g., StringBuffer has a built-in reverse method ...

Page 7: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

String MethodsSee Figure 2.5 in the text (page 86)length() returns the length of a string (as an

integer)indexOf(string2) returns the index of string2

in string or -1 if string2 is contained in stringequals(string2) returns TRUE if string

equals string2 otherwise it returns FALSE

Hint: I would know how to use the replace method...

Page 8: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Math MethodsSee Figure 6.3in the text (page 402)pow(x,y); returns xy

abs(x); returns the absolute value of xrandom(); returns a pseudo-random number

in the range 0 ≤ x < 1sqrt(x); returns the square root of x

Others exist as well as variants of some (e.g., random)

They use double, not float

Page 9: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Character MethodsSee Figure 6.4 in the text (page 407)toUpperCase()isUpperCase()Saves using ASCII to do it yourselfKnowing ASCII will also be hugely helpful for

dealing with Characters

Page 10: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Input MethodsSee Figure 2.7 in the text (page 98)User input is done using a Scanner on the input stream System.in

Paired methods: hasNextLine(), nextLine()

Page 11: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

ExerciseWrite a method called caps that takes as its

parameter a single string and prints out, to the screen, all the capital letters in the string.e.g., caps("A Cool Course!") prints ACC

Page 12: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Solutionpublic static void caps (String s) { for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i); // if (Character.isUpperCase(c)) if (('A' <= c) || (c <= 'Z')) System.out.print(c); } System.out.println("");} // end caps()

Page 13: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Programs in JavaPrograms are structured hierarchically:1. Programs have 1 or more classes

Files are named after the classes they contain

2. Classes contain 1 or more methods The class that is used to start execution must

contain a method named main

3. Methods perform 1 or more actions4. Actions are performed by statements

Page 14: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Control is Power!Control flow is actually very simple:1. Everything is sequential, unless ...2. Something is optional

Conditional Statements, i.e,. if

3. Something is repeatedLooping Statements, i.e., do, for, while

4. Something complex is broken into simpler parts

Methods (and classes, objects)

Page 15: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

MethodsMethods permit us to reuse a block of codeMethods permit us to simplify code by

replacing a complicated part with a meaningful name

Methods permit us to break difficult things into smaller (named) parts

Methods permit us to hide the details of something

Page 16: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Key ConceptDon't do hard stuff if you can't figure out

how!Just pretend that a method exists that would

do the hard part and use it.Later on, try to create (at least some parts) of

the method you used.Decompose hard problems into smaller parts.

Page 17: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

About Statements ...Statements are like sentences in a programming

languageStatements usually end with a ";" (semi-colon)Statements are performed sequentially, one after

the other (generally left to right, top to bottom)Statements DO thingsFor example:

x = x + 1;y = 2 * x;System.out.println("Y is " + y);

Page 18: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

StatementsExpressions and assignments

e.g., x = 3 * y;Conditional statements to make choices

e.g., if (x == 0) System.exit(0);Loops to repeat things

e.g., while (i > 0) System.out.println(i--);

Blocks to group statements into a single statemente.g., { statement1; statement2; statement3; ... }

Page 19: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

BlocksStatements can be grouped into a blockA block is treated like a single statement and

can be used where a single statement is expected

Blocks are formed by surrounding a group of statements with curly braces, "{" and "}"

For example: { y = x * 2; System.out.print("Two times " + x + " is " + y);}

Page 20: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

ExerciseWrite a method called rectangle that takes

two integers, a height and a width, as parameters and prints to the screen a rectangle of the specified height and width, e.g., rectangle(4,6) prints:

************************

Page 21: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Solutionpublic static void rectangle (int h, int w) { int rows, cols; for (rows = 0; rows < h; rows++) { for (cols = 0; cols < w; cols++) { System.out.print("*"); } System.out.println(""); }} // end rectangle()

Page 22: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

How to write a program1. Pick a name2. Identify the code:

// Q2 solution, by Tami Meredith

3. Define a class: public class name { ... }

4. Write a main method: public static void main (String[] args) { ... }

5. Identify the variables – what do you have to work with? What do you need to produce?

6. Determine an algorithm – how do I turn my input into my output? IMPORTANT: Do not think "using Java" at this point ... just figure out ANY way to solve the problem.

7. Translate your algorithm into Java. Does Java provide the tools you need?

Page 23: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Cheat Sheet Suggestions Program template Basic output to the screen: print, println, printf Basic input from the keyboard

Listing 1.1 covers the first 3 points String methods (Figure 2.5+) Scanner methods (Figure 2.7+) Math methods (Figure 6.3) Character methods (Figure 6.4) and ASCII examples Miscellaneous methods (Sorting, Exiting Programs, etc.) Comparisons (Figure 3.4), logical operators (Figure 3.7) such as ||

and && -- know what short circuit evaluation is Examples of how to define and call methods Examples of how to use the % operator Examples of how to create and use arrays Examples of how to examine strings one character at a time

Page 24: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

The UsualsNothing = Zero – don't leave questions blank, ANYTHING is better

than nothing!There are no penalties for incorrect answersSHOTGUN approach – write anything and everything you think is

possibly relevant if you don't know the actual answerPseudo code, point-form, flow charts ... If you don't know the Java,

answer it some other wayFor T/F, Multiple-Choice, Matching, etc. GUESS if you have to – no

negative scoring!Copy examples (from your cheat sheet) that you think might work,

don't worry about perfection, just about getting a small part rightLeave lots of space, add stuff later when you think of itWrite code in pencil, bring an eraserAnswer questions in any order – do easiest stuff first

Page 25: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Fibonacci Numbers We have done the Fibonacci sequence in the

lab, and in an exercise where we used an array to make things easier.

This sequence should not be too scary (math-wise) by now.

There are lots of other sequences that use the same rules but different starting points.

Math is fun! We can't have a final exam without any!

Page 26: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

To DoGo to the lab and ensure you have marks for

Assignments 1 to 10Prepare your "Cheat Sheet" for the exam

TWO pieces of paper, 8.5 x 11 inches, hand-written, double sided

Re-read Chapters 1-7 Practice programming: Assignments,

Exercises etc.

Page 27: Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving

Use the force, my padawans!