Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Arrays.
Spring Semester 2013 Lecture 5
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Session 7 Methods Strings Constructors this Inheritance.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 6 Methods Chapter 6 - Methods.
Chapter 3: User-Defined Functions I
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
Programming Fundamentals Enumerations and Functions.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java Generics.
Chapter 7: User-Defined Functions II
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Classes, Objects, and Methods
Object Oriented Programming in java
Java Programming Language
Class Example - Rationals
Corresponds with Chapter 5
Presentation transcript:

Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move, turn, moveTowards, and rotate.

Method Syntax In Java, a method consists of a method header and a method body. The header says who can call the method, the type of the value that is returned by the method, the name of the method, and the parameters that are passed into the method. The body gives the code for the method – that is what is actually done.

Example public Rational add(Rational b) { Rational c = new Rational(); c.n = n * b.d + b.n * d; c.d = d * b.d; return c; }

Syntax ( ) { }

Parameters Parameters are how we pass information into a method. The method header contains a parameter list – a comma separated list of parameters. Each parameter consists of a type and a variable. Parameters are similar to variable declarations, except that they are initialized to values given in the call to the method.

Calling a method Most methods are associated with an object (although some methods may be associated with the entire class). A method is called by apply the method to an object of the class. The call may occur in an expression. The syntax is:. ( ). For example: Rational rat3 = rat1.add(rat2);

Arguments In the call to the method, values are specified for the initial values of the parameters. In the previous example, the parameter b would be initialized with the value rat2. The list of arguments must match in number and type with the list of parameters. Sometimes, one type may be automatically converted to another. For example, if the parameter is of type double, and the argument is an int, the argument will be converted.

More on Methods Methods are like miniature programs. They have input (through the parameters), output (via the return statement) and processing steps (the method body). Methods may have their own local variables.

Return Value A method which returns a value does so by using the return statement. The syntax is just: return The value must be of the same type as specified in the method header.

Void Methods A method which does not return a value should be of return type void. You can return from the method before reaching the end of it, by using a return statement without a value, e.g. return; The call to a void method may not be in an expression, but should be a separate statement.

Constructors Constructors are similar to methods, but instead of returning a value, they return a newly created object. Basically, constructors are used to initialize the fields of the new object, often given information from the caller, or using default value.

Syntax Constructors look very similar to methods, but there are two differences:  Constructors do not return a value, but rather the newly created object. Thus constructors do not have a return type (not even void)  Constructors always have the same name as the class.

Example A constructor for the Rational class could be: public Rational(int n0, int d0) { n = n0; d = d0; } Constructors are almost always public.

Default Constructor If you do not declare any constructors, there is always one created by default. This is a zero- argument constructor. It takes no arguments and just creates the object. If fields are initialized, those values are used. If not, numbers are set to 0, and chars to null characters.

More on Constructors If you do declare any constructors and you use the zero-argument constructor, you must declare it yourself: public Rational() {}

Multiple Constructors Often classes contain more than one constructor. This is useful if the user may omit values and default values are to be used. For example, the Rational class may have a one-place constructor which is given the numerator, and the denominator is assumed to be 1. This converts ints to Rationals. public Rational(int n0){ n = n0; d = 1; }

Multiple Constructors (cont'd) Similarly, the zero-argument constructor should probably set the values to something reasonable, such as 0/1: public Rational() { n = 0; d = 1; }

Disambiguation So as not to confuse the compiler, constructors (like methods) must be distinguishable when called. That means that they must differ in the number or types of the arguments.

Nested Constructors One constructor may invoke another constructor. In Java convention, this is commonly done to set default arguments. For example, the one-place constructor would invoke the two-place constructor, by passing the value of n0, and passing 1 for d: public Rational(int n0) { this(n0,1); } The keyword this is used to invoke another constructor of the class.

Nested Constructors (cont'd) Similarly, the zero-argument constructor would invoke the one-argument constructor (which in turns invokes the two-place constructor). public Rational() { this(0); } This makes more sense when the code for the constructor is complicated, and we want to avoid duplication of code in all the constructors. For example, if we need to change it, we need only make the change once.

Example In our example of the Rational constructors, we forgot that Rational numbers are to be stored in lowest terms. The constructor should reduce the rational number. public Rational(int n0; int d0) { n = n0; d = d0; reduce(); } This one change effects all the constructors as they call this one (directly or indirectly).