CS1150 Principles of Computer Science Methods

Similar documents
CS1150 Principles of Computer Science Methods

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

Lecture 5: Methods CS2301

CS-201 Introduction to Programming with Java

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

CS1150 Principles of Computer Science Methods

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

Methods. CSE 114, Computer Science 1 Stony Brook University

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 6 Methods. Dr. Hikmat Jaber

CS111: PROGRAMMING LANGUAGE II

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Chapter 5 Methods / Functions

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

CS115 Principles of Computer Science

CS110: PROGRAMMING LANGUAGE I

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

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CS1150 Principles of Computer Science Loops (Part II)

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

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Lecture #6-7 Methods

JAVA Programming Concepts

Personal SE. Functions & Arrays

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

CS110D: PROGRAMMING LANGUAGE I

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

CS171:Introduction to Computer Science II

CS1150 Principles of Computer Science Arrays

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Functions BCA-105. Few Facts About Functions:

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

C Programming for Engineers Functions

CSC 2400: Computer Systems. Using the Stack for Function Calls

Top-down programming design

Benefits of Methods. Chapter 5 Methods

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

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

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

Lecture 1 Java SE Programming

Object Oriented Methods : Deeper Look Lecture Three

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Lecture 3: C Programm

Introduction to Programming (Java) 4/12

CS-201 Introduction to Programming with Java

1 Lexical Considerations

t=t + "g"; On the other hand, arrays are mutable. The assignment a[i]=e/// where e element is equal 6

Programming II (CS300)

Computer Science II (20082) Week 1: Review and Inheritance

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

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

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

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

Lab Instructor : Jean Lai

Arrays. 1 Index mapping

Lecture 9 - C Functions

CSE 307: Principles of Programming Languages

EECS168 Exam 3 Review

CP122 CS I. Iteration

Handout 7. Defining Classes part 1. Instance variables and instance methods.

CSI33 Data Structures

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

The for Loop. Lesson 11

Function Call Stack and Activation Records

In Java we have the keyword null, which is the value of an uninitialized reference type

CSC 8400: Computer Systems. Using the Stack for Function Calls

CS1150 Principles of Computer Science Arrays

Programming II (CS300)

CS111: PROGRAMMING LANGUAGE II

Cpt S 122 Data Structures. Templates

( &% class MyClass { }

CS 106 Introduction to Computer Science I

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 4 Defining Classes I

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

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

Java Memory Management

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Contents. 8-1 Copyright (c) N. Afshartous

CS1150 Principles of Computer Science Methods

CSE 230 Intermediate Programming in C and C++ Functions

This section provides some reminders and some terminology with which you might not be familiar.

CS1150 Principles of Computer Science Objects and Classes

4. C++ functions. 1. Library Function 2. User-defined Function

User Defined Functions

Computer Science II (20073) Week 1: Review and Inheritance

Technical Questions. Q 1) What are the key features in C programming language?

Implementing Abstractions

Transcription:

CS1150 Principles of Computer Science Methods Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs

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

Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); 3

Problem int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); 4

Solution public static int sum(int i1, int i2) { int sum = 0; for (int i = i1; i <= i2; i++) sum += i; return sum; System.out.println("Sum from 1 to 10 is " + sum(1, 10)); System.out.println("Sum from 20 to 30 is " + sum(20, 30)); System.out.println("Sum from 35 to 45 is " + sum(35, 45)); 5

Defining Methods A is a collection of statements that are grouped together to perform an operation. public static int max(int num1, int num2) { Define a Invoke a int z = max(x, y); actual parameters (arguments) 6

Defining Methods A is a collection of statements that are grouped together to perform an operation. header body modifier Define a return value type name return value formal parameters parameter list signature Invoke a int z = max(x, y); actual parameters (arguments) 7

Method Signature Method signature: the name and the parameter list Method name: a programmer defined name Parameter list: information that is coming into the header body modifier Define a return value type name return value formal parameters parameter list signature Invoke a int z = max(x, y); actual parameters (arguments) 8

Formal Parameters The variables defined in the header are known as formal parameters. header body modifier Define a return value type name return value formal parameters parameter list signature Invoke a int z = max(x, y); actual parameters (arguments) 9

Actual Parameters When a is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. header body modifier Define a return value type name return value formal parameters parameter list signature Invoke a int z = max(x, y); actual parameters (arguments) 10

Modifiers Both public and static are s modifier (for now, all s are public static) public: access to the is public (visible to all other classes) static: the belong to the class, not an individual instance header body modifier Define a return value type name return value formal parameters parameter list signature Invoke a int z = max(x, y); actual parameters (arguments) 11

Return Value Type A may return a value. The returnvaluetype is the data type of the value the returns. If the does not return a value, the returnvaluetype is the keyword void. For example, the returnvaluetype in the main is void. Define a Invoke a header body modifier return value type name return value formal parameters parameter list signature int z = max(x, y); actual parameters (arguments) 12

Rules for Methods A may or may not return a value A must declare a return type! o o If a returns a value Return type is the data type of the value being returned The return statement is used to return the value If a does not return a value Return type in this case is void No return statement is needed (look at max no return example) The values you pass in much match the order and type of the parameters declared in the UC. Colorado Springs

Calling Methods pass the value of i pass the value of j int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); The main is where the code starts 14

Trace Method Invocation i is now 5 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 15

Trace Method Invocation j is now 2 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 16

Trace Method Invocation invoke max(i, j) int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 17

Trace Method Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 18

Trace Method Invocation declare variable result int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 19

Trace Method Invocation (num1 > num2) is true since num1 is 5 and num2 is 2 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 20

Trace Method Invocation result is now 5 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 21

Trace Method Invocation return result, which is 5 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 22

Trace Method Invocation return max(i, j) and assign the return value to k int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); 23

