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

Similar documents
CIS 110: Introduction to Computer Programming

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

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CIS 110: Introduction to Computer Programming

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

CREATE YOUR CONTENT STRATEGY & LAUNCH PLAN Amanda Genther Inc. & Irresistible Offerings

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

CIS 110: Introduction to Computer Programming

Agenda: Discussion Week 7. May 11, 2009

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Scenario-Based Testing added to an Existing Development Program

Midterms Save the Dates!

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

CS349/SE382 A1 C Programming Tutorial

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Lecture 5: Methods CS2301

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CS 177 Week 5 Recitation Slides. Loops

Introduction to Data Structures

CSE 201 Java Programming I. Smart Coding School website:

CS Introduction to Programming Fall 2016

CSE 113 A. Announcements - Lab

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

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Lab 8: Introduction to Scala 12:00 PM, Mar 14, 2018

MAT Program Design. College of Engineering Engineering Education Innovation Center

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CIS 110: Introduction to Computer Programming

CS 113 PRACTICE FINAL

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

Lab Preparing for the ICND1 Exam

Learner User Interface (LUI): Developing Themed Graphics to Enhance Your e-learning. Kevin Thorn, AutoZone

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Building Java Programs

New York University Introduction to Computer Science Midterm2 Sample Problems 2013 Andrew I. Case. Instructions:

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Nights & Weekend ROB & PHIL DECEMBER 12, 2008

Software and Programming 1

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can!

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

CS Week 2. Jim Williams, PhD

CHAPTER : 9 FLOW OF CONTROL

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CSE 142, Summer 2015

IT150/IT152 Concepts Summary Sheet

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Bjarne Stroustrup. creator of C++

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

Announcements. Course syllabus Tutorial/lab signup form (due 4pm today) Lecture 1 notes Homework 1 Initial assessment

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Class, Variable, Constructor, Object, Method Questions

Constructors for classes

COMP 110/L Lecture 5. Kyle Dewey

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE 142, Summer 2014

Week 6 CS 302. Jim Williams, PhD

AP Computer Science A Summer Assignment 2017

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

Midterms Save the Dates!

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Programming with Java

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

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

CS 318 Principles of Operating Systems

Final Exam Practice. Partial credit will be awarded.

CT 229 Arrays in Java

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

Building Java Programs

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CE221 Programming in C++ Part 1 Introduction

Claims Management - Delay analysis Online Workshop. Week No. 1: concept of claims

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Midterms Save the Dates!

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

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

Dynamic Arrays Makefiles and Mul3ple File Compiles

CIS 110: Introduction to Computer Programming

: Dimension. Lecturer: Barwick. Wednesday 03 February 2016

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

EECS 1001 and EECS 1030M, lab 01 conflict

Midterms Save the Dates!

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10

Transcription:

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

Outline Structure and redundancy in algorithms Static methods Procedural decomposition 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 2

Reminders for the week Intro CIS 110/final exam surveys Sign up for and use Piazza! Lecture and lab content My office hours (GRW 260) MW: one hr immediately after each lecture Th: 5:30 6:30 Or email me for an appointment! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 3

In Review: Why are We Here? Learning about algorithmic thinking via computer programming! 1. Precision 2. Decomposition 3. Abstraction Our focus for most of the semester! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 4

Structure and Redundancy in Algorithms 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 5

