COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Similar documents
Building Java Programs

Topic 3 static Methods and Structured Programming

Introduction to Computer Programming

Building Java Programs

Building Java Programs

Building Java Programs Chapter 1

CS 112 Introduction to Programming

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

Building Java Programs Chapter 1

Lecture 1: Basic Java Syntax

Static Methods & Decomposition

Entering the world of Javatar

Building Java Programs

CSE 142, Summer 2015

CSc 110, Autumn Lecture 2: Functions

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

CSE 142, Summer 2014

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Procedural decomposition using static methods

clicker question Comments Using comments Topic 3 static Methods and Structured Programming

Building Java Programs. Chapter 1: Introduction to Java Programming

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Building Java Programs. Introduction to Java Programming

CSE 201 Java Programming I. Smart Coding School website:

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Building Java Programs. Introduction to Programming and Simple Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

CS 112 Introduction to Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lab # 2. For today s lab:

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Getting Started With Java

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

The CS job market. Building Java Programs Chapter 1. Programming languages. What is programming? Introduction to Java Programming

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

Week 2: Data and Output

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Building Java Programs

Building Java Programs

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Building Java Programs

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Programming with Java

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS106A, Stanford Handout #25. Methods 2

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

Introduction to Data Structures

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CS 170 Java Programming 1. Week 5: Procedures and Functions

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Getting started with Java

Everyday Algorithms. Algorithms and Introduction to Programming. bake a cake. start. mix sugar and butter. stir in 2 eggs. stir in flour.

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Repetition with for loops

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Lecture Notes for CS 150 Fall 2009; Version 0.5

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

C02: Overview of Software Development and Java

CS 177 Recitation. Week 1 Intro to Java

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

CS Week 2. Jim Williams, PhD

Basic Programming Language Syntax

COMPUTATIONAL THINKING

QUIZ. What is wrong with this code that uses default arguments?

ALICE: An introduction to progamming

SDKs - Eclipse. SENG 403, Tutorial 2

Lecture 7. Instructor: Craig Duckett OUTPUT

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008)

Topic 7 parameters. Based on slides from Marty Stepp and Stuart Reges from

CMSC201 Computer Science I for Majors

A Unicorn Named Sparkle Word Search. A New Friend for Sparkle Coloring Sheet. A Unicorn Named Sparkle s First Christmas Coloring Sheet

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

UEE1302 (1102) F10: Introduction to Computers and Programming

The diagram below is only partially complete. Use the terms in the word list to complete the empty boxes. Make dinner

C++ Support Classes (Data and Variables)

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

AP Computer Science A Summer Assignment 2017

Transcription:

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