Trace Method Invocation Execute the print statement int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Example: O01FindingMax.java and O02FindingMax.java 24

CAUTION A return statement is required for a value-returning. The shown below in (a) is logically correct, but it has an error because Java thinks it possible that this does not return any value. public static int sign(int n) { if (n > 0) return 1; if (n == 0) return 0; if (n < 0) return 1; Should be public static int sign(int n) { if (n > 0) return 1; if (n == 0) return 0; return 1; (a) To fix this problem, delete if (n < 0) in (a), so that Java will see a return statement to be reached regardless of how the if statement is evaluated. Example: FindingSign.java 25 (b)

Reuse Methods from Other Classes NOTE: One of the benefits of s is for reuse. The max can be invoked from any class besides FindingMax. If you create a new class Test, you can invoke the max using ClassName.Name (e.g., FindingMax.max) in Test. 26

Value-Returning Methods Return a value to the caller o Call to a value returning can be treated as a value or a statement returning a value SumIntegers example o o The main is where the code starts The sum is called: The value entered by the user is the actual parameter This means the value stored in number1 and number2 is sent to the o Control enters the sum UC. Colorado Springs

Value-Returning Methods SumIntegers example o o o o Substitute actual parameter (i.e. number1 and number2) into the formal parameter (i.e. i1 and i2) The now has a value for i1" and i2 that can be used within the Process for loop The return statement forces control to return to the caller (main) & sends to caller an integer value UC. Colorado Springs

Void Methods Does not return a value to the caller o o o The return type must be set to void No return statement is used Control returns to caller when the s closing curly brace is reached Call to a void must be made as a statement o See SumIntegersVoid example UC. Colorado Springs

Call Stacks 30

Call Stacks The call stack is used to keep track of active s Stack is a data structure o Like.plate stacks at the buffet line: pop a plate off the stack, push a plate onto the stack Stacks are last in, first out (last item pushed onto stack is 1st item popped off) UC. Colorado Springs

Call Stacks When is called (invoked) o Information for that is pushed onto the stack Values of parameters and variables Current line of code executing This information is called a "frame" When the returns (completes) o Information for that is popped off of the stack (frame is removed) Method on top of stack is the current executing Method stays on top until the ending curly brace is reached UC. Colorado Springs

Trace Call Stack i is declared and initialized int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); The main is invoked. i: 5 33

Trace Call Stack j is declared and initialized int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); The main is invoked. j: 2 i: 5 34

Trace Call Stack Declare k int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Space required for the main k: j: 2 i: 5 The main is invoked. 35

Trace Call Stack Invoke max(i, j) int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Space required for the main k: j: 2 i: 5 The main is invoked. 36

Trace Call Stack pass the values of i and j to num1 and num2 int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); num2: 2 num1: 5 Space required for the main k: j: 2 i: 5 The max is invoked. 37

Trace Call Stack Declare result int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); result: num2: 2 num1: 5 Space required for the main k: j: 2 i: 5 The max is invoked. 38

Trace Call Stack (num1 > num2) is true int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); result: num2: 2 num1: 5 Space required for the main k: j: 2 i: 5 The max is invoked. 39

Trace Call Stack Assign num1 to result int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Space required for the max result: 5 num2: 2 num1: 5 Space required for the main k: j: 2 i: 5 The max is invoked. 40

Trace Call Stack Return result and assign it to k int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Space required for the max result: 5 num2: 2 num1: 5 Space required for the main k:5 j: 2 i: 5 The max is invoked. 41

Trace Call Stack Execute print statement int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); Space required for the main k:5 j: 2 i: 5 The main is invoked. 42