CS 101 Computer Programming

Similar documents
SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Pointers, Arrays and Parameters

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CSE 143 Lecture 10. Recursion

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

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

Lab # 2. For today s lab:

Mobile App:IT. Methods & Classes

CS16 Exam #1 7/17/ Minutes 100 Points total

CS 106A, Lecture 7 Parameters and Return

CS 126 Lecture P1: Introduction to C

Chapter 4 Defining Classes I

Arrays in C++ Instructor: Andy Abreu

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

CS 100: Computability, Python Lists

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

COMP-202 More Complex OOP

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

CS 251 Intermediate Programming Java Basics

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

CS187 - Science Gateway Seminar for CS and Math

Lesson 2.4 Arraylist

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Slides are adapted from the originals available at

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

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

CS111: PROGRAMMING LANGUAGE II

9/19/2018 Programming Data Structures. Polymorphism And Abstract

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Friday Four Square! Today at 4:15PM, Outside Gates

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

BINARY SEARCH TREES CS 200 RECITATION 7

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Notes from the Boards Set BN19 Page

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

Pointers Ch 9, 11.3 & 13.1

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

Appendix: Common Errors

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

(Refer Slide Time 01:41 min)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CSE143 Notes for Monday, 4/25/11

Week 3 Classes and Objects

Using Word to Create a Resume

Java Puzzle Ball Nick Ristuccia

Shorthand for values: variables

Microsoft PowerPoint Presentations

The compiler is spewing error messages.

Lecture (01) Getting started. Dr. Ahmed ElShafee

1 Getting started with Processing

CS159. Nathan Sprague

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Chapter 8: Virtual Machine II: Program Control

EL2310 Scientific Programming

CS 231 Data Structures and Algorithms, Fall 2016

CS 101 Computer Programming

Welcome! COMP s1. Programming Fundamentals

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 03. REQUIREMENTS CHANGE. I Love You, You re Perfect... Now Change!

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Downloading & Installing the BIT115 Software & Programs JAVA JDK SE JGRASP BECKER.JAR ROBOTS TEXTBOOK PDFS

CS 106A, Lecture 25 Life After CS 106A, Part 1

Week - 01 Lecture - 04 Downloading and installing Python

Programming in the Real World

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSE 331 Software Design & Implementation

Assignment 2.4: Loops

CS Final Exam Review Suggestions - Spring 2014

CS103 Spring 2018 Mathematical Vocabulary

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Write for your audience

Topic 4 Expressions and variables

Microsoft PowerPoint and Digital Photos

Finding and Fixing Bugs

Halting Measures and Termination Arguments

CSE 230 Intermediate Programming in C and C++ Functions

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

Programming Exercise 7: Static Methods

Lecture 9: Lists. MIT-AITI Kenya 2005

Programming Lecture 4

5.6.1 The Special Variable this

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

(Refer Slide Time: 01.26)

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Linking, Loading, Libraries

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Control Statements. if for while

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Transcription:

CS 101 Computer Programming Methods (Chapter 5) Özyeğin University slides partially adapted from UW CSE 142 course slides

Remember? Control Flow: the order in which statements are executed CS 101 Özyeğin University 2

Another form of control flow Methods allow you to visit a block of code and then come back This code block can be elsewhere in your program (in another class) Methods are also called as functions, procedures, or subroutines call return CS 101 Özyeğin University 3

Why use methods? Here is an example case: We want to write a program that prints out different messages on the screen, and we would like to display the following pattern in-between messages to separate them *************** *************** CS 101 Özyeğin University 4

Solution approach Use two println CS 101 Özyeğin University 5

The structure of the code.. public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ Anything wrong with this? CS 101 Özyeğin University 6

The problem The solution is fine It does produce the desired result No issues with respect to correcness and functionality But, there is an issue from a different perspective How hard it would be to change the program in the future How much work is it to write the same statements over and over CS 101 Özyeğin University 7

What if.. Later on the client wants us to change The number of rows of stars The number of stars per row Use another character than a star Print the date and time with each seperator How much work is involved? CS 101 Özyeğin University 8

If we want to change anything Have to edit every copy of the code in the program It is easy to overlook some copies It can be hard to find them all They might not be written identically A piece of code that looks like serving the same purpose might be doing something else CS 101 Özyeğin University 9

The Big Idea behind methods Identify a sub-problem that has to be solved Write code for solving that sub-problem, only once Give that code a name: that makes it a method Whenever the sub-problem needs to be solved, use the method name to say: Go to that code now to take care of this sub-problem, and do not come back until you are done! CS 101 Özyeğin University 10

Example: Identify a sub-problem that has to be solved Take the repeated lines of code Wrap it as a method by giving it a name, e.g., printseparator CS 101 Özyeğin University 11

Example: public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ The code named printseparator CS 101 Özyeğin University 12

Example: public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ The code named printseparator CS 101 Özyeğin University 13

Example: public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ The code named printseparator CS 101 Özyeğin University 14

Example: public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ The code named printseparator CS 101 Özyeğin University 15

Example: public void run() { /* produce some output */ /* produce some other output */ /* produce even more output */ /* produce the final output */ The code named printseparator Question: If we need to change the separator in this new verison of the program, What do we have to do? How many places in the program have to be changed? CS 101 Özyeğin University 16

The Big Picture so far.. Methods Method control flow The motivation for methods Next.. How to define and use methods in Java Details of different type of methods and their usage CS 101 Özyeğin University 17

How to define methods The following is a typical method declaration /* print a separator line on output */ public void printseparator() { CS 101 Özyeğin University 18

How to define methods The following is a typical method declaration /* print a separator line on output */ public void printseparator() { heading comment method name a method can have any number of and any kind of statements CS 101 Özyeğin University 19

import acm.program.*; Using a method.. public class Report extends ConsoleProgram { public void run() { public void printseparator() { CS 101 Özyeğin University 20

New concepts Two new concepts that will be discussed later on.. Return values Parameters The simple method in our example do not have any return value or a parameter public void printseparator() { CS 101 Özyeğin University 21

Some Java methods We have already seen and used several methods: public void run() { println("hello world"); CS 101 Özyeğin University 22

Prewritten methods Prewritten methods are commonly collected in packages Remember import acm.program.*;?? Tells Java that you want to use methods in this package There are many other useful packages CS 101 Özyeğin University 23

Exercise: Methods Modify the program below Create and use a method that executes the highlighted section(s) of the code (name the method as endmessage) Observe the output import acm.program.*; public class Stars extends ConsoleProgram { public void run() { println("hello world"); println("-- end of message -- "); println("hello methods"); println("-- end of message -- "); println("see you next time"); println("-- end of message -- "); CS 101 Özyeğin University 24