© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Introduction to C Programming
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
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.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
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. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
© 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 Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
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. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
REEM ALMOTIRI Information Technology Department Majmaah University.
3 Introduction to Classes and Objects.
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 5 - Functions Outline 5.1 Introduction
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive Data Types and Keywords 25.3Logical Operators 25.4Method Definitions 25.5Java API Packages 25.6Random Number Generation 25.7Example: A Game of Chance 25.8Methods of Class JApplet 25.9Defining and Allocating Arrays 25.10Examples Using Arrays 25.11References and Reference Parameters 25.12Multiple-Subscripted Arrays

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this chapter, you will learn: –To understand primitive types and logical operators as they are used in Java. –To introduce the common math methods available in the Java API. –To be able to create new methods. –To understand the mechanisms used to pass information between methods. –To introduce simulation techniques using random number generation. –To understand array objects in Java. –To understand how to write and use methods that call themselves.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.1Introduction In this chapter –Differences between C, C++, and Java –Java's logical operators and methods –Packages that comprise Applications Programming Interface (API) –Craps simulator –Random numbers in Java –Arrays in Java

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.2Primitive Data Types and Keywords Primitive data types –char, byte, short, int, long, float, double, boolean –Building blocks for more complicated types All variables must have a type before being used Strongly typed language –Primitive types portable, unlike C and C++ In C/C++, write different versions of programs –Data types not guaranteed to be identical –ints may be 2 or 4 bytes, depending on system WORA - Write once, run anywhere –Default values boolean gets false, all other types are 0

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.2Primitive Data Types and Keywords (II)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.2Primitive Data Types and Keywords (III) Keywords –Reserved names, cannot be used as identifiers –Used to implement features

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators Logical operators –Form complex conditions and control structures –Logical AND ( && ) true if both conditions true –Logical OR ( || ) true if either condition true true if both conditions true (inclusive) If left condition true, skips right condition –Boolean logical AND (&), boolean logical inclusive OR ( | ) Act like counterparts, but always evaluate both expressions Useful if expression performs action: birthday == true | ++age >= 65

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators (II)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators (III) Logical Operators (continued) –Boolean logical exclusive OR ( ^ ) true if exactly one condition true false if both conditions true –Logical NOT (negation) Unary operator (one operand) –All other logical operators binary (two operands) Reverses condition If true, returns false If false, returns true != - "does not equal" if (grade != sentinelValue)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators (IV)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators (V) More GUI Classes ( javax.swing ) –JTextArea Create an area where text can be displayed Provide ( rows, columns ) to constructor to specify size JTextArea myArea; //declares object type myArea = new JTextArea( 17, 20 ); //initialize –myArea.setText( myString ); Sets the text of myArea to myString –JScrollPane Creates a window that can scroll JScrollPane myScroller = new JScrollPane ( myArea ); Declaration and initialization, allows myArea to have scrolling

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.3Logical Operators (VI) More GUI classes –showMessageDialog(null, myScroller, titleString, type); Second argument indicates that myScroller (and attached myArea ) should be displayed in message dialog

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Logical- Operators.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Logical- Operators.java (Part 2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.4Method Definitions Method definition format return-value-type method-name( parameter-list ) { declarations and statements } –Method-name: any valid identifier –Return-value-type: data type of the result void - method returns nothing Can return at most one value –Parameter-list: comma separated list, defines parameters Method call must have proper number and type of parameters –Definitions and statements: method body (block) Variables can be defined inside blocks (can be nested) Method cannot be defined inside another function

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.4Method Definitions (II) Program control –When method call encountered Control transferred from point of invocation to method –Returning control If nothing returned: return; –Or until reaches right brace If value returned: return expression ; –Returns the value of expression –Example user-defined method: public int square( int y ) { return y * y }

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.4Method Definitions (III) Calling methods –Three ways Method name and arguments –Can be used by methods of same class –square( 2 ); Dot operator - used with objects –g.drawLine( x1, y1, x2, y2 ); Dot operator - used with static methods of classes –Integer.parseInt( myString ); –More Chapter 26

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.4Method Definitions (IV) More GUI components –Content Pane - on-screen display area Attach GUI components to it to be displayed Object of class Container ( java.awt ) –getContentPane Method inherited from JApplet Returns reference to Content Pane Container c = getContentPane(); –Container method add Attaches GUI components to content pane, so they can be displayed For now, only attach one component (occupies entire area) Later, learn how to add and layout multiple components c.add( myTextArea );

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SquareInt.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SquareInt.java (Part 2 of 2) Program Output

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages As we have seen –Java has predefined, grouped classes called packages –Together, all the packages are the Applications Programming Interface (API) –Fig has a list of the packages in the API Import –Import statements specify location of classes –Large number of classes, avoid reinventing the wheel

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (II)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (III)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (IV)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (V)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (VI)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (VII)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.5Java API Packages (VIII)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.6Random Number Generation Math.random() –Returns a random double, greater than or equal to 0.0, less than 1.0 Scaling and shifting n = a + (int) ( Math.random() * b ) n = random number a = shifting value b = scaling value In C we used %, but in Java we can use * For a random number between 1 and 6, n = 1 + (int) ( Math.random() * 6 )

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. RandomInt.java Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. RollDie.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. RollDie.java (Part 1 of 2) Program Output

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.7Example: A Game of Chance Redo "craps" simulator from Chapter 5 Rules –Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4, 5, 6, 8, 9, 10 - value becomes player's "point" –player must roll his point before rolling 7 to win

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.7Example: A Game of Chance (II) User input –Till now, used message dialog and input dialog Tedious, only show one message/ get one input at a time –Now, we will use event handling for more complex GUI extends keyword –Class inherits data and methods from another class –A class can also implement an interface Keyword implements Interface - specifies methods you must define in your class Event handling –Event: user interaction (i.e., user clicking a button) –Event handler: method called in response to an event

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.7Example: A Game of Chance (III) Interface ActionListener –Requires that you define method actionPerformed actionPerformed is the event handler Class JTextField –Can display or input a line of text Class JButton –Displays a button which can perform an action if pushed –Method addActionListener( this ); Specifies this applet should listen for events from the JButton object –Each component must know which method will handle its events Registering the event handler

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.7Example: A Game of Chance (IV) Class JButton (continued) –We registered this applet with our JButton The applet "listens" for events from the –actionPerformed is the event handler Event-driven programming –User's interaction with GUI drives program final –Defines a variable constant Cannot be modified Must be initialized at definition const int MYINT = 3; Use all uppercase for final variables

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.7Example: A Game of Chance (V) Methods of class Container –Recall that the Content Pane is of class Container –Method setLayout Define layout managers (determine position and size of all components attached to container) FlowLayout - Most basic layout manager –Items placed left to right in order added to container –When end of line reached, continues on next line c = getContentPane(); c.setLayout( new FlowLayout() ); Initialized with object of class FlowLayout

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Craps.java (Pat 1 of 5)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Craps.java (Pat 2 of 5)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Craps.java (Pat 3 of 5)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Craps.java (Pat 4 of 5)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Craps.java (Pat 5 of 5)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.8Methods of Class JApplet Methods of Class JApplet –init, start, stop, paint, destroy –Called automatically during execution –By default, have empty bodies –Must define yourself, using proper first line Otherwise, will not be called automatically See Figure for proper first lines Method repaint –Dynamically change appearance of applet –Cannot call paint (do not have a Graphics object) –repaint(); calls update which passes Graphics object for us Erases previous drawings and calls paint

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.8Methods of Class JApplet (II) First line of JApplet methods (descriptions Fig ) public void init() public void start() public void paint( Graphics g ) public void stop() public void destroy()

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25.9Defining and Allocating Arrays Arrays –Specify type, use new operator –Two steps: int c[]; //definition c = new int[ 12 ]; //initialization –One step: int c[] = new int[12]; –Primitive elements initialized to zero or false Non-primitive references are null –Multiple definitions: String b[] = new String[ 100 ], x[] = new String[ 27 ]; Also: double[] array1, array2; Put brackets after data type

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Examples Using Arrays new –Dynamically creates arrays Method length –Returns length of the array myArray.length Initializer lists int myArray[] = { 1, 2, 3, 4, 5 }; new operator not needed, provided automatically

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java (Part 2 of 2) Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. SumArray.java Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentPoll.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentPoll.java (Part 2 of 2) Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Histogram.java

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. RollDie.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. RollDie.java (Part 2 of 2) Program Output

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved References and Reference Parameters Passing arguments to methods –Call-by-value: pass copy of argument –Call-by-reference: pass original argument Improve performance, weaken security In Java, cannot choose how to pass arguments –Primitive data types passed call-by-value –References to objects passed call-by-reference Original object can be changed in method –Arrays in Java treated as objects Passed call-by-reference

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Multiple-Subscripted Arrays Multiple-Subscripted Arrays –Represent tables Arranged by m rows and n columns (m by n array) Can have more than two subscripts –Java does not support multiple subscripts directly Create an array with arrays as its elements Array of arrays Definition –Double brackets int b[][]; b = new int[ 3 ][ 3 ]; Defines a 3 by 3 array

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Multiple-Subscripted Arrays (II) Definition (continued) –Initializer lists int b[][] = { { 1, 2 }, { 3, 4 } }; –Each row can have a different number of columns: int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate columns for row 0 b[ 1 ] = new int[ 3 ]; // allocate columns for row 1 –Notice how b[ 0 ] is initialized as a new int array 12 34

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InitArray.java (Part 2 of 2) Program Output