Introduction to Programming (Java) 4/12

Similar documents
CS-201 Introduction to Programming with Java

Introduction to Programming Using Java (98-388)

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture #6-7 Methods

Lecture 5: Methods CS2301

Lecture 9 - C Functions

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

More loops Ch

( &% class MyClass { }

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

Introduction to Programming (Java) 2/12

About this exam review

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

Unit 7. Functions. Need of User Defined Functions

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

Lexical Considerations

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

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS110D: PROGRAMMING LANGUAGE I

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

CS-201 Introduction to Programming with Java

CS1150 Principles of Computer Science Methods

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Lexical Considerations

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

TOPICS TO COVER:-- Array declaration and use.

Chapter 4. Defining Classes I

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

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Methods and Data (Savitch, Chapter 5)

Methods. CSE 114, Computer Science 1 Stony Brook University

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

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1 Lexical Considerations

Array. Prepared By - Rifat Shahriyar

JAVA Programming Concepts

Flow Control. CSC215 Lecture

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Course PJL. Arithmetic Operations

CS1150 Principles of Computer Science Methods

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

PROGRAMMING FUNDAMENTALS

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

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Practice problems Set 2

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

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

CS 251 Intermediate Programming Methods and Classes

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

CS 251 Intermediate Programming Methods and More

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Separate Compilation Model

Java Identifiers, Data Types & Variables

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

PROGRAMMAZIONE I A.A. 2017/2018

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

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

Introduction to Java

Object Oriented Programming with Java

CS113: Lecture 4. Topics: Functions. Function Activation Records

Functions. Systems Programming Concepts

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Midterm Exam CS 251, Intermediate Programming March 12, 2014

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

Chapter 7 User-Defined Methods. Chapter Objectives

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

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Section 2.2 Your First Program in Java: Printing a Line of Text

Lecture 04 FUNCTIONS AND ARRAYS

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

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

Java Basic Programming Constructs

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

STRUCTURING OF PROGRAM

CS260 Intro to Java & Android 03.Java Language Basics

Design Patterns: State, Bridge, Visitor

Center for Computation & Louisiana State University -

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Functions BCA-105. Few Facts About Functions:

Lecture 1: Overview of Java

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 4 Defining Classes I

CS115 Principles of Computer Science

Memory and C++ Pointers

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

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

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

a data type is Types

Transcription:

Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction to Programming (Java) 1/28

Introduction to Programming (Java) 4/12 Method is a named block with the definition of the input and output. The Meaning: the reusability of a code. The definition includes header and body. c 2006 2008 Michal Krátký Introduction to Programming (Java) 2/28

, Header Header: type name (arguments) Where: type is a return type of the method name is the method name arguments is a comma (,) separated list of arguments between parenthesis, it may be empty Each argument has the form: type name Example: int Search(String str, char ch) c 2006 2008 Michal Krátký Introduction to Programming (Java) 3/28

, Body The body is a block, i.e. a sequence of statements between braces and. Example: public s t a t i c void main ( S t r i n g [ ] args )... c 2006 2008 Michal Krátký Introduction to Programming (Java) 4/28

Introduction to Programming (Java) 4/12 The return type is void if the method does not return any value. If the return type is not void then the method must return a value by return expression; If the return type is void we can use return; for the return from this method. Notice: In this course (we are not interested in object-oriented features of Java), we will declare all methods as public static, private static or static. c 2006 2008 Michal Krátký Introduction to Programming (Java) 5/28

, Example 4.1 Task: Write the int Search(String str, char ch) method returning the number of occurrences of the ch character in the str string. c 2006 2008 Michal Krátký Introduction to Programming (Java) 6/28

, Example 4.1 s t a t i c i n t Search ( S t r i n g s t r, char ch ) i n t number = 0 ; / / the t e s t of a l l characters for ( i n t i = 0 ; i < s t r. l e n g t h ( ) ; i ++) i f ( s t r. charat ( i ) = = ch ) number ++; return number ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 7/28

Method Invocation A method is called by its name with a comma (,) separated list of parameters in parenthesis (). i n t x, y ; x = gcd ( 2 4, 1 8 ) ; A parameter may be an arbitrary expression. All parameters are evaluated and assigned to arguments of the method. x = 0 ; y = gcd ( x + 1, 3 6 ) ; The method invocation may be used in an expression. The value of the invocation is the return value of the method. System. out. p r i n t l n ( gcd ( x + 2, 1 2 ) ) ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 8/28

Method Invocation i n t x = 0, y ; y = gcd ( x + 1, 3 6 ) ; 1 Evaluation of parameters. 2 Evaluated values are stored on a stack. Stack: 1 36 3 Method invocation. 4 Execution of the method, setting the return value. 5 The return from the method, parameters are deleted from the stack. c 2006 2008 Michal Krátký Introduction to Programming (Java) 9/28

Method Invocation, Example 4.1 Task: Call the Search method returning the number of occurrences. public class Example0401 public s t a t i c void main ( S t r i n g [ ] args ) S t r i n g s t r = " This i s the s t r i n g " ; char ch = t ; i n t number = Search ( s t r, ch ) ; System. out. p r i n t l n ( " The number of occurrences : " + number ) ; / / number = 2 c 2006 2008 Michal Krátký Introduction to Programming (Java) 10/28

Method Invocation, Example 4.1 public s t a t i c i n t Search ( S t r i n g s t r, char ch ) i n t number = 0 ; / / t e s t a l l characters of the s t r i n g for ( i n t i = 0 ; i < s t r. l e n g t h ( ) ; i ++) i f ( s t r. charat ( i ) = = ch ) number ++; return number ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 11/28

Example 4.2 Task: Write the int GetOrderFirstOne(int number) method returning the index of the lowest true bit in the number. If the number does not contain any true bit then it returns -1. Example: 16 10 = 10000 2, return: 4 127 10 = 1111111 2, return: 0 0 10 = 0 2, return: -1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 12/28

Example 4.2, Method Invocation public class Example0402 public s t a t i c void main ( S t r i n g [ ] args ) i n t value1 = 1 6, value2 = 1 2 7, value3 = 0 ; System. out. p r i n t l n ( " Value : " + value1 + ", index : " + GetOrderFirstOne ( value1 ) ) ; System. out. p r i n t l n ( " Value : " + value2 + ", index : " + GetOrderFirstOne ( value2 ) ) ; System. out. p r i n t l n ( " Value : " + value3 + ", index : " + GetOrderFirstOne ( value3 ) ) ; / / Output : 4, 0, 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 13/28

Example 4.2 s t a t i c i n t GetOrderFirstOne ( i n t value ) i n t tmp = 1 ; i n t order = 1; for ( i n t i = 0 ; i < I n t e g e r. SIZE ; i ++) / / i < 32 / / t e s t a l l b i t s i f ( ( tmp & value )! = 0 ) order = i ; break ; tmp < < = 1 ; / / the sequence : 1, 2, 4, 8,... return order ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 14/28

Method Overloading can be overloaded. => A class contains more methods with the same name. must differ in the number or types of parameters. Method is selected according to the number or types of parameters. The return type need not be the same. c 2006 2008 Michal Krátký Introduction to Programming (Java) 15/28

Method Overloading public s t a t i c i n t Search ( S t r i n g s t r, char ch )... public s t a t i c i n t Search ( i n t [ ] a, i n t a )... c 2006 2008 Michal Krátký Introduction to Programming (Java) 16/28

Example 4.3 Task: Write the int Max(int []array) method returning the maximal value of the array. Example: Input: -1, 45, 78, 456, 741, -875 Output: 741 c 2006 2008 Michal Krátký Introduction to Programming (Java) 17/28

Example 4.3 public class Example0403 public s t a t i c void main ( S t r i n g [ ] args ) i n t [ ] array = 1, 4 5, 7 8, 4 5 6, 7 4 1, 8 7 5 ; System. out. p r i n t l n ( " The maximal value : " + Max( array ) ) ; / / output : 7 4 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 18/28

Example 4.3 s t a t i c i n t Max( i n t [ ] array ) i n t max = I n t e g e r. MIN_VALUE ; / / 2147483648 / / t e s t a l l array items for ( i n t i = 0 ; i < array. l e n g t h ; i ++) i f ( max < array [ i ] ) max = array [ i ] ; return max ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 19/28

Reference 1/5 A pointer (or reference) does not contain data, however it contains an address in the main memory, where data is stored. Example: / / C++ i n t a = 6 0 ; i n t pa = & a ; 0x1232 0x1235 0x1285 a 60 pa 0x1232 60 c 2006 2008 Michal Krátký Introduction to Programming (Java) 20/28

Reference, Java 2/5 Primitive vs object data types and arrays. Example: public void Compute ( ) i n t a = 6 0 ; Set ( a ) ; / / a = 60 0x1232 0x2437 60 a 60 80 public void Set ( i n t b ) b = 8 0 ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 21/28

Reference, C++ 3/5 Example: public void Compute ( ) i n t a = 6 0 ; Set (&a ) ; / / a = 80 public void Set ( i n t pa ) pa = 8 0 ; pa 0x1232 Stack: a 60 0x1232 c 2006 2008 Michal Krátký Introduction to Programming (Java) 22/28

Reference, Java 4/5 Primitive data types, like int, float, and so on, are handled by the value, arrays and instances of object data types are handled by the reference (pointer). Example: public void Compute ( ) i n t array [ ] = new i n t [ 1 ] ; array [ 0 ] = 6 0 ; Set ( array ) ; / / array [ 0 ] = 8 0 public void Set ( i n t [ ] array ) array [ 0 ] = 8 0 ; array 0x1232 Stack: 60 0x1232 c 2006 2008 Michal Krátký Introduction to Programming (Java) 23/28

Reference, Java 5/5 array 0x1232 60 public void Compute ( ) i n t array [ ] = new i n t [ 1 ] ; array [ 0 ] = 6 0 ; Set ( array ) ; / / array [ 0 ] = 6 0 Stack: 0x1282 0x1232 80 0x1282 public void Set ( i n t [ ] array ) array = new i n t [ 1 ] ; array [ 0 ] = 8 0 ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 24/28

1/4 represents a function, which calls the same function in the body. We say that the function calls itself. We show this principle at the example of factorial. The factorial function is defined: 1 pro n = 0 f (n) = f (n f (n 1)) pro n 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 25/28

2/4, Factorial f (n) = f (n f (n 1)) = f (n f ((n 1) f ((n 2)))) = = = f (n f ((n 1) f ((n 2) f (1 f (0))... )) We can follow: n! = n (n 1)! = n (n 1)(n 2)! = = n (n 1) (n 2) 1 0! c 2006 2008 Michal Krátký Introduction to Programming (Java) 26/28

3/4, Factorial s t a t i c i n t f a c t o r i a l ( i n t n ) i f ( n = = 0 ) return 1 ; else return ( n f a c t o r i a l ( n 1 ) ) ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 27/28

, Nesting of the Method Invocation 4! Invocation The First Invocation, n=4 The Second Invocation, n=3 The Third Invocation, n=2 The Fourth Invocation, n=1 The Fifth Invocation, n=0 Return 1 Return n * f(n-1) = 1 * 1 Return 2 * 1 Return 3 * 2 Return 4 * 6 Finish with the value 24 c 2006 2008 Michal Krátký Introduction to Programming (Java) 28/28