Part III © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Advertisements

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.
© 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.
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.
Chapter 5 Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© 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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
1 Lecture 3 Part 1 Functions with math and randomness.
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
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.
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.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 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.
© 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.
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.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
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.
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 Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
© by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
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.
 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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
(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,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions.
Methods Chapter 6.
Chapter 20 Generic Classes and Methods
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
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.
Presentation transcript:

Part III © 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.

 Notes:  myPoint is initialized to 0 to ensure that the application will compile.  If you do not initialize myPoint, the compiler issues an error, because myPoint is not assigned a value in every case of the switch statement, and thus the program could try to use myPoint before it is assigned a value.  gameStatus is assigned a value in every case of the switch statement—thus, it’s guaranteed to be initialized before it’s used and does not need to be initialized. © 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.

 Why Some Constants Are Not Defined as enum Constants  Java does not allow an int to be compared to an enumeration constant.  Java does not provide an easy way to convert an int value to a particular enum constant.  Translating an int into an enum constant could be done with a separate switch statement.  This would be cumbersome and not improve the readability of the program (thus defeating the purpose of using an enum ). © Copyright by Pearson Education, Inc. All Rights Reserved.

 Declarations introduce names that can be used to refer to such Java entities.  The scope of a declaration is the portion of the program that can refer to the declared entity by its name.  Such an entity is said to be “in scope” for that portion of the program.  More scope information, see the Java Language Specification, Section 6.3: Scope of a Declaration  java.sun.com/docs/books/jls/third_edition/ html/names.html# © 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.

 Method overloading  Methods of the same name declared in the same class  Must have different sets of parameters  Compiler selects the appropriate method to call by examining the number, types and order of the arguments in the call.  Used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments.  Literal integer values are treated as type int, so the method call in line 9 invokes the version of square that specifies an int parameter.  Literal floating-point values are treated as type double, so the method call in line 10 invokes the version of square that specifies a double parameter. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Distinguishing Between Overloaded Methods  The compiler distinguishes overloaded methods by their signatures—the methods’ names and the number, types and order of their parameters.  Return types of overloaded methods  Method calls cannot be distinguished by return type.  Figure 6.10 illustrates the errors generated when two methods have the same signature and different return types.  Overloaded methods can have different return types if the methods have different parameter lists.  Overloaded methods need not have the same number of parameters. © Copyright by Pearson Education, Inc. All Rights Reserved.