1 Functions/Methods. 2 Function / Method zDefinition Õcollection of statements that may be invoked by name (from another function or method) zFunctions/methods.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Alice in Action with Java
Chapter 4: Writing Classes
ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes, Objects, and Methods
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Classes CS 21a: Introduction to Computing I First Semester,
OOP Review. Key OOP Concepts zObject, Class zInstantiation, Constructors zEncapsulation zInheritance and Subclasses zAbstraction zReuse zPolymorphism,
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.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CS 21b Intro to Computing II. Object-Oriented Programming: Classes in Java.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
Department of Computer Engineering Methods Computer Programming for International Engineers.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.
C# Programming Methods.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Suppose we want to print out the word MISSISSIPPI in big letters.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
Starting Out with Java: From Control Structures through Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Group Status Project Status.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Object Oriented Programming Review
Classes, Objects and Methods
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

1 Functions/Methods

2 Function / Method zDefinition Õcollection of statements that may be invoked by name (from another function or method) zFunctions/methods that we have invoked ÕInput.readInt(), setLayout(), Math.sqrt() zFunctions/methods that we have defined Õmain(), init(), action() * the term “method” will be used from now on

3 Defining methods that the program will invoke zWhy? Õcode inside main() (or init() or action()) may be lengthy Õsometimes, the task requires something redundant (e.g., print two blocks of asterisks, one of size 5, the other of size 10) Õwe want to define our own math function zMethods allow us to organize our code into logical units

4 Defining Methods zSyntax (for applications) public static void methodName() { // statements } zFor applets, omit the static keyword zMethods are defined at the level of the class (same level as main() or init())

5 Parameters and Return Values zIt is possible to pass data to a method Õthis allows for the possibility of a different effect based on the value of the parameter zIt is possible to return data to the caller Õlike Input.readInt() or Math.sqrt(), the method can return a value zExample that uses both features Õint factorial(int num) { … }

6 Invoking Methods zInvocation ÕmethodName(expression-list) zFormal versus actual parameters Õformal parameter: the variable used to represent the argument within the method Õthe expression used during invocation that is evaluated and then passed as a value to the method

7 Object-Oriented Programming: Classes in Java

8 Object Definition: a thing that has identity, state, and behavior Õidentity: a distinguished instance of a class Õstate: collection of values for its variables Õbehavior: capability to execute methods * variables and methods are defined in a class

9 Class Definition: a collection of data (variables) and methods that operate on that data Õdata/methods define the contents/capabilities of the instances (objects) of the class Õobject creation occurs with the statement variable = new class(parameters); Õclasses can be viewed as factories for objects

10 Class zA class is a template for an object zAn object is an instance of a class

11 Class zAll data and variables defined within a class are called instance variables Õbecause each instance of that class (each object of the class) contains a copy of that variable zMethods and variables defined within a class are called members of that class

12 Two Kinds of Variables in Java zVariables of a primitive type e.g., int x; char c; zVariables of a reference type (class) e.g., Button b; String s; zConventions ÕPrimitive types are reserved words in Java and are indicated in all-lower-case letters ÕClass names: first letter usually capitalized

13 Variables and Values zPrimitive type variables int x; … x = 5; 5 X X

14 Variables and References zReference type variables Button x; … x = new Button(“click”); X X “click” Button Object

15 The new Keyword znew Button(“click”) creates a Button object and returns a reference (an address) to that object that a Button variable could hold “click” Button Object 1023: 1023 is some address in memory 1023 X

16 The Dot (“.”) Operator zAllows access to variables (primitive and reference types) and methods of reference type variables. ÕEx. TextField t = new TextField(10); t.setText(“hi”); *accessing a method using the dot operator

17 Method Invocation zSyntax for method invocation object.methodName(arguments) zMethod may return a value or simply produce an effect on the object zTo find out what methods are available for a given class Õjavap package.name.NameOfClass Õex. Javap java.awt.Button

18 Strings Revisited zStrings are objects as well zString is an existing Java class Õs = “Hello” is just a shorthand for s= new String(“Hello”); zString methods Õint length() ÕString substring(int x,int y); Õno “manipulating” methods

19 Variables and Objects Let Circle be a class with: variable r that indicates its radius method area() that computes its area ÕDeclaration:Circle c; ÕInstantiation:c = new Circle(); ÕUsage: c.r = 5.5; System.out.println(c.area());

20 The complete Circle class public class Circle { public double x,y; // center coordinates public double r; // radius // the methods public double circumference() { return 2*3.14*r; } public double area() { return 3.14*r*r; } }

21 Using the Circle class public class TestCircle { public static void main(String args[]) { Circle c; c = new Circle(); c.x = 2.0; c.y = 2.0; c.r = 5.5; System.out.println(c.area()); }