BBIT 212/ CISY 111 Object Oriented Programming (OOP)

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 5 Functions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
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.
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,
Chapter 7: User-Defined Methods
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
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.
Building java programs, chapter 3 Parameters, Methods and Objects.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Classes - Intermediate
Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Reference: COS240 Syllabus
Suppose we want to print out the word MISSISSIPPI in big letters.
AKA the birth, life, and death of variables.
Introduction to Modular Programming
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 10: Void Functions
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Functions.
Object Oriented Systems Lecture 03 Method
Chapter 6 Methods 1.
Methods.
SELECTION STATEMENTS (1)
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 Function Basics
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
CHAPTER 6 GENERAL-PURPOSE METHODS
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
Building Java Programs
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Lab6 PROGRAMMING 1 metheds chapter5.
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

BBIT 212/ CISY 111 Object Oriented Programming (OOP) Methods, Variables Scope 22.03.2011

Recap : control structures Selection structure if…else statement switch statement Repetition structure for loop while statement do .. while statement

Done? Exercise: Write a java program that outputs the following table: 5 10 15 25 50 75 100 150 225

Methods Definition Creating own methods Invoking a method Overloading methods Why use methods?

Methods in Java Definition: A method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.

A method has the following syntax: modifier returnValueType methodName (list of parameters) { // Method body; } E.g.: Public static void main (String args[]){ System.out.println (“A very popular method”)

a method has the following syntax: modifier returnValueType methodName (list of parameters) { // Method body; } public static void main (String args[]){ System.out.println (“A very popular method”)

From the notes Name: Max Two parameters – of type integer Checks which of the two is larger and returns it

NB: All methods must have a return type If the method does not return a value it is said to be void! A return type gives the method a “type’ Creation of a method (aka method definition) defines what a method does

To use a method, you have to call or invoke it To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not If the method returns a value, a call to the method is usually treated as a value. For example: int larger = max(3, 4); calls max(3, 4) and assigns the result of the method to the variable larger. Another example of a call that is treated as a value is: System.out.println(max(3, 4)); which prints the return value of the method call max(3, 4).

If the method returns void, a call to the method must be a statement If the method returns void, a call to the method must be a statement. For example, the method println returns void. The following call is a statement: System.out.println("Welcome to Java!"); Recall? personobject.printDetails()

Passing Parameters by Values The power of a method is its ability to work with parameters. Recall that objects communicate by receiving messages and sending messages When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method specification. This is known as parameter order association. E.g. print a String/message n number of times

Parameter order association is important Method definition public static void printNTimes (String message, int n) { for (int i = 0; i < n; i++) System.out.println(message); } Invocations: printNTimes (“God is Great”,4); Parameter order association is important printNTimes (4,”God is Great”);  is wrong

When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the method.

Overloading Methods Two methods have the same name but different parameter lists within one class. The Java compiler determines which method is used based on the method signature. public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }

public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } public static double max(double num1, double num2) { return num1; return num2;

If you call max with int parameters, the max method that expects int parameters will be invoked; if you call max with double parameters, the max method that expects double parameters will be invoked.

The Scope of Variables The scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a method is referred to as a local variable. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used. A method parameter is actually a local variable. The scope of a method parameter covers the entire method.

A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration to the end of the block that contains the variable

You can declare a local variable with the same name multiple times in different non nesting blocks in a method, but you cannot declare a local variable twice in nested blocks, as shown in the figure below.

Example age Job group Pay rate per hour 18 - 24 1 25 25 - 35 2 35 35+ Suppose a company pays her employees at hourly rates that depend on age as follows: We want to introduce methods that compute payRate, grossPay and netPay (at 35% PAYE taxation) Notice the local variables in each method age Job group Pay rate per hour 18 - 24 1 25 25 - 35 2 35 35+ 3 30

Your work Thanks!