Methods Additional Topics

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Fields, Constructors, Methods
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Road Map Introduction to object oriented programming. Classes
Final and Static Keywords. Creating constants  There will be occasions where data items in a program have values that do not change.  Maximum score.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Types in programming languages1 What are types, and why do we need them?
Copyright Curt Hill Variables What are they? Why do we need them?
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Chapter 6 Methods Chapter 6 - Methods.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
The Second C++ Program Variables, Types, I/O Animation!
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Lecture 5: Some more Java!
University of Central Florida COP 3330 Object Oriented Programming
Objectives Define polymorphism Overload functions
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Lecture 14 Writing Classes part 2 Richard Gesick.
A brief look at some of the new features introduced to the language
Zhen Jiang West Chester University
null, true, and false are also reserved.
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Arrays in Java What, why and how Copyright Curt Hill.
Java Lesson 36 Mr. Kalmes.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Simple Classes in Java CSCI 392 Classes – Part 1.
Java Programming Review 1
The Java switch Statement
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Corresponds with Chapter 5
Methods Scope How are names handled?
Methods Coding in Java Copyright © Curt Hill.
Unit-2 Objects and Classes
Presentation transcript:

Methods Additional Topics Copyright © 1997 - 2009 Curt Hill

Topics Returning values Name overloading Copyright © 1997 - 2009 Curt Hill

The Return Type The first required part of the method header May be any type – void, primitive or object Consider examples: public static void main(…){…} static double fixer(int i){…} private char myFunction(…){…} String massage(String y){…} Copyright © 1997 - 2009 Curt Hill

Notes The visibility is optional public, private and protected are possible visibilities The keyword static may be included or not Depends on needs of the method The return type is required for all methods Other than constructors, which must return the object constructed They also must have the same name as the class Copyright © 1997 - 2009 Curt Hill

The return statement Has two effects Ends the function and returns to calling statement Determines the value that needs to be returned Form is: return expression; Expression should match return type A void method needs no return Copyright © 1997 - 2009 Curt Hill

Call return sequence Consider this very simple sequence: static int four(){ return 4; } … public static void main(…) { System.out.println(four()); The method is called The 4 is identified as the return value This is displayed in the message Copyright © 1997 - 2009 Curt Hill

Name Overloading In many languages such as C, Pascal, VB a function is defined by one thing only: the function name C++ introduced function name overloading It determines the function to use by examining the signature Java does the same Copyright © 1997 - 2009 Curt Hill

Method Signatures A method is uniquely determined by two things: the name the parameter list The contribution of the parameter list does not include the parameter names, only number, type and order These are collectively called the signature Two signatures within a class may not be the same Copyright © 1997 - 2009 Curt Hill

Example Consider: int square(int a){ return a*a; } float square(float a){ return a*a; } Copyright © 1997 - 2009 Curt Hill

Notes There is no conflict between the two The compiler can tell which is intended Consider: int i, j; float f,g; … int j = square(i); double g = square(f); The result type does not distinguish Only name and parameter types Copyright © 1997 - 2009 Curt Hill

Why? Function name overloading means that we can use the right name for a method Even if the name has already been used Both square methods did the same thing Just on different types of parameters Since types are so important we do not have to have a different name for the integer version as the double version Copyright © 1997 - 2009 Curt Hill

Finally Most of this has been a review of previous material It needs to extra emphasis Methods are the building blocks of programs Each of which is a class Copyright © 1997 - 2009 Curt Hill