CS110D: PROGRAMMING LANGUAGE I

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

CS111: PROGRAMMING LANGUAGE II

Object Oriented Methods : Deeper Look Lecture Three

CS111: PROGRAMMING LANGUAGE II

Lecture 5: Methods CS2301

CS110: PROGRAMMING LANGUAGE I

IT 374 C# and Applications/ IT695 C# Data Structures

Lecture #6-7 Methods

A Foundation for Programming

CS-201 Introduction to Programming with Java

Chapter 4 Defining Classes I

CS-201 Introduction to Programming with Java

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)

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

Function Call Stack and Activation Records

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

Introduction to Programming Using Java (98-388)

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

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

Introduction to Programming (Java) 4/12

C++ Programming Lecture 11 Functions Part I

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

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

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Lecture 04 FUNCTIONS AND ARRAYS

Methods. CSE 114, Computer Science 1 Stony Brook University

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

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

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

CIS133J. Working with Numbers in Java

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

1.1 Your First Program

Methods: A Deeper Look

ECE 122. Engineering Problem Solving with Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

The return Statement

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

CS 106 Introduction to Computer Science I

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

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

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

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

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

Introduction to Computer Science Unit 2. Notes

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Chapter 3 Function Overloading

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Variables and data types

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

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Anatomy of a Class Encapsulation Anatomy of a Method

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

1.1 Your First Program

Chapter 6 Methods. Dr. Hikmat Jaber

AP CS Unit 3: Control Structures Notes

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

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

b. Suppose you enter input from the console, when you run the program. What is the output?

Lecture 2: Intro to Java

CS 335 Java Programming Controls. Fall 2007

Chapter 5 Methods / Functions

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

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

Functions. Systems Programming Concepts

JAVA Programming Concepts

Benefits of Methods. Chapter 5 Methods

Data Structures G5029

Introduction to Computer Science Unit 2. Notes

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

AP Computer Science Java Mr. Clausen Program 6A, 6B

C Programming for Engineers Functions

Pace University. Fundamental Concepts of CS121 1

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

Project Compiler. CS031 TA Help Session November 28, 2011

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

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

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

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

CT 229 Methods Continued

Chapter 4: Writing Classes

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Transcription:

CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 7&8: Methods

Lecture Contents What is a method? Static methods Declaring and using methods Parameters Scope of declaration Overloading dr. Amal Khalifa,Fall14

Introduction Best way to develop and maintain a large program is to construct it from small, simple pieces, or modules. divide and conquer. dr. Amal Khalifa,Fall14

Modular Programming!! Methods help you modularize a program by separating its tasks into self-contained units. Written only once Hidden from other methods Can be reused : Use existing methods as building blocks to create new programs. dr. Amal Khalifa,Fall14

Methods A method: groups a sequence of statement takes input, performs actions, and produces output In Java, each method is defined within specific class

Tips!! dr. Amal Khalifa,Fall14

Java API Java programs combine new methods and classes that you write with predefined methods and classes available in the Java Application Programming Interface and in other class libraries. Class Math provides a collection of static methods enable you to perform common mathematical calculations. Method arguments may be constants, variables or expressions Defines common mathematical constants Math.PI (3.141592653589793) Math.E (2.718281828459045) dr. Amal Khalifa,Fall14

The Math methods

Example 1 Solve quadratic equation ax 2 + bx + c = 0. public class Quadratic { public static void main(string[] args) { double a,b,c,d; // input coefficient values.. } } // calculate roots d = Math.sqrt(b*b - 4.0*a*c); double root1 = (-b + d) / (2.0*a); double root2 = (-b - d) / (2.0*a); // print them out System.out.println(root1); System.out.println(root2); Dr. Amal Khalifa, 2012

Example2: Simulation- Flipping a coin 10 public class Flip { public static void main(string[] args) { for (int i=0; i<1000; i++) if (Math.random() < 0.5) System.out.println("Heads"); else System.out.println("Tails"); } } % java Flip Heads can produce only double values in the range of 0.0 and 1.0. % java Flip Heads % java Flip Tails Dr. Amal Khalifa, 2014

The Math Class dr. Amal Khalifa,Fall14

Tip!! dr. Amal Khalifa,Fall14

User defined methods declare Call test Reuse dr. Amal Khalifa,Fall14

Method Declaration: Header A method declaration begins with a method header public class MyClass { static int min ( int num1, int num2 ) properties return type method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument

Method Declaration: Body The header is followed by the method body: class MyClass { static int min(int num1, int num2) { int minvalue = (num1 < num2)? num1 : num2; return minvalue; } } Computer Science Department

The return Statement The return type of a method indicates the type of value that the method sends back to the calling location The return statement specifies the value that will be returned Its expression must conform to the return type A method can return at most 1 value A method that does not return a value has a void return type Computer Science Department

Be careful!! dr. Amal Khalifa,Fall14

Calling a Method Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments int num = min(2, 3); static int min (int num1, int num2) { } int minvalue = (num1 < num2? num1 : num2); return minvalue; Computer Science Department

Example Method call

Example Method declaration static return

Run!!

Example: void methods 22 Write a function that outputs the multiplication table of a given number.

Method calling A method can call another method, who can call another method, main min(num1, num2, num3) println() min(1, 2, 3); println( ) dr. Amal Khalifa,Fall14

Method Call Stack Stack: data structure Analogous to a pile of dishes A dish is placed on the pile at the top (referred to as pushing the dish onto the stack). A dish is removed from the pile from the top (referred to as popping the dish off the stack). Last-in, first-out (LIFO) data structures The last item pushed (inserted) on the stack is the first item popped (removed) from the stack. dr. Amal Khalifa,Fall14

LIFO behavior: 25 the caller pushes the return address onto the stack, when it finishes, the called function pops the return address off the call stack Control is transfers to that address. memory is finite --> stack overflow may occurs

Argument Promotion and Casting Argument promotion Converting an argument s value, if possible, to the type that the method expects to receive in its corresponding parameter. Conversions may lead to compilation errors if Java s promotion rules are not satisfied. Promotion rules specify which conversions are allowed. apply to expressions containing values of two or more primitive types and to primitive-type values passed as arguments to methods. Each value is promoted to the highest type in the expression.

Argument casting In cases where information may be lost due to conversion, the Java compiler requires you to use a cast operator to explicitly force the conversion to occur otherwise a compilation error occurs.

Be careful!!

Scope of Declarations Declarations introduce names that can be used to refer to such Java entities. The scope of a declaration is the portion of the program that can refer to the declared entity by its name. Such an entity is said to be in scope for that portion of the program. If a local variable or parameter in a method has the same name as a field of the class, the field is hidden until the block terminates execution this is called shadowing. dr. Amal Khalifa,Fall14

Scope Rules 30 Global: Declared "outside" function body final double TAXRATE = 0.05; final double PI = 3.14159; Local: Declared inside" a function or block * Declared inside body of given function or block of code * Available only within that function * Can we have variables with same names declared in different method?? Dr. Hamad, Dr. Khalifa

Example

Method Overloading Methods of the same name declared in the same class Must have different sets of parameters (signatures). the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature. Method calls cannot be distinguished by return type. Overloaded methods can have different return types if the methods have different parameter lists. Overloaded methods need not have the same number of parameters.

Example Same name different tasks matching by signature Literal floating-point values are treated as type double

Be careful!!

That s all [1] 6.1 6.7, 6.11, 6.12 dr. Amal Khalifa,Fall14