Java™ How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © 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.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s Methods.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
(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,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods Chapter 6.
Chapter 3: Using Methods, Classes, and Objects
Programming Fundamentals Lecture #7 Functions
JavaScript: Functions.
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
6 Methods: A Deeper Look.
Object Oriented Programming in java
Chapter 5 Methods: A Deeper Look
Classes, Objects and Methods
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.
6 Functions.
Presentation transcript:

Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.

 Best way to develop and maintain a large program is to construct it from small, simple pieces, or modules.  divide and conquer.  Topics in this chapter  static methods  Declare a method with more than one parameter  Method-call stack  Simulation techniques with random-number generation.  How to declare values that cannot change (i.e., constants) in your programs.  Method overloading. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Java programs combine new methods and classes that you write with predefined methods and classes available in the Java Application Programming Interface and in other class libraries.  Related classes are typically grouped into packages so that they can be imported into programs and reused.  You’ll learn how to group your own classes into packages in Chapter 8. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Methods help you modularize a program by separating its tasks into self-contained units.  Statements in method bodies  Written only once  Hidden from other methods  Can be reused from several locations in a program  Divide-and-conquer approach  Constructing programs from small, simple pieces  Software reusability  Use existing methods as building blocks to create new programs.  Dividing a program into meaningful methods makes the program easier to debug and maintain. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Hierarchical form of management (Fig. 6.1).  A boss (the caller) asks a worker (the called method) to perform a task and report back (return) the results after completing the task.  The boss method does not know how the worker method performs its designated tasks.  The worker may also call other worker methods, unbeknown to the boss.  “Hiding” of implementation details promotes good software engineering. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Sometimes a method performs a task that does not depend on the contents of any object.  Applies to the class in which it’s declared as a whole  Known as a static method or a class method  It’s common for classes to contain convenient static methods to perform common tasks.  To declare a method as static, place the keyword static before the return type in the method’s declaration.  Calling a static method  ClassName. methodName ( arguments )  Class Math provides a collection of static methods that enable you to perform common mathematical calculations.  Method arguments may be constants, variables or expressions. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Math fields for common mathematical constants  Math.PI  Value of  Math.E  Value of  Declared in class Math with the modifiers public, final and static  public allows you to use these fields in your own classes.  A field declared with keyword final is constant—its value cannot change after the field is initialized.  PI and E are declared final because their values never change. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A field that represents an attribute is also known as an instance variable—each object (instance) of the class has a separate instance of the variable in memory.  Fields for which each object of a class does not have a separate instance of the field are declared static and are also known as class variables.  All objects of a class containing static fields share one copy of those fields.  Together the class variables (i.e., static variables) and instance variables represent the fields of a class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Why is method main declared static ?  The JVM attempts to invoke the main method of the class you specify—when no objects of the class have been created.  Declaring main as static allows the JVM to invoke main without creating an instance of the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Multiple parameters are specified as a comma- separated list.  There must be one argument in the method call for each parameter (sometimes called a formal parameter) in the method declaration.  Each argument must be consistent with the type of the corresponding parameter. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Implementing method maximum by reusing method Math.max  Two calls to Math.max, as follows:  return Math.max( x, Math.max( y, z ) );  The first specifies arguments x and Math.max( y, z ).  Before any method can be called, its arguments must be evaluated to determine their values.  If an argument is a method call, the method call must be performed to determine its return value.  The result of the first call is passed as the second argument to the other call, which returns the larger of its two arguments. © Copyright by Pearson Education, Inc. All Rights Reserved.

 String concatenation  Assemble String objects into larger strings with operators + or +=.  When both operands of operator + are String s, operator + creates a new String object  characters of the right operand are placed at the end of those in the left operand  Every primitive value and object in Java has a String representation.  When one of the + operator’s operands is a String, the other is converted to a String, then the two are concatenated.  If a boolean is concatenated with a String, the boolean is converted to the String "true" or "false".  All objects have a toString method that returns a String representation of the object. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Three ways to call a method:  Using a method name by itself to call another method of the same class  Using a variable that contains a reference to an object, followed by a dot (. ) and the method name to call a method of the referenced object  Using the class name and a dot (. ) to call a static method of a class © Copyright by Pearson Education, Inc. All Rights Reserved.

 A non- static method can call any method of the same class directly and can manipulate any of the class’s fields directly.  A static method can call only other static methods of the same class directly and can manipulate only static fields in the same class directly.  To access the class’s non- static members, a static method must use a reference to an object of the class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Three ways to return control to the statement that calls a method:  When the program flow reaches the method-ending right brace  When the following statement executes return;  When the method returns a result with a statement like return expression ; © Copyright by Pearson Education, Inc. All Rights Reserved.