Our Running Example public class PBJ { public static void main(string[] args) { System.out.println( Take out the ingredients and utensils. ); System.out.println( Put ingredients and utensils on the table. ); System.out.println( Remove the cap from the peanut butter. ); System.out.println( Scoop out some peanut butter. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe knife clean with a nupkin. ); System.out.println( Remove the cap from the jelly. ); System.out.println( Scoop out some jelly. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe nife clean with napkin. ); System.out.println( Put the two pieces of bread together. ); System.out.println( Put the bread into your mouth and chew. ); 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 6

Problem #1: Where s the Structure? public class PBJ { public static void main(string[] args) { System.out.println( Take out the ingredients and utensils. ); System.out.println( Put ingredients and utensils on the table. ); System.out.println( Remove the cap from the peanut butter. ); System.out.println( Scoop out some peanut butter. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe knife clean with a nupkin. ); System.out.println( Remove the cap from the jelly. ); System.out.println( Scoop out some jelly. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe nife clean with napkin. ); System.out.println( Put the two pieces of bread together. ); System.out.println( Put the bread into your mouth and chew. ); 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 7

Problem #2: We re Repeating Ourselves (Poorly) public class PBJ { public static void main(string[] args) { System.out.println( Take out the ingredients and utensils. ); System.out.println( Put ingredients and utensils on the table. ); System.out.println( Remove the cap from the peanut butter. ); System.out.println( Scoop out some peanut butter. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe knife clean with a nupkin. ); System.out.println( Remove the cap from the jelly. ); System.out.println( Scoop out some jelly. ); System.out.println( Spread it on a piece of bread. ); System.out.println( Wash knife in the sink. ); System.out.println( Wipe nife clean with napkin. ); System.out.println( Put the two pieces of bread together. ); System.out.println( Put the bread into your mouth and chew. ); 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 8

Structure and Redundancy Programs should reflect the structure of the problem at hand Better understanding Programs should not contain redundancy Better maintainability What tool can we use to solve these problems? 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 9

Static Methods 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 10

First: a Note About Learning Programming Languages 1. There are lots of layers to a language 2. As you peel away the layers, you ll cry 3. But it s not the layer that causes you to cry, it s the cutting itself that does it! It gets better the sharper your programming language knife becomes! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 11

Recall: Syntax, Syntax, Syntax Syntax: the rules to form legal programs Class template public class <name> { <method> <method> <method> Method template public static void <name>( ) { <statement> ; <statement> ; 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 12

An Example Static Method public class PBJ { Method signature or header Method body public static void printpreamble() { System.out.println( Take out the ingredients and utensils. ); System.out.println( Put ingredients and utensils on the table. ); public static void main(string[] args) { printpreamble(); Method call or invocation Method declaration 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 13

Static Method Declarations (Static) methods are named chunks of code that you can reuse. All classes can use me I m not associated with a particular object I take no arguments public static void <name>() { <statement>; I return no value <statement>; <statement>; Methods can be declared in any order in a class. main is just another method (albeit special)! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 14

Static Method Calls You use methods by invoking or calling them. [Inside some method] <name of method>(); Calling a method results in 1. Executing the body of that method. 2. Resuming execution right after you made the call. System.out.println( ) is just another method call! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 15

Where Can We Call Methods? From main public static void main(string[] args) { foo(); But main is just another method, so really public static void mymethod() { someothermethod(); We can call any method from any other method! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 16

Control flow class Foo { public static void main(string[] args) { System.out.println( main ); bar(); System.out.println( done ); public static void main( ) { public System.out.println( main ); static void bar() { bar(); System.out.println( done ); public baz(); static void baz() { System.out.println( bar ); System.out.println( baz ); public static void bar() { baz(); System.out.println( bar ); public static void baz() { System.out.println( baz ) Output: main baz bar done 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 17

When Control Flow Goes Wild class Foo { public static void main(string[] args) { System.out.println( main ); bar(); System.out.println( done ); public static void bar() { baz(); System.out.println( bar ); public static void baz() { bar(); System.out.println( baz ); An infinite loop of public static void main( ) { public System.out.println( main ); static void bar() { bar(); public static void baz() { public baz(); static void bar() { public System.out.println( done ); bar(); baz(); System.out.println( bar ); static void baz() { System.out.println( baz ); System.out.println( bar ); public bar(); static void bar() { public System.out.println( baz ); baz(); static void baz() { public bar(); System.out.println( bar ); static void bar() { System.out.println( baz ); public baz(); static void baz() { public System.out.println( bar ); bar(); static void bar() { public baz(); System.out.println( baz ); static void baz() { System.out.println( bar ); public bar(); static void bar() { public System.out.println( baz ); baz(); static void baz() { public bar(); Output: System.out.println( bar ); static void bar() { System.out.println( baz ); public main baz(); static void baz() { System.out.println( bar ); bar(); System.out.println( baz ); method calls! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 18

Runtime and Logic Errors Remember: compilation is only step #2! Your program may compile, but it might (probably!) still has errors to fix: Runtime errors, e.g., infinite method call chains. Logic errors, e.g., incorrect output. Lesson: compilation isn t the end! Always test and check your programs before you re done! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 19

Using Static Methods to Capture // Prints instructions to make a PBJ sandwich public class PBJ { Structure // Prints the PBJ preamble to the screen public static void printpreamble() { /* */ // Prints the peanut butter step to the screen public static void printpeanutbutterstep() { /* */ // Prints the jelly step to the screen public static void printjellystep() { /* */ // Prints the eating step to the screen public static void printeatstep() { /* */ public static void main(string[] args) { printpreamble(); printpeanutbutterstep(); printjellystep(); printeatstep(); 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 20

Using Static Methods to Reduce // Prints instructions to make a PBJ sandwich public class PBJ { Redundancy // Prints the clean up step to the screen public static void printcleanupstep() { /* */ // Prints the peanut butter step to the screen public static void printpeanutbutterstep() { /* */ printcleanupstep(); // Prints the jelly step to the screen public static void printjellystep() { /* */ printcleanupstep(); public static void main(string[] args) { /* */ 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 21

Procedural Decomposition 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 22

Decompose, Decompose, Decompose Our focus thus far: procedural decomposition Procedures are (non-object oriented) methods Two development strategies arise: Top-down development Start with empty main, write skeletons for methods you believe you need, fill them in. Iterative refinement Write a (relatively) complete program in main, factor out existing functionality into methods. Both approaches focus on Keeping your program in a compliable state. Constantly checking and testing your program. Not mutually exclusive, neither one better than the other. 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 23

Example #1: MarathonTraining Week 1 Monday: Rest Tuesday: 4 miles Wednesday: Rest Thursday: 1-hour run Friday: Rest Saturday: 4 miles Sunday: 6 miles Week 2 Monday: Rest Tuesday: 4 miles Wednesday: Rest Thursday: 1-hour run Friday: Rest Saturday: 4 miles Sunday: 7 miles Week 3 Monday: Rest Tuesday: 4 miles Wednesday: Rest Thursday: 6 miles Friday: Rest Saturday: Rest Sunday: 8 miles 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 24

Example #2: SimpleFigure /\ / \ / \ / \ ~~~~~~~~ -------- -------- -------- -------- /\ / \ / \ / \ ~~~~~~~~ 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 25