We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Similar documents
CS 177 Recitation. Week 8 Methods

AP Computer Science Unit 1. Programs

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Course Outline. Introduction to java

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Introduction to Computer Science Unit 2. Notes

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Methods. Bok, Jong Soon

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Java Bytecode (binary file)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

AP CS Unit 3: Control Structures Notes

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

CSSE 220 Day 12. Coupling and Cohesion Scoping. Please download VideoStore from your SVN

Methods CSC 121 Fall 2016 Howard Rosenthal

Tecniche di Progettazione: Design Patterns

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Following is the general form of a typical decision making structure found in most of the programming languages:

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

APCS Semester #1 Final Exam Practice Problems

Fundamentals of Programming Session 4

A Foundation for Programming

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

Personal SE. Functions & Arrays

Lecture 12 Modular Programming with Functions

Control Structures in Java if-else and switch

6.096 Introduction to C++

CIS133J. Working with Numbers in Java

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

Pointers and Terminal Control

CS111: PROGRAMMING LANGUAGE II

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

4. Java Project Design, Input Methods

Chapter 2, Part I Introduction to C Programming

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

CISC 1600 Lecture 3.1 Introduction to Processing

LAMBDA EXPRESSIONS. Summer 2018

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CS558 Programming Languages

DRAWING ENVIRONMENT DIAGRAMS

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

M e t h o d s a n d P a r a m e t e r s

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CS 177 Recitation. Week 1 Intro to Java

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

class objects instances Fields Constructors Methods static

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Loops. CSE 114, Computer Science 1 Stony Brook University

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

CS 101 Spring 2007 Midterm 2 Name: ID:

Intro. Scheme Basics. scm> 5 5. scm>

COMP 202 Java in one week

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

Java Programming. Computer Science 112

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

CS 211: Methods, Memory, Equality

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CS 139 Practice Midterm Questions #2

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

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

Methods CSC 121 Spring 2017 Howard Rosenthal

Declaring and ini,alizing 2D arrays

CS 106 Introduction to Computer Science I

AP Computer Science Unit 1. Writing Programs Using BlueJ

C Functions. 5.2 Program Modules in C

C Programming for Engineers Functions

CS 177 Week 15 Recitation Slides. Review

Methods (Deitel chapter 6)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Methods (Deitel chapter 6)

CS558 Programming Languages

AP Computer Science Unit 1. Writing Programs Using BlueJ

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

CSCI-1200 Data Structures Spring 2017 Lecture 15 Problem Solving Techniques, Continued

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS111: PROGRAMMING LANGUAGE II

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

Object Oriented Methods : Deeper Look Lecture Three

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Programming Exercise 7: Static Methods

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

You must bring your ID to the exam.

Controls Structure for Repetition

Transcription:

Week 9: Methods 1

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to read Sometimes you need to do similar things several times: Read some input and check that it falls within certain values Draw the same image at several different locations Find the square root of several different numbers 2

Methods allow you to package some code to run over and over again Methods usually take some input (like numbers or Strings) so that they can be parameterized Methods often give back an answer (like the square root of a number) Methods are called functions in some other languages 3

More modular programming Break a program into separate tasks Each task could be assigned to a different programmer Code reusability Use code over and over Could be in a code library (like Math.sqrt()) Less code duplication Improved readability Each method can do a few, clear tasks 4

Required syntax Type of answer Type of 1 st argument Type of last argument public static type name(type arg1,,type argn) { statement1; statement2; statementm; Code done by method Name of method Name of 1 st argument Name of last argument 5

Given two integers, find the smaller: public static int min(int a, int b) { if( a < b ) return a; else return b; 6

static methods are methods that are not connected to a particular type of object They are just supposed to perform some simple task We are going to focus on static methods until next week For now, the definition of every method will include the static keyword 7

It is possible to divide methods into two types: Value returning methods Void methods Value returning methods return an answer: int small = min(x, y); 8

Void methods are declared with void as their return type public static void help(int times) { for( int i = 1; i <= times; i++ ) System.out.println( Help! ); return; Void methods just do something If you try to save the value they give back, there will be an error 9

Like most code in Java, the code inside a method executes line by line Of course, you are allowed to put conditional statements and loops inside methods You can also put in return statements A method will stop executing and jump back to wherever it was called from when it hits a return The return statement is also where you can put a value that will be given back to the caller 10

Defining a method is only half the fun! You have to call methods to use them Calling a method means giving a method the parameters (or arguments) it needs and then waiting for its answer By now, you have done many method calls System.out.println() Math.sqrt() You can call your own methods the same way 11

Proper syntax for calling a static method is first the name of the class that the method is in, a dot, the name of the method, then the arguments Class.name(arg1, arg2, arg3); If the method is in the same class as the code calling it, you can leave off the Class. part If it is a value returning method, you can store that value into a variable of the proper type 12

Variables from outside of the method don t exist unless they have been passed in as parameters public static int add(int x, int y) { int z = x + y; return z; No matter how complex a program is, inside this method only x, y, and z variables exist 13

A magical process called binding happens which copies the values from the calling code into the parameters of the method The calling code can use variables with the same names, with different names, or even just literals (like 3 or 2.6758) The method does not change the values of the variables in the original code Remember it only has copies of the values 14

int a = 10; int x = 3; int y = add( 5, a ); //y contains 15 now public static int add(int x, int y){ int z = x + y; //5 + 10 return z; No connection between the two different x s and y s 15

1. Start a method with the keywords public static 2. Next comes the return type 3. Then the name of the method 4. Then, in parentheses, the arguments you want to give to the method 5. Inside braces, put the body of the method 6. Include a return statement that gives back a value if it is that type of method 16

Finding the Euclidean distance between two points is very important There are applications in: Route planning Graphic design software Video games Let s make a method Formula: 2 2 d = ( x x y y ) + ( ) 1 2 1 2 17

public static double euclid (double x1, double x2, double y1, double y2) { double xsq = (x1-x2)*(x1-x2); No matter how complex a program is, inside double ysq = (y1-y2)*(y1-y2); this method, only x, y, and z variables exist double d = Math.sqrt(xsq+ysq); return d; 18

A cell is represented by a block in a grid Each cell has 8 neighbors Simple rules for a cell coming to life or dying : 1. A live cell with fewer than 2 live neighbors dies from loneliness 2. A live cell with more the 3 live neighbors dies from overcrowding 3. A live cell with exactly 2 or 3 neighbors keeps living 4. A dead cell with exactly 3 neighbors comes to life 19

public static void conway (int[][] cell, int m, int n) { int live = 0; for (int i=m-1; i<=m+1; i++) { No matter how complex a program is, inside for (int j=n-1; j<=n+1; j++) this method, only x, y, and z variables exist { if (cell(i,j)==1) live++; 20

if (cell(m,n)==1) { live--; if (live < 2 live > 3) cell(m,n) = 0; else { No matter how complex a program is, inside this method, only x, y, and z variables exist if (live == 3) cell (m,n) = 1; return; 21