Lecture12_1226 - Mathematics & Computing Science

advertisement
Lecture 12: Final Review
Tami Meredith
Programming Requires
1.
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 algorithm
Control Flow is the basis of algorithms
3. Coding the solution
4. Testing, debugging, verifying the solution
Step Number 1
UNDERSTAND 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.
What is a Program?
 Programs = Data + Algorithms
a. Data: the nouns, the things we manipulate
b. Algorithms: the verbs, the actions we perform
 A program manipulates the input (data) to produce
the required output (data)
Step Number 2
SOLVE 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.
Step Number 3
LET 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 ...
String Methods
 See 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 string
 equals(string2) returns TRUE if string equals string2
otherwise it returns FALSE
Hint: I would know how to use the replace method...
Math Methods
 See Figure 6.3in the text (page 402)
 pow(x,y); returns xy
 abs(x); returns the absolute value of x
 random(); returns a pseudo-random number in the
range 0 ≤ x < 1
 sqrt(x); returns the square root of x
 Others exist as well as variants of some (e.g., random)
 They use double, not float
Character Methods
 See Figure 6.4 in the text (page 407)
 toUpperCase()
 isUpperCase()
 Saves using ASCII to do it yourself
 Knowing ASCII will also be hugely helpful for dealing
with Characters
Input Methods
 See 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()
Exercise
 Write 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
Solution
public 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()
Programs in Java
Programs 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 actions
4. Actions are performed by statements
Control is Power!
 Control flow is actually very simple:
Everything is sequential, unless ...
2. Something is optional
1.
Conditional Statements, i.e,. if
3. Something is repeated
Looping Statements, i.e., do, for, while
4. Something complex is broken into simpler parts
Methods (and classes, objects)
Methods
 Methods permit us to reuse a block of code
 Methods 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
Key Concept
 Don'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.
About Statements ...
 Statements are like sentences in a programming language
 Statements usually end with a ";" (semi-colon)
 Statements are performed sequentially, one after the other
(generally left to right, top to bottom)
 Statements DO things
 For example:
x = x + 1;
y = 2 * x;
System.out.println("Y is " + y);
Statements
 Expressions 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 statement
 e.g., { statement1; statement2; statement3; ... }
Blocks
 Statements can be grouped into a block
 A 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);
}
Exercise
 Write 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:
******
******
******
******
Solution
public 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()
How to write a program
1.
2.
Pick a name
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.
6.
7.
Identify the variables – what do you have to work with? What do you
need to produce?
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.
Translate your algorithm into Java. Does Java provide the tools you
need?
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
The Usuals
 Nothing = Zero – don't leave questions blank, ANYTHING is better








than nothing!
There are no penalties for incorrect answers
SHOTGUN approach – write anything and everything you think is
possibly relevant if you don't know the actual answer
Pseudo code, point-form, flow charts ... If you don't know the Java,
answer it some other way
For 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 right
Leave lots of space, add stuff later when you think of it
Write code in pencil, bring an eraser
Answer questions in any order – do easiest stuff first
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!
To Do
 Go to the lab and ensure you have marks for
Assignments 1 to 10
 Prepare 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.
Use the force, my padawans!
Download