* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Programming Abstract classes and Interfaces.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Java: How to Program Methods Summary Yingcai Xiao.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
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.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
10-Nov-15 Java Object Oriented Programming What is it?
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
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.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions + Overloading + Scope
Chapter 7: User-Defined Functions II
“Form Ever Follows Function” Louis Henri Sullivan
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
CHAPTER 6 GENERAL-PURPOSE METHODS
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Using java libraries CGS3416 spring 2019.
Corresponds with Chapter 5
Presentation transcript:

* * 0 Chapter 6 Java Methods

* * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method is accessible from anywhere private – restrict access to the class in which the method is declared protected – used with inheritance Default – (the access specifier is left off) accessible only from classes in the same package

* * 0 Method Syntax – continued Qualifier Static makes the method usable by any code that has access to the method and the class regardless of whether any objects of the class have been created Arguments C++ has two ways to pass arguments – call by value and call by reference All arguments in Java are call by value. – – methods ALWAYS receive a copy of the argument

* * 0 Method Syntax - continued Arguments ●Objects are not passed to methods; references to objects are passed. ●If the argument is an object reference, a copy of a reference is passed to a method. Therefore, the method can manipulate the object directly.

* * 0 3 ways to call a method ●Refer to the method name –int x = square (y); ●Call using an object –System.out.println(“Hello”); //System.out is the object and println is the method ●Call using the class name (means the qualifier is static) –Integer.parseInt(numberString);

* * 0 Scope of Declarations ●Scope of a parameter declaration is the body of the method in which the declaration appears. ●Scope of local variable is the block in which the local variable is declared. ●The scope of a local-variable declaration that appears in a for initialization is the body of the for

* * 0 Scope Declarations - continued ●The scope of a method or field of a class is the entire body of the class. (except for static methods)

* * 0 Scope Example public class Scope { int x=1; public void init() ………….. public void local1() { int x=25; } public void local2() { x*=10; }

* * 0 Method Overloading Method overloading – concept that more than 1 method in a class has the same name but different signatures. Signature – combination of method name and number and types of arguments Example of Method Overloading public double square (double value) public int square (int value)

* * 0 Method Overloading Syntax Error Example public double square (int value) { ………} public int square (int x) {………} These two methods have the same signature – results in a syntax error

* * 0 Recursive Methods Recursive method – a method which continues to call itself directly or through another method Concept: a recursive method is capable of solving only the simplest case of a problem (base case). If the problem is complex, the method divides the problem into two smaller pieces: a base case and a smaller problem

* * 0 Recursion Example long y = factorial(5); Base case :1! = 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1

* * 0 Recursion Example public long factorial (long number) { if (number <=1) return 1; else return (number * factorial (number – 1)); }

* * 0 Recursion ●Avoid recursion in situations requiring performance. Recursive calls take up processor time and memory space.

* * 0 Static Methods ●Also called class method –Method that doesn’t depend on the contents of an object ●Java’s Math class contains a collection of static methods that enable one to perform common mathematical calculations. ●When a method is static, it only can activate static fields and static methods.

* * 0 Methods is Math class ●abs(x) ●max(x,y) ●min(x,y) ●pow (x,y) x y ●sqrt (x) ●ceil (x) smallest integer not less than x –ceil (9.2) = 10 –ceil (-9.8) = -9 ●floor(x) largest integer not greater than x –floor (9.2) = 9 –floor (-9.8) = -10

* * 0 Methods in Math class ●Math.random() –Returns a double with a value from 0.0 to 1.0 –(not including 1.0) ●How to randomly pick an integer from 1 to 6 –(int)(Math.random() * 6) + 1

* * 0 Random-Number Generation ●Class Random (in java.util) –Can produce random boolean, byte, float, double, int, long and Gaussian values ●Example: –Random randomNumbers = new Random(); –int randomValue = randomNumbers.nextInt(2); –returns 0 or 1

* * 0 Fields in Math class ●PI (public final static) ●E (public final static) ●Static fields are called class variables

* * 0 Calling methods in Math int z = Math.max (x,y); float d = Math.PI * y;

* * 0 GUI - Colors and Filled Shapes ●g.setColor(Color.BLUE); ●public void fillRect (int x, int y, int width, int height) ●public void drawOval(int x, int y, int width, int height) ●public void fillOval(int x, int y, int width, int height)

* * 0 How to create GUI classes ●Classified into 3 groups –Container classes –Component classes –Helper classes ●Container classes –JFrame, JPanel, JApplet ●Component classes –JButton, JRadioButton, Jmenu, JTextField, etc ●Helper classes –Graphics, Color, Font, LayoutManager

* * 0 How to create GUI classes ●Classified into 3 groups –Container classes –Component classes –Helper classes ●Container classes –JFrame, JPanel, JApplet ●Component classes –JButton, JRadioButton, Jmenu, JTextField, etc ●Helper classes –Graphics, Color, Font, LayoutManager

* * 0 End Chapter 6 - Methods Presentation