Parameter: an input to a method

Slides:



Advertisements
Similar presentations
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
Advertisements

Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Chapter 2 types, variables, methods Pages Horstmann.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Two: Using Objects.
Chapter Two: Using Objects. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
ICOM 4015: Advanced Programming Lecture 2 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Two: Using.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Chapter Goals To learn about variables
Slides by Evan Gallagher
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
A final variable is a constant
Chapter 2 – Using Objects
John Woodward A Simple Program – Hello world
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 4 – Fundamental Data Types
5.3 Multiple Alternatives (Part 1)
7.6 Common Array Algorithm: Filling
Chapter 2 – Using Objects
Chapter Three - Implementing Classes
Chapter 2 Using Objects.
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.
Wednesday 09/23/13.
Page 535 Advanced Engineering Mathematics by Erwin Kreyszig
Use a variable to store a value that you want to use at a later time
4.3 Arithmetic Operators and Mathematical Functions
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
Unit-1 Introduction to Java
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved. Chapter 20 Managing the Multinational Financial System Tenth Edition Alan C. Shapiro Multinational.
Presentation transcript:

Parameter: an input to a method Implicit parameter: the object on which a method is invoked: System.out.println(greeting) Explicit parameters: all parameters except the implicit parameter: Not all methods have explicit parameters: greeting.length() //has no explicit parameter                                              2.5 Method Parameters and Return Values Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Passing a Parameter Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Return Values Return value: a result that the method has computed for use by the code that called it: int n = greeting.length(); // return value stored in n Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

You can also use the return value as a parameter of another method: Passing Return Values You can also use the return value as a parameter of another method: System.out.println(greeting.length()); Not all methods return values. Example: println Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

String method replace carries out a search-and-replace operation: A More Complex Call String method replace carries out a search-and-replace operation: river.replace("issipp", "our”) // constructs a new string ("Missouri") This method call has one implicit parameter: the string "Mississippi" two explicit parameters: the strings "issipp" and "our" a return value: the string "Missouri" Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Animation 2.2: Parameter Passing Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 2.12 What are the implicit parameters, explicit parameters, and return values in the method call river.length()? Answer: The implicit parameter is river. There is no explicit parameter. The return value is 11.

Self Check 2.13 What is the result of the call river.replace("p", "s")? Answer: "Missississi".

What is the result of the call Answer: 12. Self Check 2.14 What is the result of the call greeting.replace("World", "Dave").length()? Answer: 12. “Hello, World” becomes “Hello, Dave” (There are 2 spaces between Hello, and World)

Self Check 2.15 How is the toUpperCase method defined in the String class? Answer: As public String toUpperCase(), with no explicit parameter and return type String.