Introduction to Methods and Interfaces

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Introduction to Java Programming, 4E Y. Daniel Liang.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 201 Functions Debzani Deb.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
Introduction to Methods
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 Methods Chapter 6 - Methods.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
C# Programming Methods.
Programming Fundamentals Enumerations and Functions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Functions + Overloading + Scope
Reference: COS240 Syllabus
Chapter 6: User-Defined Functions I
Interfaces.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Method Parameters and Overloading
Programming Fundamentals Lecture #7 Functions
Chapter 6 Methods 1.
CSCI 161: Introduction to Programming Function
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
User Defined Functions
Chapter 5 Function Basics
Chapter 6 Methods: A Deeper Look
Interfaces CS163 Fall 2018.
Group Status Project Status.
Chapter 6 Methods.
Chapter 5 Methods.
Chapter 6: User-Defined Functions I
Week 4 Lecture-2 Chapter 6 (Methods).
Methods/Functions.
Corresponds with Chapter 5
Chapter 4: Loops and Iteration
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Introduction to Methods and Interfaces CS1: Java Programming Colorado State University Kris Brown, Wim Bohm and Ben Say 1

Methods - motivation We want to write a program that manipulates areas of certain 2D shapes rectangles, squares circles, and spheres We do not want to write the expression for these areas every time we need to compute one Similarly, we do not want to write one monster main method to do all the work! We want to divide and conquer: separate logical groups of statements together in one construct

Methods A method allows us to group a set of statements together into a logical operation There are two aspects to methods: The method definition A method is a collection of statements that are grouped together to perform an operation The method call Another method can now use the defined method to perform the operation

Method definition A method is a collection of statements that are grouped together to perform an operation. Defining a method: modifier return method formal parameters value type name public int areaRec (int length, int width) { // compute area of Rectangle int area = length * width; return area;  } method body, ending with return value;

Calling a Method A method is a called in another piece of code (main or another method). Calling a method: method actual parameters name int area = areaRec (5, 7) // definition public int areaRec(int length, int width){ // compute area of Rectangle int area = length * width; return area;  } The Method signature is the combination of the method name and the formal parameter list.

Method call: parameter passing When a method is called, the values of the actual parameters of the caller are passed (copied) to the formal parameters of the definition. areaRec(5, 7) (in our example) passes 5 to length and 7 to width

Method return A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void. When a method call is finished it returns the returnValue to the caller. In our example code int area = areaRec(5,7) areaRec(5, 7) returns 35

Call and return // definition public int areaRec(int length, int width){ // compute area of Rectangle int area = length * width; return area;  } int area = areaRec (5, 7) Let’s go check out the code . . . .

Call Stack In our example code main called doRectangularShapes() and doRectangularShapes called areaRec(9,5) When our program gets executed, a run time stack allows records called stack-frames to be stacked up and removed, thereby keeping track of the call history. As in a stack of plates, only the top plate can be accessed, and a new plate goes on top.

main starts main args: ….

main calls doRectangularShapes() area: main args: ….

doRectangularShapes calls areaRec(9,5) length: 9 width: 5 doRectangularShapes area: main args: ….

areaRec(9,5) returns 45 doRectangularShapes prints output: 9 by 5 rectangle has area 45 main args: ….

doRectangularShapes calls areaRec(12) length: width: 12 doRectangularShapes area: 45 main args: ….

areaRec calls areaRec(12,12) length: 12 width: 12 areaRec width: 12 doRectangularShapes area: 45 main args: ….

areaRec(12,12) returns 144 areaRec(12) returns 144 doRectangularShapes prints output: square with width 12 has area 144 main args: ….

doRectangularShapes returns main args: ….

Your turn! Read the program and trace what happens next Draw the run time stack with its stack frames for all the call / return events

Pass by Value areaRec(9,5) doRectangularShapes() The call in passes the integer values 9 and 5 to areaRec This will become relevant later in the course

Overloading Notice that there are e.g. two methods areaRec, with two different method signatures: public int areaRec(int length, int width) and public int areaRec(int width) We call this method overloading. A call will check the number and types of the parameters and select the method with the matching method signature.

Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method. 21

Benefits of Methods Write a method once and reuse it anywhere. Hide the implementation from the user. Reduce complexity (e.g. of main), thereby increasing the readability of your program. Simplify maintenance: if the method needs to change, you only change it in one place. (and the user does not need to know about it) 22

Your Turn! public int perimRec(int length, int width) Write two methods that will calculate the perimeter of a rectangle and of a square public int perimRec(int length, int width) and public int perimRec(int width)

Introduction to Interfaces

Interfaces - motivation Consider the task of writing classes to represent 2D shapes such as Ellipse, Circle, Rectangle and Square. There are certain attributes or operations that are common to all shapes: e.g. their area Idea of interface: contract: "I'm certified as a 2D shape. That means you can be sure that my area can be computed.”

Interfaces interface: A list of methods that a class promises to implement. Only method stubs (method without a body) and constant declarations in the interface, e.g. public double PI =  3.14159; public int areaRec(int length, int width); A class can implement an interface A rectangle has an area that can be computed by the method areaRec If a class implements an interface, it must have methods for all methods stubs in the interface.

Implementing an interface A class can declare that it implements an interface: public class <name> implements <interface name> { ... } This means the class needs to contain an implementation for each of the methods in that interface. (Otherwise, the class will fail to compile.) Let’s go look at some code . . .

Your Turn! public int perimRec(int length, int width) You wrote two methods that calculate the perimeter of a rectangle and of a square public int perimRec(int length, int width) and public int perimRec(int width) How does the Interface now change?