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

Slides:



Advertisements
Similar presentations
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
 2006 Pearson Education, Inc. All rights reserved Functions.
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 Functions.
7 7 Quiz Function call stack Passing values to functions Recursion.
© 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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
8 8 Function call stack Passing values to functions Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
 2006 Pearson Education, Inc. All rights reserved Functions.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
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.
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Computer Programming 2 Lecture 3: Methods in Advanced & Practices Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
4 th Week Spring 2011 Methods 1. Outline Introduction Why method ? Static methods vs. non-static methods Example: Math class Declare a method Signature.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
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.
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
1. 2 Framework Classes and libraries: 3.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
(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,
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Functions.
Methods Chapter 6.
Chapter 20 Generic Classes and Methods
Programming Fundamentals Lecture #7 Functions
Chapter 5 - Functions Outline 5.1 Introduction
6 Functions.
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 Methods: A Deeper Look
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
6 Methods: A Deeper Look.
Chapter 5 Methods: A Deeper Look
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
Presentation transcript:

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

 Math fields for common mathematical constants  Math.PI ( )  Math.E ( )  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.

 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.

 Stack data structure  Analogous to a pile of dishes  A dish is placed on the pile at the top (referred to as pushing the dish onto the stack).  A dish is removed from the pile from the top (referred to as popping the dish off the stack).  Last-in, first-out (LIFO) data structures  The last item pushed (inserted) on the stack is the first item popped (removed) from the stack. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When a program calls a method, the called method must know how to return to its caller  The return address of the calling method is pushed onto the program-execution (or method-call) stack.  If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order.  The program-execution stack also contains the memory for the local variables used in each invocation of a method during a program’s execution.  Stored as a portion of the program-execution stack known as the activation record or stack frame of the method call. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When a method call is made, the activation record for that method call is pushed onto the program-execution stack.  When the method returns to its caller, the method’s activation record is popped off the stack and those local variables are no longer known to the program.  If more method calls occur than can have their activation records stored on the program-execution stack, an error known as a stack overflow occurs. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Simulation and game playing  element of chance  Class Random (package java.util )  static method random of class Math.  Objects of class Random can produce random boolean, byte, float, double, int, long and Gaussian values  Math method random can produce only double values in the range 0.0  x  1.0.  Documentation for class Random  download.oracle.com/javase/6/docs/api/java/ util/Random.html © Copyright by Pearson Education, Inc. All Rights Reserved.

 Basic rules for the dice game Craps:  You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point.” To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making your point. © Copyright by Pearson Education, Inc. All Rights Reserved.

 enum type Status  A n enumeration in its simplest form declares a set of constants represented by identifiers.  Special kind of class that is introduced by the keyword enum and a type name.  Braces delimit an enum declaration’s body.  Inside the braces is a comma-separated list of enumeration constants, each representing a unique value.  The identifiers in an enum must be unique.  Variables of an enum type can be assigned only the constants declared in the enumeration. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Basic scope rules:  The scope of a parameter declaration is the body of the method in which the declaration appears.  The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block.  The scope of a local-variable declaration that appears in the initialization section of a for statement’s header is the body of the for statement and the other expressions in the header.  A method or field’s scope is the entire body of the class.  Any block may contain variable declarations.  If a local variable or parameter in a method has the same name as a field of the class, the field is “hidden” until the block terminates execution—this is called shadowing. © Copyright by Pearson Education, Inc. All Rights Reserved.