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

Slides:



Advertisements
Similar presentations
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,
Alice in Action with Java Chapter 5 Random Numbers.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
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.
©2004 Brooks/Cole Chapter 3 Assignment. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment In the last chapter we learned how to declare.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Confirmation Dialog Formatting Output.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
©Brooks/Cole, 2001 Chapter 3 Structure of a C Program.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Chapter 6 Repetition. Topics Some additional operators –increment and decrement –assignment operators Repetition –while –do-while –for Random numbers.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
©Brooks/Cole, 2001 Chapter 4 Functions. ©Brooks/Cole, 2001 Figure 4-1.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
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.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
 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.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Review for Nested loops & Math class methods & User defined methods.
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Controlling Program Flow with Decision Structures.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
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.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Introduction to programming in java Lecture 16 Random.
Java Programming Static Methods.
Methods Chapter 6.
Week 8 - Friday CS 121.
Chapter 6 Methods 1.
The Random Class and its Methods
CSC 253 Lecture 8.
CSC 253 Lecture 8.
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
CHAPTER 6 GENERAL-PURPOSE METHODS
Object Oriented Programming in java
CS2011 Introduction to Programming I Methods (II)
More About Objects and Methods
Random number generators
Java Programming Language
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

©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 method, we don't need to know what is inside The method signature tells us how to use it

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Variable Scope We declare variables in a number of places in our programs. Scope rules control where we can use the variables we declare. In Java, variables have block scope.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scope Rules Variables declared inside a method are local to the method –Variables have to be declared before they can be used –Method parameters are local to the method Variables declared in a block can be used only inside that block Class variables are visible everywhere in the class

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Three Storage Areas Created by Program 6.6

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Shadowed variables What happens if two variables with the same name are visible in a particular part of the program? –Local variable is the one that is visible Class variables can be accessed even if they are shadowed. ClassName.variableName

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Pass by Value Arguments to methods are passed by value in Java –method stores a copy of the value that is passed in memory –Changes to a parameter are changes to the local memory

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Data is passed by value Actual Values of variables passed to method are stored in parameter memory

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Numbers Many kinds of programs need random numbers –Games –Simulations On a computer, we generate pseudorandom numbers –not truly random –behave as if they were random

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Number Generation The method Math.random is a pseudorandom number generator. It returns a number of type double that is greater than or equal to 0.0 but less than 1.0.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Integers The number returned from the random method ranges from 0.0 up to (but not including) 1.0. If we want to generate random integers, we must perform a conversion so the number will fall within the desired range. –to generate a number between 0 and N-1, use the formula: y = (int)(Math.random() * N) –to generate a number between 1 and N, use the formula: y = (int)(Math.random() * N + 1)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Number Example *Ch6TestRandomGenerator generates N random numbers between 1 and 4 to simulate the suit of a drawn card. It keeps one counter for each suit, and increments the matching counter after a random number is generated. At the end of the generation, it prints the ratio count/N.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers of various types –float nextFloat() –int nextInt() –int nextInt(n) produces integer in range 0 to n-1

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Testing Programs Isolation testing Method stubs

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Point of Information: Isolation Testing