COMP 110/L Lecture 5. Kyle Dewey

Similar documents
COMP 110/L Lecture 4. Kyle Dewey

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Unit 1. Using BlueJ to Write Programs

COMP 110/L Lecture 6. Kyle Dewey

COMP 110/L Lecture 7. Kyle Dewey

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

AP Computer Science Unit 1. Programs

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

COMP-202 More Complex OOP

1001ICT Introduction To Programming Lecture Notes

4. Java Project Design, Input Methods

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

CS 177 Recitation. Week 1 Intro to Java

Unit 4: Classes and Objects Notes

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

COMP 122/L Lecture 1. Kyle Dewey

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

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

COMP 110/L Lecture 13. Kyle Dewey

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 );

Object Oriented Programming. Java-Lecture 1

Visual Programming. Lecture 2: More types, Methods, Conditionals

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Overview. Lab 5 Methods and Parameters

Data Structure and Programming Languages

ing execution. That way, new results can be computed each time the Class The Scanner

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

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

Static Methods. Why use methods?

Lecture 1 - Introduction (Class Notes)

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

More types, Methods, Conditionals. ARCS Lab.

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

Mobile App:IT. Methods & Classes

Programming with Java

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Midterms Save the Dates!

A token is a sequence of characters not including any whitespace.

Controls Structure for Repetition

Data Structure and Programming Languages

Research Group. 2: More types, Methods, Conditionals

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS 177 Week 5 Recitation Slides. Loops

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

Warm up question. 2)Which of the following operators are defined on a half integer? Which of the following would it be useful to write as behaviors?

ALICE: An introduction to progamming

CS 106 Introduction to Computer Science I

COMP-202 Unit 4: Programming with Iterations

Important Java terminology

Static methods. Not actually a valid Java static method. Fundamentals of Computer Science Keith Vertanen

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

CSE 143 Lecture 10. Recursion

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

CIS 110: Introduction to Computer Programming

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Lecture 1: Introduction to Java

Getting started with Java

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

First Java Program - Output to the Screen

1. Find the output of following java program. class MainClass { public static void main (String arg[])

AP CS Unit 3: Control Structures Notes

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CIS 110: Introduction to Computer Programming

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

COMP 110/L Lecture 10. Kyle Dewey

Consistency. CS 475, Spring 2018 Concurrent & Distributed Systems

COMP 215: INTRO TO PROGRAM DESIGN. Prof. Chris Jermaine Chris Prof. Chris Dr. Chris

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

AP Programming - Chapter 17 Lecture page 1 of 5

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

COMP 110 Programming Exercise: Simulation of the Game of Craps

Programming with Java

Lecture Set 4: More About Methods and More About Operators

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

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

Question: Total Points: Score:

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Midterms Save the Dates!

Introduction to the Java Basics: Control Flow Statements

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Lecture 5: Methods CS2301

HOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN

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

1.00 Lecture 2. What s an IDE?

Java using LEGO Mindstorms and LeJOS. University of Idaho

Warmup : Name that tune!

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Recursion. Recursion [Bono] 1

COMP15111: Introduction to Architecture

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Transcription:

COMP 110/L Lecture 5 Kyle Dewey

Outlines Methods Defining methods Calling methods

Methods

Motivation

Motivation Input Program Output -Start off with some high-level motivation -You write your program, and it s one giant block -This is difficult to reason about

Motivation -Simpler approach: write a bunch of smaller programs, and stitch them together -Each program is a lot easier to reason about than the one big program -If we re careful about how these different pieces interact with each other, then we only ever have to think about the small programs Input Subprogram 1 Subprogram 2 Subprogram 3 Subprogram 4 Output

Code Reuse

Code Reuse System.out.println(...) -You re already familiar with these

Code Reuse System.out.println(...) nextint() -You re already familiar with these

Code Reuse System.out.println(...) nextint() nextlong() -You re already familiar with these

Code Reuse System.out.println(...) nextint() nextlong() nextdouble() -You re already familiar with these

Code Reuse System.out.println(...) nextint() nextlong() nextdouble() You have used all of these multiple times. -You re already familiar with these, and you ve used them a bunch of times

Code Reuse System.out.println(...) nextint() nextlong() nextdouble() You have used all of these multiple times. These are all methods. -You re already familiar with these, and you ve used them a bunch of times

Methods Distinct subprograms.

Methods Distinct subprograms. Input Subprogram 1 Subprogram 2 Subprogram 3 Subprogram 4 Output -Taking that illustration from before...

Methods Distinct subprograms. Input Method 1 Method 2 Method 3 Method 4 Output -...each one of those subprograms is a method

Method Terminology We can define a method Make it available to the rest of the program We can call a method Execute the subprogram

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output.

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output. Input? Method Output?

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output. Input? Method Output? System.out.println( Hello );

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output. Input? Method Output? System.out.println( Hello ); One input, no outputs (cannot assign to a variable).

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output. Input? Method Output? System.out.println( Hello ); One input, no outputs (cannot assign to a variable). Math.pow(2, 3);

Method Anatomy Methods take some number of inputs (can be 0). Methods may produce an output. Input? Method Output? System.out.println( Hello ); One input, no outputs (cannot assign to a variable). Math.pow(2, 3); Two inputs, one output.

inputscanner.nextint();

inputscanner.nextint(); No inputs, one output.

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye );

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye ); One input, no outputs (cannot assign to a variable)

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye ); One input, no outputs (cannot assign to a variable) inputscanner.nextlong();

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye ); One input, no outputs (cannot assign to a variable) inputscanner.nextlong(); No inputs, one output.

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye ); One input, no outputs (cannot assign to a variable) inputscanner.nextlong(); No inputs, one output. inputscanner.nextdouble();

