cosc 236 section 101 - tripod
TRANSCRIPT
COSC 236 Section 101Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Web Site
• I hope to have a course web site up on Blackboard soon
• However, I am using the following site all semester to allow you to download course material:
http://www.class-notes.us
From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
2
Review of Quiz 02
COSC 236 :Prof. M.A. Soderstrand Lecture 04 2/5/2014
3
Review of Quiz 02
COSC 236 :Prof. M.A. Soderstrand Lecture 04 2/5/2014
4
Review of Quiz 02
COSC 236 :Prof. M.A. Soderstrand Lecture 04 2/5/2014
5
Review of Quiz 02
COSC 236 :Prof. M.A. Soderstrand Lecture 04 2/5/2014
6
Review of Quiz 02
COSC 236 :Prof. M.A. Soderstrand Lecture 04 2/5/2014
7
PROBLEM: The tab character \n in output moves the cursor to the next tab stop on the output console. The default setting for tab stops on the DrJava console is 8 spaces.
Two tab characters are required after the Gold Ring
Review• What is the output of the following println statements?
System.out.println("\ta\tb\tc");
a b c
System.out.println("\\\\");
\\
System.out.println("'");
'
System.out.println("\"\"\"");
"""
System.out.println("C:\nin\the downward spiral");
C:
in he downward spiral
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
8
Review
• Write a println statement to produce the line of output:
/ \ // \\ /// \\\
System.out.println("/ \\ // \\\\ /// \\\\\\");
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
9
Review
• What println statements will generate this output?
This program prints aquote from the Gettysburg Address.
"Four score and seven years ago,our 'fore fathers' brought forth onthis continent a new nation.“
System.out.println("This program prints a");System.out.println("quote from the Gettysburg Address.");System.out.println();System.out.println("\"Four score and seven years ago,");System.out.println("our 'fore fathers' brought forth on");System.out.println("this continent a new nation.\"");
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
10
Review
• What println statements will generate this output?A "quoted" String is'much' better if you learnthe rules of "escape sequences."
Also, "" represents an empty String.Don't forget: use \" instead of " !'' is not the same as “
System.out.println("A \"quoted\" String is");
System.out.println("'much' better if you learn");
System.out.println("the rules of \"escape sequences.\"");
System.out.println();
System.out.println("Also, \"\" represents an empty String.");
System.out.println("Don't forget: use \\\" instead of \" !");
System.out.println("'' is not the same as \"");
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
11
Review
• Where to place comments:
• at the top of each file (a "comment header")
• at the start of every method (seen in today’s lecture)
• to explain complex pieces of code
• Comments are useful for:
• Understanding larger, more complex programs.
• Multiple programmers working together, who must understand each other's code.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
12
Review
• Example of a well-commented program:
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
13
/* Suzy Student, CS 101, Fall 2019
This program prints lyrics about ... something. */
public class BaWitDaBa {
public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy diggy");
System.out.println();
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}
Chapter 1.3 (pp. 24-28)
• Program Errors (p. 24)• Every programmer, no matter how experienced, spends a lot of their time
finding errors “bugs” in their own programs!
• Some errors are found by the computer software (compiler errors and run time errors), but many errors are hidden from the computer and you must find them!
• Three Types of Errors• Syntax Errors usually found by the compiler
• Logic Errors in which the program runs but does not do what it is supposed to do
• Logic Errors that are so bad that the program will not even run (runtime errors)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
14
Chapter 1.3 (pp. 24-28)
• Syntax Errors (p. 24)• The Java Compiler is easily confused by the simplest of grammar errors! (Look at
the FIRST compiler error – the following errors may actually be generated by the first error)
• A common, but difficult to find, error made by those new to Java is to have an incorrect file name (eg: Our program with class Hello MUST be stored in a file Hello.java).
• Common Syntax Errors (pp. 24-28)• Incorrect punctuation (careful with every brace, bracket, parenthesis, simi-colon,
etc.)• Spelling Error (Java is case sensitive)• Keyword Error (misspelling or using the wrong key word or using a key word by
mistake as a variable)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
15
Chapter 1.3 (pp. 24-28)
• Logic Errors (p. 28)• Logic errors are errors in the logic of the programming that causes
the program not to do what it is intended to do
• Logic errors can be avoided by writing carefully constructed code
• Logic errors usually do not show up in the compiler – but rather at run time – hence, they are also referred to as runtime errors
• Logic errors are also called “bugs” in the program
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
16
Chapter 1.4 Procedural Decomposition (pp. 28-40)• Decomposition (pp. 28-31)
• One of the key contributions of CS-1 to your problem solving skills is the concept of decomposing complex problems into a series of simple ones
• This decomposition is the key to good problem solving
• Decomposition is also an important way to help avoid logic errors
• Decomposition is also allows you to check each small problem for possible security issues
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
17
Chapter 1.4 Procedural Decomposition(pp. 28-40)• Procedural Decomposition is about problem solving (pp. 28-30)
• Procedural decomposition is taking a complex problem and breaking it down into an algorithm that consists of well-organized simple steps
• COSC-236 (CS-1) concentrates on procedural programming as opposed to object-oriented programming which is the subject of COSC-237 (CS-2)
• Procedural programming concentrates on the actions of solving a problem (Verb oriented)
• Object-oriented programming concentrates on decomposing the problem into a set of objects that must interact (Noun oriented)
• All computer languages support procedural programming and most modern languages support object-oriented programming – JAVA supports both.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
18
Chapter 1.4 Procedural Decomposition(pp. 28-40)
• Procedural vs Object-Oriented Problem Solving (pp. 28-30)• BOTH are based on breaking problems down into an algorithm that
consists of well-organized simple steps (procedural) or simple objects (object-oriented)
• CLASS is the key concept for object-oriented programming which is the subject of COSC-237 (CS-2) so we do little with classes in COSC-236 (CS-1)
• METHODS and programming structures (loops, selection, etc.) are the key concepts for procedural programming and hence will be our main topics in COSC-236 (CS-1)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
19
Procedural Algorithms (pp. 30-31)
• algorithm: A list of steps for solving a problem.
• Example algorithm: "Bake sugar cookies"• Mix the dry ingredients.
• Cream the butter and sugar.
• Beat in the eggs.
• Stir in the dry ingredients.
• Set the oven temperature.
• Set the timer.
• Place the cookies into the oven.
• Allow the cookies to bake.
• Spread frosting and sprinkles onto the cookies.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
20
Problems with algorithms (pp. 30-31)
• lack of structure: Many tiny steps; tough to remember.
• redundancy: Consider making a double batch...• Mix the dry ingredients.• Cream the butter and sugar.• Beat in the eggs.• Stir in the dry ingredients.• Set the oven temperature.• Set the timer.• Place the first batch of cookies into the oven.• Allow the cookies to bake.• Set the timer.• Place the second batch of cookies into the oven.• Allow the cookies to bake.• Mix ingredients for frosting.• Spread frosting and sprinkles onto the cookies
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
21
Structured algorithms (pp. 30-31)
• structured algorithm: Split into coherent tasks.1 Make the cookie batter.• Mix the dry ingredients.• Cream the butter and sugar.• Beat in the eggs.• Stir in the dry ingredients.
2 Bake the cookies.• Set the oven temperature.• Set the timer.• Place the cookies into the oven.• Allow the cookies to bake.
3 Add frosting and sprinkles.• Mix the ingredients for the frosting.• Spread frosting and sprinkles onto the cookies.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
22
Removing redundancy (pp. 30-31)
• A well-structured algorithm can describe repeated tasks with less redundancy.
1 Make the cookie batter.• Mix the dry ingredients.• ...
2a Bake the cookies (first batch).• Set the oven temperature.• Set the timer.• ...
2b Bake the cookies (second batch).
3 Decorate the cookies.• ...
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
23
A program with redundancy (pp. 30-31)public class BakeCookies {
public static void main(String[] args) {
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles.");
}
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
24
Static methods (pp. 31-34)
• static method: A named group of statements.• denotes the structure of a program
• eliminates redundancy by code reuse
• procedural decomposition:
• dividing a problem into methods
• Writing a static method is likeadding a new command to Java.
class
method A
statement
statement
statement
method B
statement
statement
method C
statement
statement
statement
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
25
Using static methods (pp. 31-34)
1. Design the algorithm.• Look at the structure, and which commands are repeated.
• Decide what are the important overall tasks.
2. Declare (write down) the methods.• Arrange statements into groups and give each group a name.
3. Call (run) the methods.• The program's main method executes the other methods to perform the overall
task.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
26
Design of an algorithm (pp. 31-34)
// This program displays a delicious recipe for baking cookies.public class BakeCookies2 {
public static void main(String[] args) {// Step 1: Make the cake batter.System.out.println("Mix the dry ingredients.");System.out.println("Cream the butter and sugar.");System.out.println("Beat in the eggs.");System.out.println("Stir in the dry ingredients.");
// Step 2a: Bake cookies (first batch).System.out.println("Set the oven temperature.");System.out.println("Set the timer.");System.out.println("Place a batch of cookies into the oven.");System.out.println("Allow the cookies to bake.");
// Step 2b: Bake cookies (second batch).System.out.println("Set the oven temperature.");System.out.println("Set the timer.");System.out.println("Place a batch of cookies into the oven.");System.out.println("Allow the cookies to bake.");
// Step 3: Decorate the cookies.System.out.println("Mix ingredients for frosting.");System.out.println("Spread frosting and sprinkles.");
}}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
27
Gives your method a name so it can be executed
• Syntax:
public static void name() {statement;statement;...statement;
}
• Example:public static void printWarning() {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans.");
}
Declaring a method (pp. 31-34)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
28
Calling a method (pp. 31-34)
Executes the method's code
• Syntax:name();
• You can call the same method many times if you like.
• Example:printWarning();
• Output:
This product causes cancerin lab rats and humans.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
29
Program with static method (pp. 31-34)
public class FreshPrince {
public static void main(String[] args) {
rap(); // Calling (running) the rap methodSystem.out.println();
rap(); // Calling the rap method again
}
// This method prints the lyrics to my favorite song.
public static void rap() {System.out.println("Now this is the story all about how");
System.out.println("My life got flipped turned upside-down");
}}
Output:Now this is the story all about how
My life got flipped turned upside-down
Now this is the story all about how
My life got flipped turned upside-down
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
30
Final cookie program (pp. 31-34)// This program displays a delicious recipe for baking cookies.
public class BakeCookies3 {
public static void main(String[] args) {
makeBatter();
bake(); // 1st batch
bake(); // 2nd batch
decorate();
}
// Step 1: Make the cake batter.
public static void makeBatter() {
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
}
// Step 2: Bake a batch of cookies.
public static void bake() {
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
}
// Step 3: Decorate the cookies.
public static void decorate() {
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles.");
}
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
31
• When a method is called, the program's execution...• "jumps" into that method, executing its statements, then• "jumps" back to the point where the method was called.
public class MethodsExample {
public static void main(String[] args) {
message1();
message2();
System.out.println("Done with main.");
}
...
}
public static void message1() {
System.out.println("This is message1.");
}
public static void message2() {
System.out.println("This is message2.");
message1();
System.out.println("Done with message2.");
}
public static void message1() {
System.out.println("This is message1.");
}
Control flow (pp. 34-36)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
32
Methods calling methods (pp. 36-39)
public class MethodsExample {public static void main(String[] args) {
message1();message2();System.out.println("Done with main.");
}
public static void message1() {System.out.println("This is message1.");
}
public static void message2() {System.out.println("This is message2.");message1();System.out.println("Done with message2.");
}}
• Output:This is message1.This is message2.This is message1.Done with message2.Done with main.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
33
When to use methods (pp. 36-39)
• Place statements into a static method if:• The statements are related structurally, and/or
• The statements are repeated.
• You should not create static methods for:• An individual println statement.
• Only blank lines. (Put blank printlns in main.)
• Unrelated or weakly related statements.(Consider splitting them into two smaller methods.)
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
34
Example Runtime Error (pp. 39-40)• A static procedure must not call itself
• This sets up an infinite loop resulting in a runtime error
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
35
Example Runtime Error (pp. 39-40)• A more sophisticated infinite loop:
• This sets up an infinite loop resulting in a runtime error
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
36
Drawing complex figures with static methods
Text Section 1.5 (pp. 40-46)
Static methods question (pp. 40-41)
• Write a program to print these figures using methods.______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
38
Development strategy (pp. 40-41)
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
First version (unstructured):
Create an empty program and main method.
Copy the expected output into it, surrounding each line with System.out.println syntax.
Run it to verify the output.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
39
Program version 140-41) (pp. public class Figures1 {
public static void main(String[] args) {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println("+--------+");
System.out.println();
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("| STOP |");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("+--------+");
}
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
40
Development strategy 2 (pp. 41-42)
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
Second version (structured, with redundancy):
Identify the structure of the output.
Divide the main method into static methods based
on this structure.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
41
Output structure (pp. 41-42)______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
The structure of the output:
initial "egg" figure
second "teacup" figure
third "stop sign" figure
fourth "hat" figure
This structure can be represented by methods:
egg
teaCup
stopSign
hat
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
42
Program version 2 (pp. 41-42)public class Figures2 {
public static void main(String[] args) {
egg();
teaCup();
stopSign();
hat();
}
public static void egg() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
}
public static void teaCup() {
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println("+--------+");
System.out.println();
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
43
public static void stopSign() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("| STOP |");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
}
public static void hat() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("+--------+");
}
}
Development strategy 3 (pp. 43-44)
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
Third version (structured, without redundancy):
Identify redundancy in the output, and create methods to eliminate as much as possible.
Add comments to the program.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
44
Output redundancy (pp. 43-44)The redundancy in the output:
egg top: reused on stop sign, hat
egg bottom: reused on teacup, stop sign
divider line: used on teacup, hat
stop: Used in middle of stop sign
This redundancy can be fixed by methods:
eggTop
eggBottom
line (Not a separate method – println)
stop (Not a separate method – println)
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
45
Program version 3 (pp. 43-44) // Suzy Student, CSE 138, Spring 2094
// Prints several figures, with methods for structure and redundancy.
public class Figures3 {
public static void main(String[] args) {
egg();
teaCup();
stopSign();
hat();
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
46
// Draws the top half of an an egg figure.
public static void eggTop() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
}
// Draws the bottom half of an egg figure.
public static void eggBottom() {
System.out.println("\\ /");
System.out.println(" \\______/");
}
// Draws a complete egg figure.
public static void egg() {
eggTop();
eggBottom();
System.out.println();
}
Program version 3, cont'd. (pp. 43-44)
// Draws a teacup figure.
Public static void teacup() {
eggBottom();
line();
System.out.println();
}
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
47
// Draws a stop sign figure.
public static void stopSign() {
eggTop();
System.out.println("| STOP |");
eggBottom();
System.out.println();
}
// Draws a figure sort of like a hat.
public static void hat() {
eggTop();
line();
}
// Draws a line of dashes.
public static void line() {
System.out.println("+--------+");
}
}
Assignments• Read Chapter 2.1 and 2.2 (pp. 63-89) before class on Wednesday
• Complete the Chapter 1 Laboratory assignment• Submit your work as a Microsoft Word Document containing a report on
each problem you solved in the laboratory.• At a minimum, the report should include:
• Your first and last name• The well-commented source code for each problem• A screen shot of the DrJava screen showing successful compilation and run• A complete output (you may need to copy and paste this into your Word document)
• Email your completed Word Document to Dr. Soderstrand at:• [email protected]
• Don’t forget to take the Quiz before you leave today.
COSC 236 :Prof. M.A. Soderstrand Lecture 03 2/3/2014
48