Lecture 11 Parameters CSE 1322 4/26/2018.

Slides:



Advertisements
Similar presentations
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
By Mr. Muhammad Pervez Akhtar
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.
C# Programming Methods.
Classes - Intermediate
Methods.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Defining Your Own Classes II
Modern Programming Tools And Techniques-I
Chapter 4: Writing Classes
Building Java Programs
Constructor & Destructor
Methods.
Lecture 11 B Methods and Data Passing
Modern Programming Tools And Techniques-I Inheritance
More Object Oriented Programming
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.
Lecture 3 Functions Simple functions
CSCI 1100/1202 April 10,
Chapter 6 Methods: A Deeper Look
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
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.
Expressions and Assignment
Week 4 Lecture-2 Chapter 6 (Methods).
Inheritance with Constructor
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
Methods/Functions.
Parameters, Overloading Methods, and Random Garbage
Methods and Data Passing
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Lecture 11 Parameters CSE 1322 4/26/2018

What is a Method? Think of a method as a black box that contains the detailed implementation for a specific task. The method may use inputs (parameters) and may return an output with a specific type. 4/26/2019

Method Structure 4/26/2019

Method Structure 4/26/2019

Parameters and Argument Passing 4/26/2019

Parameters There are three main types of parameters: Value - passes a copy (value) of the variable to the method.  This is the default. Reference (C#)- passes a reference to the actual variable.  Marked with "ref", use this when you want to pass a value in and have any change to that value be persistent when the method is complete Out (C#) - passes a reference to the actual variable.  Marked with "out", use this when you want the method to generate a value and place it for later use in the actual variable (persists when the method is complete) Note that java passes all parameters by value. C++ uses both Call-by-reference and Call-by-value 4/26/2018

Example 1 and the output is ? static void Main(String[] args) { int a = 42; Console.WriteLine (a); // Prints 42 B (a); // Prints 51 // Call-by-value; Console.WriteLine (a); // Prints 42 } static void B (int x) // Built-in type argument passing x += 9; Console.WriteLine (x); A Call-by-value Method with C# 4/26/2018

Example 1 and the output is ? 4/26/2018

Example 1 and the output is ? static void Main(String[] args) { int a = 42; System.out.println (a); // 42 B (a); // 51 System.out.println (a); // 42 } static void B (int x) x += 9; System.out.println (x); 4/26/2018

A Call-By-Value Example with Java public class CallByValueEx2 { public static void main(String[] args) { String myname = "Mokter"; // myname is a is String reference local variable System.out.println("Initially, inside main(), Name = " + myname ); callMe(myname); System.out.println("After calling callMe(), Name = " + myname); // Look here ??!!?? System.out.println("Now, inside main(), Name = " + myname ); } static void callMe(String newname ) { // newname is String reference argument newname = "Dr. Mokter Hossain"; System.out.println("Inside callMe(), Name = " + newname ); 4/26/2018

A Call-By-Value Example with Java 4/26/2018

Example 2 and the output is ? static void Main(String[] args ) { int a = 42; Console.WriteLine (a); B (ref a); } static void B (ref int x) x += 9; Console.WriteLine (x); 4/26/2018

Example 2 and the output is ? 4/26/2018

Example 42 and the output is ? 4/26/2018

Example 3 and the output is ? The out works same as ref but it is optional to initialize in the caller method Example 3 and the output is ? static void Main(String[] args) { int a; // NOT initialized B (out a); Console.WriteLine (a); } static void B (out int x) { x = 9; Console.WriteLine (x); Source: https://www.dotnettricks.com/learn/csharp/difference-between-ref-and-out-parameters 4/26/2018

Example 3 and the output is ? 4/26/2018

Example 4 and the output is ? using System; namespace CSE1322ReviewCS{ class Program { class Z { public int y; } static void Main(String[] args) { Z myZ = new Z(); myZ.y = 42; Console.WriteLine(myZ.y); B(myZ); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void B(Z x) { x.y += 9; Console.WriteLine(x.y); 4/26/2018

Example 4 and the output is ? class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; System.out.println (myZ.y); B (myZ); } static void B (Z x) x.y += 9; System.out.println (x.y); 4/26/2018

Example 4 and the output is ? 4/26/2018

Example 5 and the output is ? using System; class Example5 { class Z { public int y; } static void Main(String[] args) { Console.WriteLine("Example5...."); Z myZ = new Z(); myZ.y = 42; Console.WriteLine(myZ.y); B(ref myZ); Console.ReadKey(); } static void B(ref Z x) { x = new Z(); x.y = 1; Console.WriteLine(x.y); 4/26/2018

Example 5 and the output is ? 4/26/2018

Pass-by-value vs. Pass-by-reference vs. Pass-by-reference-type Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type. Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next. 4/26/2018

Overloading Methods Method overloading is the process of using the same method name for multiple methods The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The compiler determines which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature 4/26/2018

Overloading Methods float tryMe (int x) { return x + .375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32f) Invocation 4/26/2018

Overloading Operators In C#, not only can methods be overloaded, operators can be overloaded as well. Java does not allow operator overloading 4/26/2018

4/26/2018

Operator overloading C# enables you to overload most operators to make them sensitive to the context in which they are used. Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls. Class ComplexNumber overloads the plus (+), minus (-) and multiplication (*) operators to enable programs to add, subtract and multiply instances of class ComplexNumber using common mathematical notation 4/26/2018

Operator Overloading Keyword operator, followed by an operator symbol, indicates that a method overloads the specified operator. Methods that overload binary operators must take two arguments—the first argument is the left operand, and the second argument is the right operand. Overloaded operator methods must be public and static. 4/26/2018

4/26/2018

Overload the plus operator (+) to perform addition of ComplexNumbers 4/26/2018