inputscanner.nextint(); No inputs, one output. System.out.print( Goodbye ); One input, no outputs (cannot assign to a variable) inputscanner.nextlong(); No inputs, one output. inputscanner.nextdouble(); No inputs, one output.

Defining a Method Easiest to see with real code. Example: Return42.java -The `return` reserved word says that the method should end and return with a given value at this point

Example: ReturnParameter.java

Example: MultParameters1.java

Example: MultParameters2.java

Example: MultParameters3.java

Method Definition General Form public static returntype methodname(parameter_list) {... return expression; }

Method Definition General Form public static Magic returntype methodname(parameter_list) {... return expression; }

Method Definition General Form public static returntype Magic Type of value produced methodname(parameter_list) {... return expression; }

Method Definition General Form public static returntype Magic Type of value produced methodname(parameter_list) {... return expression; } Name given to method; same naming rules as variables

Method Definition General Form public static returntype Magic Type of value produced methodname(parameter_list) {... return expression; Inputs to } method (int x) Name given to method; same naming rules as variables

Method Definition General Form public static returntype Magic Type of value produced methodname(parameter_list) {... return expression; Inputs to } method Method ends (int x) Name given to method; same naming rules as variables here, evaluates expression, and produces its result

Methods which Produce no Values Methods which produce no values have a void return type Example: ReturnNothing.java

main Method main is just another method. main serves as the entry point to your program.

-main s return type is void - it produces no value (doesn t return anything) -String[] is actually a type, so args is a parameter -Later on we ll get into what the type `String[]` is (not the same as just String), along with what this parameter to main holds main Method main is just another method. main serves as the entry point to your program. public static void main(string[] args) {... }

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Method 1 Method 2

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Execution Method 1 Method 2 -Initially, execution is in method 1

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Execution Calls Method 1 Method 2 -Method 1 then calls method 2

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Calls Execution Method 1 Method 2 -Execution transfers to method 2 as a result of the call

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Execution Calls Method 1 Method 2 Returns -Method 2 eventually completes, returning back to method 1

Calling Methods Execution enters the method calls The method is executed The method returns to wherever it was called from Execution Calls Method 1 Method 2 Returns -Once the return is complete, execution resumes back in method 1 wherever it left off