COSC 236 Section 101 Computer 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. 2
Review of Quiz 02 Lecture 04 2/5/2014 3
Review of Quiz 02 Lecture 04 2/5/2014 4
Review of Quiz 02 Lecture 04 2/5/2014 5
Review of Quiz 02 Lecture 04 2/5/2014 6
Review of Quiz 02 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 Lecture 04 2/5/2014 7
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 8
Review Write a println statement to produce the line of output: /\ //\\ System.out.println("\ /\\\ //\\\\\"); 9
Review What println statements will generate this output? This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this 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.\""); 10
Review What println statements will generate this output? A "quoted" String is 'much' better if you learn the 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 \""); 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. 12
Review Example of a wellcommented program: /* 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"); 13
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) 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) 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 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 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. 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) 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. 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 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. 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.... 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."); 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 like adding a new command to Java. class method A statement statement statement method B statement statement method C statement statement statement 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. 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."); 27
Declaring a method (pp. 31-34) Syntax: Gives your method a name so it can be executed 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."); 28
Calling a method (pp. 31-34) Syntax: name(); Executes the method's code You can call the same method many times if you like. Example: printwarning(); Output: This product causes cancer in lab rats and humans. 29
Program with static method (pp. 31-34) public class FreshPrince { public static void main(string[] args) { rap(); // Calling (running) the rap method System.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 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."); 31
Control flow (pp. 34-36) 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(); 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.");... System.out.println("Done with main."); public static void message1() { System.out.println("This is message1."); 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. 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.) 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 35
Example Runtime Error (pp. 39-40) A more sophisticated infinite loop: This sets up an infinite loop resulting in a runtime error 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 \ / \ / +--------+ 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. +--------+ 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("+--------+"); 40
Development strategy 2 (pp. 41-42) \ / \ / \ / \ / +--------+ Second version (structured, with redundancy): Identify the structure of the output. Divide the main method into static methods based on this structure. STOP \ / \ / +--------+ 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 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(); 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("+--------+"); 43
Development strategy 3 (pp. 43-44) \ / \ / \ / \ / +--------+ Third version (structured, without redundancy): Identify redundancy in the output, and create methods to eliminate as much as possible. Add comments to the program. STOP \ / \ / +--------+ 44
Output redundancy (pp. 43-44) \ / \ / \ / \ / +--------+ STOP \ / \ / +--------+ 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) 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(); // 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(); 46
Program version 3, cont'd. (pp. 43-44) // Draws a teacup figure. Public static void teacup() { eggbottom(); line(); System.out.println(); // 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("+--------+"); 47
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: Soderstrand@ieee.org Don t forget to take the Quiz before you leave today. 48