`i
`
`' J •
`
`;
`i ' , '
`
`•. ;
`
`'
`:
`
`i · J l / :·
`
`I ;
`
`•
`
`f
`' t 1
`.
`
`f J t' J • l I J 1
`' ~
`
`I
`f
`
`PALO ALTO NETWORKS Exhibit 1025 Page 1
`
`
`
`Editor-in-Chief
`Associate Editor
`Production Manager
`Production Editor
`·
`Marketing Manager
`Compositor
`Technical Artist
`Copyeditor
`Text Design
`Indexer
`Proofreading
`Cover Designer
`
`Lynne Doran Cote
`Deborah Lafferty
`Karen Wernholm
`Amy Willcutt
`Tom Ziolkowski
`Michael and Sigrid Wile
`George Nichols
`Roberta Lewis
`Ron Kosciak
`Nancy Fulton
`Phyllis Coyne et al.
`Diana Coe
`
`Library of Congress Cataloging-in-Publication Data
`Lewis, John, Ph.D.
`Java software solutions : foundation of program design I John
`Lewis, William Loftus.
`p.
`em.
`Includes index.
`ISBN 0-201-57164-1
`1. Java (Computer program language
`2. Object-oriented
`programming (Computer science)
`I. Loftus, William.
`II. Title.
`QA76.73.J38L49 1998
`005 .13 '3--dc21
`
`97-19400
`CIP
`
`Many of the designations used by manufa rurers and sellers to distinguish their
`products are claimed as trademarks.
`here tho e designations appear in this book
`and Addison-Wesley was aware of a trademark claim, the designations have been
`printed in initial caps or all caps.
`
`Cover image© Jerry Blank/SIS
`
`Access the latest information about Addison-Wesley titles from our World Wide
`Web site: http://www.awl.com/cseng
`Reprinted with corrections, January 1998.
`
`Copyright© 1998 by Addison Wesley Longman, Inc.
`
`All rights reserved. No part of this publication may be reproduced, stored in a
`retrieval system, or transmitted, in any form or by any means, electronic, mechani(cid:173)
`cal, photocopying, recording, or otherwise, without the prior written permission of
`the publisher. Printed in the United States of America.
`
`6 7 8 9 10-MA-01009998
`
`Pre I
`
`We have design
`language. It ser
`for pursuing ad
`with object-orif
`ment high-qual
`This text v
`gramming and
`ground up, witl
`emerged in mid
`Web effects. Ov
`object-oriented
`discovered that
`ing concepts wr
`In response
`nary version. Si
`ing topics to f
`students better ~
`embrace Java 1.
`earlier version, i
`
`Object-Orien1
`
`We introduce ol
`out. We have fc
`they are present
`
`PALO ALTO NETWORKS Exhibit 1025 Page 2
`
`
`
`4.4 Defining Methods
`
`We've already used many methods in various programs and we know that methods
`are part of a class. Let's now examine method definitions closely in preparation for
`defining our own classes.
`A method is a group of programming language statements that are given a
`name. A method is associated with a particular class. Each method has a method
`definition that specifies the code that gets executed when the method is invoked.
`We've invoked the println method many times, but because we didn't write
`println, we have not been concerned about its definition. We call it assuming tha
`it will do its job reliably.
`When a method is called, the flow of control transfers to that method. One by
`one, the statements of that method are executed. When that method is done, control
`returns to the location where the call was made and execution continues. Thi
`process is pictured in Fig. 4.4.
`We've defined the main method of a program many times. Its definition fol(cid:173)
`lows the same syntax as all methods:
`
`return-type method-name
`statement - list
`
`( parameter-list ) {
`
`The header of a method includes the type of the return value, the method name, an
`a list of parameters that the method accepts. The list of statements that makes up the
`
`methodl
`
`--- ~ ...
`
`method2( ); -
`
`-
`--- ,
`I
`I
`L _ ____ J
`
`mam
`
`I
`I
`
`I •
`,---
`
`methodl(); -
`
`1
`I
`I
`I
`
`I ...
`
`method2
`
`~ ----- -~
`I
`I
`I
`I
`I
`
`t
`
`-
`- --1
`I
`I
`I
`I
`I
`
`dy of the method are
`ethod called third_!
`
`_,t third_power (int
`int cube;
`cube = number * n
`return cube;
`II method third_p
`
`A method may dec
`- at method. The vari
`ethod. Local variable:
`-her methods of the s:
`- e main method. The:
`.:. not exist except wh
`a! variable is lost fn
`alue to be maintained
`- .e class level. The dec
`- , but it must be ded
`
`Key Concept A var
`used outside of it.
`
`he return sta1
`
`-
`
`ethods can return a
`rhod header. The re
`en a method does n<
`, as is always done
`the method header. 1
`
`eturn;
`
`Figure 4.4 The flow of control following method invocations
`
`eturn ex pressio
`
`134 Chapter 4 Objects and Classes
`
`PALO ALTO NETWORKS Exhibit 1025 Page 3
`
`
`
`ethods
`on for
`
`iven a
`zethod
`roked.
`write
`tg that
`
`lne by
`Jntrol
`This
`
`n fol-
`
`:, and
`!p the
`
`ody of the method are defined in a block. The following code is the definition of a
`method called third_power:
`
`~nt third_power (int number )
`int cube;
`cube = number * number * number;
`return cube;
`II met hod t hird_power
`
`A method may declare local variables in the body of the method for use only in
`that method. The variable cube in the third_power method is local to that
`method. Local variables cannot be accessed from outside of the method, even from
`other methods of the same class. In previous examples we've declared variables in
`the main method. These variables were local to the main method. Local variables
`do not exist except when the method is executing; therefore the value stored in a
`local variable is lost from one invocation of the method to the next. If you ~ant a
`value to be maintained from one call to the next, you should define the variable at
`the class level. The declaration of a local variable can be mixed into the statement
`list, but it must be declared before it is used.
`
`Key Concept A variable declared in a method is local to that method and cannot be
`used outside of it.
`
`The return statement
`
`Methods can return a value, whose type must correspond to the return type in the
`method header. The return type can be a primitive type or a reference to an object.
`When a method does not return any value, the reserved word void is used as the return
`type, as is always done with the main method. A return type must always be specified
`in the method header. The return statement in a method can take one of two forms:
`
`return;
`
`or
`
`return expression;
`
`4.4 Defining Methods 135
`
`PALO ALTO NETWORKS Exhibit 1025 Page 4
`
`
`
`The first form causes the processing flow to return to the calling location without
`returning a value. The second form returns to the calling method and specifies the
`value that is to be returned. If a return type other than void is specified in the
`method header, then the Java compiler insists that a return statement exist in the
`program and that a value of the proper type is returned.
`
`Key Concept A method must return a value consistent with the return type specified in
`the method header.
`
`The following code is another way to define the third_power method, per(cid:173)
`forming a calculation in the expression of the return statement. This modification
`eliminates the need for the local variable.
`
`int t h i r d_power (int number)
`r e turn (numbe r * number * number);
`II me thod t hird_powe r
`
`If there is no return statement in a method, processing continues until the end
`of the method is reached. If there is a return statement, then processing is stopped
`for that method when the return statement is executed, and control is returned to
`the statement that invoked the method.
`It is usually not good practice to use more than one return statement in a
`method even though it is possible to do so. In general, a method should have one
`return statement as the last line of the method body unless it makes the method
`overly complex.
`
`Parameters
`
`A parameter is a value that is passed into a method when it is invoked. The parame(cid:173)
`ter list in the header of a method specifies the types of the values that are passed and
`the names by which the called method will refer to the parameters in the method
`definition. In the method d~finition, the names of the parameters accepted are called
`formal parameters. In the invocations, the values passed into a method are called
`actual parameters. A method invocation and definition always specify the parameter
`list in parentheses after the method name. If there are no parameters, an empty set of
`parentheses are used.
`The formal parameters are identifiers that essentially act as local variables for
`the method and whose initial value comes from the calling method. Actual parame-
`
`rers can be literals,
`passed as the paran
`
`publ ic void acid.
`String title
`genera te_repo:
`I I method aci•
`
`This example is a
`mvoking another r
`ac id_t e s t are s ·
`_ ote that sub s ta
`method invocation
`m
`a c id_tes t ,
`g enera te_rep or
`When primitiv1
`'ll.eter. When objects
`-o the actual param
`arameter becomes
`Java, primitive data
`
`Key Concept A
`Therefore the actu
`
`Let's look at a1
`.\'e have a class Nun
`~ram demonstrates
`
`Demonstra tes 1
`=lass Paramete r_]
`
`public static
`(in t forma:
`Num forma :
`
`System . out
`System. out
`System. out
`System.out
`
`136 Chapter 4 Objects and Classes
`
`PALO ALTO NETWORKS Exhibit 1025 Page 5
`
`
`
`thout
`:s the
`n the
`n the
`
`edin
`
`per(cid:173)
`tion
`
`:nd
`Jed
`' to
`
`1 a
`1ne
`od
`
`e(cid:173)
`td
`ld
`:d
`d
`
`ters can be literals, variables, or full expressions that are evaluated and the result
`passed as the parameter. Let's look at an example:
`
`public void acid_test (int substance!, float substance2)
`String title= "Acid Test Order Form";
`generate_report (title, substance!, substance2);
`II method acid_test
`
`This example is a method called acid_test . The method generates a report by
`invoking another method called generate_report. The forma l parameters for
`acid_test are substance! and substance2, as listed in the parameter list.
`ote that substance! and substance2 also serve as actual parameters to the
`method invocation of generate_report . The variable title is a local variable
`serves as
`an actual parameter
`for
`the call
`in acid_test, and
`to
`generate_report .
`When primitive data is passed, a copy of the value is assigned to the actual para(cid:173)
`meter. When objects are passed, a copy of the reference to the original object is assigned
`to the actual parameter. Therefore when an object is passed as a parameter, the formal
`parameter becomes an alias of the actual parameter. Another way to say this is that, in
`Java, primitive data is passed by value and objects are passed by reference.
`
`Key Concept An object is passed by reference when it is used as a parameter.
`Therefore the actual parameter and the formal parameter are aliases of each other.
`
`Let's look at an example that tests the issue of parameter passing. Ass ume that
`we have a class Num that contains an int variable called value. The following pro(cid:173)
`gram demonstrates passing the various data types:
`
`II Demonstrates the effects possible using parameter passing .
`class Parameter_Passing {
`
`public static void change_values
`(int formal!, int formal2, Nurn formal3,
`Nurn formal4, Nurn formalS)
`
`Systern.out.println();
`Systern.out.println ( "Before changing values");
`Systern.out.println ("Formal parameter 1 : " + formall);
`Systern.out.println ("Formal parameter 2: " + formal2);
`
`4.4 Defining Methods 137
`
`PALO ALTO NETWORKS Exhibit 1025 Page 6
`
`
`
`"The book is a good one. Examples have been kept simple, focused, and
`useful. This is important to hold a student's attention. The book is based
`on JDK 1.1, which is a more mature API than the earlier version. It does
`a good job of introducing programming principles in java."
`-Vijay Srinivasan,JavaSoft, Sun Microsystems, Inc.
`
`john Lewis, Villanova University
`William Loftus, WPL Laboratories, Inc.
`
`Using Java 1.1, java Software Solutions teaches
`beginning programmers how to design and
`implement high-quality object-oriented soft(cid:173)
`ware. The authors emphasize problem solving
`through understanding requirements, exploring
`options, and designing conceptually clean solu(cid:173)
`tions. John Lewis and Bill Loftus wrote this
`book from the ground up, taking full advantage
`of Java to teach introductory programming.
`Throughout the book, the authors intertwine the
`use of applets and applications demonstrate
`computing concepts. Applets are introduced
`early, building on the excitement of the web,
`while applications help readers gain a clear
`understanding of programming concepts.
`
`HIGHLIGHTS
`• Presents objects early and reinforces the
`design principles of object-oriented program(cid:173)
`ming throughout
`
`• Combines small, readily understandable
`examples with larger, practical ones to explore
`a variety of programs
`
`• Provides a diverse wealth of self-review
`questions, exercises, programming projects
`and clear chapter objectives
`
`• Contains Java reference material, including
`15 appendices that contain style guidelines
`and more
`
`~~ADDISON-WESLEY
`
`Addison-Wesley is an Imprint
`of Addison Wesley Longman, Inc.
`
`ABOUT THE AUTHORS
`John Lewis is an Assistant Professor of Computer
`Science at Villanova University. Dr. Lewis received
`his Ph.D. from Virginia Tech in 1991. His area of
`specialization is software engineering, and he reg(cid:173)
`ularly teaches courses in introductory computing,
`software engineering, object-oriented design, oper(cid:173)
`ating systems, and algorithms & data structures.
`Dr. Lewis is a member of the ACM, the IEEE
`Computer Society, and Sigma Xi, the scientific
`research society. He is also the Conference Chair
`for the 1998 SIGCSE Technical Symposium.
`William Loftus is the Technical Director of WPL
`Laboratories, Inc. where he directs many software
`development and research projects. He is a highly
`respected expert in the areas of object-oriented
`systems development and software engineering.
`e 500
`He has personally consulted tom
`"Y U ~· U b l UKt
`•
`•
`~ 1, ,'
`9 42
`
`pute~
`
`§ \ "\~\1~\\(~\\\~\\\~n~l \\, Am··
`
`102
`· -"""
`
`~...
`. -201-.~71~ ~- 3 .
`.
`on a ut Addison-Wesley
`
`PALO ALTO NETWORKS Exhibit 1025 Page 7