Java Assignment2
ISSLJC-SETUP-ALIEN-PAREU-BEGUN-LIKES
Digital Resources for Students
Your new textbook provides 12-month access to digital resources that may include VideoNotes (step-by-step video tutorials on programming concepts), source code, web chapters, quizzes, and more. Refer to the preface in the textbook for a detailed list of resources.
Follow the instructions below to register for the Companion Website for Daniel Liang’s Introduction to Java™ Programming and Data Structures, Comprehensive Version, Eleventh Edition, Global Edition.
1. Go to www.pearsonglobaleditions.com/liang 2. Enter the title of your textbook or browse by author name. 3. Click Companion Website. 4. Click Register and follow the on-screen instructions to create a login name and password.
Use a coin to scratch off the coating and reveal your access code. Do not use a sharp knife or other sharp object as it may damage the code.
Use the login name and password you created during registration to start using the digital resources that accompany your textbook.
IMPORTANT: This prepaid subscription does not include access to MyProgrammingLab, which is available at www.myprogramminglab.com for purchase.
This access code can only be used once. This subscription is valid for 12 months upon activation and is not transferable. If the access code has already been revealed it may no longer be valid.
For technical support go to https://support.pearson.com/getsupport
Liang_11_1292221879_ifc_Final.indd 1 17/11/17 9:19 PM
Digital Resources for Students
Your new textbook provides 12-month access to digital resources that may include VideoNotes (step-by-step video tutorials on programming concepts), source code, web chapters, quizzes, and more. Refer to the preface in the textbook for a detailed list of resources.
Follow the instructions below to register for the Companion Website for Daniel Liang’s Introduction to Java™ Programming and Data Structures, Comprehensive Version, Eleventh Edition, Global Edition.
1. Go to www.pearsonglobaleditions.com/liang 2. Enter the title of your textbook or browse by author name. 3. Click Companion Website. 4. Click Register and follow the on-screen instructions to create a login name and password.
Use a coin to scratch off the coating and reveal your access code. Do not use a sharp knife or other sharp object as it may damage the code.
Use the login name and password you created during registration to start using the digital resources that accompany your textbook.
IMPORTANT: This prepaid subscription does not include access to MyProgrammingLab, which is available at www.myprogramminglab.com for purchase.
This access code can only be used once. This subscription is valid for 12 months upon activation and is not transferable. If the access code has already been revealed it may no longer be valid.
For technical support go to https://support.pearson.com/getsupport
Liang_11_1292221879_ifc_Final.indd 1 17/11/17 9:19 PM Digital_Resources_for_Students.indd 1 1/17/18 8:14 PM
IntroductIon to
Java ProgrammIng and
data StructureS comPrehenSIve verSIon
Eleventh Edition
Global Edition
Y. daniel Liang Armstrong State University
™
330 Hudson Street, NY NY 10013
A01_LIAN1878_11_GE_FM.indd 1 1/2/18 11:57 PM
To Samantha, Michael, and Michelle
Java™ and Netbeans™ screenshots ©2017 by Oracle Corporation, all rights reserved. Reprinted with permission. Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on the appropriate page within text. Microsoft and/or its respective suppliers make no representations about the suit- ability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided “as is” without warranty of any kind. Microsoft and/ or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall Microsoft and/or its respective suppliers be liable for any special, indi- rect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include techni- cal inaccuracies or typographical errors. Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified.
Pearson Education Limited KAO Two KAO Park Harlow CM17 9NA United Kingdom
and Associated Companies throughout the world
Visit us on the World Wide Web at: www.pearsonglobaleditions.com
© Pearson Education Limited 2019
The rights of Y. Daniel Liang to be identified as the author of this work have been asserted by him in accordance with the Copyright, Designs and Patents Act 1988.
Authorized adaptation from the United States edition, entitled Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition, ISBN 978-0-13-467094-2 by Y. Daniel Liang, published by Pearson Education © 2018.
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, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS.
All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners.
British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library
10 9 8 7 6 5 4 3 2 1
Typeset by SPi Global ISBN-10: 1-292-22187-9
Printed and bound by Vivar in Malaysia ISBN-13: 978-1-292-22187-8
Senior Vice President Courseware Portfolio Management: Marcia J. Horton
Director, Portfolio Management: Engineering, Computer Science & Global Editions: Julian Partridge
Higher Ed Portfolio Management: Tracy Johnson (Dunkelberger)
Portfolio Management Assistant: Kristy Alaura Managing Content Producer: Scott Disanno Content Producer: Robert Engelhardt Web Developer: Steve Wright Assistant Acquisitions Editor, Global Edition:
Aditee Agarwal Assistant Project Editor, Global Edition: Shaoni
Mukherjee
Manager, Media Production, Global Edition: Vikram Kumar
Senior Manufacturing Controller, Production, Global Edition: Jerry Kataria
Rights and Permissions Manager: Ben Ferrini Manufacturing Buyer, Higher Ed, Lake Side
Communications Inc (LSC): Maura Zaldivar-Garcia Inventory Manager: Ann Lam Marketing Manager: Demetrius Hall Product Marketing Manager: Bram Van Kempen Marketing Assistant: Jon Bryant Cover Designer: Lumina Datamatics Cover Image: Eduardo Rocha/ shutterstock.com Full-Service Project Management: Shylaja Gattupalli,
SPi Global
A01_LIAN1878_11_GE_FM.indd 2 1/2/18 11:57 PM
3
Dear Reader,
Many of you have provided feedback on earlier editions of this book, and your comments and suggestions have greatly improved the book. This edition has been substantially enhanced in presentation, organization, examples, exercises, and supplements.
The book is fundamentals first by introducing basic programming concepts and techniques before designing custom classes. The fundamental concepts and techniques of selection statements, loops, methods, and arrays are the foundation for programming. Building this strong foundation prepares students to learn object-oriented programming and advanced Java programming.
This book teaches programming in a problem-driven way that focuses on problem solv- ing rather than syntax. We make introductory programming interesting by using thought- provoking problems in a broad context. The central thread of early chapters is on problem solving. Appropriate syntax and library are introduced to enable readers to write programs for solving the problems. To support the teaching of programming in a problem-driven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. To appeal to students in all majors, the problems cover many application areas, including math, science, business, financial, gaming, animation, and multimedia.
The book seamlessly integrates programming, data structures, and algorithms into one text. It employs a practical approach to teach data structures. We first introduce how to use various data structures to develop efficient algorithms, and then show how to implement these data structures. Through implementation, students gain a deep understanding on the efficiency of data structures and on how and when to use certain data structures. Finally, we design and implement custom data structures for trees and graphs.
The book is widely used in the introductory programming, data structures, and algorithms courses in the universities around the world. This comprehensive version covers fundamen- tals of programming, object-oriented programming, GUI programming, data structures, algo- rithms, concurrency, networking, database, and Web programming. It is designed to prepare students to become proficient Java programmers. A brief version (Introduction to Java Pro- gramming, Brief Version, Eleventh Edition, Global Edition) is available for a first course on programming, commonly known as CS1. The brief version contains the first 18 chapters of the comprehensive version.
The best way to teach programming is by example, and the only way to learn programming is by doing. Basic concepts are explained by example and a large number of exercises with various levels of difficulty are provided for students to practice. For our programming courses, we assign programming exercises after each lecture.
Our goal is to produce a text that teaches problem solving and programming in a broad context using a wide variety of interesting examples. If you have any comments on and suggestions for improving the book, please email me.
Sincerely,
Y. Daniel Liang y.daniel.liang@gmail.com www.pearsonglobaleditions.com/Liang
fundamentals-first
problem-driven
data structures
comprehensive version
brief version
Preface
A01_LIAN1878_11_GE_FM.indd 3 1/2/18 11:57 PM
4 Preface
ACM/IEEE Curricular 2013 and ABET Course Assessment The new ACM/IEEE Computer Science Curricular 2013 defines the Body of Knowledge organized into 18 Knowledge Areas. To help instructors design the courses based on this book, we provide sample syllabi to identify the Knowledge Areas and Knowledge Units. The sample syllabi are for a three semester course sequence and serve as an example for institutional customization. The sample syllabi are accessible from the Instructor Resource Center.
Many of our users are from the ABET-accredited programs. A key component of the ABET accreditation is to identify the weakness through continuous course assessment against the course outcomes. We provide sample course outcomes for the courses and sam- ple exams for measuring course outcomes on the Instructor Resource Center.
What’s New in This Edition? This edition is completely revised in every detail to enhance clarity, presentation, content, examples, and exercises. The major improvements are as follows:
■■ The book’s title is changed to Introduction to Java Programming and Data Structures with new enhancements on data structures. The book uses a practical approach to introduce design, implement, and use data structures and covers all topics in a typical data structures course. Additionally, it provides bonus chapters that cover advanced data structures such as 2-4 trees, B-trees, and red-black trees.
■■ Updated to the latest Java technology. Examples and exercises are improved and simplified by using the new features in Java 8.
■■ The default and static methods are introduced for interfaces in Chapter 13.
■■ The GUI chapters are updated to JavaFX 8. The examples are revised. The user interfaces in the examples and exercises are now resizable and displayed in the center of the window.
■■ Inner classes, anonymous inner classes, and lambda expressions are covered using practi- cal examples in Chapter 15.
■■ More examples and exercises in the data structures chapters use lambda expressions to simplify coding. Method references are introduced along with the Comparator interface in Section 20.6.
■■ The forEach method is introduced in Chapter 20 as a simple alternative to the foreach loop for applying an action to each element in a collection.
■■ Use the default methods for interfaces in Java 8 to redesign and simplify MyList, MyArrayList, MyLinkedList, Tree, BST, AVLTree, MyMap, MyHashMap, MySet, MyHashSet, Graph, UnweightedGraph, and WeightedGraph in Chapters 24–29.
■■ Chapter 30 is brand new to introduce aggregate operations for collection streams.
■■ FXML and the Scene Builder visual tool are introduced in Chapter 31.
■■ The Companion Website has been redesigned with new interactive quiz, CheckPoint ques- tions, animations, and live coding.
■■ More than 200 additional programming exercises with solutions are provided to the instructor on the Instructor Resource Center. These exercises are not printed in the text.
A01_LIAN1878_11_GE_FM.indd 4 1/2/18 11:57 PM
Preface 5
Pedagogical Features The book uses the following elements to help students get the most from the material:
■■ The Objectives at the beginning of each chapter list what students should learn from the chapter. This will help them determine whether they have met the objectives after completing the chapter.
■■ The Introduction opens the discussion with a thought-provoking question to motivate the reader to delve into the chapter.
■■ Key Points highlight the important concepts covered in each section.
■■ Check Points provide review questions to help students track their progress as they read through the chapter and evaluate their learning.
■■ Problems and Case Studies, carefully chosen and presented in an easy-to-follow style, teach problem solving and programming concepts. The book uses many small, simple, and stimulating examples to demonstrate important ideas.
■■ The Chapter Summary reviews the important subjects that students should understand and remember. It helps them reinforce the key concepts they have learned in the chapter.
■■ Quizzes are accessible online, grouped by sections, for students to do self-test on programming concepts and techniques.
■■ Programming Exercises are grouped by sections to provide students with opportunities to apply the new skills they have learned on their own. The level of difficulty is rated as easy (no asterisk), moderate (*), hard (**), or challenging (***). The trick of learning program- ming is practice, practice, and practice. To that end, the book provides a great many exer- cises. Additionally, more than 200 programming exercises with solutions are provided to the instructors on the Instructor Resource Center. These exercises are not printed in the text.
■■ Notes, Tips, Cautions, and Design Guides are inserted throughout the text to offer valuable advice and insight on important aspects of program development.
Note Provides additional information on the subject and reinforces important concepts.
Tip Teaches good programming style and practice.
Caution Helps students steer away from the pitfalls of programming errors.
Design Guide Provides guidelines for designing programs.
Flexible Chapter Orderings The book is designed to provide flexible chapter orderings to enable GUI, exception handling, recursion, generics, and the Java Collections Framework to be covered earlier or later. The diagram on the next page shows the chapter dependencies.
A01_LIAN1878_11_GE_FM.indd 5 1/2/18 11:57 PM
C ha
pt er
3 9
Ja va
Se rv
er F
ac es
C ha
pt er
4 0
R M
I
C ha
pt er
4 1
W eb
S er
vi ce
s
C ha
pt er
4 4
T es
ti ng
U si
ng J
U ni
t
C ha
pt er
3 8
Ja va
Se rv
er P
ag es
C ha
pt er
2 5
B in
ar y
Se ar
ch T
re es
C ha
pt er
2 6
A V
L T
re es
C ha
pt er
2 9
W ei
gh te
d G
ra ph
s an
d A
pp lic
at io
ns
C ha
pt er
2 8
G ra
ph s
an d
A
pp lic
at io
ns
C ha
pt er
2 1
Se ts
a nd
M ap
s
C ha
pt er
2 2
D ev
el op
pi ng
E
f� ci
en t A
lg or
it hm
s
C ha
pt er
1 I
nt ro
du ct
io n
to
C om
pu te
rs , P
ro gr
am s,
a nd
J
av a
C ha
pt er
2 E
le m
en ta
ry
P ro
gr am
m in
g
C ha
pt er
5 L
oo ps
C ha
pt er
7 S
in gl
e- D
im en
si on
al
A rr
ay s
C ha
pt er
8 M
ul ti
di m
en si
on al
A
rr ay
s
C ha
pt er
4 M
at he
m at
ic al
F
un ct
io ns
, C ha
ra ct
er s,
a
nd S
tr in
gs
P ar
t I : F
un da
m en
ta ls
o f
P
ro gr
am m
in g
C ha
pt er
3 S
el ec
ti on
s
C ha
pt er
9 O
bj ec
ts a
nd C
la ss
es
C ha
pt er
1 7
B in
ar y
I/ O
N ot
e: C
ha pt
er s
1– 18
a re
in th
e br
ie f v
er si
on o
f t hi
s bo
ok .
N ot
e: C
ha pt
er s
1– 30
a re
in th
e co
m pr
eh en
si ve
v er
si on
.
N ot
e: C
ha pt
er s
31 –4
4 ar
e bo
nu s
ch ap
te rs
a va
ila bl
e fr
om th
e C
om pa
ni on
W eb
si te
.
C ha
pt er
1 0
T hi
nk in
g in
O bj
ec ts
C ha
pt er
1 1
In he
ri ta
nc e
an d
P
ol ym
or ph
is m
C ha
pt er
1 2
E xc
ep ti
on
H an
dl in
g a
nd T
ex t I
/O
C ha
pt er
1 3
A bs
tr ac
t C la
ss es
a
nd I
nt er
fa ce
s
C ha
pt er
6 M
et ho
ds
P ar
t I I:
O bj
ec t-
O ri
en te
d
P ro
gr am
m in
g
C ha
pt er
3 2
M ul
ti th
re ad
in g
an d
P ar
al le
l P ro
gr am
m in
g
C ha
pt er
3 6
In te
rn at
io na
liz at
io n
C ha
pt er
3 3
N et
w or
ki ng
C ha
pt er
3 4
Ja va
D at
ab as
e
P
ro gr
am m
in g
C ha
pt er
3 5
A dv
an ce
d D
at ab
as e
P ro
gr am
m in
g
C ha
pt er
3 7
Se rv
le ts
P ar
t V : A
dv an
ce d
Ja va
P
ro gr
am m
in g
C ha
pt er
1 4
Ja va
F X
B as
ic s
C ha
pt er
1 5
E ve
nt -D
ri ve
n
P ro
gr am
m in
g an
d
A ni
m at
io ns
C ha
pt er
2 0
L is
ts , S
ta ck
s, Q
ue ue
s,
a nd
P ri
or it
y Q
ue ue
s C
ha pt
er 1
6 Ja
va F
X C
on tr
ol s
a
nd M
ul ti
m ed
ia
C ha
pt er
3 1
A dv
an ce
d J
av aF
X
a nd
F X
M L
P ar
t I II
: G U
I P
ro gr
am m
in g
C ha
pt er
1 8
R ec
ur si
on C
h 7
C ha
pt er
1 9
G en
er ic
s
C ha
pt er
2 4
Im pl
em en
ti ng
L is
ts ,
S
ta ck
s, Q
ue ue
s, a
nd P
ri or
it y
Q
ue ue
s
P ar
t I V
: D at
a St
ru ct
ur es
a nd
A
lg or
it hm
s
C h
13
C h
16
C h
9
C ha
pt er
3 0
A gg
re ga
te O
pe ra
tio ns
an d
C ol
le ct
io n
St re
am s
C ha
pt er
4 2
2- 4
T re
es a
nd B
-
T re
es
C ha
pt er
4 3
R ed
-B la
ck T
re es
C ha
pt er
2 7
H as
hi ng
C ha
pt er
2 3
So rt
in g
6 Preface
A01_LIAN1878_11_GE_FM.indd 6 1/2/18 11:57 PM
Organization of the Book The chapters can be grouped into five parts that, taken together, form a comprehensive introduc- tion to Java programming, data structures and algorithms, and database and Web programming. Because knowledge is cumulative, the early chapters provide the conceptual basis for under- standing programming and guide students through simple examples and exercises; subsequent chapters progressively present Java programming in detail, culminating with the development of comprehensive Java applications. The appendixes contain a mixed bag of topics, including an introduction to number systems, bitwise operations, regular expressions, and enumerated types.
Part I: Fundamentals of Programming (Chapters 1–8)
The first part of the book is a stepping stone, preparing you to embark on the journey of learning Java. You will begin to learn about Java (Chapter 1) and fundamental programming techniques with primitive data types, variables, constants, assignments, expressions, and operators ( Chapter 2), selection statements (Chapter 3), mathematical functions, characters, and strings (Chapter 4), loops (Chapter 5), methods (Chapter 6), and arrays (Chapters 7–8). After Chapter 7, you can jump to Chapter 18 to learn how to write recursive methods for solving inherently recursive problems.
Part II: Object-Oriented Programming (Chapters 9–13, and 17)
This part introduces object-oriented programming. Java is an object-oriented programming language that uses abstraction, encapsulation, inheritance, and polymorphism to provide great flexibility, modularity, and reusability in developing software. You will learn program- ming with objects and classes (Chapters 9–10), class inheritance (Chapter 11), polymorphism ( Chapter 11), exception handling (Chapter 12), abstract classes (Chapter 13), and interfaces (Chapter 13). Text I/O is introduced in Chapter 12 and binary I/O is discussed in Chapter 17.
Part III: GUI Programming (Chapters 14–16 and Bonus Chapter 31)
JavaFX is a new framework for developing Java GUI programs. It is not only useful for developing GUI programs, but also an excellent pedagogical tool for learning object-oriented programming. This part introduces Java GUI programming using JavaFX in Chapters 14–16. Major topics include GUI basics (Chapter 14), container panes (Chapter 14), drawing shapes (Chapter 14), event-driven programming (Chapter 15), animations (Chapter 15), and GUI controls (Chapter 16), and playing audio and video (Chapter 16). You will learn the architecture of JavaFX GUI programming and use the controls, shapes, panes, image, and video to develop useful applications. Chapter 31 covers advanced features in JavaFX.
Part IV: Data Structures and Algorithms (Chapters 18–30 and Bonus Chapters 42–43)
This part covers the main subjects in a typical data structures and algorithms course. Chapter 18 introduces recursion to write methods for solving inherently recursive problems. Chapter 19 presents how generics can improve software reliability. Chapters 20 and 21 introduce the Java Collection Framework, which defines a set of useful API for data structures. Chapter 22 discusses measur- ing algorithm efficiency in order to choose an appropriate algorithm for applications. Chapter 23 describes classic sorting algorithms. You will learn how to implement several classic data struc- tures lists, queues, and priority queues in Chapter 24. Chapters 25 and 26 introduce binary search trees and AVL trees. Chapter 27 presents hashing and implementing maps and sets using hashing. Chapters 28 and 29 introduce graph applications. Chapter 30 introduces aggregate operations for collection streams. The 2-4 trees, B-trees, and red-black trees are covered in Bonus Chapters 42–43.
Part V: Advanced Java Programming (Chapters 32-41, 44)
This part of the book is devoted to advanced Java programming. Chapter 32 treats the use of multithreading to make programs more responsive and interactive and introduces parallel pro- gramming. Chapter 33 discusses how to write programs that talk with each other from different
Preface 7
A01_LIAN1878_11_GE_FM.indd 7 1/2/18 11:57 PM
hosts over the Internet. Chapter 34 introduces the use of Java to develop database projects. Chapter 35 delves into advanced Java database programming. Chapter 36 covers the use of internationalization support to develop projects for international audiences. Chapters 37 and 38 introduce how to use Java servlets and JavaServer Pages to generate dynamic content from Web servers. Chapter 39 introduces modern Web application development using JavaServer Faces. Chapter 40 introduces remote method invocation and Chapter 41 discusses Web ser- vices. Chapter 44 introduces testing Java programs using JUnit.
Appendixes
This part of the book covers a mixed bag of topics. Appendix A lists Java keywords. Appendix B gives tables of ASCII characters and their associated codes in decimal and in hex. Appen- dix C shows the operator precedence. Appendix D summarizes Java modifiers and their usage. Appendix E discusses special floating-point values. Appendix F introduces number systems and conversions among binary, decimal, and hex numbers. Finally, Appendix G introduces bitwise operations. Appendix H introduces regular expressions. Appendix I covers enumerated types.
Java Development Tools You can use a text editor, such as the Windows Notepad or WordPad, to create Java programs and to compile and run the programs from the command window. You can also use a Java development tool, such as NetBeans or Eclipse. These tools support an integrated develop- ment environment (IDE) for developing Java programs quickly. Editing, compiling, building, executing, and debugging programs are integrated in one graphical user interface. Using these tools effectively can greatly increase your programming productivity. NetBeans and Eclipse are easy to use if you follow the tutorials. Tutorials on NetBeans and Eclipse can be found in the supplements on the Companion Website www.pearsonglobaleditions.com/Liang.
Student Resources The Companion Website (www.pearsonglobaleditions.com/Liang) contains the following resources:
■■ Answers to CheckPoint questions
■■ Solutions to majority of even-numbered programming exercises
■■ Source code for the examples in the book
■■ Interactive quiz (organized by sections for each chapter)
■■ Supplements
■■ Debugging tips
■■ Video notes
■■ Algorithm animations
Supplements The text covers the essential subjects. The supplements extend the text to introduce additional topics that might be of interest to readers. The supplements are available from the Companion Website.
IDE tutorials
8 Preface
A01_LIAN1878_11_GE_FM.indd 8 1/2/18 11:57 PM
Instructor Resources The Companion Website, accessible from www.pearsonglobaleditions.com/Liang, contains the following resources:
■■ Microsoft PowerPoint slides with interactive buttons to view full-color, syntax-highlighted source code and to run programs without leaving the slides.
■■ Solutions to a majority of odd-numbered programming exercises.
■■ More than 200 additional programming exercises and 300 quizzes organized by chapters. These exercises and quizzes are available only to the instructors. Solutions to these exercises and quizzes are provided.
■■ Web-based quiz generator. (Instructors can choose chapters to generate quizzes from a large database of more than two thousand questions.)
■■ Sample exams. Most exams have four parts:
■■ Multiple-choice questions or short-answer questions
■■ Correct programming errors
■■ Trace programs
■■ Write programs
■■ Sample exams with ABET course assessment.
■■ Projects. In general, each project gives a description and asks students to analyze, design, and implement the project.
Some readers have requested the materials from the Instructor Resource Center. Please understand that these are for instructors only. Such requests will not be answered.
Online Practice and Assessment with MyProgrammingLab MyProgrammingLab helps students fully grasp the logic, semantics, and syntax of program- ming. Through practice exercises and immediate, personalized feedback, MyProgrammingLab improves the programming competence of beginning students who often struggle with the basic concepts and paradigms of popular high-level programming languages.
A self-study and homework tool, a MyProgrammingLab course consists of hundreds of small practice problems organized around the structure of this textbook. For students, the system auto- matically detects errors in the logic and syntax of their code submissions and offers targeted hints that enable students to figure out what went wrong—and why. For instructors, a comprehensive gradebook tracks correct and incorrect answers and stores the code inputted by students for review.
MyProgrammingLab is offered to users of this book in partnership with Turing’s Craft, the makers of the CodeLab interactive programming exercise system. For a full demonstration, to see feedback from instructors and students, or to get started using MyProgrammingLab in your course, visit www.myprogramminglab.com.
Video Notes We are excited about the new Video Notes feature that is found in this new edition. These videos provide additional help by presenting examples of key topics and showing how to solve problems completely from design through coding. Video Notes are available from www.pearsonglobaleditions.com/Liang.
VideoNote
Preface 9
A01_LIAN1878_11_GE_FM.indd 9 04/01/18 6:28 pm
Algorithm Animations We have provided numerous animations for algorithms. These are valuable pedagogical tools to demonstrate how algorithms work. Algorithm animations can be accessed from the Com- panion Website.
Acknowledgments I would like to thank Armstrong State University for enabling me to teach what I write and for supporting me in writing what I teach. Teaching is the source of inspiration for continuing to improve the book. I am grateful to the instructors and students who have offered comments, suggestions, corrections, and praise. My special thanks go to Stefan Andrei of Lamar Univer- sity and William Bahn of University of Colorado Colorado Springs for their help to improve the data structures part of this book.
This book has been greatly enhanced thanks to outstanding reviews for this and previous edi- tions. The reviewers are: Elizabeth Adams (James Madison University), Syed Ahmed (North Georgia College and State University), Omar Aldawud (Illinois Institute of Technology), Ste- fan Andrei (Lamar University), Yang Ang (University of Wollongong, Australia), Kevin Bierre (Rochester Institute of Technology), Aaron Braskin (Mira Costa High School), David Champion (DeVry Institute), James Chegwidden (Tarrant County College), Anup Dargar (University of North Dakota), Daryl Detrick (Warren Hills Regional High School), Charles Dierbach (Towson Univer- sity), Frank Ducrest (University of Louisiana at Lafayette), Erica Eddy (University of Wisconsin at Parkside), Summer Ehresman (Center Grove High School), Deena Engel (New York University), Henry A. Etlinger (Rochester Institute of Technology), James Ten Eyck (Marist College), Myers Foreman (Lamar University), Olac Fuentes (University of Texas at El Paso), Edward F. Gehringer (North Carolina State University), Harold Grossman (Clemson University), Barbara Guillot (Loui- siana State University), Stuart Hansen (University of Wisconsin, Parkside), Dan Harvey (Southern Oregon University), Ron Hofman (Red River College, Canada), Stephen Hughes (Roanoke Col- lege), Vladan Jovanovic (Georgia Southern University), Deborah Kabura Kariuki (Stony Point High School), Edwin Kay (Lehigh University), Larry King (University of Texas at Dallas), Nana Kofi (Langara College, Canada), George Koutsogiannakis (Illinois Institute of Technology), Roger Kraft (Purdue University at Calumet), Norman Krumpe (Miami University), Hong Lin (DeVry Institute), Dan Lipsa (Armstrong State University), James Madison (Rensselaer Polytechnic Insti- tute), Frank Malinowski (Darton College), Tim Margush (University of Akron), Debbie Masada (Sun Microsystems), Blayne Mayfield (Oklahoma State University), John McGrath (J.P. McGrath Consulting), Hugh McGuire (Grand Valley State), Shyamal Mitra (University of Texas at Austin), Michel Mitri (James Madison University), Kenrick Mock (University of Alaska Anchorage), Frank Murgolo (California State University, Long Beach), Jun Ni (University of Iowa), Benjamin Nystuen (University of Colorado at Colorado Springs), Maureen Opkins (CA State University, Long Beach), Gavin Osborne (University of Saskatchewan), Kevin Parker (Idaho State University), Dale Par- son (Kutztown University), Mark Pendergast (Florida Gulf Coast University), Richard Povinelli (Marquette University), Roger Priebe (University of Texas at Austin), Mary Ann Pumphrey (De Anza Junior College), Pat Roth (Southern Polytechnic State University), Amr Sabry (Indiana Uni- versity), Ben Setzer (Kennesaw State University), Carolyn Schauble (Colorado State University), David Scuse (University of Manitoba), Ashraf Shirani (San Jose State University), Daniel Spiegel (Kutztown University), Joslyn A. Smith (Florida Atlantic University), Lixin Tao (Pace University), Ronald F. Taylor (Wright State University), Russ Tront (Simon Fraser University), Deborah Trytten (University of Oklahoma), Michael Verdicchio (Citadel), Kent Vidrine (George Washington Uni- versity), and Bahram Zartoshty (California State University at Northridge).
It is a great pleasure, honor, and privilege to work with Pearson. I would like to thank Tracy Johnson and her colleagues Marcia Horton, Demetrius Hall, Yvonne Vannatta, Kristy Alaura, Carole Snyder, Scott Disanno, Bob Engelhardt, Shylaja Gattupalli, and their colleagues for organizing, producing, and promoting this project.
As always, I am indebted to my wife, Samantha, for her love, support, and encouragement.
Animation
10 Preface
A01_LIAN1878_11_GE_FM.indd 10 1/2/18 11:57 PM
Acknowledgments for the Global Edition Pearson would like to thank and acknowledge Yvan Maillot (Univresite Haute-Alsace) and Steven Yuwono (National University of Singapore) for contributing to this Global Edition, and Arif Ahmed (National Institute of Technology, Silchar), Annette Bieniusa (University of Kaiserslautern), Shaligram Prajapat (Devi Ahilya Vishwavidyalaya, Indore), and Ram Gopal Raj (University of Malaya) for reviewing this Global Edition.
Preface 11
A01_LIAN1878_11_GE_FM.indd 11 1/2/18 11:57 PM
12
contentS chapter 1 Introduction to computers,
Programs, and Java™ 23 1.1 Introduction 24 1.2 What Is a Computer? 24 1.3 Programming Languages 29 1.4 Operating Systems 31 1.5 Java, the World Wide Web, and Beyond 32 1.6 The Java Language Specification, API, JDK,
JRE, and IDE 33 1.7 A Simple Java Program 34 1.8 Creating, Compiling, and Executing a Java Program 37 1.9 Programming Style and Documentation 40 1.10 Programming Errors 42 1.11 Developing Java Programs Using NetBeans 45 1.12 Developing Java Programs Using Eclipse 47
chapter 2 elementary Programming 55 2.1 Introduction 56 2.2 Writing a Simple Program 56 2.3 Reading Input from the Console 59 2.4 Identifiers 62 2.5 Variables 62 2.6 Assignment Statements and Assignment Expressions 64 2.7 Named Constants 65 2.8 Naming Conventions 66 2.9 Numeric Data Types and Operations 67 2.10 Numeric Literals 70 2.11 Evaluating Expressions and Operator Precedence 72 2.12 Case Study: Displaying the Current Time 74 2.13 Augmented Assignment Operators 76 2.14 Increment and Decrement Operators 77 2.15 Numeric Type Conversions 79 2.16 Software Development Process 81 2.17 Case Study: Counting Monetary Units 85 2.18 Common Errors and Pitfalls 87
chapter 3 Selections 97 3.1 Introduction 98 3.2 boolean Data Type 98 3.3 if Statements 100 3.4 Two-Way if-else Statements 102 3.5 Nested if and Multi-Way if-else Statements 103 3.6 Common Errors and Pitfalls 105 3.7 Generating Random Numbers 109 3.8 Case Study: Computing Body Mass Index 111 3.9 Case Study: Computing Taxes 112 3.10 Logical Operators 115 3.11 Case Study: Determining Leap Year 119 3.12 Case Study: Lottery 120 3.13 switch Statements 122
A01_LIAN1878_11_GE_FM.indd 12 1/2/18 11:57 PM
3.14 Conditional Operators 125 3.15 Operator Precedence and Associativity 126 3.16 Debugging 128
chapter 4 mathematical functions, characters, and Strings 141
4.1 Introduction 142 4.2 Common Mathematical Functions 142 4.3 Character Data Type and Operations 147 4.4 The String Type 152 4.5 Case Studies 161 4.6 Formatting Console Output 167
chapter 5 Loops 181 5.1 Introduction 182 5.2 The while Loop 182 5.3 Case Study: Guessing Numbers 185 5.4 Loop Design Strategies 188 5.5 Controlling a Loop with User Confirmation or a Sentinel Value 190 5.6 The do-while Loop 192 5.7 The for Loop 195 5.8 Which Loop to Use? 198 5.9 Nested Loops 200 5.10 Minimizing Numeric Errors 202 5.11 Case Studies 204 5.12 Keywords break and continue 208 5.13 Case Study: Checking Palindromes 211 5.14 Case Study: Displaying Prime Numbers 213
chapter 6 methods 227 6.1 Introduction 228 6.2 Defining a Method 228 6.3 Calling a Method 230 6.4 void vs. Value-Returning Methods 233 6.5 Passing Parameters by Values 236 6.6 Modularizing Code 239 6.7 Case Study: Converting Hexadecimals to Decimals 241 6.8 Overloading Methods 243 6.9 The Scope of Variables 246 6.10 Case Study: Generating Random Characters 247 6.11 Method Abstraction and Stepwise Refinement 249
chapter 7 Single-dimensional arrays 269 7.1 Introduction 270 7.2 Array Basics 270 7.3 Case Study: Analyzing Numbers 277 7.4 Case Study: Deck of Cards 278 7.5 Copying Arrays 280 7.6 Passing Arrays to Methods 281 7.7 Returning an Array from a Method 284 7.8 Case Study: Counting the Occurrences of Each Letter 285 7.9 Variable-Length Argument Lists 288 7.10 Searching Arrays 289 7.11 Sorting Arrays 293
Contents 13
A01_LIAN1878_11_GE_FM.indd 13 1/2/18 11:57 PM
7.12 The Arrays Class 294 7.13 Command-Line Arguments 296
chapter 8 multidimensional arrays 311 8.1 Introduction 312 8.2 Two-Dimensional Array Basics 312 8.3 Processing Two-Dimensional Arrays 315 8.4 Passing Two-Dimensional Arrays to Methods 317 8.5 Case Study: Grading a Multiple-Choice Test 318 8.6 Case Study: Finding the Closest Pair 320 8.7 Case Study: Sudoku 322 8.8 Multidimensional Arrays 325
chapter 9 objects and classes 345 9.1 Introduction 346 9.2 Defining Classes for Objects 346 9.3 Example: Defining Classes and Creating Objects 348 9.4 Constructing Objects Using Constructors 353 9.5 Accessing Objects via Reference Variables 354 9.6 Using Classes from the Java Library 358 9.7 Static Variables, Constants, and Methods 361 9.8 Visibility Modifiers 366 9.9 Data Field Encapsulation 368 9.10 Passing Objects to Methods 371 9.11 Array of Objects 375 9.12 Immutable Objects and Classes 377 9.13 The Scope of Variables 379 9.14 The this Reference 380
chapter 10 object-oriented thinking 389 10.1 Introduction 390 10.2 Class Abstraction and Encapsulation 390 10.3 Thinking in Objects 394 10.4 Class Relationships 397 10.5 Case Study: Designing the Course Class 400 10.6 Case Study: Designing a Class for Stacks 402 10.7 Processing Primitive Data Type Values as Objects 404 10.8 Automatic Conversion between Primitive Types
and Wrapper Class Types 407 10.9 The BigInteger and BigDecimal Classes 408 10.10 The String Class 410 10.11 The StringBuilder and StringBuffer Classes 416
chapter 11 Inheritance and Polymorphism 433
11.1 Introduction 434 11.2 Superclasses and Subclasses 434 11.3 Using the super Keyword 440 11.4 Overriding Methods 443 11.5 Overriding vs. Overloading 444 11.6 The Object Class and Its toString() Method 446 11.7 Polymorphism 447 11.8 Dynamic Binding 447 11.9 Casting Objects and the instanceof Operator 451 11.10 The Object’s equals Method 455
14 Contents
A01_LIAN1878_11_GE_FM.indd 14 1/2/18 11:57 PM
15
11.11 The ArrayList Class 456 11.12 Useful Methods for Lists 462 11.13 Case Study: A Custom Stack Class 463 11.14 The protected Data and Methods 464 11.15 Preventing Extending and Overriding 467
chapter 12 exception handling and text I/o 475
12.1 Introduction 476 12.2 Exception-Handling Overview 476 12.3 Exception Types 481 12.4 More on Exception Handling 484 12.5 The finally Clause 492 12.6 When to Use Exceptions 493 12.7 Rethrowing Exceptions 494 12.8 Chained Exceptions 495 12.9 Defining Custom Exception Classes 496 12.10 The File Class 499 12.11 File Input and Output 502 12.12 Reading Data from the Web 508 12.13 Case Study: Web Crawler 510
chapter 13 abstract classes and Interfaces 521 13.1 Introduction 522 13.2 Abstract Classes 522 13.3 Case Study: the Abstract Number Class 527 13.4 Case Study: Calendar and GregorianCalendar 529 13.5 Interfaces 532 13.6 The Comparable Interface 535 13.7 The Cloneable Interface 540 13.8 Interfaces vs. Abstract Classes 545 13.9 Case Study: The Rational Class 548 13.10 Class-Design Guidelines 553
chapter 14 JavafX Basics 563 14.1 Introduction 564 14.2 JavaFX vs Swing and AWT 564 14.3 The Basic Structure of a JavaFX Program 564 14.4 Panes, Groups, UI Controls, and Shapes 567 14.5 Property Binding 570 14.6 Common Properties and Methods for Nodes 573 14.7 The Color Class 575 14.8 The Font Class 576 14.9 The Image and ImageView Classes 578 14.10 Layout Panes and Groups 580 14.11 Shapes 589 14.12 Case Study: The ClockPane Class 602
chapter 15 event-driven Programming and animations 615
15.1 Introduction 616 15.2 Events and Event Sources 618 15.3 Registering Handlers and Handling Events 619 15.4 Inner Classes 623 15.5 Anonymous Inner Class Handlers 624
Contents 15
A01_LIAN1878_11_GE_FM.indd 15 1/2/18 11:57 PM
15.6 Simplifying Event Handling Using Lambda Expressions 627 15.7 Case Study: Loan Calculator 631 15.8 Mouse Events 633 15.9 Key Events 635 15.10 Listeners for Observable Objects 638 15.11 Animation 640 15.12 Case Study: Bouncing Ball 648 15.13 Case Study: US Map 652
chapter 16 JavafX uI controls and multimedia 665
16.1 Introduction 666 16.2 Labeled and Label 666 16.3 Button 668 16.4 CheckBox 670 16.5 RadioButton 673 16.6 TextField 676 16.7 TextArea 677 16.8 ComboBox 681 16.9 ListView 684 16.10 ScrollBar 687 16.11 Slider 690 16.12 Case Study: Developing a Tic-Tac-Toe Game 693 16.13 Video and Audio 698 16.14 Case Study: National Flags and Anthems 701
chapter 17 Binary I/o 713 17.1 Introduction 714 17.2 How Is Text I/O Handled in Java? 714 17.3 Text I/O vs. Binary I/O 715 17.4 Binary I/O Classes 716 17.5 Case Study: Copying Files 726 17.6 Object I/O 728 17.7 Random-Access Files 733
chapter 18 recursion 741 18.1 Introduction 742 18.2 Case Study: Computing Factorials 742 18.3 Case Study: Computing Fibonacci
Numbers 745 18.4 Problem Solving Using Recursion 748 18.5 Recursive Helper Methods 750 18.6 Case Study: Finding the Directory Size 753 18.7 Case Study: Tower of Hanoi 755 18.8 Case Study: Fractals 758 18.9 Recursion vs. Iteration 762 18.10 Tail Recursion 762
chapter 19 generics 773 19.1 Introduction 774 19.2 Motivations and Benefits 774 19.3 Defining Generic Classes and Interfaces 776 19.4 Generic Methods 778 19.5 Case Study: Sorting an Array of Objects 780
16 Contents
A01_LIAN1878_11_GE_FM.indd 16 1/2/18 11:57 PM
19.6 Raw Types and Backward Compatibility 782 19.7 Wildcard Generic Types 783 19.8 Erasure and Restrictions on Generics 786 19.9 Case Study: Generic Matrix Class 788
chapter 20 Lists, Stacks, Queues, and Priority Queues 797
20.1 Introduction 798 20.2 Collections 798 20.3 Iterators 802 20.4 Using the forEach Method 803 20.5 Lists 804 20.6 The Comparator Interface 809 20.7 Static Methods for Lists and Collections 813 20.8 Case Study: Bouncing Balls 816 20.9 Vector and Stack Classes 820 20.10 Queues and Priority Queues 821 20.11 Case Study: Evaluating Expressions 825
chapter 21 Sets and maps 837 21.1 Introduction 838 21.2 Sets 838 21.3 Comparing the Performance of Sets and Lists 846 21.4 Case Study: Counting Keywords 849 21.5 Maps 850 21.6 Case Study: Occurrences of Words 855 21.7 Singleton and Unmodifiable Collections and Maps 857
chapter 22 developing efficient algorithms 861
22.1 Introduction 862 22.2 Measuring Algorithm Efficiency Using Big O Notation 862 22.3 Examples: Determining Big O 864 22.4 Analyzing Algorithm Time Complexity 868 22.5 Finding Fibonacci Numbers Using Dynamic
Programming 871 22.6 Finding Greatest Common Divisors Using Euclid’s
Algorithm 873 22.7 Efficient Algorithms for Finding Prime Numbers 877 22.8 Finding the Closest Pair of Points Using
Divide-and-Conquer 883 22.9 Solving the Eight Queens Problem Using Backtracking 886 22.10 Computational Geometry: Finding a Convex Hull 889
chapter 23 Sorting 903 23.1 Introduction 904 23.2 Insertion Sort 904 23.3 Bubble Sort 906 23.4 Merge Sort 909 23.5 Quick Sort 912 23.6 Heap Sort 916 23.7 Bucket and Radix Sorts 923 23.8 External Sort 925
Contents 17
A01_LIAN1878_11_GE_FM.indd 17 1/2/18 11:57 PM
chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues 939
24.1 Introduction 940 24.2 Common Operations for Lists 940 24.3 Array Lists 944 24.4 Linked Lists 951 24.5 Stacks and Queues 965 24.6 Priority Queues 969
chapter 25 Binary Search trees 975 25.1 Introduction 976 25.2 Binary Search Trees 976 25.3 Deleting Elements from a BST 989 25.4 Tree Visualization and MVC 995 25.5 Iterators 998 25.6 Case Study: Data Compression 1000
chapter 26 avL trees 1011 26.1 Introduction 1012 26.2 Rebalancing Trees 1012 26.3 Designing Classes for AVL Trees 1015 26.4 Overriding the insert Method 1016 26.5 Implementing Rotations 1017 26.6 Implementing the delete Method 1018 26.7 The AVLTree Class 1018 26.8 Testing the AVLTree Class 1024 26.9 AVL Tree Time Complexity Analysis 1027
chapter 27 hashing 1031 27.1 Introduction 1032 27.2 What Is Hashing? 1032 27.3 Hash Functions and Hash Codes 1033 27.4 Handling Collisions Using Open Addressing 1035 27.5 Handling Collisions Using Separate Chaining 1039 27.6 Load Factor and Rehashing 1039 27.7 Implementing a Map Using Hashing 1041 27.8 Implementing Set Using Hashing 1050
chapter 28 graphs and applications 1061 28.1 Introduction 1062 28.2 Basic Graph Terminologies 1063 28.3 Representing Graphs 1064 28.4 Modeling Graphs 1070 28.5 Graph Visualization 1080 28.6 Graph Traversals 1083 28.7 Depth-First Search (DFS) 1084 28.8 Case Study: The Connected Circles Problem 1088 28.9 Breadth-First Search (BFS) 1090 28.10 Case Study: The Nine Tails Problem 1093
18 Contents
A01_LIAN1878_11_GE_FM.indd 18 1/2/18 11:57 PM
chapter 29 Weighted graphs and applications 1107
29.1 Introduction 1108 29.2 Representing Weighted Graphs 1109 29.3 The WeightedGraph Class 1111 29.4 Minimum Spanning Trees 1119 29.5 Finding Shortest Paths 1125 29.6 Case Study: The Weighted Nine Tails Problem 1134
chapter 30 aggregate operations for collection Streams 1145
30.1 Introduction 1146 30.2 Stream Pipelines 1146 30.3 IntStream, LongStream, and DoubleStream 1152 30.4 Parallel Streams 1155 30.5 Stream Reduction Using the reduce Method 1157 30.6 Stream Reduction Using the collect Method 1160 30.7 Grouping Elements Using the groupingby Collector 1163 30.8 Case Studies 1166
chapter 31–44 are available from the companion Website at www.pearsonglobaleditions.com/Liang
chapter 31 advanced JavafX and fXmL chapter 32 multithreading and Parallel
Programming
chapter 33 networking chapter 34 Java database Programming chapter 35 advanced Java database Programming chapter 36 Internationalization chapter 37 Servlets chapter 38 JavaServer Pages chapter 39 JavaServer faces chapter 40 remote method Invocation chapter 41 Web Services chapter 42 2-4 trees and B-trees chapter 43 red-Black trees chapter 44 testing using Junit
Contents 19
A01_LIAN1878_11_GE_FM.indd 19 1/2/18 11:57 PM
Appendixes 1177
appendix a Java Keywords 1179 appendix B the aScII character Set 1180 appendix c operator Precedence chart 1182 appendix d Java modifiers 1184 appendix e Special floating-Point values 1186 appendix f number Systems 1187 appendix g Bitwise operations 1191 appendix h regular expressions 1192 appendix I enumerated types 1197
Quick Reference 1203 Index 1205
20 Contents
A01_LIAN1878_11_GE_FM.indd 20 1/2/18 11:57 PM
Chapter 1 Introduction to Computers, Programs, and Java™ 23 Your first Java program 34 Compile and run a Java program 39 NetBeans brief tutorial 45 Eclipse brief tutorial 47
Chapter 2 Elementary Programming 55 Obtain input 59 Use operators / and % 74 Software development process 81 Compute loan payments 82 Compute BMI 94
Chapter 3 Selections 97 Program addition quiz 99 Program subtraction quiz 109 Use multi-way if-else statements 112 Sort three integers 132 Check point location 134
Chapter 4 Mathematical Functions, Characters, and Strings 141 Introduce Math functions 142 Introduce strings and objects 152 Convert hex to decimal 165 Compute great circle distance 173 Convert hex to binary 176
Chapter 5 Loops 181 Use while loop 182 Guess a number 185 Multiple subtraction quiz 188 Use do-while loop 192 Minimize numeric errors 202 Display loan schedule 219 Sum a series 220
Chapter 6 Methods 227 Define/invoke max method 230 Use void method 233 Modularize code 239 Stepwise refinement 249 Reverse an integer 258 Estimate p 261
Chapter 7 Single-Dimensional Arrays 269 Random shuffling 274 Deck of cards 278 Selection sort 293 Command-line arguments 297
Coupon collector’s problem 304 Consecutive four 306
Chapter 8 Multidimensional Arrays 311 Find the row with the largest sum 316 Grade multiple-choice test 318 Sudoku 322 Multiply two matrices 331 Even number of 1s 338
Chapter 9 Objects and Classes 345 Define classes and objects 346 Use classes 358 Static vs. instance 361 Data field encapsulation 368 The this keyword 380 The Fan class 386
Chapter 10 Object-Oriented Thinking 389 The Loan class 391 The BMI class 394 The StackOfIntegers class 402 Process large numbers 408 The String class 410 The MyPoint class 424
Chapter 11 Inheritance and Polymorphism 433 Geometric class hierarchy 434 Polymorphism and dynamic binding demo 448 The ArrayList class 456 The MyStack class 463 New Account class 470
Chapter 12 Exception Handling and Text I/O 475 Exception-handling advantages 476 Create custom exception classes 496 Write and read data 502 HexFormatException 515
Chapter 13 Abstract Classes and Interfaces 521 Abstract GeometricObject class 522 Calendar and Gregorian Calendar classes 529 The concept of interface 532 Redesign the Rectangle class 558
Chapter 14 JavaFX Basics 563 Getting started with JavaFX 564 Understand property binding 570 Use Image and ImageView 578 Use layout panes 580 Use shapes 589
VideoNotes Locations of VideoNotes www.pearsonglobaleditions.com/Liang
21
VideoNote
A01_LIAN1878_11_GE_FM.indd 21 1/2/18 11:57 PM
22 VideoNotes
Display a tic-tac-toe board 608 Display a bar chart 610
Chapter 15 Event-Driven Programming and Animations 615 Handler and its registration 622 Anonymous handler 625 Move message using the mouse 634 Animate a rising flag 640 Flashing text 646 Simple calculator 656 Check mouse-point location 658 Display a running fan 661
Chapter 16 JavaFX UI Controls and Multimedia 665 Use ListView 684 Use Slider 690
Tic-Tac-Toe 693 Use Media, MediaPlayer, and MediaView 698 Use radio buttons and text fields 705 Set fonts 707
Chapter 17 Binary I/O 713 Copy file 726 Object I/O 728 Split a large file 738
Chapter 18 Recursion 741 Binary search 752 Directory size 753 Fractal (Sierpinski triangle) 758 Search a string in a directory 769 Recursive tree 772
Chapter 7 Single-Dimensional Arrays 269 linear search animation on Companion Website 290 binary search animation on Companion Website 290 selection sort animation on Companion Website 293
Chapter 8 Multidimensional Arrays 311 closest-pair animation on the Companion Website 320
Chapter 22 Developing Efficient Algorithms 861 binary search animation on the Companion Website 868 selection sort animation on the Companion Website 868 closest-pair animation on Companion Website 883 Eight Queens animation on the Companion Website 886 convex hull animation on the Companion Website 889
Chapter 23 Sorting 903 insertion-sort animation on Companion Website 904 bubble sort animation on the Companion Website 907 merge animation on Companion Website 911 partition animation on Companion Website 915 heap animation on Companion Website 917
radix sort on Companion Website 924
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues 939 list animation on Companion Website 940 stack and queue animation on Companion Website 965
Chapter 25 Binary Search Trees 975 BST animation on Companion Website 976
Chapter 26 AVL Trees 1011 AVL tree animation on Companion Website 1012
Chapter 27 Hashing 1031 linear probing animation on Companion Website 1036 quadratic probing animation on Companion Website 1037 separate chaining animation on Companion Website 1040
Chapter 28 Graphs and Applications 1061 graph learning tool on Companion Website 1064 U.S. Map Search 1086
Chapter 29 Weighted Graphs and Applications 1107 weighted graph learning tool animation on Companion Website 1108
Animations
A01_LIAN1878_11_GE_FM.indd 22 1/2/18 11:57 PM
Chapter
1 Introduction to Computers, Programs, and Java™
Objectives ■■ To understand computer basics, programs, and operating systems
(§§1.2–1.4).
■■ To describe the relationship between Java and the World Wide Web (§1.5).
■■ To understand the meaning of Java language specification, API, JDK™, JRE™, and IDE (§1.6).
■■ To write a simple Java program (§1.7).
■■ To display output on the console (§1.7).
■■ To explain the basic syntax of a Java program (§1.7).
■■ To create, compile, and run Java programs (§1.8).
■■ To use sound Java programming style and document programs properly (§1.9).
■■ To explain the differences between syntax errors, runtime errors, and logic errors (§1.10).
■■ To develop Java programs using NetBeans™ (§1.11).
■■ To develop Java programs using Eclipse™ (§1.12).
M01_LIAN1878_11_GE_C01.indd 23 1/2/18 10:57 PM
24 Chapter 1 Introduction to Computers, Programs, and Java™
1.1 Introduction The central theme of this book is to learn how to solve problems by writing a program.
This book is about programming. So, what is programming? The term programming means to create (or develop) software, which is also called a program. In basic terms, software contains instructions that tell a computer—or a computerized device—what to do.
Software is all around you, even in devices you might not think would need it. Of course, you expect to find and use software on a personal computer, but software also plays a role in running airplanes, cars, cell phones, and even toasters. On a personal computer, you use word processors to write documents, web browsers to explore the Internet, and e-mail programs to send and receive messages. These programs are all examples of software. Software developers create software with the help of powerful tools called programming languages.
This book teaches you how to create programs by using the Java programming language. There are many programming languages, some of which are decades old. Each language was invented for a specific purpose—to build on the strengths of a previous language, for example, or to give the programmer a new and unique set of tools. Knowing there are so many program- ming languages available, it would be natural for you to wonder which one is best. However, in truth, there is no “best” language. Each one has its own strengths and weaknesses. Experienced programmers know one language might work well in some situations, whereas a different language may be more appropriate in other situations. For this reason, seasoned programmers try to master as many different programming languages as they can, giving them access to a vast arsenal of software-development tools.
If you learn to program using one language, you should find it easy to pick up other lan- guages. The key is to learn how to solve problems using a programming approach. That is the main theme of this book.
You are about to begin an exciting journey: learning how to program. At the outset, it is helpful to review computer basics, programs, and operating systems (OSs). If you are already familiar with such terms as central processing unit (CPU), memory, disks, operating systems, and programming languages, you may skip Sections 1.2–1.4.
1.2 What Is a Computer? A computer is an electronic device that stores and processes data.
A computer includes both hardware and software. In general, hardware comprises the visible, physical elements of the computer, and software provides the invisible instructions that control the hardware and make it perform specific tasks. Knowing computer hardware isn’t essential to learning a programming language, but it can help you better understand the effects that a program’s instructions have on the computer and its components. This section introduces computer hardware components and their functions.
A computer consists of the following major hardware components (see Figure 1.1):
■■ A central processing unit (CPU)
■■ Memory (main memory)
■■ Storage devices (such as disks and CDs)
■■ Input devices (such as the mouse and the keyboard)
■■ Output devices (such as monitors and printers)
■■ Communication devices (such as modems and network interface cards (NIC))
A computer’s components are interconnected by a subsystem called a bus. You can think of a bus as a sort of system of roads running among the computer’s components; data and power travel along the bus from one part of the computer to another. In personal computers,
Point Key
what is programming? programming program
Point Key
hardware software
bus
M01_LIAN1878_11_GE_C01.indd 24 1/2/18 10:57 PM
1.2 What Is a Computer? 25
the bus is built into the computer’s motherboard, which is a circuit case that connects all of the parts of a computer together.
1.2.1 Central Processing Unit The central processing unit (CPU) is the computer’s brain. It retrieves instructions from the memory and executes them. The CPU usually has two components: a control unit and an arithmetic/logic unit. The control unit controls and coordinates the actions of the other com- ponents. The arithmetic/logic unit performs numeric operations (addition, subtraction, multi- plication, and division) and logical operations (comparisons).
Today’s CPUs are built on small silicon semiconductor chips that contain millions of tiny electric switches, called transistors, for processing information.
Every computer has an internal clock that emits electronic pulses at a constant rate. These pulses are used to control and synchronize the pace of operations. A higher clock speed enables more instructions to be executed in a given period of time. The unit of measurement of clock speed is the hertz (Hz), with 1 Hz equaling 1 pulse per second. In the 1990s, computers meas- ured clock speed in megahertz (MHz), but CPU speed has been improving continuously; the clock speed of a computer is now usually stated in gigahertz (GHz). Intel’s newest processors run at about 3 GHz.
CPUs were originally developed with only one core. The core is the part of the processor that performs the reading and executing of instructions. In order to increase the CPU processing power, chip manufacturers are now producing CPUs that contain multiple cores. A multicore CPU is a single component with two or more independent cores. Today’s consumer comput- ers typically have two, three, and even four separate cores. Soon, CPUs with dozens or even hundreds of cores will be affordable.
1.2.2 Bits and Bytes Before we discuss memory, let’s look at how information (data and programs) are stored in a computer.
A computer is really nothing more than a series of switches. Each switch exists in two states: on or off. Storing information in a computer is simply a matter of setting a sequence of switches on or off. If the switch is on, its value is 1. If the switch is off, its value is 0. These 0s and 1s are interpreted as digits in the binary number system and are called bits (binary digits).
The minimum storage unit in a computer is a byte. A byte is composed of eight bits. A small number such as 3 can be stored as a single byte. To store a number that cannot fit into a single byte, the computer uses several bytes.
Data of various kinds, such as numbers and characters, are encoded as a series of bytes. As a programmer, you don’t need to worry about the encoding and decoding of data, which the computer system performs automatically, based on the encoding scheme. An encoding scheme is a set of rules that govern how a computer translates characters and numbers into data with which the computer can actually work. Most schemes translate each character into a
motherboard
CPU
speed
hertz megahertz
gigahertz
core
bits
byte
encoding scheme
Figure 1.1 A computer consists of a CPU, memory, storage devices, input devices, output devices, and communication devices.
Memory
e.g., Disk, CD, and Tape
e.g., Modem, and NIC
e.g., Keyboard, Mouse
e.g., Monitor, Printer
CPU
Bus
Storage Devices
Communication Devices
Input Devices
Output Devices
M01_LIAN1878_11_GE_C01.indd 25 1/2/18 10:57 PM
26 Chapter 1 Introduction to Computers, Programs, and Java™
predetermined string of bits. In the popular ASCII encoding scheme, for example, the character C is represented as 01000011 in 1 byte.
A computer’s storage capacity is measured in bytes and multiples of the byte, as follows:
■■ A kilobyte (KB) is about 1,000 bytes.
■■ A megabyte (MB) is about 1 million bytes.
■■ A gigabyte (GB) is about 1 billion bytes.
■■ A terabyte (TB) is about 1 trillion bytes.
A typical one-page word document might take 20 KB. Therefore, 1 MB can store 50 pages of documents, and 1 GB can store 50,000 pages of documents. A typical two-hour high- resolution movie might take 8 GB, so it would require 160 GB to store 20 movies.
1.2.3 Memory A computer’s memory consists of an ordered sequence of bytes for storing programs as well as data with which the program is working. You can think of memory as the computer’s work area for executing a program. A program and its data must be moved into the computer’s memory before they can be executed by the CPU.
Every byte in the memory has a unique address, as shown in Figure 1.2. The address is used to locate the byte for storing and retrieving the data. Since the bytes in the memory can be accessed in any order, the memory is also referred to as random-access memory (RAM).
kilobyte (KB)
megabyte (MB)
gigabyte (GB)
terabyte (TB)
memory
unique address
RAM
Figure 1.2 Memory stores data and program instructions in uniquely addressed memory locations.
01000011 01110010 01100101 01110111 00000011
Encoding for character ‘C’ Encoding for character ‘r’ Encoding for character ‘e’ Encoding for character ‘w’ Decimal number 3
2000 2001 2002 2003 2004
Memory address Memory content
Today’s personal computers usually have at least 4 GB of RAM, but they more commonly have 6 to 8 GB installed. Generally speaking, the more RAM a computer has, the faster it can operate, but there are limits to this simple rule of thumb.
A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.
Like the CPU, memory is built on silicon semiconductor chips that have millions of transis- tors embedded on their surface. Compared to CPU chips, memory chips are less complicated, slower, and less expensive.
1.2.4 Storage Devices A computer’s memory (RAM) is a volatile form of data storage: Any information that has been saved in memory is lost when the system’s power is turned off. Programs and data are permanently stored on storage devices and are moved, when the computer actu- storage devices
M01_LIAN1878_11_GE_C01.indd 26 1/2/18 10:57 PM
1.2 What Is a Computer? 27
ally uses them, to memory, which operates at much faster speeds than permanent storage devices can.
There are three main types of storage devices:
■■ Magnetic disk drives
■■ Optical disc drives (CD and DVD)
■■ Universal serial bus (USB) flash drives
Drives are devices for operating a medium, such as disks and CDs. A storage medium physically stores data and program instructions. The drive reads data from the medium and writes data onto the medium.
Disks
A computer usually has at least one hard disk drive. Hard disks are used for permanently stor- ing data and programs. Newer computers have hard disks that can store from 500 GB to 1 TB of data. Hard disk drives are usually encased inside the computer, but removable hard disks are also available.
CDs and DVDs
CD stands for compact disc. There are three types of CDs: CD-ROM, CD-R, and CD-RW. A CD- ROM is a prepressed disc. It was popular for distributing software, music, and video. Software, music, and video are now increasingly distributed on the Internet without using CDs. A CD-R (CD-Recordable) is a write-once medium. It can be used to record data once and read any number of times. A CD-RW (CD-ReWritable) can be used like a hard disk; that is, you can write data onto the disc, then overwrite that data with new data. A single CD can hold up to 700 MB.
DVD stands for digital versatile disc or digital video disc. DVDs and CDs look alike, and you can use either to store data. A DVD can hold more information than a CD; a standard DVD’s storage capacity is 4.7 GB. There are two types of DVDs: DVD-R (Recordable) and DVD-RW (ReWritable).
USB Flash Drives
Universal serial bus (USB) connectors allow the user to attach many kinds of peripheral devices to the computer. You can use an USB to connect a printer, digital camera, mouse, external hard disk drive, and other devices to the computer.
An USB flash drive is a device for storing and transporting data. A flash drive is small—about the size of a pack of gum. It acts like a portable hard drive that can be plugged into your computer’s USB port. USB flash drives are currently available with up to 256 GB storage capacity.
1.2.5 Input and Output Devices Input and output devices let the user communicate with the computer. The most common input devices are the keyboard and mouse. The most common output devices are monitors and printers.
The Keyboard
A keyboard is a device for entering input. Compact keyboards are available without a numeric keypad.
Function keys are located across the top of the keyboard and are prefaced with the letter F. Their functions depend on the software currently being used.
A modifier key is a special key (such as the Shift, Alt, and Ctrl keys) that modifies the normal action of another key when the two are pressed simultaneously.
drive
hard disk
CD-ROM
CD-R
CD-RW
DVD
function key
modifier key
M01_LIAN1878_11_GE_C01.indd 27 1/2/18 10:57 PM
28 Chapter 1 Introduction to Computers, Programs, and Java™
The numeric keypad, located on the right side of most keyboards, is a separate set of keys styled like a calculator to use for quickly entering numbers.
Arrow keys, located between the main keypad and the numeric keypad, are used to move the mouse pointer up, down, left, and right on the screen in many kinds of programs.
The Insert, Delete, Page Up, and Page Down keys are used in word processing and other programs for inserting text and objects, deleting text and objects, and moving up or down through a document one screen at a time.
The Mouse
A mouse is a pointing device. It is used to move a graphical pointer (usually in the shape of an arrow) called a cursor around the screen, or to click on-screen objects (such as a button) to trigger them to perform an action.
The Monitor
The monitor displays information (text and graphics). The screen resolution and dot pitch determine the quality of the display.
The screen resolution specifies the number of pixels in horizontal and vertical dimensions of the display device. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. A common resolution for a 17-inch screen, for example, is 1,024 pixels wide and 768 pixels high. The resolution can be set manually. The higher the resolution, the sharper and clearer the image is.
The dot pitch is the amount of space between pixels, measured in millimeters. The smaller the dot pitch, the sharper is the display.
1.2.6 Communication Devices Computers can be networked through communication devices, such as a dial-up modem (modulator/demodulator), a digital subscriber line (DSL) or cable modem, a wired network interface card, or a wireless adapter.
■■ A dial-up modem uses a phone line to dial a phone number to connect to the Internet and can transfer data at a speed up to 56,000 bps (bits per second).
■■ A digital subscriber line (DSL) connection also uses a standard phone line, but it can transfer data 20 times faster than a standard dial-up modem.
■■ A cable modem uses the cable line maintained by the cable company and is generally faster than DSL.
■■ A network interface card (NIC) is a device that connects a computer to a local area network (LAN). LANs are commonly used to connect computers within a limited area such as a school, a home, and an office. A high-speed NIC called 1000BaseT can transfer data at 1,000 million bits per second (mbps).
■■ Wireless networking is now extremely popular in homes, businesses, and schools. Every laptop computer sold today is equipped with a wireless adapter that enables the computer to connect to the LAN and the Internet.
Note Answers to the CheckPoint questions are available at www.pearsonglobaleditions .com/Liang. Choose this book and click Companion Website to select CheckPoint.
1.2.1 What are hardware and software? 1.2.2 List the five major hardware components of a computer.
numeric keypad
arrow keys
Insert key Delete key
Page Up key Page Down key
screen resolution pixels
dot pitch
dial-up modem
digital subscriber line (DSL)
cable modem
network interface card (NIC)
local area network (LAN) million bits per second
(mbps)
Point Check
M01_LIAN1878_11_GE_C01.indd 28 1/2/18 10:57 PM
1.3 Programming Languages 29
1.2.3 What does the acronym CPU stand for? What unit is used to measure CPU speed? 1.2.4 What is a bit? What is a byte? 1.2.5 What is memory for? What does RAM stand for? Why is memory called RAM? 1.2.6 What unit is used to measure memory size? What unit is used to measure disk size? 1.2.7 What is the primary difference between memory and a storage device?
1.3 Programming Languages Computer programs, known as software, are instructions that tell a computer what to do.
Computers do not understand human languages, so programs must be written in a language a computer can use. There are hundreds of programming languages, and they were developed to make the programming process easier for people. However, all programs must be converted into the instructions the computer can execute.
1.3.1 Machine Language A computer’s native language, which differs among different types of computers, is its machine language—a set of built-in primitive instructions. These instructions are in the form of binary code, so if you want to give a computer an instruction in its native language, you have to enter the instruction as binary code. For example, to add two numbers, you might have to write an instruction in binary code as follows:
1101101010011010
1.3.2 Assembly Language Programming in machine language is a tedious process. Moreover, programs written in machine language are very difficult to read and modify. For this reason, assembly language was created in the early days of computing as an alternative to machine languages. Assembly language uses a short descriptive word, known as a mnemonic, to represent each of the machine-language instructions. For example, the mnemonic add typically means to add num- bers, and sub means to subtract numbers. To add the numbers 2 and 3 and get the result, you might write an instruction in assembly code as follows:
add 2, 3, result
Assembly languages were developed to make programming easier. However, because the computer cannot execute assembly language, another program—called an assembler—is used to translate assembly-language programs into machine code, as shown in Figure 1.3.
Point Key
machine language
assembly language
assembler
Figure 1.3 An assembler translates assembly-language instructions into machine code.
Assembly Source File
... add 2, 3, result
...
Machine-Code File
... 1101101010011010
...
Assembler
Writing code in assembly language is easier than in machine language. However, it is still tedious to write code in assembly language. An instruction in assembly language essentially corresponds to an instruction in machine code. Writing in assembly language requires that you
M01_LIAN1878_11_GE_C01.indd 29 1/2/18 10:57 PM
30 Chapter 1 Introduction to Computers, Programs, and Java™
know how the CPU works. Assembly language is referred to as a low-level language, because assembly language is close in nature to machine language and is machine dependent.
1.3.3 High-Level Language In the 1950s, a new generation of programming languages known as high-level languages emerged. They are platform independent, which means that you can write a program in a high- level language and run it in different types of machines. High-level languages are similar to English and easy to learn and use. The instructions in a high-level programming language are called statements. Here, for example, is a high-level language statement that computes the area of a circle with a radius of 5:
area = 5 * 5 * 3.14159;
There are many high-level programming languages, and each was designed for a specific purpose. Table 1.1 lists some popular ones.
low-level language
high-level language
statement
Language Description
Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. Developed for the Department of Defense and used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. Designed to be learned and used easily by beginners.
C Developed at Bell Laboratories. Combines the power of an assembly language with the ease of use and portability of a high-level language.
C++ An object-oriented language, based on C
C# Pronounced “C Sharp.” An object-oriented programming language developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. An object-oriented programming language, widely used for developing platform-independent Internet applications.
JavaScript A Web programming language developed by Netscape
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. A simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Basic Visual Basic was developed by Microsoft. Enables the programmers to rapidly develop Windows-based applications.
Table 1.1 Popular High-Level Programming Languages
A program written in a high-level language is called a source program or source code. Because a computer cannot execute a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler.
■■ An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, then executes it right away, as shown in Figure 1.4a. Note a statement from the source code may be translated into several machine instructions.
source program source code
interpreter
compiler
M01_LIAN1878_11_GE_C01.indd 30 1/2/18 10:57 PM
1.4 Operating Systems 31
Figure 1.4 (a) An interpreter translates and executes a program one statement at a time. (b) A compiler translates the entire source program into a machine-language file for execution.
Machine-Code File
... 0101100011011100 1111100011000100
...
High-Level Source File
... area = 5 * 5 * 3.1415;
...
(b)
Compiler Executor
High-Level Source File
... area = 5 * 5 * 3.1415;
...
(a)
Interpreter Output
Output
■■ A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed, as shown in Figure 1.4b.
1.3.1 What language does the CPU understand? 1.3.2 What is an assembly language? What is an assembler? 1.3.3 What is a high-level programming language? What is a source program? 1.3.4 What is an interpreter? What is a compiler? 1.3.5 What is the difference between an interpreted language and a compiled language?
Point Check
1.4 Operating Systems The operating system (OS) is the most important program that runs on a computer. The OS manages and controls a computer’s activities.
The popular operating systems for general-purpose computers are Microsoft Windows, Mac OS, and Linux. Application programs, such as a web browser or a word processor, cannot run unless an operating system is installed and running on the computer. Figure 1.5 shows the interrelationship of hardware, operating system, application software, and the user.
Point Key
operating system (OS)
Figure 1.5 Users and applications access the computer’s hardware via the operating system.
User
Application Programs
Operating System
Hardware
M01_LIAN1878_11_GE_C01.indd 31 1/2/18 10:57 PM
32 Chapter 1 Introduction to Computers, Programs, and Java™
The major tasks of an operating system are as follows:
■■ Controlling and monitoring system activities
■■ Allocating and assigning system resources
■■ Scheduling operations
1.4.1 Controlling and Monitoring System Activities Operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the monitor, keeping track of files and folders on storage devices, and controlling peripheral devices such as disk drives and printers. An operating system must also ensure different programs and users working at the same time do not interfere with each other. In addition, the OS is responsible for security, ensuring unauthorized users and programs are not allowed to access the system.
1.4.2 Allocating and Assigning System Resources The operating system is responsible for determining what computer resources a program needs (such as CPU time, memory space, disks, and input and output devices) and for allocating and assigning them to run the program.
1.4.3 Scheduling Operations The OS is responsible for scheduling programs’ activities to make efficient use of system resources. Many of today’s operating systems support techniques such as multiprogramming, multithreading, and multiprocessing to increase system performance.
Multiprogramming allows multiple programs such as Microsoft Word, E-mail, and web browser to run simultaneously by sharing the same CPU. The CPU is much faster than the computer’s other components. As a result, it is idle most of the time—for example, while wait- ing for data to be transferred from a disk or waiting for other system resources to respond. A multiprogramming OS takes advantage of this situation by allowing multiple programs to use the CPU when it would otherwise be idle. For example, multiprogramming enables you to use a word processor to edit a file at the same time as your web browser is downloading a file.
Multithreading allows a single program to execute multiple tasks at the same time. For instance, a word-processing program allows users to simultaneously edit text and save it to a disk. In this example, editing and saving are two tasks within the same program. These two tasks may run concurrently.
Multiprocessing is similar to multithreading. The difference is that multithreading is for running multithreads concurrently within one program, but multiprocessing is for running multiple programs concurrently using multiple processors.
1.4.1 What is an operating system? List some popular operating systems. 1.4.2 What are the major responsibilities of an operating system? 1.4.3 What are multiprogramming, multithreading, and multiprocessing?
1.5 Java, the World Wide Web, and Beyond Java is a powerful and versatile programming language for developing software run- ning on mobile devices, desktop computers, and servers.
This book introduces Java programming. Java was developed by a team led by James Gosling at Sun Microsystems. Sun Microsystems was purchased by Oracle in 2010. Originally called Oak, Java was designed in 1991 for use in embedded chips in consumer electronic appliances.
multiprogramming multithreading multiprocessing
Point Check
Point Key
M01_LIAN1878_11_GE_C01.indd 32 1/2/18 10:57 PM
1.6 The Java Language Specification, API, JDK, JRE, and IDE 33
In 1995, renamed Java, it was redesigned for developing web applications. For the history of Java, see www.java.com/en/javahistory/index.jsp.
Java has become enormously popular. Its rapid rise and wide acceptance can be traced to its design characteristics, particularly its promise that you can write a program once and run it anywhere. As stated by its designer, Java is simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, and dynamic. For the anatomy of Java characteristics, see liveexample.pearsoncmg.com/etc/ JavaCharacteristics.pdf.
Java is a full-featured, general-purpose programming language that can be used to develop robust mission-critical applications. Today, it is employed not only for web programming but also for developing stand-alone applications across platforms on servers, desktop computers, and mobile devices. It was used to develop the code to communicate with and control the robotic rover on Mars. Many companies that once considered Java to be more hype than sub- stance are now using it to create distributed applications accessed by customers and partners across the Internet. For every new project being developed today, companies are asking how they can use Java to make their work easier.
The World Wide Web is an electronic information repository that can be accessed on the Internet from anywhere in the world. The Internet, the Web’s infrastructure, has been around for more than 40 years. The colorful World Wide Web and sophisticated web browsers are the major reason for the Internet’s popularity.
Java initially became attractive because Java programs can run from a web browser. Such programs are called applets. Today applets are no longer allowed to run from a Web browser in the latest version of Java due to security issues. Java, however, is now very popular for developing applications on web servers. These applications process data, perform computa- tions, and generate dynamic webpages. Many commercial Websites are developed using Java on the backend.
Java is a versatile programming language: You can use it to develop applications for desktop computers, servers, and small handheld devices. The software for Android cell phones is developed using Java.
1.5.1 Who invented Java? Which company owns Java now? 1.5.2 What is a Java applet? 1.5.3 What programming language does Android use?
1.6 The Java Language Specification, API, JDK, JRE, and IDE
Java syntax is defined in the Java language specification, and the Java library is defined in the Java application program interface (API). The JDK is the software for compiling and running Java programs. An IDE is an integrated development environ- ment for rapidly developing programs.
Computer languages have strict rules of usage. If you do not follow the rules when writing a program, the computer will not be able to understand it. The Java language specification and the Java API define the Java standards.
The Java language specification is a technical definition of the Java programming language’s syntax and semantics. You can find the complete Java language specification at docs.oracle.com/javase/specs/.
The application program interface (API), also known as library, contains predefined classes and interfaces for developing Java programs. The API is still expanding. You can view and download the latest version of the Java API at download.java.net/jdk8/docs/api/.
Point Check
Point Key
Java language specification
API library
M01_LIAN1878_11_GE_C01.indd 33 1/2/18 10:57 PM
34 Chapter 1 Introduction to Computers, Programs, and Java™
Java is a full-fledged and powerful language that can be used in many ways. It comes in three editions:
■■ Java Standard Edition (Java SE) to develop client-side applications. The applications can run on desktop.
■■ Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).
■■ Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones.
This book uses Java SE to introduce Java programming. Java SE is the foundation upon which all other Java technology is based. There are many versions of Java SE. The latest, Java SE 8, is used in this book. Oracle releases each version with a Java Development Toolkit (JDK). For Java SE 8, the Java Development Toolkit is called JDK 1.8 (also known as Java 8 or JDK 8).
The JDK consists of a set of separate programs, each invoked from a command line, for compiling, running, and testing Java programs. The program for running Java programs is known as JRE (Java Runtime Environment). Instead of using the JDK, you can use a Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for developing Java programs quickly. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. You simply enter source code in one window or open an existing file in a window, and then click a button or menu item or press a function key to compile and run the program.
1.6.1 What is the Java language specification? 1.6.2 What does JDK stand for? What does JRE stand for? 1.6.3 What does IDE stand for? 1.6.4 Are tools like NetBeans and Eclipse different languages from Java, or are they dia-
lects or extensions of Java?
1.7 A Simple Java Program A Java program is executed from the main method in the class.
Let’s begin with a simple Java program that displays the message Welcome to Java! on the console. (The word console is an old computer term that refers to the text entry and display device of a computer. Console input means to receive input from the keyboard, and console output means to display output on the monitor.) The program is given in Listing 1.1.
lisTing 1.1 Welcome.java 1 public class Welcome { 2 public static void main(String[] args) { 3 // Display message Welcome to Java! on the console 4 System.out.println("Welcome to Java!"); 5 } 6 }
Java SE, EE, and ME
Java Development Toolkit (JDK)
JDK 1.8 = JDK 8
Java Runtime Environment (JRE)
Integrated development environment
Point Check
Point Key
what is a console? console input
console output
class main method
display message
VideoNote
Your first Java program
Welcome to Java!
Note the line numbers are for reference purposes only; they are not part of the program. So, don’t type line numbers in your program.
line numbers
M01_LIAN1878_11_GE_C01.indd 34 1/2/18 10:57 PM
1.7 A Simple Java Program 35
Line 1 defines a class. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.
Line 2 defines the main method. The program is executed from the main method. A class may contain several methods. The main method is the entry point where the program begins execution.
A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement displays the string Welcome to Java! on the console (line 4). String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semi- colon (;), known as the statement terminator.
Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in this program are public, static, and void.
Line 3 is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They are not programming statements, and thus are ignored by the compiler. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. When the compiler sees //, it ignores all text after // on the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */. Here are examples of comments:
// This application program displays Welcome to Java! /* This application program displays Welcome to Java! */ /* This application program
displays Welcome to Java! */
A pair of braces in a program forms a block that groups the program’s components. In Java, each block begins with an opening brace ({) and ends with a closing brace (}). Every class has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statements in the method. Blocks can be nested, meaning that one block can be placed within another, as shown in the following code:
class name
main method
string
statement terminator reserved word keyword
comment
line comment block comment
block
match braces
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Method block Class block
Tip An opening brace must be matched by a closing brace. Whenever you type an opening brace, immediately type a closing brace to prevent the missing-brace error. Most Java IDEs automatically insert the closing brace for each opening brace.
Caution Java source programs are case sensitive. It would be wrong, for example, to replace main in the program with Main.
You have seen several special characters (e.g., { }, //, ;) in the program. They are used in almost every program. Table 1.2 summarizes their uses.
The most common errors you will make as you learn to program will be syntax errors. Like any programming language, Java has its own syntax, and you need to write code that conforms
case sensitive
special characters
common errors
M01_LIAN1878_11_GE_C01.indd 35 1/2/18 10:57 PM
36 Chapter 1 Introduction to Computers, Programs, and Java™
to the syntax rules. If your program violates a rule—for example, if the semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled—the Java compiler will report syntax errors. Try to compile the program with these errors and see what the com- piler reports.
Note You are probably wondering why the main method is defined this way and why System.out.println(...) is used to display a message on the console. For the time being, simply accept that this is how things are done. Your questions will be fully answered in subsequent chapters.
The program in Listing 1.1 displays one message. Once you understand the program, it is easy to extend it to display more messages. For example, you can rewrite the program to display three messages, as shown in Listing 1.2.
lisTing 1.2 WelcomeWithThreeMessages.java 1 public class WelcomeWithThreeMessages { 2 public static void main(String[] args) { 3 System.out.println("Programming is fun!"); 4 System.out.println("Fundamentals First"); 5 System.out.println("Problem Driven"); 6 } 7 }
syntax rules
class main method display message
Character Name Description
{} Opening and closing braces Denote a block to enclose statements.
() Opening and closing parentheses Used with methods.
[] Opening and closing brackets Denote an array.
// Double slashes Precede a comment line.
"" Opening and closing quotation marks Enclose a string (i.e., sequence of characters).
; Semicolon Mark the end of a statement.
Table 1.2 Special Characters
Programming is fun! Fundamentals First Problem Driven
Further, you can perform mathematical computations and display the result on the console.
Listing 1.3 gives an example of evaluating 10.5 + 2 * 3
45 - 3.5 .
lisTing 1.3 ComputeExpression.java 1 public class ComputeExpression { 2 public static void main(String[] args) { 3 System.out.print("(10.5 + 2 * 3) / (45 – 3.5) = "); 4 System.out.println((10.5 + 2 * 3) / (45 – 3.5)); 5 } 6 }
class main method
compute expression
(10.5 + 2 * 3) / (45 – 3.5) = 0.39759036144578314
M01_LIAN1878_11_GE_C01.indd 36 1/2/18 10:57 PM
1.8 Creating, Compiling, and Executing a Java Program 37
The print method in line 3
System.out.print("(10.5 + 2 * 3) / (45 – 3.5) = ");
is identical to the println method except that println moves to the beginning of the next line after displaying the string, but print does not advance to the next line when completed.
The multiplication operator in Java is *. As you can see, it is a straightforward process to translate an arithmetic expression to a Java expression. We will discuss Java expressions fur- ther in Chapter 2.
1.7.1 What is a keyword? List some Java keywords. 1.7.2 Is Java case sensitive? What is the case for Java keywords? 1.7.3 What is a comment? Is the comment ignored by the compiler? How do you denote a
comment line and a comment paragraph? 1.7.4 What is the statement to display a string on the console? 1.7.5 Show the output of the following code:
public class Test { public static void main(String[] args) { System.out.println("3.5 * 4 / 2 – 2.5 is "); System.out.println(3.5 * 4 / 2 – 2.5); } }
1.8 Creating, Compiling, and Executing a Java Program You save a Java program in a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine (JVM).
You have to create your program and compile it before it can be executed. This process is repetitive, as shown in Figure 1.6. If your program has compile errors, you have to modify the program to fix them, then recompile it. If your program has runtime errors or does not produce the correct result, you have to modify the program, recompile it, and execute it again.
You can use any text editor or IDE to create and edit a Java source-code file. This section demonstrates how to create, compile, and run Java programs from a command window. Sec- tions 1.11 and 1.12 will introduce developing Java programs using NetBeans and Eclipse. From the command window, you can use a text editor such as Notepad to create the Java source-code file, as shown in Figure 1.7.
Note The source file must end with the extension .java and must have the same exact name as the public class name. For example, the file for the source code in Listing 1.1 should be named Welcome.java, since the public class name is Welcome.
A Java compiler translates a Java source file into a Java bytecode file. The following com- mand compiles Welcome.java:
javac Welcome.java
Note You must first install and configure the JDK before you can compile and run programs. See Supplement I.B, Installing and Configuring JDK 8, for how to install the JDK and set up the environment to compile and run Java programs. If you have trouble compiling and running programs, see Supplement I.C, Compiling and Running Java from the Command Window. This supplement also explains how to use basic DOS commands and how to use Windows Notepad to create and edit files. All the supplements are accessible from the Companion Website.
print vs. println
Point Check
Point Key
command window
file name Welcome.java,
compile
Supplement I.B
Supplement I.C
M01_LIAN1878_11_GE_C01.indd 37 1/2/18 10:57 PM
38 Chapter 1 Introduction to Computers, Programs, and Java™
Figure 1.6 The Java program-development process consists of repeatedly creating/modifying source code, compiling, and executing programs.
Create/Modify Source Code
Result
Compile Source Code e.g., javac Welcome.java
Saved on the disk
Stored on the disk If compile errors occur
If runtime errors or incorrect result
Source code (developed by the programmer)
Bytecode (generated by the compiler for JVM to read and interpret)
… Method Welcome() 0 aload_0 …
Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Run Bytecode e.g., java Welcome
Source Code
Bytecode
“Welcome to Java” is displayed on the console
Welcome to Java!
Figure 1.7 You can create a Java source file using Windows Notepad.
If there aren’t any syntax errors, the compiler generates a bytecode file with a .class extension. Thus, the preceding command generates a file named Welcome.class, as shown in Figure 1.8a. The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM), as shown in Figure 1.8b. Rather than a physical machine, the virtual machine is a program that interprets Java bytecode. This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems. Java source code is compiled into Java bytecode, and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM exe- cutes your code along with the code in the library.
To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time, rather than the whole pro- gram as a single unit. Each step is executed immediately after it is translated.
.class bytecode file
bytecode
Java Virtual Machine (JVM)
interpret bytecode
M01_LIAN1878_11_GE_C01.indd 38 1/2/18 10:57 PM
1.8 Creating, Compiling, and Executing a Java Program 39
The following command runs the bytecode for Listing 1.1:
java Welcome
Figure 1.9 shows the javac command for compiling Welcome.java. The compiler generates the Welcome.class file, and this file is executed using the java command.
Note For simplicity and consistency, all source-code and class files used in this book are placed under c:\book unless specified otherwise.
run
javac command java command
c:\book
VideoNote
Compile and run a Java program
Figure 1.8 (a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with a Java Virtual Machine.
Ja
va Vir
tual Machine
Any Computer
Java Bytecode
Welcome.java (Java source-
code �le)
Welcome.class (Java bytecode executable �le)
Library Code
JVMJavaCompiler
compiled by generates
executed by
(a) (b)
Figure 1.9 The output of Listing 1.1 displays the message “Welcome to Java!”
Show �les
Run
Compile
Caution Do not use the extension .class in the command line when executing the program. Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class.
Tip If you execute a class file that does not exist, a NoClassDefFoundError will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur.
java ClassName
NoClassDefFoundError
NoSuchMethodError
M01_LIAN1878_11_GE_C01.indd 39 1/2/18 10:57 PM
40 Chapter 1 Introduction to Computers, Programs, and Java™
Note When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called the bytecode verifier to check the validity of the bytecode and to ensure that the bytecode does not violate Java’s security restrictions. Java enforces strict security to make sure Java class files are not tampered with and do not harm your computer.
Pedagogical Note Your instructor may require you to use packages for organizing programs. For example, you may place all programs in this chapter in a package named chapter1. For instructions on how to use packages, see Supplement I.F, Using Packages to Organize the Classes in the Text.
1.8.1 What is the Java source filename extension, and what is the Java bytecode filename extension?
1.8.2 What are the input and output of a Java compiler? 1.8.3 What is the command to compile a Java program? 1.8.4 What is the command to run a Java program? 1.8.5 What is the JVM? 1.8.6 Can Java run on any machine? What is needed to run Java on a computer? 1.8.7 If a NoClassDefFoundError occurs when you run a program, what is the cause
of the error? 1.8.8 If a NoSuchMethodError occurs when you run a program, what is the cause of the
error?
1.9 Programming Style and Documentation Good programming style and proper documentation make a program easy to read and help programmers prevent errors.
Programming style deals with what programs look like. A program can compile and run properly even if written on only one line, but writing it all on one line would be bad pro- gramming style because it would be hard to read. Documentation is the body of explanatory remarks and comments pertaining to a program. Programming style and documentation are as important as coding. Good programming style and appropriate documentation reduce the chance of errors and make programs easy to read. This section gives several guidelines. For more detailed guidelines, see Supplement I.D, Java Coding Style Guidelines, on the Com- panion Website.
1.9.1 Appropriate Comments and Comment Styles Include a summary at the beginning of the program that explains what the program does, its key features, and any unique techniques it uses. In a long program, you should also include comments that introduce each major step and explain anything that is difficult to read. It is important to make comments concise so that they do not crowd the program or make it difficult to read.
In addition to line comments (beginning with //) and block comments (beginning with /*), Java supports comments of a special type, referred to as javadoc comments. javadoc comments begin with /** and end with */. They can be extracted into an HTML file using the JDK’s javadoc command. For more information, see Supplement III.Y, javadoc Comments, on the Companion Website.
class loader
bytecode verifier
use package
Point Check
Point Key
programming style documentation
javadoc comment
M01_LIAN1878_11_GE_C01.indd 40 1/2/18 10:57 PM
1.9 Programming Style and Documentation 41
Use javadoc comments (/** . . . */) for commenting on an entire class or an entire method. These comments must precede the class or the method header in order to be extracted into a javadoc HTML file. For commenting on steps inside a method, use line comments (//). To see an example of a javadoc HTML file, check out liveexample.pearsoncmg.com/javadoc/ Exercise1.html. Its corresponding Java code is shown in liveexample.pearsoncmg.com/java- doc/Exercise1.txt.
1.9.2 Proper Indentation and Spacing A consistent indentation style makes programs clear and easy to read, debug, and maintain. Indentation is used to illustrate the structural relationships between a program’s compo- nents or statements. Java can read the program even if all of the statements are on the same long line, but humans find it easier to read and maintain code that is aligned properly. Indent each subcomponent or statement at least two spaces more than the construct within which it is nested.
A single space should be added on both sides of a binary operator, as shown in (a), rather in (b).
System.out.println(3 + 4 * 4); System.out.println(3+4*4);
(a) Good style (b) Bad style
1.9.3 Block Styles A block is a group of statements surrounded by braces. There are two popular styles, next-line style and end-of-line style, as shown below.
public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } }
public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } }
Next-line style End-of-line style
The next-line style aligns braces vertically and makes programs easy to read, whereas the end-of-line style saves space and may help avoid some subtle programming errors. Both are acceptable block styles. The choice depends on personal or organizational preference. You should use a block style consistently—mixing styles is not recommended. This book uses the end-of-line style to be consistent with the Java API source code.
1.9.1 Reformat the following program according to the programming style and documen- tation guidelines. Use the end-of-line brace style.
public class Test { // Main method public static void main(String[] args) { /** Display output */ System.out.println("Welcome to Java"); } }
indent code
Point Check
M01_LIAN1878_11_GE_C01.indd 41 1/2/18 10:57 PM
42 Chapter 1 Introduction to Computers, Programs, and Java™
1.10 Programming Errors Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors.
1.10.1 Syntax Errors Errors that are detected by the compiler are called syntax errors or compile errors. Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some necessary punctuation, or using an opening brace without a corresponding closing brace. These errors are usually easy to detect because the compiler tells you where they are and what caused them. For example, the program in Listing 1.4 has a syntax error, as shown in Figure 1.10.
lisTing 1.4 ShowSyntaxErrors.java 1 public class ShowSyntaxErrors { 2 public static main(String[] args) { 3 System.out.println("Welcome to Java); 4 } 5 }
Four errors are reported, but the program actually has two errors:
■■ The keyword void is missing before main in line 2.
■■ The string Welcome to Java should be closed with a closing quotation mark in line 3.
Since a single error will often display many lines of compile errors, it is a good practice to fix errors from the top line and work downward. Fixing errors that occur earlier in the program may also fix additional errors that occur later.
Point Key
syntax errors
compile errors
Figure 1.10 The compiler reports syntax errors.
Compile
Tip If you don’t know how to correct an error, compare your program closely, character by character, with similar examples in the text. In the first few weeks of this course, you will probably spend a lot of time fixing syntax errors. Soon you will be familiar with Java syntax, and can quickly fix syntax errors.
fix syntax errors
M01_LIAN1878_11_GE_C01.indd 42 1/2/18 10:57 PM
1.10 Programming Errors 43
1.10.2 Runtime Errors Runtime errors are errors that cause a program to terminate abnormally. They occur while a program is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input error occurs when the program is waiting for the user to enter a value, but the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors to occur in the program.
Another example of runtime errors is division by zero. This happens when the divisor is zero for integer divisions. For instance, the program in Listing 1.5 would cause a runtime error, as shown in Figure 1.11.
lisTing 1.5 ShowRuntimeErrors.java 1 public class ShowRuntimeErrors { 2 public static void main(String[] args) { 3 System.out.println(1 / 0); 4 } 5 }
runtime errors
runtime error
Figure 1.11 The runtime error causes the program to terminate abnormally.
Run
1.10.3 Logic Errors Logic errors occur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons. For example, suppose you wrote the program in Listing 1.6 to convert Celsius 35 degrees to a Fahrenheit degree:
lisTing 1.6 ShowLogicErrors.java 1 public class ShowLogicErrors { 2 public static void main(String[] args) { 3 System.out.print("Celsius 35 is Fahrenheit degree "); 4 System.out.println((9 / 5) * 35 + 32); 5 } 6 }
logic errors
Celsius 35 is Fahrenheit degree 67
You will get Fahrenheit 67 degrees, which is wrong. It should be 95.0. In Java, the division for integers is the quotient—the fractional part is truncated—so in Java 9 / 5 is 1. To get the correct result, you need to use 9.0 / 5, which results in 1.8.
In general, syntax errors are easy to find and easy to correct because the compiler gives indications as to where the errors came from and why they are wrong. Runtime errors are not difficult to find, either, since the reasons and locations for the errors are displayed on the console when the program aborts. Finding logic errors, on the other hand, can be very challenging. In the upcoming chapters, you will learn the techniques of tracing programs and finding logic errors.
M01_LIAN1878_11_GE_C01.indd 43 1/2/18 10:57 PM
44 Chapter 1 Introduction to Computers, Programs, and Java™
1.10.4 Common Errors Missing a closing brace, missing a semicolon, missing quotation marks for strings, and mis- spelling names are common errors for new programmers.
Common Error 1: Missing Braces
The braces are used to denote a block in the program. Each opening brace must be matched by a closing brace. A common error is missing the closing brace. To avoid this error, type a closing brace whenever an opening brace is typed, as shown in the following example:
public class Welcome {
} Type this closing brace right away to match the opening brace.
If you use an IDE such as NetBeans and Eclipse, the IDE automatically inserts a closing brace for each opening brace typed.
Common Error 2: Missing Semicolons
Each statement ends with a statement terminator (;). Often, a new programmer forgets to place a statement terminator for the last statement in a block, as shown in the following example:
public static void main(String[] args) { System.out.println("Programming is fun!"); System.out.println("Fundamentals First"); System.out.println("Problem Driven") }
Missing a semicolon
Common Error 3: Missing Quotation Marks
A string must be placed inside the quotation marks. Often, a new programmer forgets to place a quotation mark at the end of a string, as shown in the following example:
System.out.println("Problem Driven);
Missing a quotation mark
If you use an IDE such as NetBeans and Eclipse, the IDE automatically inserts a closing quotation mark for each opening quotation mark typed.
Common Error 4: Misspelling Names
Java is case sensitive. Misspelling names is a common error for new programmers. For exam- ple, the word main is misspelled as Main and String is misspelled as string in the follow- ing code:
1 public class Test { 2 public static void Main(string[] args) { 3 System.out.println((10.5 + 2 * 3) / (45 – 3.5)); 4 } 5 }
1.10.1 What are syntax errors (compile errors), runtime errors, and logic errors? 1.10.2 Give examples of syntax errors, runtime errors, and logic errors. 1.10.3 If you forget to put a closing quotation mark on a string, what kind error of will be raised? 1.10.4 If your program needs to read integers, but the user entered strings, an error would
occur when running this program. What kind of error is this?
Point Check
M01_LIAN1878_11_GE_C01.indd 44 1/2/18 10:57 PM
1.11 Developing Java Programs Using NetBeans 45
1.10.5 Suppose you write a program for computing the perimeter of a rectangle and you mistak- enly write your program so it computes the area of a rectangle. What kind of error is this?
1.10.6 Identify and fix the errors in the following code:
1 public class Welcome { 2 public void Main(String[] args) { 3 System.out.println('Welcome to Java!); 4 } 5 )
Note Section 1.8 introduced developing programs from the command line. Many of our readers also use an IDE. The following two sections introduce two most popular Java IDEs: NetBeans and Eclipse. These two sections may be skipped.
1.11 Developing Java Programs Using NetBeans You can edit, compile, run, and debug Java Programs using NetBeans.
NetBeans and Eclipse are two free popular integrated development environments for develop- ing Java programs. They are easy to learn if you follow simple instructions. We recommend that you use either one for developing Java programs. This section gives the essential instruc- tions to guide new users to create a project, create a class, compile, and run a class in NetBeans. The use of Eclipse will be introduced in the next section. For instructions on downloading and installing latest version of NetBeans, see Supplement II.B.
1.11.1 Creating a Java Project Before you can create Java programs, you need to first create a project. A project is like a folder to hold Java programs and all supporting files. You need to create a project only once. Here are the steps to create a Java project:
1. Choose File, New Project to display the New Project dialog box, as shown in Figure 1.12.
2. Select Java in the Categories section and Java Application in the Projects section, and then click Next to display the New Java Application dialog box, as shown in Figure 1.13.
3. Type demo in the Project Name field and c:\michael in Project Location field. Uncheck Use Dedicated Folder for Storing Libraries and uncheck Create Main Class.
4. Click Finish to create the project, as shown in Figure 1.14.
Point Key
VideoNote
NetBeans brief tutorial
Figure 1.12 The New Project dialog is used to create a new project and specify a project type. Source: Copyright © 1995–2016 Oracle and/or its affiliates. All rights reserved. Used with permission.
M01_LIAN1878_11_GE_C01.indd 45 1/2/18 10:57 PM
46 Chapter 1 Introduction to Computers, Programs, and Java™
1.11.2 Creating a Java Class After a project is created, you can create Java programs in the project using the following steps:
1. Right-click the demo node in the project pane to display a context menu. Choose New, Java Class to display the New Java Class dialog box, as shown in Figure 1.15.
2. Type Welcome in the Class Name field and select the Source Packages in the Location field. Leave the Package field blank. This will create a class in the default package.
3. Click Finish to create the Welcome class. The source-code file Welcome.java is placed under the <default package> node.
4. Modify the code in the Welcome class to match Listing 1.1 in the text, as shown in Figure 1.16.
1.11.3 Compiling and Running a Class To run Welcome.java, right-click Welcome.java to display a context menu and choose Run File, or simply press Shift + F6. The output is displayed in the Output pane, as shown in Figure 1.16. The Run File command automatically compiles the program if the program has been changed.
Figure 1.13 The New Java Application dialog is for specifying a project name and location. Source: Copyright © 1995–2016 Oracle and/or its affiliates. All rights reserved. Used with permission.
Figure 1.14 A New Java project named demo is created. Source: Copyright © 1995–2016 Oracle and/or its affiliates. All rights reserved. Used with permission.
M01_LIAN1878_11_GE_C01.indd 46 1/2/18 10:57 PM
1.12 Developing Java Programs Using Eclipse 47
Figure 1.15 The New Java Class dialog box is used to create a new Java class. Source: Copyright © 1995–2016 Oracle and/or its affiliates. All rights reserved. Used with permission.
Figure 1.16 You can edit a program and run it in NetBeans. Source: Copyright © 1995–2016 Oracle and/or its affiliates. All rights reserved. Used with permission.
Edit pane
Output pane
1.12 Developing Java Programs Using Eclipse You can edit, compile, run, and debug Java Programs using Eclipse.
The preceding section introduced developing Java programs using NetBeans. You can also use Eclipse to develop Java programs. This section gives the essential instructions to guide new users to create a project, create a class, and compile/run a class in Eclipse. For instructions on downloading and installing latest version of Eclipse, see Supplement II.D.
1.12.1 Creating a Java Project Before creating Java programs in Eclipse, you need to first create a project to hold all files. Here are the steps to create a Java project in Eclipse:
1. Choose File, New, Java Project to display the New Project wizard, as shown in Figure 1.17.
2. Type demo in the Project name field. As you type, the Location field is automatically set by default. You may customize the location for your project.
Point Key
VideoNote
Eclipse brief tutorial
M01_LIAN1878_11_GE_C01.indd 47 1/2/18 10:57 PM
48 Chapter 1 Introduction to Computers, Programs, and Java™
3. Make sure you selected the options Use project folder as root for sources and class files so the .java and .class files are in the same folder for easy access.
4. Click Finish to create the project, as shown in Figure 1.18.
Figure 1.17 The New Java Project dialog is for specifying a project name and the properties. Source: Eclipse Foundation, Inc.
Figure 1.18 A New Java project named demo is created. Source: Eclipse Foundation, Inc.
M01_LIAN1878_11_GE_C01.indd 48 1/2/18 10:57 PM
1.12 Developing Java Programs Using Eclipse 49
1.12.2 Creating a Java Class After a project is created, you can create Java programs in the project using the following steps:
1. Choose File, New, Class to display the New Java Class wizard.
2. Type Welcome in the Name field.
3. Check the option public static void main(String[] args).
4. Click Finish to generate the template for the source code Welcome.java, as shown in Figure 1.19.
1.12.3 Compiling and Running a Class To run the program, right-click the class in the project to display a context menu. Choose Run, Java Application in the context menu to run the class. The output is displayed in the Console pane, as shown in Figure 1.20. The Run command automatically compiles the program if the program has been changed.
Figure 1.19 The New Java Class dialog box is used to create a new Java class. Source: Eclipse Foundation, Inc.
M01_LIAN1878_11_GE_C01.indd 49 1/2/18 10:57 PM
54 Chapter 1 Introduction to Computers, Programs, and Java™
*1.13 (Algebra: solve 2 * 2 linear equations) You can use Cramer’s rule to solve the following 2 * 2 system of linear equation provided that ad – bc is not 0:
ax + by = e cx + dy = f
x = ed - bf ad - bc
y = af - ec ad - bc
Write a program that solves the following equation and displays the value for x and y: (Hint: replace the symbols in the formula with numbers to compute x and y. This exercise can be done in Chapter 1 without using materials in later chapters.)
3.4x + 50.2y = 44.5 2.1x + .55y = 5.9
Note More than 200 additional programming exercises with solutions are provided to the instructors on the Instructor Resource Website.
M01_LIAN1878_11_GE_C01.indd 54 1/2/18 10:57 PM
50 Chapter 1 Introduction to Computers, Programs, and Java™
Figure 1.20 You can edit a program and run it in Eclipse. Source: Eclipse Foundation, Inc.
Edit pane
Output pane
Key Terms Application Program Interface (API) 33 assembler 29 assembly language 29 bit 25 block 35 block comment 35 bus 24 byte 25 bytecode 38 bytecode verifier 40 cable modem 28 central processing unit (CPU) 25 class loader 40 comment 35 compiler 30 console 34 dot pitch 28 DSL (digital subscriber line) 28 encoding scheme 25 hardware 24 high-level language 30 integrated development environment
(IDE) 34 interpreter 30 java command 39 Java Development Toolkit (JDK) 34 Java language specification 33
Java Runtime Environment (JRE) 34 Java Virtual Machine (JVM) 38 javac command 39 keyword (or reserved word) 35 library 33 line comment 35 logic error 43 low-level language 30 machine language 29 main method 35 memory 26 dial-up modem 28 motherboard 25 network interface card (NIC) 28 operating system (OS) 31 pixel 28 program 24 programming 24 runtime error 43 screen resolution 28 software 24 source code 30 source program 30 statement 30 statement terminator 35 storage devices 26 syntax error 42
Note The above terms are defined in this chapter. Supplement I.A, Glossary, lists all the key terms and descriptions in the book, organized by chapters.
Supplement I.A
M01_LIAN1878_11_GE_C01.indd 50 1/2/18 10:57 PM
Chapter Summary 51
ChapTer summary 1. A computer is an electronic device that stores and processes data.
2. A computer includes both hardware and software.
3. Hardware is the physical aspect of the computer that can be touched.
4. Computer programs, known as software, are the invisible instructions that control the hardware and make it perform tasks.
5. Computer programming is the writing of instructions (i.e., code) for computers to perform.
6. The central processing unit (CPU) is a computer’s brain. It retrieves instructions from memory and executes them.
7. Computers use zeros and ones because digital devices have two stable states, referred to by convention as zero and one.
8. A bit is a binary digit 0 or 1.
9. A byte is a sequence of 8 bits.
10. A kilobyte is about 1,000 bytes, a megabyte about 1 million bytes, a gigabyte about 1 billion bytes, and a terabyte about 1,000 gigabytes.
11. Memory stores data and program instructions for the CPU to execute.
12. A memory unit is an ordered sequence of bytes.
13. Memory is volatile, because information is lost when the power is turned off.
14. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them.
15. The machine language is a set of primitive instructions built into every computer.
16. Assembly language is a low-level programming language in which a mnemonic is used to represent each machine-language instruction.
17. High-level languages are English-like and easy to learn and program.
18. A program written in a high-level language is called a source program.
19. A compiler is a software program that translates the source program into a machine- language program.
20. The operating system (OS) is a program that manages and controls a computer’s activities.
21. Java is platform independent, meaning you can write a program once and run it on any computer.
22. The Java source file name must match the public class name in the program. Java source- code files must end with the .java extension.
23. Every class is compiled into a separate bytecode file that has the same name as the class and ends with the .class extension.
24. To compile a Java source-code file from the command line, use the javac command.
M01_LIAN1878_11_GE_C01.indd 51 1/2/18 10:57 PM
52 Chapter 1 Introduction to Computers, Programs, and Java™
25. To run a Java class from the command line, use the java command.
26. Every Java program is a set of class definitions. The keyword class introduces a class definition. The contents of the class are included in a block.
27. A block begins with an opening brace ({) and ends with a closing brace (}).
28. Methods are contained in a class. To run a Java program, the program must have a main method. The main method is the entry point where the program starts when it is executed.
29. Every statement in Java ends with a semicolon (;), known as the statement terminator.
30. Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.
31. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or para- graph comment. Comments are ignored by the compiler.
32. Java source programs are case sensitive.
33. Programming errors can be categorized into three types: syntax errors, runtime errors, and logic errors. Errors reported by a compiler are called syntax errors or compile errors. Runtime errors are errors that cause a program to terminate abnormally. Logic errors occur when a program does not perform the way it was intended to.
Quiz Answer the quiz for this chapter at www.pearsonglobaleditions.com/Liang. Choose this book and click Companion Website to select Quiz.
programming exerCises
Pedagogical Note We cannot stress enough the importance of learning programming through exercises. For this reason, the book provides a large number of programming exercises at various levels of difficulty. The problems cover many application areas, including math, science, business, financial, gaming, animation, and multimedia. Solutions to most even- numbered programming exercises are on the Companion Website. Solutions to most odd-numbered programming exercises are on the Instructor Resource Website. The level of difficulty is rated easy (no star), moderate (*), hard (**), or challenging (***).
1.1 (Display three messages) Write a program that displays Welcome to Java, Learning Java Now, and Programming is fun.
1.2 (Display five messages) Write a program that displays I love Java five times. *1.3 (Display a pattern) Write a program that displays the following pattern:
J J aaa v vaaa J J aa v v a a J aaaa v aaaa
level of difficulty
M01_LIAN1878_11_GE_C01.indd 52 1/2/18 10:57 PM
Programming Exercises 53
1.4 (Print a table) Write a program that displays the following table:
a a^2 a^3 a^4 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256
1.5 (Compute expressions) Write a program that displays the result of 7.5 * 6.5 - 4.5 * 3
47.5 - 5.5 .
1.6 (Summation of a series) Write a program that displays the result of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10.
1.7 (Approximate p) p can be computed using the following formula:
p = 4 * ¢1 - 1 3
+ 1 5
- 1 7
+ 1 9
- 1 11
+ c ≤ Write a program that displays the result of 4 * ¢1 - 1
3 +
1 5
- 1 7
+ 1 9
- 1 11
≤ and 4 * ¢1 - 1
3 +
1 5
- 1 7
+ 1 9
- 1 11
+ 1 13
≤. Use 1.0 instead of 1 in your program.
1.8 (Area and perimeter of a circle) Write a program that displays the area and perim- eter of a circle that has a radius of 6.5 using the following formula:
p = 3.14159 perimeter = 2 * radius * p
area = radius * radius * p
1.9 (Area and perimeter of a rectangle) Write a program that displays the area and perim- eter of a rectangle with a width of 5.3 and height of 8.6 using the following formula:
area = width * height
perimeter = 2 * (width + height)
1.10 (Average speed in miles) Assume that a runner runs 15 kilometers in 50 minutes and 30 seconds. Write a program that displays the average speed in miles per hour. (Note that 1 mile is 1.6 kilometers.)
*1.11 (Population projection) The U.S. Census Bureau projects population based on the following assumptions:
■■ One birth every 7 seconds ■■ One death every 13 seconds ■■ One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume that the current population is 312,032,486, and one year has 365 days. Hint: In Java, if two integers perform division, the result is an integer. The fractional part is truncated. For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.
1.12 (Average speed in kilometers) Assume that a runner runs 24 miles in 1 hour, 40 minutes, and 35 seconds. Write a program that displays the average speed in kilometers per hour. (Note 1 mile is equal to 1.6 kilometers.)
M01_LIAN1878_11_GE_C01.indd 53 1/2/18 10:57 PM
Elementary Programming
Objectives ■■ To write Java programs to perform simple computations (§2.2).
■■ To obtain input from the console using the Scanner class (§2.3).
■■ To use identifiers to name variables, constants, methods, and classes (§2.4).
■■ To use variables to store data (§§2.5 and 2.6).
■■ To program with assignment statements and assignment expressions (§2.6).
■■ To use constants to store permanent data (§2.7).
■■ To name classes, methods, variables, and constants by following their naming conventions (§2.8).
■■ To explore Java numeric primitive data types: byte, short, int, long, float, and double (§2.9.1).
■■ To read a byte, short, int, long, float, or double value from the keyboard (§2.9.2).
■■ To perform operations using operators +, -, *, /, and % (§2.9.3).
■■ To perform exponent operations using Math.pow(a, b) (§2.9.4).
■■ To write integer literals, floating-point literals, and literals in scientific notation (§2.10).
■■ To write and evaluate numeric expressions (§2.11).
■■ To obtain the current system time using System.currentTimeMillis() (§2.12).
■■ To use augmented assignment operators (§2.13).
■■ To distinguish between postincrement and preincrement and between postdecrement and predecrement (§2.14).
■■ To cast the value of one type to another type (§2.15).
■■ To describe the software development process and apply it to develop the loan payment program (§2.16).
■■ To write a program that converts a large amount of money into smaller units (§2.17).
■■ To avoid common errors and pitfalls in elementary programming (§2.18).
Chapter
2
M02_LIAN1878_11_GE_C02.indd 55 12/29/17 3:47 PM
56 Chapter 2 Elementary Programming
2.1 Introduction The focus of this chapter is on learning elementary programming techniques to solve problems.
In Chapter 1, you learned how to create, compile, and run very basic Java programs. You will learn how to solve problems by writing programs. Through these problems, you will learn elementary programming using primitive data types, variables, constants, operators, expres- sions, and input and output.
Suppose, for example, you need to take out a student loan. Given the loan amount, loan term, and annual interest rate, can you write a program to compute the monthly payment and total payment? This chapter shows you how to write programs like this. Along the way, you will learn the basic steps that go into analyzing a problem, designing a solution, and implement- ing the solution by creating a program.
2.2 Writing a Simple Program Writing a program involves designing a strategy for solving the problem then using a programming language to implement that strategy.
Let’s first consider the simple problem of computing the area of a circle. How do we write a program for solving this problem?
Writing a program involves designing algorithms and translating algorithms into program- ming instructions, or code. An algorithm lists the steps you can follow to solve a problem. Algorithms can help the programmer plan a program before writing it in a programming language. Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code). The algorithm for calculating the area of a circle can be described as follows:
1. Read in the circle’s radius.
2. Compute the area using the following formula:
area = radius * radius * p
3. Display the result.
Tip It’s always a good practice to outline your program (or its underlying problem) in the form of an algorithm before you begin coding.
When you code—that is, when you write a program—you translate an algorithm into a program. You already know every Java program begins with a class definition in which the keyword class is followed by the class name. Assume you have chosen ComputeArea as the class name. The outline of the program would look as follows:
public class ComputeArea { // Details to be given later }
As you know, every Java program must have a main method where program execution begins. The program is then expanded as follows:
public class ComputeArea { public static void main(String[] args) { // Step 1: Read in radius
// Step 2: Compute area
Point Key
Point Key
problem
algorithm
pseudocode
M02_LIAN1878_11_GE_C02.indd 56 12/29/17 3:47 PM
2.2 Writing a Simple Program 57
// Step 3: Display the area } }
The program needs to read the radius entered by the user from the keyboard. This raises two important issues:
■■ Reading the radius
■■ Storing the radius in the program
Let’s address the second issue first. In order to store the radius, the program needs to declare a symbol called a variable. A variable represents a value stored in the computer’s memory.
Rather than using x and y as variable names, choose descriptive names: in this case, radius for radius and area for area. To let the compiler know what radius and area are, specify their data types. That is the kind of data stored in a variable, whether an integer, real number, or something else. This is known as declaring variables. Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as primitive data types or fundamental types.
Real numbers (i.e., numbers with a decimal point) are represented using a method known as floating-point in computers. Therefore, the real numbers are also called floating-point numbers. In Java, you can use the keyword double to declare a floating-point variable. Declare radius and area as double. The program can be expanded as follows:
public class ComputeArea { public static void main(String[] args) { double radius; double area;
// Step 1: Read in radius
// Step 2: Compute area
// Step 3: Display the area } }
The program declares radius and area as variables. The reserved word double indicates that radius and area are floating-point values stored in the computer.
The first step is to prompt the user to designate the circle’s radius. You will soon learn how to prompt the user for information. For now, to learn how variables work, you can assign a fixed value to radius in the program as you write the code. Later, you’ll modify the program to prompt the user for this value.
The second step is to compute area by assigning the result of the expression radius * radius * 3.14159 to area.
In the final step, the program will display the value of area on the console by using the System.out.println method.
Listing 2.1 shows the complete program, and a sample run of the program is shown in Figure 2.1.
lisiTing 2.1 ComputeArea.java 1 public class ComputeArea { 2 public static void main(String[] args) { 3 double radius; // Declare radius 4 double area; // Declare area 5 6 // Assign a radius 7 radius = 20; // radius is now 20
variable descriptive names
declare variables data type
primitive data types
floating-point
M02_LIAN1878_11_GE_C02.indd 57 12/29/17 3:47 PM
58 Chapter 2 Elementary Programming
8 9 // Compute area 10 area = radius * radius * 3.14159; 11 12 // Display results 13 System.out.println("The area for the circle of radius " + 14 radius + " is " + area); 15 } 16 }
Figure 2.1 The program displays the area of a circle.
Compile
Run
Variables such as radius and area correspond to memory locations. Every variable has a name, a type, and a value. Line 3 declares that radius can store a double value. The value is not defined until you assign a value. Line 7 assigns 20 into the variable radius. Similarly, line 4 declares the variable area, and line 10 assigns a value into area. The following table shows the value in the memory for area and radius as the program is executed. Each row in the table shows the values of variables after the statement in the corresponding line in the program is executed. This method of reviewing how a program works is called tracing a pro- gram. Tracing programs are helpful for understanding how programs work, and they are useful tools for finding errors in programs.
line# radius area
3 no value
4 no value
7 20
10 1256.636
The plus sign (+) has two meanings: one for addition, and the other for concatenating (com- bining) strings. The plus sign (+) in lines 13–14 is called a string concatenation operator. It combines two strings into one. If a string is combined with a number, the number is converted into a string and concatenated with the other string. Therefore, the plus signs (+) in lines 13–14 concatenate strings into a longer string, which is then displayed in the output. Strings and string concatenation will be discussed further in Chapter 4.
Caution A string cannot cross lines in the source code. Thus, the following statement would result in a compile error:
System.out.println("Introduction to Java Programming, by Y. Daniel Liang");
To fix the error, break the string into separate substrings, and use the concatenation operator (+) to combine them:
System.out.println("Introduction to Java Programming, " + "by Y. Daniel Liang");
declare variable assign value
tracing program
concatenate strings with numbers
concatenate strings
break a long string
M02_LIAN1878_11_GE_C02.indd 58 12/29/17 3:47 PM
2.3 Reading Input from the Console 59
2.2.1 Identify and fix the errors in the following code:
1 public class Test { 2 public void main(string[] args) { 3 double i = 50.0; 4 double k = i + 50.0; 5 double j = k + 1; 6 7 System.out.println("j is " + j + " and 8 k is " + k); 9 } 10 }
2.3 Reading Input from the Console Reading input from the console enables the program to accept input from the user.
In Listing 2.1, the radius is fixed in the source code. To use a different radius, you have to modify the source code and recompile it. Obviously, this is not convenient, so instead you can use the Scanner class for console input.
Java uses System.out to refer to the standard output device, and System.in to the standard input device. By default, the output device is the display monitor, and the input device is the keyboard. To perform console output, you simply use the println method to display a primitive value or a string to the console. To perform console input, you need to use the Scanner class to create an object to read input from System.in, as follows:
Scanner input = new Scanner(System.in);
The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner input declares that input is a variable whose type is Scanner. The whole line Scanner input = new Scanner(System.in) creates a Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method on an object is to ask the object to perform a task. You can invoke the nextDouble() method to read a double value as follows:
double radius = input.nextDouble();
This statement reads a number from the keyboard and assigns the number to radius. Listing 2.2 rewrites Listing 2.1 to prompt the user to enter a radius.
Listing 2.2 ComputeAreaWithConsoleInput.java 1 import java.util.Scanner; // Scanner is in the java.util package 2 3 public class ComputeAreaWithConsoleInput { 4 public static void main(String[] args) { 5 // Create a Scanner object 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter a radius 9 System.out.print("Enter a number for radius: "); 10 double radius = input.nextDouble(); 11 12 // Compute area 13 double area = radius * radius * 3.14159; 14 15 // Display results 16 System.out.println("The area for the circle of radius " +
Point Check
Point Key
VideoNote Obtain input
import class
read a double
create a Scanner
M02_LIAN1878_11_GE_C02.indd 59 12/29/17 3:47 PM
60 Chapter 2 Elementary Programming
17 radius + " is " + area); 18 } 19 }
Enter a number for radius: 2.5
The area for the circle of radius 2.5 is 19.6349375
Enter a number for radius: 23
The area for the circle of radius 23.0 is 1661.90111
The Scanner class is in the java.util package. It is imported in line 1. Line 6 creates a Scanner object. Note the import statement can be omitted if you replace Scanner by java. util.Scanner in line 6.
Line 9 displays a string "Enter a number for radius: " to the console. This is known as a prompt, because it directs the user to enter an input. Your program should always tell the user what to enter when expecting input from the keyboard.
Recall that the print method in line 9 is identical to the println method, except that println moves to the beginning of the next line after displaying the string, but print does not advance to the next line when completed.
Line 6 creates a Scanner object. The statement in line 10 reads input from the keyboard.
double radius = input.nextDouble();
After the user enters a number and presses the Enter key, the program reads the number and assigns it to radius.
More details on objects will be introduced in Chapter 9. For the time being, simply accept that this is how we obtain input from the console.
The Scanner class is in the java.util package. It is imported in line 1. There are two types of import statements: specific import and wildcard import. The specific import specifies a single class in the import statement. For example, the following statement imports Scanner from the package java.util.
import java.util.Scanner;
The wildcard import imports all the classes in a package by using the asterisk as the wildcard. For example, the following statement imports all the classes from the package java.util.
import java.util.*;
The information for the classes in an imported package is not read in at compile time or runtime unless the class is used in the program. The import statement simply tells the compiler where to locate the classes. There is no performance difference between a specific import and a wildcard import declaration.
Listing 2.3 gives an example of reading multiple inputs from the keyboard. The program reads three numbers and displays their average.
Listing 2.3 ComputeAverage.java 1 import java.util.Scanner; // Scanner is in the java.util package 2 3 public class ComputeAverage { 4 public static void main(String[] args) { 5 // Create a Scanner object 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter three numbers 9 System.out.print("Enter three numbers: ");
prompt
specific import
wildcard import
no performance difference
import class
create a Scanner
M02_LIAN1878_11_GE_C02.indd 60 12/29/17 3:47 PM
2.3 Reading Input from the Console 61
10 double number1 = input.nextDouble(); 11 double number2 = input.nextDouble(); 12 double number3 = input.nextDouble(); 13 14 // Compute average 15 double average = (number1 + number2 + number3) / 3; 16 17 // Display results 18 System.out.println("The average of " + number1 + " " + number2 19 + " " + number3 + " is " + average); 20 } 21 }
read a double
enter input in one line
enter input in multiple lines
Enter three numbers: 10.5
11
11.5
The average of 10.5 11.0 11.5 is 11.0
Enter three numbers: 1 2 3
The average of 1.0 2.0 3.0 is 2.0
The codes for importing the Scanner class (line 1) and creating a Scanner object (line 6) are the same as in the preceding example, as well as in all new programs you will write for reading input from the keyboard.
Line 9 prompts the user to enter three numbers. The numbers are read in lines 10–12. You may enter three numbers separated by spaces, then press the Enter key, or enter each number followed by a press of the Enter key, as shown in the sample runs of this program.
If you entered an input other than a numeric value, a runtime error would occur. In Chapter 12, you will learn how to handle the exception so the program can continue to run.
Note Most of the programs in the early chapters of this book perform three steps— input, process, and output—called IPO. Input is receiving input from the user; process is pro- ducing results using the input; and output is displaying the results.
Note If you use an IDE such as Eclipse or NetBeans, you will get a warning to ask you to close the input for preventing a potential resource leak. Ignore the warning for the time being because the input is automatically closed when your program is terminated. In this case, there will be no resource leaking.
2.3.1 How do you write a statement to let the user enter a double value from the keyboard? What happens if you entered 5a when executing the following code?
double radius = input.nextDouble();
2.3.2 Are there any performance differences between the following two import statements?
import java.util.Scanner; import java.util.*;
runtime error
IPO
Warning in IDE
Point Check
M02_LIAN1878_11_GE_C02.indd 61 12/29/17 3:48 PM
62 Chapter 2 Elementary Programming
2.4 Identifiers Identifiers are the names that identify the elements such as classes, methods, and variables in a program.
As you see in Listing 2.3, ComputeAverage, main, input, number1, number2, number3, and so on are the names of things that appear in the program. In programming terminology, such names are called identifiers. All identifiers must obey the following rules:
■■ An identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($).
■■ An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit.
■■ An identifier cannot be a reserved word. (See Appendix A for a list of reserved words.)
■■ An identifier cannot be true, false, or null.
■■ An identifier can be of any length.
For example, $2, ComputeArea, area, radius, and print are legal identifiers, whereas 2A and d+4 are not because they do not follow the rules. The Java compiler detects illegal identifiers and reports syntax errors.
Note Since Java is case sensitive, area, Area, and AREA are all different identifiers.
Tip Identifiers are for naming variables, methods, classes, and other items in a program. Descriptive identifiers make programs easy to read. Avoid using abbreviations for identi- fiers. Using complete words is more descriptive. For example, numberOfStudents is better than numStuds, numOfStuds, or numOfStudents. We use descriptive names for complete programs in the text. However, we will occasionally use variable names such as i, j, k, x, and y in the code snippets for brevity. These names also provide a generic tone to the code snippets.
Tip Do not name identifiers with the $ character. By convention, the $ character should be used only in mechanically generated source code.
2.4.1 Which of the following identifiers are valid? Which are Java keywords?
miles, Test, a++, ––a, 4#R, $4, #44, apps class, public, int, x, y, radius
2.5 Variables Variables are used to represent values that may be changed in the program.
As you see from the programs in the preceding sections, variables are used to store values to be used later in a program. They are called variables because their values can be changed. In the program in Listing 2.2, radius and area are variables of the double type. You can assign any numerical value to radius and area, and the values of radius and area can be reassigned. For example, in the following code, radius is initially 1.0 (line 2) then changed to 2.0 (line 7), and area is set to 3.14159 (line 3) then reset to 12.56636 (line 8).
Point Key
identifiers
identifier naming rules
case sensitive
descriptive names
the $ character
Point Check
Point Key
why called variables?
M02_LIAN1878_11_GE_C02.indd 62 12/29/17 3:48 PM
2.5 Variables 63
1 // Compute the first area 2 radius = 1.0; radius: 1.0 3 area = radius * radius * 3.14159; area: 3.14159 4 System.out.println("The area is " + area + " for radius " + radius); 5 6 // Compute the second area 7 radius = 2.0; radius: 2.0 8 area = radius * radius * 3.14159; area: 12.56636 9 System.out.println("The area is " + area + " for radius " + radius);
Variables are for representing data of a certain type. To use a variable, you declare it by telling the compiler its name as well as what type of data it can store. The variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. The syntax for declaring a variable is
datatype variableName;
Here are some examples of variable declarations:
int count; // Declare count to be an integer variable double radius; // Declare radius to be a double variable double interestRate; // Declare interestRate to be a double variable
These examples use the data types int and double. Later you will be introduced to additional data types, such as byte, short, long, float, char, and boolean.
If variables are of the same type, they can be declared together, as follows:
datatype variable1, variable2, ..., variablen;
The variables are separated by commas. For example,
int i, j, k; // Declare i, j, and k as int variables
Variables often have initial values. You can declare a variable and initialize it in one step. Consider, for instance, the following code:
int count = 1;
This is equivalent to the next two statements:
int count; count = 1;
You can also use a shorthand form to declare and initialize variables of the same type together. For example,
int i = 1, j = 2;
Tip A variable must be declared before it can be assigned a value. A variable declared in a method must be assigned a value before it can be used.
Whenever possible, declare a variable and assign its initial value in one step. This will make the program easy to read and avoid programming errors.
Every variable has a scope. The scope of a variable is the part of the program where the variable can be referenced. The rules that define the scope of a variable will be gradually introduced later in the book. For now, all you need to know is that a variable must be declared and initialized before it can be used.
declare variable
initialize variables
M02_LIAN1878_11_GE_C02.indd 63 12/29/17 3:48 PM
64 Chapter 2 Elementary Programming
2.5.1 Identify and fix the errors in the following code:
1 public class Test { 2 public static void main(String[] args) { 3 int i = k + 2; 4 System.out.println(i); 5 } 6 }
2.6 Assignment Statements and Assignment Expressions
An assignment statement designates a value for a variable. An assignment statement can be used as an expression in Java.
After a variable is declared, you can assign a value to it by using an assignment statement. In Java, the equal sign (=) is used as the assignment operator. The syntax for assignment state- ments is as follows:
variable = expression;
An expression represents a computation involving values, variables, and operators that, taking them together, evaluates to a value. For example, consider the following code:
int y = 1; // Assign 1 to variable y double radius = 1.0; // Assign 1.0 to variable radius int x = 5 * (3 / 2); // Assign the value of the expression to x x = y + 1; // Assign the addition of y and 1 to x double area = radius * radius * 3.14159; // Compute area
You can use a variable in an expression. A variable can also be used in both sides of the = operator. For example,
x = x + 1;
In this assignment statement, the result of x + 1 is assigned to x. If x is 1 before the statement is executed, then it becomes 2 after the statement is executed.
To assign a value to a variable, you must place the variable name to the left of the assign- ment operator. Thus, the following statement is wrong:
1 = x; // Wrong
Note In mathematics, x = 2 * x + 1 denotes an equation. However, in Java, x = 2 * x + 1 is an assignment statement that evaluates the expression 2 * x + 1 and assigns the result to x.
In Java, an assignment statement is essentially an expression that evaluates to the value to be assigned to the variable on the left side of the assignment operator. For this reason, an assignment statement is also known as an assignment expression. For example, the following statement is correct:
System.out.println(x = 1);
which is equivalent to
x = 1; System.out.println(x);
Point Check
Point Key
assignment statement assignment operator
expression
assignment expression
M02_LIAN1878_11_GE_C02.indd 64 12/29/17 3:48 PM
2.7 Named Constants 65
If a value is assigned to multiple variables, you can use the following syntax:
i = j = k = 1;
which is equivalent to
k = 1; j = k; i = j;
Note In an assignment statement, the data type of the variable on the left must be compatible with the data type of the value on the right. For example, int x = 1.0 would be ille- gal, because the data type of x is int. You cannot assign a double value (1.0) to an int variable without using type casting. Type casting will be introduced in Section 2.15.
2.6.1 Identify and fix the errors in the following code:
1 public class Test { 2 public static void main(String[] args) { 3 int i = j = k = 2; 4 System.out.println(i + " " + j + " " + k); 5 } 6 }
2.7 Named Constants A named constant is an identifier that represents a permanent value.
The value of a variable may change during the execution of a program, but a named constant, or simply constant, represents permanent data that never changes. A constant is also known as a final variable in Java. In our ComputeArea program, p is a constant. If you use it frequently, you don’t want to keep typing 3.14159; instead, you can declare a constant for p. Here is the syntax for declaring a constant:
final datatype CONSTANTNAME = value;
A constant must be declared and initialized in the same statement. The word final is a Java keyword for declaring a constant. By convention, all letters in a constant are in uppercase. For example, you can declare p as a constant and rewrite Listing 2.2, as in Listing 2.4.
Listing 2.4 ComputeAreaWithConstant.java
1 import java.util.Scanner; // Scanner is in the java.util package 2 3 public class ComputeAreaWithConstant { 4 public static void main(String[] args) { 5 final double PI = 3.14159; // Declare a constant 6 7 // Create a Scanner object 8 Scanner input = new Scanner(System.in); 9 10 // Prompt the user to enter a radius 11 System.out.print("Enter a number for radius: "); 12 double radius = input.nextDouble(); 13 14 // Compute area
Point Check
Point Key
constant
final keyword
M02_LIAN1878_11_GE_C02.indd 65 12/29/17 3:48 PM
66 Chapter 2 Elementary Programming
15 double area = radius * radius * PI; 16 17 // Display result 18 System.out.println("The area for the circle of radius " + 19 radius + " is " + area); 20 } 21 }
There are three benefits of using constants: (1) you don’t have to repeatedly type the same value if it is used multiple times; (2) if you have to change the constant value (e.g., from 3.14 to 3.14159 for PI), you need to change it only in a single location in the source code; and (3) a descriptive name for a constant makes the program easy to read.
2.7.1 What are the benefits of using constants? Declare an int constant SIZE with value 20.
2.8 Naming Conventions Sticking with the Java naming conventions makes your programs easy to read and avoids errors.
Make sure you choose descriptive names with straightforward meanings for the vari- ables, constants, classes, and methods in your program. As mentioned earlier, names are case sensitive. Listed below are the conventions for naming variables, methods, and classes.
■■ Use lowercase for variables and methods—for example, the variables radius and area, and the method print. If a name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each sub- sequent word—for example, the variable numberOfStudents. This naming style is known as the camelCase because the uppercase characters in the name resemble a camel’s humps.
■■ Capitalize the first letter of each word in a class name—for example, the class names ComputeArea and System.
■■ Capitalize every letter in a constant, and use underscores between words—for exam- ple, the constants PI and MAX_VALUE.
It is important to follow the naming conventions to make your programs easy to read.
Caution Do not choose class names that are already used in the Java library. For example, since the System class is defined in Java, you should not name your class System.
2.8.1 What are the naming conventions for class names, method names, constants, and variables? Which of the following items can be a constant, a method, a variable, or a class according to the Java naming conventions?
MAX_VALUE, Test, read, readDouble
2.8.2 Translate the following algorithm into Java code: Step 1: Declare a double variable named miles with an initial value 100.
Step 2: Declare a double constant named KILOMETERS_PER_MILE with value 1.609.
Step 3: Declare a double variable named kilometers, multiply miles and KILOMETERS_PER_MILE, and assign the result to kilometers.
benefits of constants
Point Check
Point Key
name variables and methods
name classes
name constants
name classes
Point Check
M02_LIAN1878_11_GE_C02.indd 66 12/29/17 3:48 PM
2.9 Numeric Data Types and Operations 67
Step 4: Display kilometers to the console.
What is kilometers after Step 4?
2.9 Numeric Data Types and Operations Java has six numeric types for integers and floating-point numbers with operators +, -, *, /, and %.
2.9.1 Numeric Types Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. Java provides eight primitive data types for numeric values, characters, and Boolean values. This section introduces numeric data types and operators.
Table 2.1 lists the six numeric data types, their ranges, and their storage sizes.
Point Key
byte type
short type
int type
long type
float type
double type
Name Range Storage Size
byte -27 to 27 -1 (-128 to 127) 8-bit signed short -215 to 215 -1 (-32768 to 32767) 16-bit signed int -231 to 231 -1 (-2147483648 to 2147483647) 32-bit signed long -263 to 263-1 64-bit signed
(i.e., -9223372036854775808 to 9223372036854775807) float Negative range: -3.4028235E + 38 to -1.4E -45 32-bit IEEE 754
Positive range: 1.4E -45 to 3.4028235E+38 double Negative range: -1.7976931348623157E+308 to -4.9E -324 64-bit IEEE 754
Positive range: 4.9E -324 to 1.7976931348623157E+308
tabLe 2.1 Numeric Data Types
Note IEEE 754 is a standard approved by the Institute of Electrical and Electronics Engineers for representing floating-point numbers on computers. The standard has been widely adopted. Java uses the 32-bit IEEE 754 for the float type and the 64-bit IEEE 754 for the double type. The IEEE 754 standard also defines special floating-point values, which are listed in Appendix E.
Java uses four types for integers: byte, short, int, and long. Choose the type that is most appropriate for your variable. For example, if you know an integer stored in a variable is within a range of a byte, declare the variable as a byte. For simplicity and consistency, we will use int for integers most of the time in this book.
Java uses two types for floating-point numbers: float and double. The double type is twice as big as float, so the double is known as double precision, and float as single preci- sion. Normally, you should use the double type, because it is more accurate than the float type.
2.9.2 Reading Numbers from the Keyboard You know how to use the nextDouble() method in the Scanner class to read a double value from the keyboard. You can also use the methods listed in Table 2.2 to read a number of the byte, short, int, long, and float type.
integer types
floating-point types
M02_LIAN1878_11_GE_C02.indd 67 12/29/17 3:48 PM
68 Chapter 2 Elementary Programming
Here are examples for reading values of various types from the keyboard:
1 Scanner input = new Scanner(System.in); 2 System.out.print("Enter a byte value: "); 3 byte byteValue = input.nextByte(); 4 5 System.out.print("Enter a short value: "); 6 short shortValue = input.nextShort(); 7 8 System.out.print("Enter an int value: "); 9 int intValue = input.nextInt(); 10 11 System.out.print("Enter a long value: "); 12 long longValue = input.nextLong(); 13 14 System.out.print("Enter a float value: "); 15 float floatValue = input.nextFloat();
If you enter a value with an incorrect range or format, a runtime error would occur. For example, if you enter a value 128 for line 3, an error would occur because 128 is out of range for a byte type integer.
2.9.3 Numeric Operators The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and remainder (%), as listed in Table 2.3. The operands are the values operated by an operator.operands
operators +, -, *, /, and %
Method Description
nextByte() reads an integer of the byte type.
nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
tabLe 2.2 Methods for Scanner Objects
Name Meaning Example Result
+ Addition 34 + 1 35 - Subtraction 34.0 - 0.1 33.9 * Multiplication 300*30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
tabLe 2.3 Numeric Operators
When both operands of a division are integers, the result of the division is the quotient and the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields –2, not –2.5. To perform a floating-point division, one of the operands must be a floating-point number. For example, 5.0 / 2 yields 2.5.
The % operator, known as remainder, yields the remainder after division. The operand on the left is the dividend, and the operand on the right is the divisor. Therefore, 7 % 3 yields 1, 3 % 7 yields 3, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.
integer division
M02_LIAN1878_11_GE_C02.indd 68 12/29/17 3:48 PM
2.9 Numeric Data Types and Operations 69
Enter an integer for seconds: 500
500 seconds is 8 minutes and 20 seconds
4 12
12 0
3
8 26
24 2
3
Remainder
Quotient
Divisor Dividend13 20
13 7
1
7 3
0 3
0
3 7
6 1
2
The % operator is often used for positive integers, but it can also be used with negative inte- gers and floating-point values. The remainder is negative only if the dividend is negative. For example, -7 % 3 yields -1, -12 % 4 yields 0, -26 % -8 yields -2, and 20 % -13 yields 7.
Remainder is very useful in programming. For example, an even number % 2 is always 0 and a positive odd number % 2 is always 1. Thus, you can use this property to determine whether a number is even or odd. If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends are going to meet in 10 days. What will be the day in 10 days? You can find that the day is Tuesday using the following expression:
(6 + 10) % 7 is 2
After 10 days
Day 2 in a week is Tuesday Note: Day 0 in a week is Sunday
A week has 7 days Day 6 in a week is Saturday
The program in Listing 2.5 obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.
Listing 2.5 DisplayTime.java 1 import java.util.Scanner; 2 3 public class DisplayTime { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 // Prompt the user for input 7 System.out.print("Enter an integer for seconds: "); 8 int seconds = input.nextInt(); 9 10 int minutes = seconds / 60; // Find minutes in seconds 11 int remainingSeconds = seconds % 60; // Seconds remaining 12 System.out.println(seconds + " seconds is " + minutes + 13 " minutes and " + remainingSeconds + " seconds"); 14 } 15 }
import Scanner
create a Scanner
read an integer
divide remainder
line# seconds minutes remainingSeconds
8 500
10 8
11 20
The nextInt() method (line 8) reads an integer for seconds. Line 10 obtains the minutes using seconds / 60. Line 11 (seconds % 60) obtains the remaining seconds after taking away the minutes.
M02_LIAN1878_11_GE_C02.indd 69 12/29/17 3:48 PM
70 Chapter 2 Elementary Programming
The + and - operators can be both unary and binary. A unary operator has only one operand; a binary operator has two. For example, the – operator in –5 is a unary operator to negate number 5, whereas the – operator in 4 – 5 is a binary operator for subtracting 5 from 4.
2.9.4 Exponent Operations The Math.pow(a, b) method can be used to compute ab. The pow method is defined in the Math class in the Java API. You invoke the method using the syntax Math.pow(a, b) (e.g., Math.pow(2, 3)), which returns the result of ab (23). Here, a and b are parameters for the pow method and the numbers 2 and 3 are actual values used to invoke the method. For example,
System.out.println(Math.pow(2, 3)); // Displays 8.0 System.out.println(Math.pow(4, 0.5)); // Displays 2.0 System.out.println(Math.pow(2.5, 2)); // Displays 6.25 System.out.println(Math.pow(2.5, –2)); // Displays 0.16
Chapter 6 introduces more details on methods. For now, all you need to know is how to invoke the pow method to perform the exponent operation.
2.9.1 Find the largest and smallest byte, short, int, long, float, and double. Which of these data types requires the least amount of memory?
2.9.2 Show the result of the following remainders: 56 % 6 78 % -4 -34 % 5 -34 % -5 5 % 1 1 % 5
2.9.3 If today is Tuesday, what will be the day in 100 days? 2.9.4 What is the result of 25 / 4? How would you rewrite the expression if you wished
the result to be a floating-point number?
2.9.5 Show the result of the following code: System.out.println(2 * (5 / 2 + 5 / 2)); System.out.println(2 * 5 / 2 + 2 * 5 / 2); System.out.println(2 * (5 / 2)); System.out.println(2 * 5 / 2);
2.9.6 Are the following statements correct? If so, show the output. System.out.println("25 / 4 is " + 25 / 4); System.out.println("25 / 4.0 is " + 25 / 4.0); System.out.println("3 * 2 / 4 is " + 3 * 2 / 4); System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);
2.9.7 Write a statement to display the result of 23.5. 2.9.8 Suppose m and r are integers. Write a Java expression for mr2 to obtain a floating-point
result.
2.10 Numeric Literals A literal is a constant value that appears directly in a program.
For example, 34 and 0.305 are literals in the following statements:
int numberOfYears = 34; double weight = 0.305;
unary operator
binary operator
Math.pow(a, b) method
Point Check
Point Key
literal
M02_LIAN1878_11_GE_C02.indd 70 12/29/17 3:48 PM
2.10 Numeric Literals 71
2.10.1 Integer Literals An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compile error will occur if the literal is too large for the variable to hold. The statement byte b = 128, for example, will cause a compile error, because 128 cannot be stored in a variable of the byte type. (Note the range for a byte value is from –128 to 127.)
An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) and 231 -1 (2147483647). To denote an integer literal of the long type, append the letter L or l to it. For example, to write integer 2147483648 in a Java program, you have to write it as 2147483648L or 2147483648l, because 2147483648 exceeds the range for the int value. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).
Note By default, an integer literal is a decimal integer number. To denote a binary integer literal, use a leading 0b or 0B (zero B); to denote an octal integer literal, use a leading 0 (zero); and to denote a hexadecimal integer literal, use a leading 0x or 0X (zero X). For example,
System.out.println(0B1111); // Displays 15 System.out.println(07777); // Displays 4095 System.out.println(0XFFFF); // Displays 65535
Hexadecimal numbers, binary numbers, and octal numbers will be introduced in Appendix F.
Note To improve readability, Java allows you to use underscores between two digits in a number literal. For example, the following literals are correct.
long ssn = 232_45_4519; long creditCardNumber = 2324_4545_4519_3415L;
However, 45_ or _45 is incorrect. The underscore must be placed between two digits.
2.10.2 Floating-Point Literals Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and you can make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.
Note The double type values are more accurate than the float type values. For example,
System.out.println("1.0 / 3.0 is " + 1.0 / 3.0);
displays 1.0 / 3.0 is 0 .3333333333333333
16 digits
System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F);
displays 1.0F / 3.0F is 0.33333334
8 digits
A float value has 7–8 numbers of significant digits, and a double value has 15–17 numbers of significant digits.
binary, octal, and hex literals
underscores in numbers
suffix f or F suffix d or D
double vs. floatxs
M02_LIAN1878_11_GE_C02.indd 71 12/29/17 3:48 PM
72 Chapter 2 Elementary Programming
2.10.3 Scientific Notation Floating-point literals can be written in scientific notation in the form of a * 10b. For example, the scientific notation for 123.456 is 1.23456 * 102 and for 0.0123456 is 1.23456 * 10-2. A special syntax is used to write scientific notation numbers. For example, 1.23456 * 102 is written as 1.23456E2 or 1.23456E+2 and 1.23456 * 10-2 as 1.23456E-2. E (or e) repre- sents an exponent, and can be in either lowercase or uppercase.
Note The float and double types are used to represent numbers with a decimal point. Why are they called floating-point numbers? These numbers are stored in scientific notation internally. When a number such as 50.534 is converted into scientific notation, such as 5.0534E+1, its decimal point is moved (i.e., floated) to a new position.
2.10.1 How many accurate digits are stored in a float or double type variable?
2.10.2 Which of the following are correct literals for floating-point numbers? 12.3, 12.3e+2, 23.4e-2, –334.4, 20.5, 39F, 40D
2.10.3 Which of the following are the same as 52.534? 5.2534e+1, 0.52534e+2, 525.34e-1, 5.2534e+0
2.10.4 Which of the following are correct literals? 5_2534e+1, _2534, 5_2, 5_
2.11 Evaluating Expressions and Operator Precedence Java expressions are evaluated in the same way as arithmetic expressions.
Writing a numeric expression in Java involves a straightforward translation of an arithmetic expression using Java operators. For example, the arithmetic expression
3 + 4x 5
- 10(y - 5)(a + b + c)
x + 9¢ 4
x +
9 + x y
≤ can be translated into a Java expression as follows:
(3 + 4 * x) / 5 – 10 * (y - 5) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y)
Although Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression is the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression. Operators contained within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the expression in the inner parentheses is evaluated first. When more than one operator is used in an expression, the following operator precedence rule is used to determine the order of evaluation:.
■■ Multiplication, division, and remainder operators are applied first. If an expression contains several multiplication, division, and remainder operators, they are applied from left to right.
■■ Addition and subtraction operators are applied last. If an expression contains several addition and subtraction operators, they are applied from left to right.
why called floating-point?
Point Check
Point Key
evaluating an expression
operator precedence rule
M02_LIAN1878_11_GE_C02.indd 72 12/29/17 3:48 PM
2.11 Evaluating Expressions and Operator Precedence 73
Here is an example of how an expression is evaluated:
3 + 4 * 4 + 5 * (4 + 3) - 1
3 + 4 * 4 + 5 * 7 – 1
3 + 16 + 5 * 7 – 1
3 + 16 + 35 – 1
19 + 35 – 1
54 – 1
53
(1) inside parentheses �rst
(2) multiplication
(3) multiplication
(4) addition
(5) addition
(6) subtraction
Listing 2.6 gives a program that converts a Fahrenheit degree to Celsius using the formula Celsius = (59 )(Fahrenheit - 32).
Listing 2.6 FahrenheitToCelsius.java 1 import java.util.Scanner; 2 3 public class FahrenheitToCelsius { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 System.out.print("Enter a degree in Fahrenheit: "); 8 double fahrenheit = input.nextDouble(); 9 10 // Convert Fahrenheit to Celsius 11 double celsius = (5.0 / 9) * (fahrenheit - 32); 12 System.out.println("Fahrenheit " + fahrenheit + " is " + 13 celsius + " in Celsius"); 14 } 15 }
divide
Enter a degree in Fahrenheit: 100
Fahrenheit 100.0 is 37.77777777777778 in Celsius
line# fahrenheit celsius 8 100
11 37.77777777777778
Be careful when applying division. Division of two integers yields an integer in Java. 59 is coded 5.0 / 9 instead of 5 / 9 in line 11, because 5 / 9 yields 0 in Java.
2.11.1 How would you write the following arithmetic expressions in Java?
a. 4
3(r + 34) - 9(a + bc) +
3 + d(2 + a) a + bd
b. 5.5 * (r + 2.5)2.5 + t
integer vs. floating-point division
Point Check
M02_LIAN1878_11_GE_C02.indd 73 12/29/17 3:48 PM
74 Chapter 2 Elementary Programming
2.12 Case Study: Displaying the Current Time You can invoke System.currentTimeMillis() to return the current time.
The problem is to develop a program that displays the current time in GMT (Greenwich Mean Time) in the format hour:minute:second, such as 13:19:8.
The currentTimeMillis method in the System class returns the current time in milli- seconds elapsed since the time midnight, January 1, 1970 GMT, as shown in Figure 2.2. This time is known as the UNIX epoch. The epoch is the point when time starts, and 1970 was the year when the UNIX operating system was formally introduced.
Point Key
VideoNote
Use operators / and %
currentTimeMillis UNIX epoch
Figure 2.2 The System.currentTimeMillis() returns the number of milliseconds since the UNIX epoch.
UNIX epoch 01-01-1970
00:00:00 GMT
Elapsed time
Current time System.currentTimeMillis()
Time
You can use this method to obtain the current time, then compute the current second, min- ute, and hour as follows:
1. Obtain the total milliseconds since midnight, January 1, 1970, in totalMilliseconds by invoking System.currentTimeMillis() (e.g., 1203183068328 milliseconds).
2. Obtain the total seconds totalSeconds by dividing totalMilliseconds by 1000 (e.g., 1203183068328 milliseconds / 1000 = 1203183068 seconds).
3. Compute the current second from totalSeconds % 60 (e.g., 1203183068 seconds % 60 = 8, which is the current second).
4. Obtain the total minutes totalMinutes by dividing totalSeconds by 60 (e.g., 1203183068 seconds / 60 = 20053051 minutes).
5. Compute the current minute from totalMinutes % 60 (e.g., 20053051 minutes % 60 = 31, which is the current minute).
6. Obtain the total hours totalHours by dividing totalMinutes by 60 (e.g., 20053051 minutes / 60 = 334217 hours).
7. Compute the current hour from totalHours % 24 (e.g., 334217 hours % 24 = 17, which is the current hour).
Listing 2.7 gives the complete program.
Listing 2.7 ShowCurrentTime.java 1 public class ShowCurrentTime { 2 public static void main(String[] args) { 3 // Obtain the total milliseconds since midnight, Jan 1, 1970 4 long totalMilliseconds = System.currentTimeMillis(); 5 6 // Obtain the total seconds since midnight, Jan 1, 1970 7 long totalSeconds = totalMilliseconds / 1000; 8 9 // Compute the current second in the minute in the hour 10 long currentSecond = totalSeconds % 60; 11
currentSecond
totalSeconds
totalMilliseconds
M02_LIAN1878_11_GE_C02.indd 74 12/29/17 3:48 PM
2.12 Case Study: Displaying the Current Time 75
12 // Obtain the total minutes 13 long totalMinutes = totalSeconds / 60; 14 15 // Compute the current minute in the hour 16 long currentMinute = totalMinutes % 60; 17 18 // Obtain the total hours 19 long totalHours = totalMinutes / 60; 20 21 // Compute the current hour 22 long currentHour = totalHours % 24; 23 24 // Display results 25 System.out.println("Current time is " + currentHour + ":" 26 + currentMinute + ":" + currentSecond + " GMT"); 27 } 28 }
totalMinutes
currentMinute
totalHours
currentHour
display output
Current time is 17:31:8 GMT
Line 4 invokes System.currentTimeMillis() to obtain the current time in milliseconds as a long value. Thus, all the variables are declared as the long type in this program. The seconds, minutes, and hours are extracted from the current time using the / and % operators (lines 6–22).
line#
variables
4 7 10 13 16 19 22
totalMilliseconds 1203183068328
totalSeconds 1203183068
currentSecond 8
totalMinutes 20053051
currentMinute 31
totalHours 334217
currentHour 17
In the sample run, a single digit 8 is displayed for the second. The desirable output would be 08. This can be fixed by using a method that formats a single digit with a prefix 0 (see Programming Exercise 6.37).
The hour displayed in this program is in GMT. Programming Exercise 2.8 enables to display the hour in any time zone.
Java also provides the System.nanoTime() method that returns the elapse time in nano- seconds. nanoTime() is more precise and accurate than currentTimeMillis().
2.12.1 How do you obtain the current second, minute, and hour?
nanoTime
Point Check
M02_LIAN1878_11_GE_C02.indd 75 12/29/17 3:48 PM
76 Chapter 2 Elementary Programming
2.13 Augmented Assignment Operators The operators +, -, *, /, and % can be combined with the assignment operator to form augmented operators.
Very often, the current value of a variable is used, modified, then reassigned back to the same variable. For example, the following statement increases the variable count by 1:
count = count + 1;
Java allows you to combine assignment and addition operators using an augmented (or compound) assignment operator. For example, the preceding statement can be written as
count += 1;
The += is called the addition assignment operator. Table 2.4 shows other augmented assign- ment operators.
Point Key
addition assignment operator
Operator Name Example Equivalent
+= Addition assignment i += 8 i = i + 8
-= Subtraction assignment i -= 8 i = i – 8
*= Multiplication assignment i *= 8 i = i * 8
/= Division assignment i /= 8 i = i / 8
%= Remainder assignment i %= 8 i = i % 8
tabLe 2.4 Augmented Assignment Operators
The augmented assignment operator is performed last after all the other operators in the expression are evaluated. For example,
x /= 4 + 5.5 * 1.5;
is same as
x = x / (4 + 5.5 * 1.5);
Caution There are no spaces in the augmented assignment operators. For example, + = should be +=.
Note Like the assignment operator (=), the operators (+=, -=, *=, /=, and %=) can be used to form an assignment statement as well as an expression. For example, in the follow- ing code, x += 2 is a statement in the first line, and an expression in the second line:
x += 2; // Statement System.out.println(x += 2); // Expression
2.13.1 Show the output of the following code:
double a = 6.5; a += a + 1;
Point Check
M02_LIAN1878_11_GE_C02.indd 76 12/29/17 3:48 PM
2.14 Increment and Decrement Operators 77
System.out.println(a); a = 6; a /= 2; System.out.println(a);
2.14 Increment and Decrement Operators The increment operator (+ +) and decrement operator (- -) are for incrementing and decrementing a variable by 1.
The ++ and — — are two shorthand operators for incrementing and decrementing a variable by 1. These are handy because that’s often how much the value needs to be changed in many program- ming tasks. For example, the following code increments i by 1 and decrements j by 1.
int i = 3, j = 3; i++; // i becomes 4 j— —; // j becomes 2
i++ is pronounced as "i plus plus" and i—— as "i minus minus." These operators are known as postfix increment (or postincrement) and postfix decrement (or postdecrement), because the operators ++ and —— are placed after the variable. These operators can also be placed before the variable. For example,
int i = 3, j = 3; ++i; // i becomes 4 — —j; // j becomes 2
++i increments i by 1 and ——j decrements j by 1. These operators are known as prefix increment (or preincrement) and prefix decrement (or predecrement).
As you see, the effect of i++ and ++i or i—— and ——i are the same in the preceding exam- ples. However, their effects are different when they are used in statements that do more than just increment and decrement. Table 2.5 describes their differences and gives examples.
Point Key
increment operator (+ +) decrement operator (- -)
postincrement
postdecrement
preincrement
predecrement
Operator Name Description Example (assume i = 1)
++var preincrement Increment var by 1, and use the new var value in the statement
int j = ++i; // j is 2, i is 2
var++ postincrement Increment var by 1, but use the original var value in the statement
int j = i++; // j is 1, i is 2
——var predecrement Decrement var by 1, and use the new var value in the statement
int j = — —i; // j is 0, i is 0
var—— postdecrement Decrement var by 1, and use the original var value in the statement
int j = i— —; // j is 1, i is 0
tabLe 2.5 Increment and Decrement Operators
Here are additional examples to illustrate the differences between the prefix form of ++ (or ——) and the postfix form of ++ (or ——). Consider the following code:
int i = 10; int newNum = 10 * i++;
Same effect as int newNum = 10 * i; i = i + 1;System.out.print("i is " + i
+ ", newNum is " + newNum);
i is 11, newNum is 100
M02_LIAN1878_11_GE_C02.indd 77 12/29/17 3:48 PM
78 Chapter 2 Elementary Programming
In this case, i is incremented by 1, then the old value of i is used in the multiplication. Thus, newNum becomes 100. If i++ is replaced by ++i, then it becomes as follows:
int i = 10; int newNum = 10 * (++i);
Same effect as i = i + 1; int newNum = 10 * i;
System.out.print("i is " + i + ", newNum is " + newNum);
i is 11, newNum is 110
i is incremented by 1, and the new value of i is used in the multiplication. Thus, newNum becomes 110.
Here is another example:
double x = 1.0; double y = 5.0; double z = x–– + (++y);
After all three lines are executed, y becomes 6.0, z becomes 7.0, and x becomes 0.0.
Operands are evaluated from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. This rule takes precedence over any other rules that govern expressions. Here is an example:
int i = 1; int k = ++i + i * 3;
++i is evaluated and returns 2. When evaluating i * 3, i is now 2. Therefore, k becomes 8.
Tip Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables or the same variable multiple times, such as this one: int k = ++i + i * 3.
2.14.1 Which of these statements are true?
a. Any expression can be used as a statement.
b. The expression x++ can be used as a statement.
c. The statement x = x + 5 is also an expression.
d. The statement x = y = x = 0 is illegal.
2.14.2 Show the output of the following code:
int a = 6; int b = a++; System.out.println(a); System.out.println(b); a = 6; b = ++a; System.out.println(a); System.out.println(b);
Point Check
M02_LIAN1878_11_GE_C02.indd 78 12/29/17 3:48 PM
2.15 Numeric Type Conversions 79
2.15 Numeric Type Conversions Floating-point numbers can be converted into integers using explicit casting.
Can you perform binary operations with two operands of different types? Yes. If an integer and a floating-point number are involved in a binary operation, Java automatically converts the integer to a floating-point value. Therefore, 3 * 4.5 is the same as 3.0 * 4.5.
You can always assign a value to a numeric variable whose type supports a larger range of values; thus, for instance, you can assign a long value to a float variable. You cannot, how- ever, assign a value to a variable of a type with a smaller range unless you use type casting. Casting is an operation that converts a value of one data type into a value of another data type. Casting a type with a small range to a type with a larger range is known as widening a type. Casting a type with a large range to a type with a smaller range is known as narrowing a type. Java will automatically widen a type, but you must narrow a type explicitly.
The syntax for casting a type is to specify the target type in parentheses, followed by the variable’s name or the value to be cast. For example, the following statement
System.out.println((int)1.7);
displays 1. When a double value is cast into an int value, the fractional part is truncated. The following statement
System.out.println((double)1 / 2);
displays 0.5, because 1 is cast to 1.0 first, then 1.0 is divided by 2. However, the statement
System.out.println(1 / 2);
displays 0, because 1 and 2 are both integers and the resulting value should also be an integer.
Caution Casting is necessary if you are assigning a value to a variable of a smaller type range, such as assigning a double value to an int variable. A compile error will occur if casting is not used in situations of this kind. However, be careful when using casting, as loss of information might lead to inaccurate results.
Note Casting does not change the variable being cast. For example, d is not changed after casting in the following code:
double d = 4.5; int i = (int)d; // i becomes 4, but d is still 4.5
Note In Java, an augmented expression of the form x1 op= x2 is implemented as x1 = (T) (x1 op x2), where T is the type for x1. Therefore, the following code is correct:
int sum = 0; sum += 4.5; // sum becomes 4 after this statement sum += 4.5 is equivalent to sum = (int)(sum + 4.5).
Note To assign a variable of the int type to a variable of the short or byte type, explicit casting must be used. For example, the following statements have a compile error:
int i = 1; byte b = i; // Error because explicit casting is required
Point Key
casting
widening a type narrowing a type
possible loss of precision
casting in an augmented expression
M02_LIAN1878_11_GE_C02.indd 79 12/29/17 3:48 PM
80 Chapter 2 Elementary Programming
However, so long as the integer literal is within the permissible range of the target vari- able, explicit casting is not needed to assign an integer literal to a variable of the short or byte type (see Section 2.10, Numeric Literals).
The program in Listing 2.8 displays the sales tax with two digits after the decimal point.
Listing 2.8 SalesTax.java 1 import java.util.Scanner; 2 3 public class SalesTax { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 System.out.print("Enter purchase amount: "); 8 double purchaseAmount = input.nextDouble(); 9 10 double tax = purchaseAmount * 0.06; 11 System.out.println("Sales tax is $" + (int)(tax * 100) / 100.0); 12 } 13 }
casting
Enter purchase amount: 197.55
Sales tax is $11.85
line# purchaseAmount tax Output
8 197.55
10 11.853
11 11.85
Using the input in the sample run, the variable purchaseAmount is 197.55 (line 8). The sales tax is 6% of the purchase, so the tax is evaluated as 11.853 (line 10). Note
tax * 100 is 1185.3 (int)(tax * 100) is 1185 (int)(tax * 100) / 100.0 is 11.85
Thus, the statement in line 11 displays the tax 11.85 with two digits after the decimal point. Note the expression (int)(tax * 100) / 100.0 rounds down tax to two decimal places. If tax is 3.456, (int)(tax * 100) / 100.0 would be 3.45. Can it be rounded up to two decimal places? Note any double value x can be rounded up to an integer using (int)(x + 0.5). Thus, tax can be rounded up to two decimal places using (int)(tax * 100 + 0.5) / 100.0.
2.15.1 Can different types of numeric values be used together in a computation?
2.15.2 What does an explicit casting from a double to an int do with the fractional part of the double value? Does casting change the variable being cast?
2.15.3 Show the following output:
float f = 12.5F; int i = (int)f; System.out.println("f is " + f); System.out.println("i is " + i);
formatting numbers
Point Check
M02_LIAN1878_11_GE_C02.indd 80 12/29/17 3:48 PM
2.16 Software Development Process 81
2.15.4 If you change (int)(tax * 100) / 100.0 to (int)(tax * 100) / 100 in line 11 in Listing 2.8, what will be the output for the input purchase amount of 197.556?
2.15.5 Show the output of the following code:
double amount = 5; System.out.println(amount / 2); System.out.println(5 / 2);
2.15.6 Write an expression that rounds up a double value in variable d to an integer.
2.16 Software Development Process The software development life cycle is a multistage process that includes requirements specification, analysis, design, implementation, testing, deployment, and maintenance.
Developing a software product is an engineering process. Software products, no matter how large or how small, have the same life cycle: requirements specification, analysis, design, implementation, testing, deployment, and maintenance, as shown in Figure 2.3.
Point Key
VideoNote
Software development process
Figure 2.3 At any stage of the software development life cycle, it may be necessary to go back to a previous stage to correct errors or deal with other issues that might prevent the software from functioning as expected.
Requirements Speci�cation
System Analysis
System Design
Testing
Input, Process, Output IPO
Implementation
Maintenance
Deployment
Requirements specification is a formal process that seeks to understand the problem the software will address, and to document in detail what the software system needs to do. This phase involves close interaction between users and developers. Most of the examples in this book are simple, and their requirements are clearly stated. In the real world, however, problems are not always well defined. Developers need to work closely with their customers (the indi- viduals or organizations that will use the software) and study the problem carefully to identify what the software needs to do.
System analysis seeks to analyze the data flow and to identify the system’s input and output. When you perform analysis, it helps to identify what the output is first, then figure out what input data you need in order to produce the output.
requirements specification
system analysis
M02_LIAN1878_11_GE_C02.indd 81 12/29/17 3:48 PM
82 Chapter 2 Elementary Programming
System design is to design a process for obtaining the output from the input. This phase involves the use of many levels of abstraction to break down the problem into manageable components and design strategies for implementing each component. You can view each com- ponent as a subsystem that performs a specific function of the system. The essence of system analysis and design is input, process, and output (IPO).
Implementation involves translating the system design into programs. Separate programs are written for each component then integrated to work together. This phase requires the use of a programming language such as Java. The implementation involves coding, self-testing, and debugging (that is, finding errors, called bugs, in the code).
Testing ensures the code meets the requirements specification and weeds out bugs. An independent team of software engineers not involved in the design and implementation of the product usually conducts such testing.
Deployment makes the software available for use. Depending on the type of software, it may be installed on each user’s machine, or installed on a server accessible on the Internet.
Maintenance is concerned with updating and improving the product. A software product must continue to perform and improve in an ever-evolving environment. This requires periodic upgrades of the product to fix newly discovered bugs and incorporate changes.
To see the software development process in action, we will now create a program that computes loan payments. The loan can be a car loan, a student loan, or a home mortgage loan. For an introductory programming course, we focus on requirements specification, analysis, design, implementation, and testing.
Stage 1: Requirements Specification
The program must satisfy the following requirements:
■■ It must let the user enter the interest rate, the loan amount, and the number of years for which payments will be made.
■■ It must compute and display the monthly payment and total payment amounts.
Stage 2: System Analysis
The output is the monthly payment and total payment, which can be obtained using the fol- lowing formulas:
monthlyPayment = loanAmount * monthlyInterestRate
1 - 1
(1 + monthlyInterestRate)numberOfYears * 12
totalPayment = monthlyPayment * numberOfYears * 12
Therefore, the input needed for the program is the monthly interest rate, the length of the loan in years, and the loan amount.
Note The requirements specification says the user must enter the annual interest rate, the loan amount, and the number of years for which payments will be made. During analysis, however, it is possible you may discover that input is not sufficient or some values are unnecessary for the output. If this happens, you can go back and modify the require- ments specification.
Note In the real world, you will work with customers from all walks of life. You may develop software for chemists, physicists, engineers, economists, and psychologists, and of course you will not have (or need) complete knowledge of all these fields. Therefore,
system design
IPO implementation
testing
deployment
maintenance
VideoNote
Compute loan payments
M02_LIAN1878_11_GE_C02.indd 82 12/29/17 3:48 PM
2.16 Software Development Process 83
you don’t have to know how formulas are derived, but given the monthly interest rate, the number of years, and the loan amount, you can compute the monthly payment in this program. You will, however, need to communicate with customers and understand how a mathematical model works for the system.
Stage 3: System Design
During system design, you identify the steps in the program.
Step 3.1. Prompt the user to enter the annual interest rate, the number of years, and the loan amount.
(The interest rate is commonly expressed as a percentage of the principal for a period of one year. This is known as the annual interest rate.)
Step 3.2. The input for the annual interest rate is a number in percent format, such as 4.5%. The program needs to convert it into a decimal by dividing it by 100. To obtain the monthly interest rate from the annual interest rate, divide it by 12, since a year has 12 months. Thus, to obtain the monthly interest rate in decimal format, you need to divide the annual interest rate in percentage by 1200. For example, if the annual interest rate is 4.5%, then the monthly interest rate is 4.5/1200 = 0.00375.
Step 3.3. Compute the monthly payment using the preceding formula.
Step 3.4. Compute the total payment, which is the monthly payment multiplied by 12 and multiplied by the number of years.
Step 3.5. Display the monthly payment and total payment.
Stage 4: Implementation
Implementation is also known as coding (writing the code). In the formula, you have to com- pute (1 + monthlyInterestRate)numberOfYears*12, which can be obtained using Math. pow(1 + monthlyInterestRate, numberOfYears * 12).
Listing 2.9 gives the complete program.
Listing 2.9 ComputeLoan.java 1 import java.util.Scanner; 2 3 public class ComputeLoan { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 8 // Enter annual interest rate in percentage, e.g., 7.25 9 System.out.print("Enter annual interest rate, e.g., 7.25: "); 10 double annualInterestRate = input.nextDouble(); 11 12 // Obtain monthly interest rate 13 double monthlyInterestRate = annualInterestRate / 1200; 14 15 // Enter number of years 16 System.out.print( 17 "Enter number of years as an integer, e.g., 5: "); 18 int numberOfYears = input.nextInt(); 19 20 // Enter loan amount 21 System.out.print("Enter loan amount, e.g., 120000.95: ");
Math.pow(a, b) method
enter years
enter interest rate
create a Scanner
import class
M02_LIAN1878_11_GE_C02.indd 83 12/29/17 3:48 PM
84 Chapter 2 Elementary Programming
22 double loanAmount = input.nextDouble(); 23 24 // Calculate payment 25 double monthlyPayment = loanAmount * monthlyInterestRate / (1 26 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); 27 double totalPayment = monthlyPayment * numberOfYears * 12; 28 29 // Display results 30 System.out.println("The monthly payment is $" + 31 (int)(monthlyPayment * 100) / 100.0); 32 System.out.println("The total payment is $" + 33 (int)(totalPayment * 100) / 100.0); 34 } 35 }
monthlyPayment
enter loan amount
totalPayment
casting
casting
Enter annual interest rate, for example, 7.25: 5.75
Enter number of years as an integer, for example, 5: 15
Enter loan amount, for example, 120000.95: 250000
The monthly payment is $2076.02
The total payment is $373684.53
line# 10 13 18 22 25 27
variables
annualInterestRate 5.75
monthlyInterestRate 0.0047916666666
numberOfYears 15
loanAmount 250000
monthlyPayment 2076.0252175
totalPayment 373684.539
Line 10 reads the annual interest rate, which is converted into the monthly interest rate in line 13.
Choose the most appropriate data type for the variable. For example, numberOfYears is best declared as an int (line 18), although it could be declared as a long, float, or double. Note byte might be the most appropriate for numberOfYears. For simplicity, however, the examples in this book will use int for integer and double for floating-point values.
The formula for computing the monthly payment is translated into Java code in lines 25–27. Casting is used in lines 31 and 33 to obtain a new monthlyPayment and totalPayment
with two digits after the decimal points. The program uses the Scanner class, imported in line 1. The program also uses the Math
class, and you might be wondering why that class isn’t imported into the program. The Math class is in the java.lang package, and all classes in the java.lang package are implicitly imported. Therefore, you don’t need to explicitly import the Math class.
Stage 5: Testing
After the program is implemented, test it with some sample input data and verify whether the output is correct. Some of the problems may involve many cases, as you will see in later chap- ters. For these types of problems, you need to design test data that cover all cases.
java.lang package
M02_LIAN1878_11_GE_C02.indd 84 12/29/17 3:48 PM
2.17 Case Study: Counting Monetary Units 85
Tip The system design phase in this example identified several steps. It is a good approach to code and test these steps incrementally by adding them one at a time. This approach makes it much easier to pinpoint problems and debug the program.
2.16.1 How would you write the following arithmetic expression?
-b + 2b2 - 4ac 2a
2.17 Case Study: Counting Monetary Units This section presents a program that breaks a large amount of money into smaller units.
Suppose you want to develop a program that changes a given amount of money into smaller monetary units. The program lets the user enter an amount as a double value representing a total in dollars and cents, and outputs a report listing the monetary equivalent in the maximum number of dollars, quarters, dimes, nickels, and pennies, in this order, to result in the minimum number of coins.
Here are the steps in developing the program:
1. Prompt the user to enter the amount as a decimal number, such as 11.56.
2. Convert the amount (e.g., 11.56) into cents (1156).
3. Divide the cents by 100 to find the number of dollars. Obtain the remaining cents using the cents remainder 100.
4. Divide the remaining cents by 25 to find the number of quarters. Obtain the remaining cents using the remaining cents remainder 25.
5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining cents using the remaining cents remainder 10.
6. Divide the remaining cents by 5 to find the number of nickels. Obtain the remaining cents using the remaining cents remainder 5.
7. The remaining cents are the pennies.
8. Display the result.
The complete program is given in Listing 2.10.
Listing 2.10 ComputeChange.java 1 import java.util.Scanner; 2 3 public class ComputeChange { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 8 // Receive the amount 9 System.out.print( 10 "Enter an amount in double, for example 11.56: "); 11 double amount = input.nextDouble(); 12 13 int remainingAmount = (int)(amount * 100); 14 15 // Find the number of one dollars
incremental code and test
Point Check
Point Key
enter input
import class
M02_LIAN1878_11_GE_C02.indd 85 12/29/17 3:48 PM
86 Chapter 2 Elementary Programming
16 int numberOfOneDollars = remainingAmount / 100; 17 remainingAmount = remainingAmount % 100; 18 19 // Find the number of quarters in the remaining amount 20 int numberOfQuarters = remainingAmount / 25; 21 remainingAmount = remainingAmount % 25; 22 23 // Find the number of dimes in the remaining amount 24 int numberOfDimes = remainingAmount / 10; 25 remainingAmount = remainingAmount % 10; 26 27 // Find the number of nickels in the remaining amount 28 int numberOfNickels = remainingAmount / 5; 29 remainingAmount = remainingAmount % 5; 30 31 // Find the number of pennies in the remaining amount 32 int numberOfPennies = remainingAmount; 33 34 // Display results 35 System.out.println("Your amount " + amount + " consists of"); 36 System.out.println(" " + numberOfOneDollars + " dollars"); 37 System.out.println(" " + numberOfQuarters + " quarters "); 38 System.out.println(" " + numberOfDimes + " dimes"); 39 System.out.println(" " + numberOfNickels + " nickels"); 40 System.out.println(" " + numberOfPennies + " pennies"); 41 } 42 }
pennies
nickels
dimes
quarters
dollars
output
Enter an amount in double, for example, 11.56: 11.56
Your amount 11.56 consists of
11 dollars 2 quarters 0 dimes 1 nickels 1 pennies
line#
variables
11 13 16 17 20 21 24 25 28 29 32
amount 11.56
remainingAmount 1156 56 6 6 1
numberOfOneDollars 11
numberOfQuarters 2
numberOfDimes 0
numberOfNickels 1
numberOfPennies 1
The variable amount stores the amount entered from the console (line 11). This variable is not changed, because the amount has to be used at the end of the program to display the results. The program introduces the variable remainingAmount (line 13) to store the chang- ing remaining amount.
M02_LIAN1878_11_GE_C02.indd 86 12/29/17 3:48 PM
2.18 Common Errors and Pitfalls 87
The variable amount is a double decimal representing dollars and cents. It is converted to an int variable remainingAmount, which represents all the cents. For instance, if amount is 11.56, then the initial remainingAmount is 1156. The division operator yields the integer part of the division, so 1156 / 100 is 11. The remainder operator obtains the remainder of the division, so 1156 % 100 is 56.
The program extracts the maximum number of singles from the remaining amount and obtains a new remaining amount in the variable remainingAmount (lines 16–17). It then extracts the maximum number of quarters from remainingAmount and obtains a new remainingAmount (lines 20–21). Continuing the same process, the program finds the maxi- mum number of dimes, nickels, and pennies in the remaining amount.
One serious problem with this example is the possible loss of precision when casting a double amount to an int remainingAmount. This could lead to an inaccurate result. If you try to enter the amount 10.03, 10.03 * 100 becomes 1002.9999999999999. You will find that the program displays 10 dollars and 2 pennies. To fix the problem, enter the amount as an integer value representing cents (see Programming Exercise 2.22).
2.17.1 Show the output of Listing 2.10 with the input value 1.99.
2.18 Common Errors and Pitfalls Common elementary programming errors often involve undeclared variables, uninitialized variables, integer overflow, unintended integer division, and round-off errors.
Common Error 1: Undeclared/Uninitialized Variables and Unused Variables
A variable must be declared with a type and assigned a value before using it. A common error is not declaring a variable or initializing a variable. Consider the following code:
double interestRate = 0.05; double interest = interestrate * 45;
This code is wrong, because interestRate is assigned a value 0.05; but interestrate has not been declared and initialized. Java is case sensitive, so it considers interestRate and interestrate to be two different variables.
If a variable is declared, but not used in the program, it might be a potential programming error. Therefore, you should remove the unused variable from your program. For example, in the following code, taxRate is never used. It should be removed from the code.
double interestRate = 0.05; double taxRate = 0.05; double interest = interestRate * 45; System.out.println("Interest is " + interest);
If you use an IDE such as Eclipse and NetBeans, you will receive a warning on unused variables.
Common Error 2: Integer Overflow
Numbers are stored with a limited numbers of digits. When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. For example, executing the following statement causes overflow, because the largest value that can be stored in a variable of the int type is 2147483647. 2147483648 will be too large for an int value:
int value = 2147483647 + 1; // value will actually be -2147483648
loss of precision
Point Check
Point Key
what is overflow?
M02_LIAN1878_11_GE_C02.indd 87 12/29/17 3:48 PM
88 Chapter 2 Elementary Programming
Likewise, executing the following statement also causes overflow, because the smallest value that can be stored in a variable of the int type is -2147483648. -2147483649 is too large in size to be stored in an int variable.
int value = –2147483648 – 1; // value will actually be 2147483647
Java does not report warnings or errors on overflow, so be careful when working with integers close to the maximum or minimum range of a given type.
When a floating-point number is too small (i.e., too close to zero) to be stored, it causes underflow. Java approximates it to zero, so normally you don’t need to be concerned about underflow.
Common Error 3: Round-off Errors
A round-off error, also called a rounding error, is the difference between the calculated approximation of a number and its exact mathematical value. For example, 1/3 is approxi- mately 0.333 if you keep three decimal places, and is 0.3333333 if you keep seven decimal places. Since the number of digits that can be stored in a variable is limited, round-off errors are inevitable. Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example,
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
displays 0.5000000000000001, not 0.5, and
System.out.println(1.0 - 0.9);
displays 0.09999999999999998, not 0.1. Integers are stored precisely. Therefore, calcula- tions with integers yield a precise integer result.
Common Error 4: Unintended Integer Division
Java uses the same divide operator, namely /, to perform both integer and floating-point divi- sion. When two operands are integers, the / operator performs an integer division. The result of the operation is an integer. The fractional part is truncated. To force two integers to perform a floating-point division, make one of the integers into a floating-point number. For example, the code in (a) displays that average as 1 and the code in (b) displays that average as 1.5.
what is underflow?
floating-point approximation
int number1 = 1; int number2 = 2; double average = (number1 + number2) / 2; System.out.println(average);
int number1 = 1; int number2 = 2; double average = (number1 + number2) / 2.0; System.out.println(average);
(a) (b)
Common Pitfall 1: Redundant Input Objects
New programmers often write the code to create multiple input objects for each input. For example, the following code reads an integer and a double value:
Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int v1 = input.nextInt();
Scanner input1 = new Scanner(System.in); BAD CODE System.out.print("Enter a double value: "); double v2 = input1.nextDouble();
M02_LIAN1878_11_GE_C02.indd 88 12/29/17 3:48 PM
Chapter Summary 89
The code is not good. It creates two input objects unnecessarily and may lead to some subtle errors. You should rewrite the code as follows:
Scanner input = new Scanner(System.in); GOOD CODE System.out.print("Enter an integer: "); int v1 = input.nextInt(); System.out.print("Enter a double value: "); double v2 = input.nextDouble();
2.18.1 Can you declare a variable as int and later redeclare it as double?
2.18.2 What is an integer overflow? Can floating-point operations cause overflow?
2.18.3 Will overflow cause a runtime error?
2.18.4 What is a round-off error? Can integer operations cause round-off errors? Can floating-point operations cause round-off errors?
Point Check
Key Terms algorithm 56 assignment operator (=) 64 assignment statement 64 byte type 67 casting 79 constant 65 data type 57 declare variables 57 decrement operator (– –) 77 double type 67 expression 64 final keyword 65 float type 67 floating-point number 57 identifier 62 increment operator (++) 77 incremental code and testing 85 int type 67 IPO 61 literal 70 long type 67
narrowing a type 79 operand 68 operator 68 overflow 87 postdecrement 77 postincrement 77 predecrement 77 preincrement 77 primitive data type 57 pseudocode 56 requirements specification 81 scope of a variable 63 short type 67 specific import 60 system analysis 81 system design 82 underflow 88 UNIX epoch 74 variable 57 widening a type 79 wildcard import 60
ChapTer summary 1. Identifiers are names for naming elements such as variables, constants, methods, classes,
and packages in a program.
2. An identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter or an underscore. It cannot start with a digit. An identifier cannot be a reserved word. An identifier can be of any length.
3. Variables are used to store data in a program. To declare a variable is to tell the compiler what type of data a variable can hold.
M02_LIAN1878_11_GE_C02.indd 89 12/29/17 3:48 PM
90 Chapter 2 Elementary Programming
4. There are two types of import statements: specific import and wildcard import. The specific import specifies a single class in the import statement. The wildcard import imports all the classes in a package.
5. In Java, the equal sign (=) is used as the assignment operator.
6. A variable declared in a method must be assigned a value before it can be used.
7. A named constant (or simply a constant) represents permanent data that never changes.
8. A named constant is declared by using the keyword final.
9. Java provides four integer types (byte, short, int, and long) that represent integers of four different sizes.
10. Java provides two floating-point types (float and double) that represent floating-point numbers of two different precisions.
11. Java provides operators that perform numeric operations: + (addition), – (subtraction), * (multiplication), / (division), and % (remainder).
12. Integer arithmetic (/) yields an integer result.
13. The numeric operators in a Java expression are applied the same way as in an arithmetic expression.
14. Java provides the augmented assignment operators += (addition assignment), –= (sub- traction assignment), *= (multiplication assignment), /= (division assignment), and %= (remainder assignment).
15. The increment operator (++) and the decrement operator (––) increment or decrement a variable by 1.
16. When evaluating an expression with values of mixed types, Java automatically converts the operands to appropriate types.
17. You can explicitly convert a value from one type to another using the (type)value notation.
18. Casting a variable of a type with a small range to a type with a larger range is known as widening a type.
19. Casting a variable of a type with a large range to a type with a smaller range is known as narrowing a type.
20. Widening a type can be performed automatically without explicit casting. Narrowing a type must be performed explicitly.
21. In computer science, midnight of January 1, 1970, is known as the UNIX epoch.
Quiz Answer the quiz for this chapter online at the Companion Website.
M02_LIAN1878_11_GE_C02.indd 90 12/29/17 3:48 PM
Programming Exercises 91
programming exerCises
Debugging Tip The compiler usually gives a reason for a syntax error. If you don’t know how to correct it, compare your program closely, character by character, with similar examples in the text.
Pedagogical Note Instructors may ask you to document your analysis and design for selected exercises. Use your own words to analyze the problem, including the input, output, and what needs to be computed, and describe how to solve the problem in pseudocode.
Pedagogical Note The solution to most even-numbered programming exercises are provided to students. These exercises serve as additional examples for a variety of programs. To maximize the benefits of these solutions, students should first attempt to complete the even-numbered exercises and then compare their solutions with the solutions provided in the book. Since the book provides a large number of programming exercises, it is sufficient if you can complete all even-numbered programming exercises.
Sections 2.2–2.12 2.1 (Convert mile to kilometer) Write a program that reads a mile in a double value
from the console, converts it to kilometers, and displays the result. The formula for the conversion is as follows:
1 mile = 1.6 kilometers
Here is a sample run:
learn from examples
document analysis and design
even-numbered programming exercises
Enter miles: 96
96 miles is 153.6 kilometers
2.2 (Compute the volume of a triangle) Write a program that reads in the length of sides of an equilateral triangle and computes the area and volume using the fol- lowing formulas:
area = 23 4 (length of sides)2
volume = area * length
Here is a sample run:
Enter length of the sides and height of the Equilateral triangle: 3.5 The area is 3.89 The volume of the Triangular prism is 19.48
2.3 (Convert meters into feet) Write a program that reads a number in meters, converts it to feet, and displays the result. One meter is 3.2786 feet. Here is a sample run:
Enter a value for meter: 10 10.0 meters is 32.786 feet
M02_LIAN1878_11_GE_C02.indd 91 12/29/17 3:48 PM
92 Chapter 2 Elementary Programming
*2.5 (Financial application: calculate tips) Write a program that reads the subtotal and the gratuity rate and then computes the gratuity and total. For example, if the user enters 10 for subtotal and 12% for gratuity rate, the program displays $1.2 as gratuity and $11.2 as total. Here is a sample run:
Enter the subtotal and a gratuity rate: 10 12
The gratuity is $1.2 and total is $11.2
**2.6 (Multiply the digits in an integer) Write a program that reads an integer between 0 and 1000 and multiplies all the digits in the integer. For example, if an integer is 932, the multiplication of all its digits is 54.
Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.
Here is a sample run:
Enter a number between 0 and 1000: 999
The multiplication of all digits in 999 is 729
*2.7 (Find the number of years) Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and remaining days for the minutes. For simplicity, assume that a year has 365 days. Here is a sample run:
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days
*2.8 (Current time) Listing 2.7, ShowCurrentTime.java, gives a program that displays the current time in GMT. Revise the program so it prompts the user to enter the time zone offset to GMT and displays the time in the specified time zone. Here is a sample run:
Enter the time zone offset to GMT: -5
The current time is 4:50:34
2.9 (Physics: acceleration) Average acceleration is defined as the change of velocity divided by the time taken to make the change, as given by the following formula:
a = v1 - v0
t
2.4 (Convert square meter into ping) Write a program that converts square meter into ping. The program prompts the user to enter a number in square meter, converts it to ping, and displays the result. One square meter is 0.3025 ping.
Here is a sample run:
Enter a number in square meters: 50
50.0 square meters is 15.125 pings
M02_LIAN1878_11_GE_C02.indd 92 12/29/17 3:48 PM
Programming Exercises 93
2.10 (Science: calculating energy) Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water. The formula to compute the energy is
Q = M * (finalTemperature – initialTemperature) * 4184
where M is the weight of water in kilograms, initial and final temperatures are in degrees Celsius, and energy Q is measured in joules. Here is a sample run:
Enter v0, v1, and t: 5.5 50.9 4.5
The average acceleration is 10.0889
Enter the amount of water in kilograms: 55.5
Enter the initial temperature: 3.5
Enter the final temperature: 10.5
The energy needed is 1625484.0
2.11 (Population projection) Rewrite Programming Exercise 1.11 to prompt the user to enter the number of years and display the population after the number of years. Use the hint in Programming Exercise 1.11 for this program. Here is a sample run of the program:
Enter the number of years: 5
The population in 5 years is 325932969
2.12 (Physics: finding runway length) Given an airplane’s acceleration a and take-off speed v, you can compute the minimum runway length needed for an airplane to take off using the following formula:
length = v2
2a Write a program that prompts the user to enter v in meters/second (m/s) and the
acceleration a in meters/second squared (m/s2), then, displays the minimum run- way length. Here is a sample run:
Enter speed and acceleration: 60 3.5
The minimum runway length for this airplane is 514.286
**2.13 (Financial application: compound value) Suppose you save $100 each month in a savings account with annual interest rate 3.75%. Thus, the monthly interest rate is 0.0375/12 = 0.003125. After the first month, the value in the account becomes
Write a program that prompts the user to enter the starting velocity v0 in meters/ second, the ending velocity v1 in meters/second, and the time span t in seconds, then displays the average acceleration. Here is a sample run:
M02_LIAN1878_11_GE_C02.indd 93 12/29/17 3:48 PM
94 Chapter 2 Elementary Programming
Enter the monthly saving amount: 100 After the first month, the account value is 100.3125 After the second month, the account value is 200.9384765625 After the third month, the account value is 301.8789093017578 After the sixth month, the account value is 606.5967866995037
*2.14 (Health application: computing BMI) Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing, by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note one pound is 0.45359237 kilograms and one inch is 0.0254 meters. Here is a sample run:
VideoNote
Compute BMI
Enter weight in pounds: 95.5
Enter height in inches: 50
BMI is 26.8573
2.15 (Geometry: distance of two points) Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance. The formula for computing the distance is 2(x2 - x1)2 + (y2 - y1)2. Note you can use Math. pow(a, 0.5) to compute 2a. Here is a sample run: Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance between the two points is 8.764131445842194
2.16 (Geometry: area of a hexagon) Write a program that prompts the user to enter the side of a hexagon and displays its area. The formula for computing the area of a hexagon is
Area = 323
2 s2,
where s is the length of a side. Here is a sample run:
100 * (1 + 0.003125) = 100.3125
After the second month, the value in the account becomes
(100 + 100.3125) * (1 + 0.003125) = 200.938
After the third month, the value in the account becomes
(100 + 200.938) * (1 + 0.003125) = 301.878
and so on. Write a program that prompts the user to enter a monthly saving amount and
displays the account value after the sixth month. (In Exercise 5.30, you will use a loop to simplify the code and display the account value for any month.)
M02_LIAN1878_11_GE_C02.indd 94 12/29/17 3:48 PM
Programming Exercises 95
Enter the length of the side: 5.5
The area of the hexagon is 78.5918
*2.17 (Science: wind-chill temperature) How cold is it outside? The temperature alone is not enough to provide the answer. Other factors including wind speed, relative humidity, and sunshine play important roles in determining coldness outside. In 2001, the National Weather Service (NWS) implemented the new wind-chill temperature to measure the coldness using temperature and wind speed. The formula is
twc = 35.74 + 0.6215ta - 35.75v0.16 + 0.4275tav0.16
where ta is the outside temperature measured in degrees Fahrenheit, v is the speed measured in miles per hour, and twc is the wind-chill temperature. The formula cannot be used for wind speeds below 2 mph or temperatures below -58°F or above 41°F.
Write a program that prompts the user to enter a temperature between -58°F and 41°F and a wind speed greater than or equal to 2 then displays the wind-chill temperature. Use Math.pow(a, b) to compute v0.16. Here is a sample run:
Enter the temperature in Fahrenheit between -58°F and 41°F: 5.3
Enter the wind speed (7 = 2) in miles per hour: 6 The wind chill index is -5.56707
Enter the coordinates of three points separated by spaces like x1 y1 x2 y2 x3 y3: 1.5 -3.4 4.6 5 9.5 -3.4 The area of the triangle is 33.6
2.18 (Print a table) Write a program that displays the following table. Calculate the middle point of two points.
a b Middle Point (0, 0) (2, 1) (1.0, 0.5) (1, 4) (4, 2) (2.5, 3.0) (2, 7) (6, 3) (4.0, 5.0) (3, 9) (10, 5) (6.5, 7.0) (4, 11) (12, 7) (8.0, 9.0)
*2.19 (Geometry: area of a triangle) Write a program that prompts the user to enter three points, (x1, y1), (x2, y2), and (x3, y3), of a triangle then displays its area. The formula for computing the area of a triangle is
s = (side1 + side2 + side3)/2;
area = 2s(s - side1)(s - side2)(s - side3) Here is a sample run:
M02_LIAN1878_11_GE_C02.indd 95 12/29/17 3:48 PM
96 Chapter 2 Elementary Programming
Note More than 200 additional programming exercises with solutions are provided to the instructors on the Instructor Resource Website.
*2.21 (Financial application: calculate future investment value) Write a program that reads in investment amount, annual interest rate, and number of years and displays the future investment value using the following formula:
futureInvestmentValue =
investmentAmount * (1 + monthlyInterestRate)numberOfYears*12
For example, if you enter amount 1000, annual interest rate 3.25%, and number of years 1, the future investment value is 1032.98.
Here is a sample run:
Enter investment amount: 1000.56
Enter annual interest rate in percentage: 4.25
Enter number of years: 1
Future value is $1043.92
*2.22 (Financial application: monetary units) Rewrite Listing 2.10, ComputeChange. java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1156 represents 11 dollars and 56 cents.
*2.23 (Cost of driving) Write a program that prompts the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price per gallon then displays the cost of the trip. Here is a sample run:
Enter the driving distance: 900.5
Enter miles per gallon: 25.5
Enter price per gallon: 3.55
The cost of driving is $125.36
Enter balance and interest rate (e.g., 3 for 3%): 1000 3.5
The interest is 2.91667
Sections 2.13–2.17 *2.20 (Financial application: calculate interest) If you know the balance and the annual
percentage interest rate, you can compute the interest on the next monthly pay- ment using the following formula:
interest = balance * (annualInterestRate/1200)
Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month. Here is a sample run:
M02_LIAN1878_11_GE_C02.indd 96 12/29/17 3:49 PM
Selections
Objectives ■■ To declare boolean variables and write Boolean expressions using
relational operators (§3.2).
■■ To implement selection control using one-way if statements (§3.3).
■■ To implement selection control using two-way if-else statements (§3.4).
■■ To implement selection control using nested if and multi-way if statements (§3.5).
■■ To avoid common errors and pitfalls in if statements (§3.6).
■■ To generate random numbers using the Math.random() method (§3.7).
■■ To program using selection statements for a variety of examples (Sub- tractionQuiz, BMI, ComputeTax) (§§3.7–3.9).
■■ To combine conditions using logical operators (!, &&, ||, and ^) (§3.10).
■■ To program using selection statements with combined conditions (LeapYear, Lottery) (§§3.11 and 3.12).
■■ To implement selection control using switch statements (§3.13).
■■ To write expressions using the conditional operator (§3.14).
■■ To examine the rules governing operator precedence and associativity (§3.15).
■■ To apply common techniques to debug errors (§3.16).
Chapter
3
M03_LIAN1878_11_GE_C03.indd 97 12/29/17 3:54 PM
98 Chapter 3 Selections
3.1 Introduction The program can decide which statements to execute based on a condition.
If you enter a negative value for radius in Listing 2.2, ComputeAreaWithConsoleInput.java, the program displays an invalid result. If the radius is negative, you don’t want the program to compute the area. How can you deal with this situation?
Like all high-level programming languages, Java provides selection statements: statements that let you choose actions with alternative courses. You can use the following selection state- ment to replace lines 12–17 in Listing 2.2:
if (radius < 0) { System.out.println("Incorrect input"); } else { double area = radius * radius * 3.14159; System.out.println("Area is " + area); }
Selection statements use conditions that are Boolean expressions. A Boolean expression is an expression that evaluates to a Boolean value: true or false. We now introduce the boolean type and relational operators.
3.2 boolean Data Type The boolean data type declares a variable with the value either true or false.
How do you compare two values, such as whether a radius is greater than 0, equal to 0, or less than 0? Java provides six relational operators (also known as comparison opera- tors), shown in Table 3.1, which can be used to compare two values (assume radius is 5 in the table).
Point Keyproblem
selection statements
Boolean expression Boolean value
Point Key
boolean data type relational operators
Java Operator Mathematics Symbol Name Example (radius is 5) Result
< < Less than radius < 0 false
<= ≤ Less than or equal to radius <= 0 false
> > Greater than radius > 0 true
>= ≥ Greater than or equal to radius >= 0 true
== = Equal to radius == 0 false
!= ≠ Not equal to radius != 0 true
Table 3.1 Relational Operators
Caution The equality testing operator is two equal signs (==), not a single equal sign (=). The latter symbol is for assignment.
The result of the comparison is a Boolean value: true or false. For example, the follow- ing statement displays true:
double radius = 1; System.out.println(radius > 0);
A variable that holds a Boolean value is known as a Boolean variable. The boolean data type is used to declare Boolean variables. A boolean variable can hold one of the two
== vs. =
Boolean variable
M03_LIAN1878_11_GE_C03.indd 98 12/29/17 3:54 PM
3.2 boolean Data Type 99
values: true or false. For example, the following statement assigns true to the variable lightsOn:
boolean lightsOn = true;
true and false are literals, just like a number such as 10. They are treated as reserved words and cannot be used as identifiers in the program.
Suppose you want to develop a program to let a first-grader practice addition. The pro- gram randomly generates two single-digit integers, number1 and number2, and displays to the student a question such as “What is 1 + 7?, ” as shown in the sample run in Listing 3.1. After the student types the answer, the program displays a message to indicate whether it is true or false.
There are several ways to generate random numbers. For now, generate the first integer using System.currentTimeMillis() % 10 (i.e., the last digit in the current time) and the second using System.currentTimeMillis() / 10 % 10 (i.e., the second last digit in the current time). Listing 3.1 gives the program. Lines 5–6 generate two numbers, number1 and number2. Line 14 obtains an answer from the user. The answer is graded in line 18 using a Boolean expression number1 + number2 == answer.
lisTing 3.1 AdditionQuiz.java 1 import java.util.Scanner; 2 3 public class AdditionQuiz { 4 public static void main(String[] args) { 5 int number1 = (int)(System.currentTimeMillis() % 10); 6 int number2 = (int)(System.currentTimeMillis() / 10 % 10); 7 8 // Create a Scanner 9 Scanner input = new Scanner(System.in); 10 11 System.out.print( 12 "What is " + number1 + " + " + number2 + "? "); 13 14 int answer = input.nextInt(); 15 16 System.out.println( 17 number1 + " + " + number2 + " = " + answer + " is " + 18 (number1 + number2 == answer)); 19 } 20 }
Boolean literals
VideoNote
Program addition quiz
generate number1 generate number2
show question
receive answer
display result
What is 1 + 7? 8
1 + 7 = 8 is true
What is 4 + 8? 9
4 + 8 = 9 is false
line# number1 number2 answer output
5 4
6 8
14 9
16 4 + 8 = 9 is false
M03_LIAN1878_11_GE_C03.indd 99 12/29/17 3:54 PM
100 Chapter 3 Selections
3.2.1 List six relational operators. 3.2.2 Assuming x is 1, show the result of the following Boolean expressions:
(x > 0) (x < 0) (x != 0) (x >= 0) (x != 1)
3.2.3 Can the following conversions involving casting be allowed? Write a test program to verify it.
boolean b = true; i = (int)b;
int i = 1; boolean b = (boolean)i;
3.3 if Statements An if statement is a construct that enables a program to specify alternative paths of execution.
The preceding program displays a message such as “6 + 2 = 7 is false.” If you wish the message to be “6 + 2 = 7 is incorrect,” you have to use a selection statement to make this minor change.
Java has several types of selection statements: one-way if statements, two-way if-else statements, nested if statements, multi-way if-else statements, switch statements, and conditional operators.
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is as follows:
if (boolean-expression) { statement(s); }
The flowchart in Figure 3.1a illustrates how Java executes the syntax of an if statement. A flowchart is a diagram that describes an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Process operations are repre- sented in these boxes, and the arrows connecting them represent the flow of control. A diamond box denotes a Boolean condition, and a rectangle box represents statements.
Point Check
Point Key
why if statement?
if statement
flowchart
Figure 3.1 An if statement executes statements if the boolean-expression evaluates to true.
statement(s)
boolean- expression
true
false
(a)
area = radius * radius * PI; System.out.println("The area for the circle of" + " radius " + radius + " is " + area);
(radius >= 0)
true
false
(b)
M03_LIAN1878_11_GE_C03.indd 100 12/29/17 3:55 PM
3.3 if Statements 101
If the boolean-expression evaluates to true, the statements in the block are executed. As an example, see the following code:
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); }
The flowchart of the preceding statement is shown in Figure 3.1b. If the value of radius is greater than or equal to 0, then the area is computed and the result is displayed; otherwise, the two statements in the block will not be executed.
The boolean-expression is enclosed in parentheses. For example, the code in (a) is wrong. It should be corrected, as shown in (b).
if i > 0 { System.out.println(“i is positive"); }
(a) Wrong
if (i > 0) { System.out.println(“i is positive"); }
(b) Correct
The block braces can be omitted if they enclose a single statement. For example, the fol- lowing statements are equivalent:
if (i > 0) { System.out.println(“i is positive"); }
(a)
if (i > 0) System.out.println(“i is positive");
(b)
Equivalent
Caution Omitting braces makes the code shorter, but it is prone to errors. It is a common mistake to forget the braces when you go back to modify the code that omits the braces.
Listing 3.2 gives a program that prompts the user to enter an integer. If the number is a mul- tiple of 5, the program displays HiFive. If the number is divisible by 2, it displays HiEven.
lisTing 3.2 SimpleIfDemo.java 1 import java.util.Scanner; 2 3 public class SimpleIfDemo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("Enter an integer: "); 7 int number = input.nextInt(); 8 9 if (number % 5 == 0) 10 System.out.println("HiFive"); 11 12 if (number % 2 == 0) 13 System.out.println("HiEven"); 14 } 15 }
Omitting braces or not
enter input
check 5
check even
M03_LIAN1878_11_GE_C03.indd 101 12/29/17 3:55 PM
102 Chapter 3 Selections
The program prompts the user to enter an integer (lines 6–7) and displays HiFive if it is divisible by 5 (lines 9–10) and HiEven if it is divisible by 2 (lines 12–13).
3.3.1 Write an if statement that assigns 1 to x if y is greater than 0. 3.3.2 Write an if statement that increases pay by 3% if score is greater than 90. 3.3.3 What is wrong in the following code?
if radius >= 0 { area = radius * radius * PI; System.out.println("The area for the circle of " + " radius " + radius + " is " + area); }
3.4 Two-Way if-else Statements An if-else statement decides the execution path based on whether the condition is true or false.
A one-way if statement performs an action if the specified condition is true. If the condition is false, nothing is done. But what if you want to take alternative actions when the condition is false? You can use a two-way if-else statement. The actions that a two-way if-else statement specifies differ based on whether the condition is true or false.
Here is the syntax for a two-way if-else statement:
if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }
The flowchart of the statement is shown in Figure 3.2.
Point Check
Point Key
Figure 3.2 An if-else statement executes statements for the true case if the boolean- expression evaluates to true; otherwise, statements for the false case are executed.
statement(s) for the true case statement(s) for the false case
boolean- expression
true false
Enter an integer: 30
HiFive HiEven
Enter an integer: 4
HiEven
M03_LIAN1878_11_GE_C03.indd 102 12/29/17 3:55 PM
3.5 Nested if and Multi-way if-else Statements 103
If the boolean-expression evaluates to true, the statement(s) for the true case are executed; otherwise, the statement(s) for the false case are executed. For example, consider the following code:
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); }
If radius >= 0 is true, area is computed and displayed; if it is false, the message "Negative input" is displayed.
As usual, the braces can be omitted if there is only one statement within them. The braces enclosing the System.out.println("Negative input") statement can therefore be omit- ted in the preceding example.
Here is another example of using the if-else statement. The example checks whether a number is even or odd, as follows:
if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");
3.4.1 Write an if statement that increases pay by 3% if score is greater than 90, other- wise increases pay by 1%.
3.4.2 What is the output of the code in (a) and (b) if number is 30? What if number is 35?
two-way if-else statement
Point Check
if (number % 2 == 0) System.out.println(number + " is even."); System.out.println(number + " is odd.");
(a)
if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");
(b)
3.5 Nested if and Multi-Way if-else Statements An if statement can be inside another if statement to form a nested if statement.
The statement in an if or if-else statement can be any legal Java statement, including another if or if-else statement. The inner if statement is said to be nested inside the outer if statement. The inner if statement can contain another if statement; in fact, there is no limit to the depth of the nesting. For example, the following is a nested if statement:
if (i > k) { if (j > k) System.out.println("i and j are greater than k"); } else System.out.println("i is less than or equal to k");
The if (j > k) statement is nested inside the if (i > k) statement. The nested if statement can be used to implement multiple alternatives. The statement
given in Figure 3.3a, for instance, prints a letter grade according to the score, with multiple alternatives.
Point Key
nested if statement
M03_LIAN1878_11_GE_C03.indd 103 12/29/17 3:55 PM
104 Chapter 3 Selections
The execution of this if statement proceeds as shown in Figure 3.4. The first condition (score >= 90) is tested. If it is true, the grade is A. If it is false, the second condition (score >= 80) is tested. If the second condition is true, the grade is B. If that condi- tion is false, the third condition and the rest of the conditions (if necessary) are tested until a condition is met or all of the conditions prove to be false. If all of the conditions are false, the grade is F. Note a condition is tested only when all of the conditions that come before it are false.
Figure 3.4 You can use a multi-way if-else statement to assign a grade.
grade is A
true
false
false
false
false
grade is B
score >= 80
true
grade is C
score >= 70
true
grade is D
score >= 60
true
grade is F
score >= 90
Figure 3.3 A preferred format for multiple alternatives is shown in (b) using a multi-way if-else statement.
if (score >= 90) System.out.print("A"); else if (score >= 80) System.out.print("B"); else if (score >= 70) System.out.print("C"); else if (score >= 60) System.out.print("D"); else System.out.print("F");
(a)
if (score >= 90) System.out.print("A"); else if (score >= 80) System.out.print("B"); else if (score >= 70) System.out.print("C"); else if (score >= 60) System.out.print("D"); else System.out.print("F");
(b)
Equivalent
This is better
M03_LIAN1878_11_GE_C03.indd 104 12/29/17 3:55 PM
3.6 Common Errors and Pitfalls 105
The if statement in Figure 3.3a is equivalent to the if statement in Figure 3.3b. In fact, Figure 3.3b is the preferred coding style for multiple alternative if statements. This style, called multi-way if-else statements, avoids deep indentation and makes the program easy to read.
3.5.1 Suppose x = 3 and y = 2; show the output, if any, of the following code. What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2? Draw a flowchart of the code.
if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else System.out.println("x is " + x);
3.5.2 Suppose x = 2 and y = 3. Show the output, if any, of the following code. What is the output if x = 3 and y = 2? What is the output if x = 3 and y = 3?
if (x > 2) if (y > 2) { int z = x + y; System.out.println("z is " + z); } else System.out.println("x is " + x);
3.5.3 What is wrong in the following code? if (score >= 60) System.out.println("D"); else if (score >= 70) System.out.println("C"); else if (score >= 80) System.out.println("B"); else if (score >= 90) System.out.println("A"); else System.out.println("F");
3.6 Common Errors and Pitfalls Forgetting necessary braces, ending an if statement in the wrong place, mistaking == for =, and dangling else clauses are common errors in selection statements. Dupli- cated statements in if-else statements and testing equality of double values are common pitfalls.
The following errors are common among new programmers.
Common Error 1: Forgetting Necessary Braces
The braces can be omitted if the block contains a single statement. However, forgetting the braces when they are needed for grouping multiple statements is a common programming error. If you modify the code by adding new statements in an if statement without braces, you will have to insert the braces. For example, the following code in (a) is wrong. It should be written with braces to group multiple statements, as shown in (b).
multi-way if statement
Point Check
Point Key
M03_LIAN1878_11_GE_C03.indd 105 12/29/17 3:55 PM
106 Chapter 3 Selections
In (a), the console output statement is not part of the if statement. It is the same as the fol- lowing code:
if (radius >= 0) area = radius * radius * PI; System.out.println(“The area " + " is " + area);
(a) Wrong
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area " + " is " + area); }
(b) Correct
if (radius >= 0) area = radius * radius * PI;
System.out.println(“The area " + “ is " + area);
Regardless of the condition in the if statement, the console output statement is always executed.
Common Error 2: Wrong Semicolon at the if Line
Adding a semicolon at the end of an if line, as shown in (a) below, is a common mistake.
Logic error
if (radius >= 0); { area = radius * radius * PI; System.out.println("The area " + " is " + area); }
(a)
if (radius >= 0) { }; { area = radius * radius * PI; System.out.println("The area " + " is " + area); }
(b)
Empty block
Equivalent
This mistake is hard to find, because it is neither a compile error nor a runtime error; it is a logic error. The code in (a) is equivalent to that in (b) with an empty block.
This error often occurs when you use the next-line block style. Using the end-of-line block style can help prevent this error.
Common Error 3: Redundant Testing of Boolean Values
To test whether a boolean variable is true or false in a test condition, it is redundant to use the equality testing operator like the code in (a):
(a)
if (even == true) System.out.println( "It is even.");
if (even) System.out.println( "It is even.");
Equivalent
This is better (b)
Instead, it is better to test the boolean variable directly, as shown in (b). Another good reason for doing this is to avoid errors that are difficult to detect. Using the = operator instead of the == operator to compare the equality of two items in a test condition is a common error. It could lead to the following erroneous statement:
if (even = true) System.out.println("It is even.");
This statement does not have compile errors. It assigns true to even, so even is always true.
M03_LIAN1878_11_GE_C03.indd 106 12/29/17 3:55 PM
3.6 Common Errors and Pitfalls 107
Common Error 4: Dangling else Ambiguity
The code in (a) below has two if clauses and one else clause. Which if clause is matched by the else clause? The indentation indicates that the else clause matches the first if clause. However, the else clause actually matches the second if clause. This situation is known as the dangling else ambiguity. The else clause always matches the most recent unmatched if clause in the same block. Therefore, the statement in (a) is equivalent to the code in (b).
dangling else ambiguity
int i = 1, j = 2, k = 3;
if (i > j) if (i > k) System.out.println("A"); else System.out.println("B");
(a)
Equivalent
This is better with correct indentation
int i = 1, j = 2, k = 3;
if (i > j) if (i > k) System.out.println("A"); else System.out.println("B");
(b)
Since (i > j) is false, nothing is displayed from the statements in (a) and (b). To force the else clause to match the first if clause, you must add a pair of braces:
int i = 1, j = 2, k = 3;
if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B");
This statement displays B.
Common Error 5: Equality Test of Two Floating-Point Values
As discussed in Common Error 3 in Section 2.8, floating-point numbers have a limited precision and calculations; involving floating-point numbers can introduce round-off errors. Therefore, equality test of two floating-point values is not reliable. For example, you expect the following code to display true, but surprisingly, it displays false:
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; System.out.println(x == 0.5);
Here, x is not exactly 0.5, but is 0.5000000000000001. You cannot reliably test equality of two floating-point values. However, you can compare whether they are close enough by testing whether the difference of the two numbers is less than some threshold. That is, two numbers x and y are very close if � x - y � 6 e, for a very small value, e. e, a Greek letter pronounced "epsilon", is commonly used to denote a very small value. Normally, you set e to 10-14 for comparing two values of the double type, and to 10-7 for comparing two values of the float type. For example, the following code
final double EPSILON = 1E-14; double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1; if (Math.abs(x - 0.5) < EPSILON) System.out.println(x + " is approximately 0.5");
will display
0.5000000000000001 is approximately 0.5.
The Math.abs(a) method can be used to return the absolute value of a.
M03_LIAN1878_11_GE_C03.indd 107 12/29/17 3:55 PM
108 Chapter 3 Selections
Common Pitfall 1: Simplifying Boolean Variable Assignment
Often, new programmers write the code that assigns a test condition to a boolean variable like the code in (a):
This is not an error, but it should be better written as shown in (b).
Common Pitfall 2: Avoiding Duplicate Code in Different Cases
Often, new programmers write the duplicate code in different cases that should be combined in one place. For example, the highlighted code in the following statement is duplicated:
if (inState) { tuition = 5000; System.out.println("The tuition is " + tuition); } else { tuition = 15000; System.out.println("The tuition is " + tuition); }
This is not an error, but it should be better written as follows:
if (inState) { tuition = 5000; } else { tuition = 15000; } System.out.println("The tuition is " + tuition);
The new code removes the duplication and makes the code easy to maintain, because you only need to change in one place if the print statement is modified.
3.6.1 Which of the following statements are equivalent? Which ones are correctly indented?Point
Check
if (number % 2 == 0) even = true; else even = false;
boolean even = number % 2 == 0;Equivalent
This is better
(a) (b)
3.6.2 Rewrite the following statement using a Boolean expression: if (count % 10 == 0) newLine = true; else newLine = false;
if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;
(a)
if (i > 0) { if (j > 0) x = 0; else if (k > 0) y = 0; } else z = 0;
(b)
if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;
(c)
if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;
(d)
M03_LIAN1878_11_GE_C03.indd 108 12/29/17 3:55 PM
3.7 Generating Random Numbers 109
3.6.3 Are the following statements correct? Which one is better?
3.6.4 What is the output of the following code if number is 14, 15, or 30?
3.7 Generating Random Numbers You can use Math.random() to obtain a random double value between 0.0 and 1.0, excluding 1.0.
Suppose you want to develop a program for a first-grader to practice subtraction. The program randomly generates two single-digit integers, number1 and number2, with number1 >= number2, and it displays to the student a question such as “What is 9 - 2?” After the student enters the answer, the program displays a message indicating whether it is correct.
The previous programs generate random numbers using System.currentTimeMillis(). A better approach is to use the random() method in the Math class. Invoking this method returns a random double value d such that 0.0 … d 6 1.0. Thus, (int)(Math.random() * 10) returns a random single-digit integer (i.e., a number between 0 and 9).
The program can work as follows:
1. Generate two single-digit integers into number1 and number2.
2. If number1 < number2, swap number1 with number2.
3. Prompt the student to answer, "What is number1 − number2?"
4. Check the student’s answer and display whether the answer is correct.
The complete program is given in Listing 3.3.
lisTing 3.3 SubtractionQuiz.java 1 import java.util.Scanner; 2 3 public class SubtractionQuiz { 4 public static void main(String[] args) { 5 // 1. Generate two random single-digit integers 6 int number1 = (int)(Math.random() * 10); 7 int number2 = (int)(Math.random() * 10); 8 9 // 2. If number1 < number2, swap number1 with number2 10 if (number1 < number2) { 11 int temp = number1;
Point Key
VideoNote
Program subtraction quiz
random() method
random number
if (age < 16) System.out.println (“Cannot get a driver’s license"); if (age >= 16) System.out.println (“Can get a driver’s license");
if (age < 16) System.out.println (“Cannot get a driver’s license"); else System.out.println (“Can get a driver’s license");
(a) (b)
if (number % 2 == 0) System.out.println (number + “ is even"); if (number % 5 == 0) System.out.println (number + “ is multiple of 5");
if (number % 2 == 0) System.out.println (number + “ is even"); else if (number % 5 == 0) System.out.println (number + “ is multiple of 5");
(a) (b)
M03_LIAN1878_11_GE_C03.indd 109 12/29/17 3:55 PM
110 Chapter 3 Selections
12 number1 = number2; 13 number2 = temp; 14 } 15 16 // 3. Prompt the student to answer "What is number1 – number2?" 17 System.out.print 18 ("What is " + number1 + " − " + number2 + "? "); 19 Scanner input = new Scanner(System.in); 20 int answer = input.nextInt(); 21 22 // 4. Grade the answer and display the result 23 if (number1 − number2 == answer) 24 System.out.println("You are correct!"); 25 else { 26 System.out.println("Your answer is wrong."); 27 System.out.println(number1 + " − " + number2 + 28 " should be " + (number1 − number2)); 29 } 30 } 31 }
get answer
check the answer
What is 6 − 6? 0
You are correct!
What is 9 − 2? 5
Your answer is wrong
9 − 2 is 7
line# number1 number2 temp answer output
6 2
7 9
11 2
12 9
13 2
20 5
26 Your answer is wrong 9 − 2 should be 7
To swap two variables number1 and number2, a temporary variable temp (line 11) is used to first hold the value in number1. The value in number2 is assigned to number1 (line 12), and the value in temp is assigned to number2 (line 13).
3.7.1 Which of the following is a possible output from invoking Math.random()? 323.4, 0.5, 34, 1.0, 0.0, 0.234
3.7.2 a. How do you generate a random integer i such that 0 … i 6 20?
b. How do you generate a random integer i such that 10 … i 6 20?
c. How do you generate a random integer i such that 10 … i … 50?
d. Write an expression that returns 0 or 1 randomly.
Point Check
M03_LIAN1878_11_GE_C03.indd 110 12/29/17 3:55 PM
3.8 Case Study: Computing Body Mass Index 111
3.8 Case Study: Computing Body Mass Index You can use nested if statements to write a program that interprets body mass index.
Body mass index (BMI) is a measure of health based on height and weight. It can be calculated by taking your weight in kilograms and dividing it by the square of your height in meters. The interpretation of BMI for people 20 years or older is as follows:
BMI Interpretation BMI 6 18.5 Underweight 18.5 … BMI 6 25.0 Normal 25.0 … BMI 6 30.0 Overweight 30.0 … BMI Obese
Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note that one pound is 0.45359237 kilograms, and one inch is 0.0254 meters. Listing 3.4 gives the program.
lisTing 3.4 ComputeAndInterpretBMI.java 1 import java.util.Scanner; 2 3 public class ComputeAndInterpretBMI { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter weight in pounds 8 System.out.print("Enter weight in pounds: "); 9 double weight = input.nextDouble(); 10 11 // Prompt the user to enter height in inches 12 System.out.print("Enter height in inches: "); 13 double height = input.nextDouble(); 14 15 final double KILOGRAMS_PER_POUND = 0.45359237; // Constant 16 final double METERS_PER_INCH = 0.0254; // Constant 17 18 // Compute BMI 19 double weightInKilograms = weight * KILOGRAMS_PER_POUND; 20 double heightInMeters = height * METERS_PER_INCH; 21 double bmi = weightInKilograms / 22 (heightInMeters * heightInMeters); 23 24 // Display result 25 System.out.println("BMI is " + bmi); 26 if (bmi < 18.5) 27 System.out.println("Underweight"); 28 else if (bmi < 25) 29 System.out.println("Normal"); 30 else if (bmi < 30) 31 System.out.println("Overweight"); 32 else 33 System.out.println("Obese"); 34 } 35 }
Point Key
input weight
input height
compute bmi
display output
M03_LIAN1878_11_GE_C03.indd 111 12/29/17 3:55 PM
112 Chapter 3 Selections
line# weight height weightInKilograms heightInMeters bmi output
9 146
13 70
19 66.22448602
20 1.778
21 20.9486
25 BMI is 20.95
29 Normal
The constants KILOGRAMS_PER_POUND and METERS_PER_INCH are defined in lines 15–16. Using constants here makes programs easy to read.
You should test the input that covers all possible cases for BMI to ensure that the program works for all cases.
3.9 Case Study: Computing Taxes You can use nested if statements to write a program for computing taxes.
The U.S. federal personal income tax is calculated based on filing status and taxable income. There are four filing statuses: single filers, married filing jointly or qualified widow(er), mar- ried filing separately, and head of household. The tax rates vary every year. Table 3.2 shows the rates for 2009. If you are single with a taxable income of $10,000, for example, the first $8,350 is taxed at 10% and the other $1,650 is taxed at 15%, so your total tax is $1,082.50.
test all cases
Point Key
VideoNote
Use multi-way if-else statements
Enter weight in pounds: 146
Enter height in inches: 70
BMI is 20.948603801493316
Normal
Marginal Tax Rate Single
Married Filing Jointly or Qualifying Widow(er) Married Filing Separately Head of Household
10% $0–$8,350 $0–$16,700 $0–$8,350 $0–$11,950
15% $8,351–$33,950 $16,701–$67,900 $8,351–$33,950 $11,951–$45,500
25% $33,951–$82,250 $67,901–$137,050 $33,951–$68,525 $45,501–$117,450
28% $82,251–$171,550 $137,051–$208,850 $68,526–$104,425 $117,451–$190,200
33% $171,551–$372,950 $208,851–$372,950 $104,426–$186,475 $190,201–$372,950
35% +372,951+ +372,951+ +186,476+ +372,951+
Table 3.2 2009 U.S. Federal Personal Tax Rates
You are to write a program to compute personal income tax. Your program should prompt the user to enter the filing status and taxable income and compute the tax. Enter 0 for single filers, 1 for married filing jointly or qualified widow(er), 2 for married filing separately, and 3 for head of household.
M03_LIAN1878_11_GE_C03.indd 112 12/29/17 3:55 PM
3.9 Case Study: Computing Taxes 113
Your program computes the tax for the taxable income based on the filing status. The filing status can be determined using if statements outlined as follows:
if (status == 0) { // Compute tax for single filers } else if (status == 1) { // Compute tax for married filing jointly or qualifying widow(er) } else if (status == 2) { // Compute tax for married filing separately } else if (status == 3) { // Compute tax for head of household } else { // Display wrong status }
For each filing status there are six tax rates. Each rate is applied to a certain amount of tax- able income. For example, of a taxable income of $400,000 for single filers, $8,350 is taxed at 10%, (33,950 - 8,350) at 15%, (82,250 - 33,950) at 25%, (171,550 - 82,250) at 28%, (372,950 - 171,550) at 33%, and (400,000 - 372,950) at 35%.
Listing 3.5 gives the solution for computing taxes for single filers. The complete solution is left as an exercise.
lisTing 3.5 ComputeTax.java 1 import java.util.Scanner; 2 3 public class ComputeTax { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter filing status 9 System.out.print("(0-single filer, 1-married jointly or " + 10 "qualifying widow(er), 2-married separately, 3-head of " + 11 "household) Enter the filing status: "); 12 13 int status = input.nextInt(); 14 15 // Prompt the user to enter taxable income 16 System.out.print("Enter the taxable income: "); 17 double income = input.nextDouble(); 18 19 // Compute tax 20 double tax = 0; 21 22 if (status == 0) { // Compute tax for single filers 23 if (income <= 8350) 24 tax = income * 0.10; 25 else if (income <= 33950) 26 tax = 8350 * 0.10 + (income − 8350) * 0.15; 27 else if (income <= 82250) 28 tax = 8350 * 0.10 + (33950 − 8350) * 0.15 + 29 (income − 33950) * 0.25; 30 else if (income <= 171550) 31 tax = 8350 * 0.10 + (33950 − 8350) * 0.15 + 32 (82250 - 33950) * 0.25 + (income − 82250) * 0.28;
input status
input income
compute tax
M03_LIAN1878_11_GE_C03.indd 113 12/29/17 3:55 PM
114 Chapter 3 Selections
33 else if (income <= 372950) 34 tax = 8350 * 0.10 + (33950 − 8350) * 0.15 + 35 (82250 - 33950) * 0.25 + (171550 − 82250) * 0.28 + 36 (income - 171550) * 0.33; 37 else 38 tax = 8350 * 0.10 + (33950 − 8350) * 0.15 + 39 (82250 − 33950) * 0.25 + (171550 − 82250) * 0.28 + 40 (372950 − 171550) * 0.33 + (income − 372950) * 0.35; 41 } 42 else if (status == 1) { // Left as an exercise 43 // Compute tax for married file jointly or qualifying widow(er) 44 } 45 else if (status == 2) { // Compute tax for married separately 46 // Left as an exercise in Programming Exercise 3.13 47 } 48 else if (status == 3) { // Compute tax for head of household 49 // Left as an exercise in Programming Exercise 3.13 50 } 51 else { 52 System.out.println("Error: invalid status"); 53 System.exit(1); 54 } 55 56 // Display the result 57 System.out.println("Tax is " + (int)(tax * 100) / 100.0); 58 } 59 }
exit program
display output
line# status income Tax output
13 0
17 400000
20 0
38 117683.5
57 Tax is 117683.5
(0-single filer, 1-married jointly or qualifying widow(er),
2-married separately, 3-head of household) Enter the filing status: 0 Enter the taxable income: 400000 Tax is 117683.5
The program receives the filing status and taxable income. The multi-way if-else state- ments (lines 22, 42, 45, 48, and 51) check the filing status and compute the tax based on the filing status.
System.exit(status) (line 53) is defined in the System class. Invoking this method terminates the program. The status 0 indicates that the program is terminated normally. A nonzero status code indicates abnormal termination.
An initial value of 0 is assigned to tax (line 20). A compile error would occur if it had no initial value, because all of the other statements that assign values to tax are within the if statement. The compiler thinks these statements may not be executed, and therefore reports a compile error.
System.exit(status)
M03_LIAN1878_11_GE_C03.indd 114 12/29/17 3:55 PM
3.10 Logical Operators 115
To test a program, you should provide the input that covers all cases. For this program, your input should cover all statuses (0, 1, 2, 3). For each status, test the tax for each of the six brackets. Thus, there are a total of 24 cases.
Tip For all programs, you should write a small amount of code and test it before moving on to add more code. This is called incremental development and testing. This approach makes testing easier, because the errors are likely in the new code you just added.
3.9.1 Are the following two statements equivalent?
test all cases
incremental development and testing
Point Check
Operator Name Description
! not Logical negation
&& and Logical conjunction
|| or Logical disjunction
^ exclusive or Logical exclusion
Table 3.3 Boolean Operators
p !p Example (assume age = 24, weight = 140)
true false !(age > 18) is false, because (age > 18) is true.
false true !(weight == 150) is true, because (weight == 150) is false.
Table 3.4 Truth Table for Operator !
if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income − 10000) * 0.15;
if (income <= 10000) tax = income * 0.1; else if (income > 10000 && income <= 20000) tax = 1000 + (income − 10000) * 0.15;
3.10 Logical Operators The logical operators !, &&, ||, and ^ can be used to create a compound Boolean expression.
Sometimes, whether a statement is executed is determined by a combination of several condi- tions. You can use logical operators to combine these conditions to form a compound Boolean expression. Logical operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value. Table 3.3 lists the Boolean operators. Table 3.4 defines the not (!) operator, which negates true to false and false to true. Table 3.5 defines the and (&&) operator. The and (&&) of two Boolean operands is true if and only if both the operands are true. Table 3.6 defines the or (||) operator. The or (||) of two Boolean operands is true if at least one of the operands is true. Table 3.7 defines the exclusive or (^) operator. The exclusive or (^) of two Boolean operands is true if and only if the two operands have different Boolean values. Note p1 ^ p2 is the same as p1 != p2.
Point Key
M03_LIAN1878_11_GE_C03.indd 115 12/29/17 3:55 PM
116 Chapter 3 Selections
Listing 3.6 gives a program that checks whether a number is divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both.
lisTing 3.6 TestBooleanOperators.java 1 import java.util.Scanner; 2 3 public class TestBooleanOperators { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 8 // Receive an input 9 System.out.print("Enter an integer: "); 10 int number = input.nextInt(); 11 12 if (number % 2 == 0 && number % 3 == 0) 13 System.out.println(number + " is divisible by 2 and 3."); 14
import class
input
and
p1 p2 p1 || p2 Example (assume age = 24, weight = 140)
false false false (age > 34) || (weight >= 150) is false, because (age > 34) and (weight >= 150) are both false.
false true true
true false true (age > 18) || (weight < 140) is true, because (age > 18) is true.
true true true
Table 3.6 Truth Table for Operator ||
p1 p2 p1 ^ p2 Example (assume age = 24, weight = 140)
false false false (age > 34) ^ (weight > 140) is false, because (age > 34) and (weight > 140) are both false.
false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is false but (weight >= 140) is true.
true false true
true true false
Table 3.7 Truth Table for Operator ^
p1 p2 p1 && p2 Example (assume age = 24, weight = 140)
false false false
false true false (age > 28) && (weight <= 140) is false, because (age > 28) is false.
true false false
true true true (age > 18) && (weight >= 140) is true, because (age > 18) and (weight >= 140) are both true.
Table 3.5 Truth Table for Operator &&
M03_LIAN1878_11_GE_C03.indd 116 12/29/17 3:55 PM
3.10 Logical Operators 117
15 if (number % 2 == 0 || number % 3 == 0) 16 System.out.println(number + " is divisible by 2 or 3."); 17 18 if (number % 2 == 0 ^ number % 3 == 0) 19 System.out.println(number + 20 " is divisible by 2 or 3, but not both."); 21 } 22 }
Enter an integer: 4 4 is divisible by 2 or 3. 4 is divisible by 2 or 3, but not both.
Enter an integer: 18 18 is divisible by 2 and 3. 18 is divisible by 2 or 3.
(number % 2 == 0 && number % 3 == 0) (line 12) checks whether the number is divisible by both 2 and 3. (number % 2 == 0 || number % 3 == 0) (line 15) checks whether the number is divisible by 2 or by 3. (number % 2 == 0 ^ number % 3 == 0) (line 18) checks whether the number is divisible by 2 or 3, but not both.
Caution In mathematics, the expression
28 <= numberOfDaysInAMonth <= 31 is correct. However, it is incorrect in Java, because 28 <= numberOfDaysInAMonth is evaluated to a boolean value, which cannot be compared with 31. Here, two oper- ands (a boolean value and a numeric value) are incompatible. The correct expression in Java is
28 <= numberOfDaysInAMonth && numberOfDaysInAMonth <= 31
Note De Morgan’s law, named after Indian-born British mathematician and logician Augustus De Morgan (1806–1871), can be used to simplify Boolean expressions. The law states the following:
!(condition1 && condition2) is the same as !condition1 || !condition2 !(condition1 || condition2) is the same as !condition1 && !condition2
For example,
!(number % 2 == 0 && number % 3 == 0)
can be simplified using an equivalent expression:
number % 2 != 0 || number % 3 != 0
As another example,
!(number == 2 || number == 3)
is better written as
number != 2 && number != 3
or
exclusive or
incompatible operands
De Morgan’s law
M03_LIAN1878_11_GE_C03.indd 117 12/29/17 3:55 PM
118 Chapter 3 Selections
If one of the operands of an && operator is false, the expression is false; if one of the operands of an || operator is true, the expression is true. Java uses these properties to improve the performance of these operators. When evaluating p1 && p2, Java first evaluates p1 then, if p1 is true, evaluates p2; if p1 is false, it does not evaluate p2. When evaluating p1 || p2, Java first evaluates p1 then, if p1 is false, evaluates p2; if p1 is true, it does not evaluate p2. In programming language terminology, && and || are known as the short- circuit or lazy operators. Java also provides the & and | operators, which are covered in Sup- plement III.C for advanced readers.
3.10.1 Assuming that x is 1, show the result of the following Boolean expressions: (true) && (3 > 4) !(x > 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x == 0) (x >= 0) || (x < 0) (x != 1) == !(x == 1)
3.10.2 (a) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100. (b) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
3.10.3 (a) Write a Boolean expression for � x - 5 � 6 4.5. (b) Write a Boolean expres- sion for � x - 5 � 7 4.5.
3.10.4 Assume x and y are int type. Which of the following are legal Java expressions? x > y > 0 x = y && y x /= y x or y x and y (x != 0) || (x = 0)
3.10.5 Are the following two expressions the same?
(a) x % 2 == 0 && x % 3 == 0
(b) x % 6 == 0
3.10.6 What is the value of the expression x >= 50 && x <= 100 if x is 45, 67, or 101? 3.10.7 Suppose, when you run the following program, you enter the input 2 3 6 from
the console. What is the output?
public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble();
System.out.println("(x < y && y < z) is " + (x < y && y < z)); System.out.println("(x < y || y < z) is " + (x < y || y < z)); System.out.println("!(x < y) is " + !(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y > z) is " + (x + y > z)); } }
3.10.8 Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.
short-circuit operator
lazy operator
Point Check
M03_LIAN1878_11_GE_C03.indd 118 12/29/17 3:55 PM
3.11 Case Study: Determining Leap Year 119
3.10.9 Write a Boolean expression that evaluates to true if weight is greater than 50 pounds or height is greater than 60 inches.
3.10.10 Write a Boolean expression that evaluates to true if weight is greater than 50 pounds and height is greater than 60 inches.
3.10.11 Write a Boolean expression that evaluates to true if either weight is greater than 50 pounds or height is greater than 60 inches, but not both.
3.11 Case Study: Determining Leap Year A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400.
A leap year has 366 days. The February of a leap year has 29 days. You can use the following Boolean expressions to check whether a year is a leap year:
// A leap year is divisible by 4 boolean isLeapYear = (year % 4 == 0);
// A leap year is divisible by 4 but not by 100 isLeapYear = isLeapYear && (year % 100 != 0);
// A leap year is divisible by 4 but not by 100 or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0);
Or you can combine all these expressions into one as follows:
isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Listing 3.7 gives the program that lets the user enter a year and checks whether it is a leap year.
lisTing 3.7 LeapYear.java 1 import java.util.Scanner; 2 3 public class LeapYear { 4 public static void main(String[] args) { 5 // Create a Scanner 6 Scanner input = new Scanner(System.in); 7 System.out.print("Enter a year: "); 8 int year = input.nextInt(); 9 10 // Check if the year is a leap year 11 boolean isLeapYear = 12 (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 13 14 // Display the result 15 System.out.println(year + " is a leap year? " + isLeapYear); 16 } 17 }
Point Key
input
leap year?
display result
Enter a year: 2008
2008 is a leap year? true
Enter a year: 1900
1900 is a leap year? false
M03_LIAN1878_11_GE_C03.indd 119 12/29/17 3:55 PM
120 Chapter 3 Selections
3.11.1 How many days in the February of a leap year? Which of the following is a leap year? 500, 1000, 2000, 2016, and 2020?
3.12 Case Study: Lottery The lottery program involves generating random numbers, comparing digits, and using Boolean operators.
Suppose you want to develop a program to play lottery. The program randomly generates a lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is $10,000.
2. If all digits in the user input match all digits in the lottery number, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.
Note the digits of a two-digit number may be 0. If a number is less than 10, we assume that the number is preceded by a 0 to form a two-digit number. For example, number 8 is treated as 08, and number 0 is treated as 00 in the program. Listing 3.8 gives the complete program.
lisTing 3.8 Lottery.java 1 import java.util.Scanner; 2 3 public class Lottery { 4 public static void main(String[] args) { 5 // Generate a lottery number 6 int lottery = (int)(Math.random() * 100); 7 8 // Prompt the user to enter a guess 9 Scanner input = new Scanner(System.in); 10 System.out.print("Enter your lottery pick (two digits): "); 11 int guess = input.nextInt(); 12 13 // Get digits from lottery 14 int lotteryDigit1 = lottery / 10; 15 int lotteryDigit2 = lottery % 10; 16 17 // Get digits from guess 18 int guessDigit1 = guess / 10; 19 int guessDigit2 = guess % 10; 20 21 System.out.println("The lottery number is " + lottery); 22 23 // Check the guess 24 if (guess == lottery) 25 System.out.println("Exact match: you win $10,000"); 26 else if (guessDigit2 == lotteryDigit1 27 && guessDigit1 == lotteryDigit2) 28 System.out.println("Match all digits: you win $3,000"); 29 else if (guessDigit1 == lotteryDigit1 30 || guessDigit1 == lotteryDigit2 31 || guessDigit2 == lotteryDigit1
Point Check
Point Key
generate a lottery number
enter a guess
exact match?
match all digits?
match one digit?
Enter a year: 2002
2002 is a leap year? false
M03_LIAN1878_11_GE_C03.indd 120 12/29/17 3:55 PM
3.12 Case Study: Lottery 121
32 || guessDigit2 == lotteryDigit2) 33 System.out.println("Match one digit: you win $1,000"); 34 else 35 System.out.println("Sorry, no match"); 36 } 37 }
Enter your lottery pick: 23
The lottery number is 34 Match one digit: you win $1,000
Enter your lottery pick: 23
The lottery number is 14 Sorry: no match
Enter your lottery pick (two digits): 15
The lottery number is 15 Exact match: you win $10,000
Enter your lottery pick (two digits): 45
The lottery number is 54 Match all digits: you win $3,000
line#
variable
6 11 14 15 18 19 33
lottery 34
guess 23
lotteryDigit1 3
lotteryDigit2 4
guessDigit1 2
guessDigit2 3
Output Match one digit:
you win $1,000
The program generates a lottery using the random() method (line 6) and prompts the user to enter a guess (line 11). Note guess % 10 obtains the last digit from guess and guess /10 obtains the first digit from guess, since guess is a two-digit number (lines 18 and 19).
The program checks the guess against the lottery number in this order:
1. First, check whether the guess matches the lottery exactly (line 24).
2. If not, check whether the reversal of the guess matches the lottery (lines 26 and 27).
3. If not, check whether one digit is in the lottery (lines 29–32).
4. If not, nothing matches and display "Sorry, no match" (lines 34 and 35).
3.12.1 What happens if you enter an integer as 05? Point Check
M03_LIAN1878_11_GE_C03.indd 121 12/29/17 3:55 PM
122 Chapter 3 Selections
3.13 switch Statements A switch statement executes statements based on the value of a variable or an expression.
The if statement in Listing 3.5, ComputeTax.java, makes selections based on a single true or false condition. There are four cases for computing taxes, which depend on the value of status. To fully account for all the cases, nested if statements were used. Overuse of nested if statements makes a program difficult to read. Java provides a switch statement to simplify coding for multiple conditions. You can write the following switch statement to replace the nested if statement in Listing 3.5:
switch (status) { case 0: compute tax for single filers; break; case 1: compute tax for married jointly or qualifying widow(er); break; case 2: compute tax for married filing separately; break; case 3: compute tax for head of household; break; default: System.out.println("Error: invalid status"); System.exit(1); }
The flowchart of the preceding switch statement is shown in Figure 3.5.
Point Key
Figure 3.5 The switch statement checks all cases and executes the statements in the matched case.
Compute tax for single �lers
Compute tax for married jointly or qualifying widow(er)
Compute tax for head of household
Default actions
status is 0
status is 1
status is 2
status is 3
default
break
break
break
break
Compute tax for married �ling separately
This statement checks to see whether the status matches the value 0, 1, 2, or 3, in that order. If matched, the corresponding tax is computed; if not matched, a message is displayed. Here is the full syntax for the switch statement:
switch (switch-expression) { case value1: statement(s)1; break;
switch statement
M03_LIAN1878_11_GE_C03.indd 122 12/29/17 3:55 PM
3.13 switch Statements 123
case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
The switch statement observes the following rules:
■■ The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses. (The char and String types will be introduced in Chapter 4.)
■■ The value1, . . ., and valueN must have the same data type as the value of the switch-expression. Note that value1, . . ., and valueN are constant expressions, meaning they cannot contain variables, such as 1 + x.
■■ When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached.
■■ The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
■■ The keyword break is optional. The break statement immediately ends the switch statement.
Caution Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as fall-through behavior. For example, the following code displays Weekday for days 1–5 and Weekend for day 0 and day 6.
without break
fall-through behavior
switch (day) { case 1: case 2: case 3: case 4: case 5: System.out.println(“Weekday"); break; case 0: case 6: System.out.println(“Weekend"); }
Tip To avoid programming errors and improve code maintainability, it is a good idea to put a comment in a case clause if break is purposely omitted.
Now let us write a program to find out the Chinese Zodiac sign for a given year. The Chi- nese Zodiac is based on a 12-year cycle, with each year represented by an animal—monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep—in this cycle, as shown in Figure 3.6.
Note year % 12 determines the Zodiac sign. 1900 is the year of the rat because 1900 % 12 is 4. Listing 3.9 gives a program that prompts the user to enter a year and displays the animal for the year.
M03_LIAN1878_11_GE_C03.indd 123 12/29/17 3:55 PM
124 Chapter 3 Selections
lisTing 3.9 ChineseZodiac.java 1 import java.util.Scanner; 2 3 public class ChineseZodiac { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 System.out.print("Enter a year: "); 8 int year = input.nextInt(); 9 10 switch (year % 12) { 11 case 0: System.out.println("monkey"); break; 12 case 1: System.out.println("rooster"); break; 13 case 2: System.out.println("dog"); break; 14 case 3: System.out.println("pig"); break; 15 case 4: System.out.println("rat"); break; 16 case 5: System.out.println("ox"); break; 17 case 6: System.out.println("tiger"); break; 18 case 7: System.out.println("rabbit"); break; 19 case 8: System.out.println("dragon"); break; 20 case 9: System.out.println("snake"); break; 21 case 10: System.out.println("horse"); break; 22 case 11: System.out.println("sheep"); 23 } 24 } 25 }
enter year
determine Zodiac sign
Figure 3.6 The Chinese Zodiac is based on a 12-year cycle.
rat 0: monkey 1: rooster 2: dog 3: pig 4: rat 5: ox 6: tiger 7: rabbit 8: dragon 9: snake 10: horse 11: sheep
ox
tiger
rabbit
dragon
snakehorse
sheep
monkey
rooster
dog
pig
year % 12 =
Enter a year: 1963
rabbit
Enter a year: 1877
ox
M03_LIAN1878_11_GE_C03.indd 124 12/29/17 3:55 PM
3.14 Conditional Operators 125
3.13.1 What data types are required for a switch variable? If the keyword break is not used after a case is processed, what is the next statement to be executed? Can you convert a switch statement to an equivalent if statement, or vice versa? What are the advantages of using a switch statement?
3.13.2 What is y after the following switch statement is executed? Rewrite the code using an if-else statement.
x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; }
3.13.3 What is x after the following if-else statement is executed? Use a switch statement to rewrite it and draw the flowchart for the new switch statement.
int x = 1, a = 3; if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;
3.13.4 Write a switch statement that displays Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6, respectively.
3.13.5 Rewrite Listing 3.9 using an if-else statement.
3.14 Conditional Operators A conditional operator evaluates an expression based on a condition.
You might want to assign a value to a variable that is restricted by certain conditions. For example, the following statement assigns 1 to y if x is greater than 0 and -1 to y if x is less than or equal to 0:
if (x > 0) y = 1; else y = −1;
Alternatively, as in the following example, you can use a conditional operator to achieve the same result.
y = (x > 0) ? 1 : −1;
The symbols ? and : appearing together is called a conditional operator (also known as a ternary operator because it uses three operands. It is the only ternary operator in Java. The conditional operator is in a completely different style, with no explicit if in the statement. The syntax to use the operator is as follows:
boolean-expression ? expression1 : expression2
The result of this expression is expression1 if boolean-expression is true; otherwise the result is expression2.
Suppose you want to assign the larger number of variable num1 and num2 to max. You can simply write a statement using the conditional operator:
max = (num1 > num2) ? num1 : num2;
Point Check
Point Key
conditional operator
ternary operator
M03_LIAN1878_11_GE_C03.indd 125 12/29/17 3:55 PM
126 Chapter 3 Selections
For another example, the following statement displays the message “num is even” if num is even, and otherwise displays “num is odd.”
System.out.println((num % 2 == 0) ? "num is even" : "num is odd");
As you can see from these examples, the conditional operator enables you to write short and concise code.
3.14.1 Suppose when you run the following program, you enter the input 2 3 6 from the console. What is the output?
public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble();
System.out.println((x < y && y < z) ? "sorted" : "not sorted"); } }
3.14.2 Rewrite the following if statements using the conditional operator.
Point Check
if (ages >= 16) ticketPrice = 20; else ticketPrice = 10;
3.14.3 Rewrite the following codes using if-else statements.
a. score = (x > 10) ? 3 * scale : 4 * scale; b. tax = (income > 10000) ? income * 0.2 : income * 0.17 + 1000; c. System.out.println((number % 3 == 0) ? i : j);
3.14.4 Write an expression using a conditional operator that returns randomly -1 or 1.
3.15 Operator Precedence and Associativity Operator precedence and associativity determine the order in which operators are evaluated.
Section 2.11 introduced operator precedence involving arithmetic operators. This section dis- cusses operator precedence in more detail. Suppose you have this expression:
3 + 4 * 4 > 5 * (4 + 3) – 1 && (4 – 3 > 5)
What is its value? What is the execution order of the operators? The expression within parentheses is evaluated first. (Parentheses can be nested, in which
case the expression within the inner parentheses is executed first.) When evaluating an expres- sion without parentheses, the operators are applied according to the precedence rule and the associativity rule.
The precedence rule defines precedence for operators, as shown in Table 3.8, which con- tains the operators you have learned so far. Operators are listed in decreasing order of prece- dence from top to bottom. The logical operators have lower precedence than the relational operators, and the relational operators have lower precedence than the arithmetic operators. Operators with the same precedence appear in the same group. (See Appendix C, Operator Precedence Chart, for a complete list of Java operators and their precedence.)
Point Key
operator precedence
M03_LIAN1878_11_GE_C03.indd 126 12/29/17 3:55 PM
3.15 Operator Precedence and Associativity 127
Assignment operators are right associative. Therefore, the expression
Precedence Operator
var++ and var−− (Postfix)
+, − (Unary plus and minus), ++var and −−var (Prefix)
(type) (Casting)
!(Not)
*, /, % (Multiplication, division, and remainder)
+, − (Binary addition and subtraction)
<, <=, >, >= (Relational)
==, != (Equality)
^ (Exclusive OR)
&& (AND)
|| (OR)
=, +=, −=, *=, /=, %= (Assignment operators)
Table 3.8 Operator Precedence Chart
a – b + c – d is equivalent to
((a – b) + c) - d
a = b += c = d is equivalent to
a = (b += (c = 5))
Suppose a, b, and c are 1 before the assignment; after the whole expression is evaluated, a becomes 6, b becomes 6, and c becomes 5. Note left associativity for the assignment operator would not make sense.
Note Java has its own way to evaluate an expression internally. The result of a Java evaluation is the same as that of its corresponding arithmetic evaluation. Advanced readers may refer to Sup- plement III.B for more discussions on how an expression is evaluated in Java behind the scenes.
3.15.1 List the precedence order of the Boolean operators. Evaluate the following expressions:
true || true && false true && true || false
3.15.2 True or false? All the binary operators except = are left associative. 3.15.3 Evaluate the following expressions:
2 * 2 – 3 > 2 && 4 – 2 > 5 2 * 2 – 3 > 2 || 4 – 2 > 5
3.15.4 Is (x > 0 && x < 10) the same as ((x > 0) && (x < 10))? Is (x > 0 || x < 10) the same as ((x > 0) || (x < 10))? Is (x > 0 || x < 10 && y < 0) the same as (x > 0 ||
(x < 10 && y < 0))?
behind the scenes
Point Check
If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left associative. For example, since + and − are of the same precedence and are left associative, the expression operator associativity
M03_LIAN1878_11_GE_C03.indd 127 12/29/17 3:55 PM
128 Chapter 3 Selections
3.16 Debugging Debugging is the process of finding and fixing errors in a program.
As mentioned in Section 1.10, syntax errors are easy to find and easy to correct because the compiler gives indications as to where the errors came from and why they are there. Runtime errors are not difficult to find either, because the Java interpreter displays them on the console when the program aborts. Finding logic errors, on the other hand, can be very challenging.
Logic errors are called bugs. The process of finding and correcting errors is called debug- ging. A common approach to debugging is to use a combination of methods to help pinpoint the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. These approaches might work for debug- ging a short, simple program, but for a large, complex program, the most effective approach is to use a debugger utility.
JDK includes a command-line debugger, jdb, which is invoked with a class name. jdb is itself a Java program, running its own copy of Java interpreter. All the Java IDE tools, such as Eclipse and NetBeans, include integrated debuggers. The debugger utilities let you follow the execution of a program. They vary from one system to another, but they all support most of the following helpful features.
■■ Executing a single statement at a time: The debugger allows you to execute one statement at a time so that you can see the effect of each statement.
■■ Tracing into or stepping over a method: If a method is being executed, you can ask the debugger to enter the method and execute one statement at a time in the method, or you can ask it to step over the entire method. You should step over the entire method if you know that the method works. For example, always step over system-supplied methods, such as System.out.println.
■■ Setting breakpoints: You can also set a breakpoint at a specific statement. Your program pauses when it reaches a breakpoint. You can set as many breakpoints as you want. Breakpoints are particularly useful when you know where your programming error starts. You can set a breakpoint at that statement, and have the program execute until it reaches the breakpoint.
■■ Displaying variables: The debugger lets you select several variables and display their values. As you trace through a program, the content of a variable is continu- ously updated.
■■ Displaying call stacks: The debugger lets you trace all of the method calls. This feature is helpful when you need to see a large picture of the program-execution flow.
■■ Modifying variables: Some debuggers enable you to modify the value of a variable when debugging. This is convenient when you want to test a program with different samples, but do not want to leave the debugger.
Tip If you use an IDE such as Eclipse or NetBeans, please refer to Learning Java Effectively with Eclipse/NetBeans in Supplements II.C and II.E on the Companion Website. The supplement shows you how to use a debugger to trace programs, and how debugging can help in learning Java effectively.
Point Key
bugs debugging hand-traces
debugging in IDE
M03_LIAN1878_11_GE_C03.indd 128 12/29/17 3:55 PM
Chapter Summary 129
ChapTer summary 1. A boolean-type variable can store a true or false value.
2. The relational operators (<, <=, ==, !=, >, and >=) yield a Boolean value.
3. Selection statements are used for programming with alternative courses of actions. There are several types of selection statements: one-way if statements, two-way if-else statements, nested if statements, multi-way if-else statements, switch statements, and conditional operators.
4. The various if statements all make control decisions based on a Boolean expression. Based on the true or false evaluation of the expression, these statements take one of the two possible courses.
5. The Boolean operators &&, ||, !, and ^ operate with Boolean values and variables.
6. When evaluating p1 && p2, Java first evaluates p1 then evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2. When evaluating p1 || p2, Java first evaluates p1 then evaluates p2 if p1 is false; if p1 is true, it does not evaluate p2. Therefore, && is referred to as the short-circuit or lazy AND operator, and || is referred to as the short-circuit or lazy OR operator.
7. The switch statement makes control decisions based on a switch expression of type char, byte, short, int, or String.
8. The keyword break is optional in a switch statement, but it is normally used at the end of each case in order to skip the remainder of the switch statement. If the break statement is not present, the next case statement will be executed.
9. The operators in expressions are evaluated in the order determined by the rules of paren- theses, operator precedence, and operator associativity.
10. Parentheses can be used to force the order of evaluation to occur in any sequence.
11. Operators with higher precedence are evaluated earlier. For operators of the same prec- edence, their associativity determines the order of evaluation.
12. All binary operators except assignment operators are left associative; assignment opera- tors are right associative.
Key Terms Boolean expression 98 boolean data type 98 Boolean value 98 conditional operator 125 dangling else ambiguity 107 debugging 128 fall-through behavior 123
flowchart 100 lazy operator 118 operator associativity 127 operator precedence 126 selection statement 98 short-circuit operator 118
M03_LIAN1878_11_GE_C03.indd 129 12/29/17 3:55 PM
130 Chapter 3 Selections
Quiz Answer the quiz for this chapter online at the Companion Website.
programming exerCiSeS
Pedagogical Note For each exercise, carefully analyze the problem requirements and design strategies for solving the problem before coding.
Debugging Tip Before you ask for help, read and explain the program to yourself, and trace it using several representative inputs by hand or using an IDE debugger. You learn how to pro- gram by debugging your own mistakes.
Section 3.2 *3.1 (Algebra: solve quadratic equations) The two roots of a quadratic equation
ax2 + bx + c = 0 can be obtained using the following formula:
r1 = -b + 2b2 - 4ac
2a and r2 =
-b - 2b2 - 4ac 2a
b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display “The equation has no real roots.”
Note you can use Math.pow(x, 0.5) to compute 2x. Here are some sample runs:
think before coding
learn from mistakes
Enter a, b, c: 1.0 3 1
The equation has two roots -0.381966 and -2.61803
Enter a, b, c: 1 2.0 1
The equation has one root -1.0
Enter a, b, c: 1 2 3
The equation has no real roots
3.2 (Game: multiply three numbers) The program in Listing 3.1, AdditionQuiz.java, generates two integers and prompts the user to enter the product of these two inte- gers. Revise the program to generate three single-digit integers and prompt the user to enter the multiplication of these three integers.
M03_LIAN1878_11_GE_C03.indd 130 12/29/17 3:55 PM
Programming Exercises 131
Sections 3.3–3.7 *3.3 (Algebra: solve 2 * 2 linear equations) A linear equation can be solved using
Cramer’s rule given in Programming Exercise 1.13. Write a program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that “The equation has no solution.”
Enter a, b, c, d, e, f: 9.0 4.0 3.0 -5.0 -6.0 -21.0
x is -2.0 and y is 3.0
Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0
The equation has no solution
Enter today’s day: 1
Enter the number of days elapsed since today: 3 Today is Monday and the future day is Thursday
Enter today’s day: 0
Enter the number of days elapsed since today: 31
Today is Sunday and the future day is Wednesday
Enter weight in pounds: 140
Enter feet: 5
Enter inches: 10
BMI is 20.087702275404553
Normal
**3.4 (Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month names January, February, . . . , December for the numbers 1, 2, . . . , 12, accordingly.
*3.5 (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, . . . , and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. Here is a sample run:
*3.6 (Health application: BMI) Revise Listing 3.4, ComputeAndInterpretBMI.java, to let the user enter weight, feet, and inches. For example, if a person is 5 feet and 10 inches, you will enter 5 for feet and 10 for inches. Here is a sample run:
3.7 (Financial application: monetary units) Modify Listing 2.10, ComputeChange. java, to display the nonzero denominations only, using singular words for single units such as 1 dollar and 1 penny, and plural words for more than one unit such as 2 dollars and 3 pennies.
M03_LIAN1878_11_GE_C03.indd 131 12/29/17 3:55 PM
132 Chapter 3 Selections
*3.8 (Sort three integers) Write a program that prompts the user to enter three integers and display the integers in non-decreasing order.
**3.9 (Business: check ISBN-10) An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the other 9 digits using the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 +
d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9),11
If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer. Here are sample runs:
VideoNote
Sort three integers
Enter the first 9 digits of an ISBN as integer: 013601267
The ISBN-10 number is 0136012671
Enter the first 9 digits of an ISBN as integer: 013031997
The ISBN-10 number is 013031997X
Enter a three-digit integer: 121
121 is a palindrome
Enter a three-digit integer: 123
123 is not a palindrome
3.10 (Game: multiplication quiz) Listing 3.3, SubtractionQuiz.java, randomly generates a subtraction question. Revise the program to randomly generate a multiplication question with two integers less than 1000.
Sections 3.8–3.16 *3.11 (Find the number of days in a month) Write a program that prompts the user to enter
the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days.
3.12 (Palindrome integer) Write a program that prompts the user to enter a three-digit integer and determines whether it is a palindrome integer. An integer is palindrome if it reads the same from right to left and from left to right. A negative integer is treated the same as a positive integer. Here are sample runs of this program:
*3.13 (Financial application: compute taxes) Listing 3.5, ComputeTax.java, gives the source code to compute taxes for single filers. Complete this program to compute taxes for all filing statuses.
3.14 (Game: heads or tails) Write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the user to enter a guess, and reports whether the guess is correct or incorrect.
M03_LIAN1878_11_GE_C03.indd 132 12/29/17 3:55 PM
Programming Exercises 133
**3.15 (Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is $12,000.
2. If all digits in the user input match all digits in the lottery number, the award is $5,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $2,000.
3.16 (Random point) Write a program that displays a random coordinate in a rectangle. The rectangle is centred at (0, 0) with width 50 and height 150.
*3.17 (Game: scissor, rock, paper) Write a program that plays the popular scissor–rock– paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:
scissor (0), rock (1), paper (2): 1
The computer is scissor. You are rock. You won
scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw
*3.18 (Cost of shipping) A shipping company uses the following function to calculate the cost (in dollars) of shipping based on the weight of the package (in pounds).
c(w) = d 2.5, if 0 6 w 6 = 24.5, if 2 6 w 6 = 4 7.5, if 4 6 w 6 = 10 10.5, if 10 6 w 6 = 20
Write a program that prompts the user to enter the weight of the package and display the shipping cost. If the weight is greater than 20, display a message “the package cannot be shipped.”
**3.19 (Compute the perimeter of a triangle) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.
*3.20 (Science: wind-chill temperature) Programming Exercise 2.17 gives a formula to compute the wind-chill temperature. The formula is valid for temperatures in the range between -58°F and 41°F and wind speed greater than or equal to 2. Write a program that prompts the user to enter a temperature and a wind speed. The pro- gram displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid.
M03_LIAN1878_11_GE_C03.indd 133 12/29/17 3:55 PM
134 Chapter 3 Selections
Comprehensive **3.21 (Science: day of the week) Zeller’s congruence is an algorithm developed by
Christian Zeller to calculate the day of the week. The formula is
h = aq + 26(m + 1) 10
+ k + k 4
+ j
4 + 5jb,7
where
■■ h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, and 6: Friday).
■■ q is the day of the month. ■■ m is the month (3: March, 4: April, . . ., 12: December). January and February
are counted as months 13 and 14 of the previous year.
■■ j is year
100 .
■■ k is the year of the century (i.e., year % 100).
Note all divisions in this exercise perform an integer division. Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week. Here are some sample runs:
Enter year: (e.g., 2012): 2015
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday
Enter year: (e.g., 2012): 2012
Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
(Hint: January and February are counted as 13 and 14 in the formula, so you need to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year. For example, if the user enters 1 for m and 2015 for year, m will be 13 and year will be 2014 used in the formula.)
** 3.22 (Geometry: point in a circle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle, as shown in Figure 3.7a.
(Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for computing the distance is 2(x2 - x1)2 + (y2 - y1)2. Test your program to cover all cases.) Two sample runs are shown below:
VideoNote
Check point location
Enter a point with two coordinates: 4 5
Point (4.0, 5.0) is in the circle
Enter a point with two coordinates: 9 9
Point (9.0, 9.0) is not in the circle
M03_LIAN1878_11_GE_C03.indd 134 12/29/17 3:55 PM
** 3.23 (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centred at (1, 1) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in Figure 3.7b. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to 10 / 2 and its vertical distance to (0, 0) is less than or equal to 5.0 / 2. Test your program to cover all cases.) Here are two sample runs.
Figure 3.7 (a) Points inside and outside of the circle. (b) Points inside and outside of the rectangle.
x-axis(0, 0)
(a) (b)
y-axis
(4, 5)
(9, 9)
(2, 2) (6, 4)
x-axis
y-axis
(0, 0)
Enter a point with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle
Enter a point with two coordinates: 6 4
Point (6.0, 4.0) is not in the rectangle
**3.24 (Game: pick a card) Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card. Here is a sample run of the program:
The card you picked is Jack of Hearts
*3.25 (Geometry: intersecting point) Two points on line 1 are given as (x1, y1) and (x2, y2) and on line 2 as (x3, y3) and (x4, y4), as shown in Figure 3.8a and b.
The intersecting point of the two lines can be found by solving the following linear equations:
(y1 - y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1 (y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3
This linear equation can be solved using Cramer’s rule (see Programming Exer- cise 3.3). If the equation has no solutions, the two lines are parallel (see Figure 3.8c).
Programming Exercises 135
M03_LIAN1878_11_GE_C03.indd 135 12/29/17 3:55 PM
136 Chapter 3 Selections
Write a program that prompts the user to enter four points and displays the intersect- ing point. Here are sample runs:
Figure 3.8 Two lines intersect in (a and b) and two lines are parallel in (c).
(x1, y1)
(x2, y2) (x3, y3)
(x4, y4)
(a) (b) (c)
(x1, y1)
(x2, y2)
(x3, y3)
(x4, y4) (x1, y1)
(x2, y2)
(x3, y3)
(x4, y4)
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at (2.88889, 1.1111)
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
The two lines are parallel
Enter an integer: 10
Is 10 divisible by 4 and 5? false
Is 10 divisible by 4 or 5? true
Is 10 divisible by 4 or 5 but not both? true
3.26 (Use the &&, || and ^ operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 4 and 5, whether it is divisible by 4 or 5, and whether it is divisible by 4 or 5 but not both. Here is a sample run of this program:
** 3.27 (Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0) and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle. Here are the sample runs:
(0, 100)
(0, 0) (200, 0)
p2
p1
Enter a point’s x- and y-coordinates: 100.5 25.5
The point is in the triangle
M03_LIAN1878_11_GE_C03.indd 136 12/29/17 3:55 PM
** 3.28 (Geometry: two rectangles) Write a program that prompts the user to enter the center x-, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure 3.9. Test your program to cover all cases.
Enter a point’s x- and y-coordinates: 100.5 50.5
The point is not in the triangle
Figure 3.9 (a) A rectangle is inside another one. (b) A rectangle overlaps another one.
(a)
w1
(x1, y1) (x2, y2)
w2
h2h1
(b)
w1
(x1, y1)
(x2, y2)
w2
h2
h1
Here are the sample runs:
Enter r1’s center x-, y-coordinates, width, and height: 2.5 4 2.5 43
Enter r2’s center x-, y-coordinates, width, and height: 1.5 5 0.5 3
r2 is inside r1
Enter r1’s center x-, y-coordinates, width, and height: 1 2 3 5.5
Enter r2’s center x-, y-coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1
Enter r1’s center x-, y-coordinates, width, and height: 1 2 3 3
Enter r2’s center x-, y-coordinates, width, and height: 40 45 3 2
r2 does not overlap r1
** 3.29 (Geometry: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first, as shown in Figure 3.10. (Hint: circle2 is inside circle1 if the distance between the two centers 6 = r1 - r2 and circle2 overlaps circle1 if the distance between the two centers 6 = r1 + r2. Test your program to cover all cases.)
Here are the sample runs:
Enter circle1’s center x-, y-coordinates, and radius: 0.5 5.1 13
Enter circle2’s center x-, y-coordinates, and radius: 1 1.7 4.5
circle2 is inside circle1
Programming Exercises 137
M03_LIAN1878_11_GE_C03.indd 137 12/29/17 3:55 PM
138 Chapter 3 Selections
*3.30 (Current time) Revise Programming Exercise 2.8 to display the hour using a 12-hour clock. Here is a sample run:
Figure 3.10 (a) A circle is inside another circle. (b) A circle overlaps another circle.
(a) (b)
(x1, y1)
(x2, y2)
r2
r1
(x1, y1)
r1
(x2, y2)
r2
Enter circle1’s center x-, y-coordinates, and radius: 3.4 5.7 5.5
Enter circle2’s center x-, y-coordinates, and radius: 6.7 3.5 3
circle2 overlaps circle1
Enter circle1’s center x-, y-coordinates, and radius: 3.4 5.5 1
Enter circle2’s center x-, y-coordinates, and radius: 5.5 7.2 1
circle2 does not overlap circle1
*3.31 (Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Chinese RMB. Prompt the user to enter 0 to convert from U.S. dollars to Chinese RMB and 1 to convert from Chinese RMB to U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Chinese RMB to convert it to Chinese RMB or U.S. dollars, respectively. Here are the sample runs:
Enter the time zone offset to GMT: -5
The current time is 4:50:34 AM
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 1
Enter the RMB amount: 10000
10000.0 yuan is $1468.43
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 0
Enter the dollar amount: 100
$100.0 is 681.0 yuan
M03_LIAN1878_11_GE_C03.indd 138 12/29/17 3:55 PM
*3.32 (Geometry: point position) Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point p2(x2, y2) is on the left of the line, on the right, or on the same line (see Figure 3.11):
(x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0) c 70 p2 is on the left side of the line=0 p2 is on the same line 60 p2 is on the right side of the line
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 5
Incorrect input
Figure 3.11 (a) p2 is on the left of the line. (b) p2 is on the right of the line. (c) p2 is on the same line.
p0
p2 p1
p0
p2
p1
p0
p2
p1
(a) (b) (c)
Write a program that prompts the user to enter the three points for p0, p1, and p2 and displays whether p2 is on the left of the line from p0 to p1, to the right, or on the same line. Here are some sample runs:
Enter three points for p0, p1, and p2: 4.4 2 6.5 9.5 -5 4
p2 is on the left side of the line
Enter three points for p0, p1, and p2: 1 1 5 5 2 2
p2 is on the same line
Enter three points for p0, p1, and p2: 3.4 2 6.5 9.5 5 2.5
p2 is on the right side of the line
*3.33 (Financial: compare costs) Suppose you shop for rice in two different packages. You would like to write a program to compare the cost. The program prompts the user to enter the weight and price of each package and displays the one with the better price. Here is a sample run:
Enter weight and price for package 1: 50 24.59
Enter weight and price for package 2: 25 11.99
Package 2 has a better price.
Programming Exercises 139
M03_LIAN1878_11_GE_C03.indd 139 12/29/17 3:55 PM
140 Chapter 3 Selections
*3.34 (Geometry: point on line segment) Exercise 3.32 shows how to test whether a point is on an unbounded line. Revise Exercise 3.32 to test whether a point is on a line segment. Write a program that prompts the user to enter the three points for p0, p1, and p2 and displays whether p2 is on the line segment from p0 to p1. Here are some sample runs:
Enter weight and price for package 1: 50 25
Enter weight and price for package 2: 25 12.5
Two packages have the same price.
Enter three points for p0, p1, and p2: 1 1 2.5 2.5 1.5 1.5
(1.5, 1.5) is on the line segment from (1.0, 1.0) to (2.5, 2.5)
Enter an integer: 200
200 is even number
Enter an integer: 211
211 is odd number
Enter three points for p0, p1, and p2: 1 1 2 2 3.5 3.5
(3.5, 3.5) is not on the line segment from (1.0, 1.0) to (2.0, 2.0)
3.35 (Even or odd number) Write a program that prompts the user to enter an integer and displays whether the integer is an odd number or not. Here are the sample runs:
Note More than 200 additional programming exercises with solutions are provided to the instructors on the Instructor Resource Website.
M03_LIAN1878_11_GE_C03.indd 140 12/29/17 3:55 PM
Mathematical Functions, Characters, and Strings
Objectives ■■ To solve mathematical problems by using the methods in the Math class (§4.2).
■■ To represent characters using the char type (§4.3).
■■ To encode characters using ASCII and Unicode (§4.3.1).
■■ To represent special characters using the escape sequences (§4.3.2).
■■ To cast a numeric value to a character and cast a character to an integer (§4.3.3).
■■ To compare and test characters using the static methods in the Character class (§4.3.4).
■■ To introduce objects and instance methods (§4.4).
■■ To represent strings using the String object (§4.4).
■■ To return the string length using the length() method (§4.4.1).
■■ To return a character in the string using the charAt(i) method (§4.4.2).
■■ To use the + operator to concatenate strings (§4.4.3).
■■ To return an uppercase string or a lowercase string and to trim a string (§4.4.4).
■■ To read strings from the console (§4.4.5).
■■ To read a character from the console (§4.4.6).
■■ To compare strings using the equals and the compareTo methods (§4.4.7).
■■ To obtain substrings (§4.4.8).
■■ To find a character or a substring in a string using the indexOf method (§4.4.9).
■■ To program using characters and strings (GuessBirthday) (§4.5.1).
■■ To convert a hexadecimal character to a decimal value (HexDigit2Dec) (§4.5.2).
■■ To revise the lottery program using strings (LotteryUsingStrings) (§4.5.3).
■■ To format output using the System.out.printf method (§4.6).
Chapter
4
M04_LIAN1878_11_GE_C04.indd 141 12/29/17 3:56 PM
142 Chapter 4 Mathematical Functions, Characters, and Strings
4.1 Introduction The focus of this chapter is to introduce mathematical functions, characters, string objects, and use them to develop programs.
The preceding chapters introduced fundamental programming techniques and taught you how to write simple programs to solve basic problems using selection statements. This chapter introduces methods for performing common mathematical operations. You will learn how to create custom methods in Chapter 6.
Suppose you need to estimate the area enclosed by four cities, given the GPS locations (lati- tude and longitude) of these cities, as shown in the following diagram. How would you write a program to solve this problem? You will be able to write such a program in this chapter.
Point Key
problem
Orlando (28.5383355, –81.3792365)
Savannah (32.0835407, –81.0998342)
Charlotte (35.2270869, –80.8431267)
Atlanta (33.7489954, –84.3879824)
Method Description
sin(radians) Returns the trigonometric sine of an angle in radians.
cos(radians) Returns the trigonometric cosine of an angle in radians.
tan(radians) Returns the trigonometric tangent of an angle in radians.
toRadians(degree) Returns the angle in radians for the angle in degrees.
toDegrees(radians) Returns the angle in degrees for the angle in radians.
asin(a) Returns the angle in radians for the inverse of sine.
acos(a) Returns the angle in radians for the inverse of cosine.
atan(a) Returns the angle in radians for the inverse of tangent.
Table 4.1 Trigonometric Methods in the Math Class
Because strings are frequently used in programming, it is beneficial to introduce strings early so that you can begin to use them to develop useful programs. This chapter also gives a brief introduction to string objects; you will learn more on objects and strings in Chapters 9 and 10.
4.2 Common Mathematical Functions Java provides many useful methods in the Math class for performing common math- ematical functions.
A method is a group of statements that performs a specific task. You have already used the pow(a, b) method to compute ab in Section 2.9.4, Exponent Operations and the random() method for generating a random number in Section 3.7. This section introduces other useful methods in the Math class. They can be categorized as trigonometric methods, exponent meth- ods, and service methods. Service methods include the rounding, min, max, absolute, and ran- dom methods. In addition to methods, the Math class provides two useful double constants, PI and E (the base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.
4.2.1 Trigonometric Methods The Math class contains the following methods as listed in Table 4.1 for performing t rigonometric functions:
Point Key
VideoNote
Introduce Math functions
M04_LIAN1878_11_GE_C04.indd 142 12/29/17 3:56 PM
4.2 Common Mathematical Functions 143
The parameter for sin, cos, and tan is an angle in radians. The return value for asin and atan is an angle in radians in the range between -p/2 and p/2, and for acos is between 0 and p. One degree is equal to p/180 in radians, 90 degrees is equal to p/2 in radians, and 30 degrees is equal to p/6 in radians.
For example,
Math.toDegrees(Math.PI / 2) returns 90.0 Math.toRadians(30) returns 0.5236 (same as π/6) Math.sin(0) returns 0.0 Math.sin(Math.toRadians(270)) returns −1.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 Math.cos(Math.PI / 2) returns 0 Math.asin(0.5) returns 0.523598333 (same as π/6) Math.acos(0.5) returns 1.0472 (same as π/3) Math.atan(1.0) returns 0.785398 (same as π/4)
4.2.2 Exponent Methods There are five methods related to exponents in the Math class as listed in Table 4.2.
Method Description
exp(x) Returns e raised to power of x (ex).
log(x) Returns the natural logarithm of x (ln(x) = loge(x)). log10(x) Returns the base 10 logarithm of x (log10(x)).
pow(a, b) Returns a raised to the power of b (ab). sqrt(x) Returns the square root of x (2x) for x 7 = 0.
Table 4.2 Exponent Methods in the Math Class
For example,
e3.5 is Math.exp(3.5), which returns 33.11545 ln(3.5) is Math.log(3.5), which returns 1.25276 log10 (3.5) is Math.log10(3.5), which returns 0.544 23 is Math.pow(2, 3), which returns 8.0 32 is Math.pow(3, 2), which returns 9.0 4.52.5 is Math.pow(4.5, 2.5), which returns 42.956724 is Math.sqrt(4), which returns 2.0210.5 is Math.sqrt(10.5), which returns 3.24
4.2.3 The Rounding Methods The Math class contains four rounding methods as listed in Table 4.3.
Method Description
ceil(x) x is rounded up to its nearest integer. This integer is returned as a double value.
floor(x) x is rounded down to its nearest integer. This integer is returned as a double value.
rint(x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double value.
round(x) Returns (int)Math.floor(x + 0.5) if x is a float and returns (long)Math.floor(x + 0.5) if x is a double.
Table 4.3 Rounding Methods in the Math Class
M04_LIAN1878_11_GE_C04.indd 143 12/29/17 3:56 PM
144 Chapter 4 Mathematical Functions, Characters, and Strings
For example,
Math.ceil(2.1) returns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(−2.0) returns −2.0 Math.ceil(−2.1) returns −2.0 Math.floor(2.1) returns 2.0 Math.floor(2.0) returns 2.0 Math.floor(−2.0) returns −2.0 Math.floor(−2.1) returns −3.0 Math.rint(2.1) returns 2.0 Math.rint(−2.0) returns −2.0 Math.rint(−2.1) returns −2.0 Math.rint(2.5) returns 2.0 Math.rint(4.5) returns 4.0 Math.rint(−2.5) returns −2.0 Math.round(2.6f) returns 3 // Returns int Math.round(2.0) returns 2 // Returns long Math.round(−2.0f) returns −2 // Returns int Math.round(−2.6) returns −3 // Returns long Math.round(−2.4) returns −2 // Returns long
4.2.4 The min, max, and abs Methods The min and max methods return the minimum and maximum numbers of two numbers (int, long, float, or double). For example, max(4.4, 5.0) returns 5.0, and min(3, 2) returns 2.
The abs method returns the absolute value of the number (int, long, float, or double). For example,
Math.max(2, 3) returns 3 Math.min(2.5, 4.6) returns 2.5 Math.max(Math.max(2.5, 4.6), Math.min(3, 5.6)) returns 4.6 Math.abs(−2) returns 2 Math.abs(−2.1) returns 2.1
4.2.5 The random Method You used the random() method in the preceding chapter. This method generates a random double value greater than or equal to 0.0 and less than 1.0 (0 <= Math.random() < 1.0). You can use it to write a simple expression to generate random numbers in any range. For example,
(int)(Math.random() * 10) Returns a random integer
between 0 and 9.
50 + (int)(Math.random() * 50) Returns a random integer
between 50 and 99.
In general,
a + Math.random() * b Returns a random number between a
and a + b, excluding a + b.
M04_LIAN1878_11_GE_C04.indd 144 12/29/17 3:56 PM
4.2 Common Mathematical Functions 145
4.2.6 Case Study: Computing Angles of a Triangle You can use the math methods to solve many computational problems. Given the three sides of a triangle, for example, you can compute the angles by using the following formulas:
A
B
C
a
b
c
x1, y1
x2, y2
x3, y3
A = acos((a * a − b * b − c * c) / (−2 * b * c)) B = acos((b * b − a * a − c * c) / (−2 * a * c)) C = acos((c * c − b * b − a * a) / (−2 * a * b))
Don’t be intimidated by the mathematical formula. As we discussed early in Listing 2.9, ComputeLoan.java, you don’t have to know how the mathematical formula is derived in order to write a program for computing the loan payments. Here, in this example, given the length of three sides, you can use this formula to write a program to compute the angles without having to know how the formula is derived. In order to compute the lengths of the sides, we need to know the coordinates of three corner points and compute the distances between the points.
Listing 4.1 is an example of a program that prompts the user to enter the x- and y-coordinates of the three corner points in a triangle then displays the three angles.
lisTing 4.1 ComputeAngles.java 1 import java.util.Scanner; 2 3 public class ComputeAngles { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter three points 8 System.out.print("Enter three points: "); 9 double x1 = input.nextDouble(); 10 double y1 = input.nextDouble(); 11 double x2 = input.nextDouble(); 12 double y2 = input.nextDouble(); 13 double x3 = input.nextDouble(); 14 double y3 = input.nextDouble(); 15 16 // Compute three sides 17 double a = Math.sqrt((x2 − x3) * (x2 − x3) 18 + (y2 − y3) * (y2 − y3)); 19 double b = Math.sqrt((x1 − x3) * (x1 − x3) 20 + (y1 − y3) * (y1 − y3)); 21 double c = Math.sqrt((x1 − x2) * (x1 − x2) 22 + (y1 − y2) * (y1 − y2)); 23 24 // Compute three angles 25 double A = Math.toDegrees(Math.acos((a * a − b * b − c * c) 26 / (−2 * b * c))); 27 double B = Math.toDegrees(Math.acos((b * b − a * a − c * c) 28 / (−2 * a * c))); 29 double C = Math.toDegrees(Math.acos((c * c − b * b − a * a) 30 / (−2 * a * b))); 31
compute sides
enter three points
M04_LIAN1878_11_GE_C04.indd 145 12/29/17 3:56 PM
146 Chapter 4 Mathematical Functions, Characters, and Strings
32 // Display results 33 System.out.println("The three angles are " + 34 Math.round(A * 100) / 100.0 + " " + 35 Math.round(B * 100) / 100.0 + " " + 36 Math.round(C * 100) / 100.0); 37 } 38 }
display result
Enter three points: 1 1 6.5 1 6.5 2.5
The three angles are 15.26 90.0 74.74
The program prompts the user to enter three points (line 8). This prompting message is not clear. You should give the user explicit instructions on how to enter these points as follows:
Sy stem.out.print("Enter the coordinates of three points separated " + "by spaces like x1 y1 x2 y2 x3 y3: ");
Note that the distance between two points (x1, y1) and (x2, y2) can be computed using the formula 2(x2 - x1)2 + (y2 - y1)2. The program computes the distances between two points (lines 17–22), and applies the formula to compute the angles (lines 25–30). The angles are rounded to display up to two digits after the decimal point (lines 34–36).
The Math class is used in the program, but not imported, because it is in the java.lang package. All the classes in the java.lang package are implicitly imported in a Java program.
4.2.1 Evaluate the following method calls: Point
Check
(a) Math.sqrt(4)
(b) Math.sin(2 * Math.PI)
(c) Math.cos(2 * Math.PI)
(d) Math.pow(2, 2)
(e) Math.log(Math.E)
(f) Math.exp(1)
(g) Math.max(2, Math.min(3, 4))
(h) Math.rint(−2.5)
(i) Math.ceil(−2.5)
(j) Math.floor(−2.5)
(k) Math.round(−2.5f)
(l) Math.round(−2.5)
(m) Math.rint(2.5)
(n) Math.ceil(2.5)
(o) Math.floor(2.5)
(p) Math.round(2.5f)
(q) Math.round(2.5)
(r) Math.round(Math.abs(−2.5))
4.2.2 True or false? The argument for trigonometric methods is an angle in radians. 4.2.3 Write a statement that converts 47 degrees to radians and assigns the result to a
variable.
4.2.4 Write a statement that converts PI to an angle in degrees and assigns the result to a variable.
4.2.5 Write an expression that obtains a random integer between 34 and 55. Write an expression that obtains a random integer between 0 and 999. Write an expression that obtains a random number between 5.5 and 55.5.
4.2.6 Why does the Math class not need to be imported? 4.2.7 What is Math.log(Math.exp(5.5))? What is Math.exp(Math.log(5.5))? What is Math.asin(Math.sin(Math.PI / 6))? What is Math.sin(Math.asin(Math.PI / 6))?
M04_LIAN1878_11_GE_C04.indd 146 12/29/17 3:57 PM
4.3 Character Data Type and Operations 147
4.3 Character Data Type and Operations A character data type represents a single character.
In addition to processing numeric values, you can process characters in Java. The character data type, char, is used to represent a single character. A character literal is enclosed in single quotation marks. Consider the following code:
char letter = 'A'; char numChar = '4';
The first statement assigns character A to the char variable letter. The second statement assigns digit character 4 to the char variable numChar.
Caution A string literal must be enclosed in double quotation marks (" "). A character literal is a single character enclosed in single quotation marks (' '). Therefore, "A" is a string, but 'A' is a character.
4.3.1 Unicode and ASCII code Computers use binary numbers internally. A character is stored in a computer as a sequence of 0s and 1s. Mapping a character to its binary representation is called encoding. There are different ways to encode a character. How characters are encoded is defined by an encoding scheme.
Java supports Unicode, an encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world’s diverse lan- guages. Unicode was originally designed as a 16-bit character encoding. The primitive data type char was intended to take advantage of this design by providing a simple data type that could hold any character. However, it turned out that the 65,536 characters possible in a 16-bit encoding are not sufficient to represent all the characters in the world. The Unicode standard therefore has been extended to allow up to 1,112,064 characters. Those characters that go beyond the original 16-bit limit are called supplementary characters. Java supports the supplementary characters. The processing and representing of supplementary characters are beyond the scope of this book. For simplicity, this book considers only the original 16-bit Unicode characters. These characters can be stored in a char type variable.
A 16-bit Unicode takes two bytes, preceded by \u, expressed in four hexadecimal digits that run from \u0000 to \uFFFF. Hexadecimal numbers are introduced in Appendix F, Number Systems. For example, the English word welcome is translated into Chinese using two characters, . The Unicodes of these two characters are \u6B22\u8FCE. The Unicodes for the Greek letters a b g are \u03b1 \u03b2 \u03b4 respectively.
Most computers use ASCII (American Standard Code for Information Interchange), an 8-bit encoding scheme, for representing all uppercase and lowercase letters, digits, punctuation marks, and control characters. Unicode includes ASCII code, with \u0000 to \u007F corre- sponding to the 128 ASCII characters. Table 4.4 shows the ASCII code for some commonly used characters. Appendix B, “The ASCII Character Set,” gives a complete list of ASCII characters and their decimal and hexadecimal codes.
Point Key
char type
char literal
encoding
Unicode
original Unicode
supplementary Unicode
ASCII
Characters Code Value in Decimal Unicode Value
'0' to '9' 48 to 57 \u0030 to \u0039
'A' to 'Z' 65 to 90 \u0041 to \u005A
'a' to 'z' 97 to 122 \u0061 to \u007A
Table 4.4 ASCII Code for Commonly Used Characters
M04_LIAN1878_11_GE_C04.indd 147 12/29/17 3:57 PM
148 Chapter 4 Mathematical Functions, Characters, and Strings
You can use ASCII characters such as 'X', '1', and '$' in a Java program as well as Unicodes. Thus, for example, the following statements are equivalent:
char letter = 'A'; char letter = '\u0041'; // Character A’s Unicode is 0041
Both statements assign character A to the char variable letter.
Note The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b:
char ch = 'a'; System.out.println(++ch);
4.3.2 Escape Sequences for Special Characters Suppose you want to print a message with quotation marks in the output. Can you write a statement like this?
System.out.println("He said "Java is fun"");
No, this statement has a compile error. The compiler thinks the second quotation character is the end of the string and does not know what to do with the rest of the characters.
To overcome this problem, Java uses a special notation to represent special characters, as listed in Table 4.5. This special notation, called an escape sequence, consists of a backslash (\) followed by a character or a combination of digits. For example, \t is an escape sequence for the Tab character, and an escape sequence such as \u03b1 is used to represent a Unicode. The symbols in an escape sequence are interpreted as a whole rather than individually. An escape sequence is considered as a single character.
So, now you can print the quoted message using the following statement:
System.out.println("He said \"Java is fun\"");
The output is
He said "Java is fun"
Note the symbols \ and " together represent one character.
char increment and decrement
escape sequence
Escape Sequence Name Unicode Code Decimal Value
\b Backspace \u0008 8
\t Tab \u0009 9
\n Linefeed \u000A 10
\f Formfeed \u000C 12
\r Carriage Return \u000D 13
\\ Backslash \u005C 92
\" Double Quote \u0022 34
Table 4.5 Escape Sequences
The backslash \ is called an escape character. It is a special character. To display this character, you have to use an escape sequence \\. For example, the following code
System.out.println("\\t is a tab character");
displays
\t is a tab character
escape character
M04_LIAN1878_11_GE_C04.indd 148 12/29/17 3:57 PM
4.3 Character Data Type and Operations 149
4.3.3 Casting between char and Numeric Types A char can be cast into any numeric type, and vice versa. When an integer is cast into a char, only its lower 16 bits of data are used; the other part is ignored. For example:
// Note a hex integer is written using prefix 0X char ch = (char)0XAB0041; // The lower 16 bits hex code 0041 is
// assigned to ch System.out.println(ch); // ch is character A
When a floating-point value is cast into a char, the floating-point value is first cast into an int, which is then cast into a char.
char ch = (char)65.25; // Decimal 65 is assigned to ch System.out.println(ch); // ch is character A
When a char is cast into a numeric type, the character’s Unicode is cast into the specified numeric type.
int i = (int)'A'; // The Unicode of character A is assigned to i System.out.println(i); // i is 65
Implicit casting can be used if the result of a casting fits into the target variable. Otherwise, explicit casting must be used. For example, since the Unicode of 'a' is 97, which is within the range of a byte, these implicit castings are fine:
byte b = 'a'; int i = 'a';
But the following statement is incorrect, because the Unicode \uFFF4 cannot fit into a byte:
byte b = '\uFFF4';
To force this assignment, use explicit casting, as follows:
byte b = (byte)'\uFFF4';
Any positive integer between 0 and FFFF in hexadecimal can be cast into a character implicitly. Any number not in this range must be cast into a char explicitly.
All numeric operators can be applied to char operands. A char operand is automatically cast into a number if the other operand is a number or a character. If the other operand is a string, the character is concatenated with the string. For example, the following statements
int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51 System.out.println("i is " + i); // i is 101 int j = 2 + 'a'; // (int)'a' is 97 System.out.println("j is " + j); // j is 99 System.out.println(j + " is the Unicode for character ") + (char)j); // 99 is the Unicode for character c System.out.println("Chapter " + '2');
display
i is 101 j is 99 99 is the Unicode for character c Chapter 2
numeric operators on characters
M04_LIAN1878_11_GE_C04.indd 149 12/29/17 3:57 PM
150 Chapter 4 Mathematical Functions, Characters, and Strings
4.3.4 Comparing and Testing Characters Two characters can be compared using the relational operators just like comparing two numbers. This is done by comparing the Unicodes of the two characters. For example,
'a' < 'b' is true because the Unicode for 'a' (97) is less than the Unicode for 'b' (98).
'a' < 'A' is false because the Unicode for 'a' (97) is greater than the Unicode for 'A' (65).
'1' < '8' is true because the Unicode for '1' (49) is less than the Unicode for '8' (56).
Often in the program, you need to test whether a character is a number, a letter, an uppercase letter, or a lowercase letter. As given in Appendix B, the ASCII character set, that the Unicodes for lowercase letters are consecutive integers starting from the Unicode for 'a', then for 'b', 'c', . . . , and 'z'. The same is true for the uppercase letters and for numeric characters. This property can be used to write the code to test characters. For example, the following code tests whether a character ch is an uppercase letter, a lowercase letter, or a digital character:
if (ch >= 'A' && ch <= 'Z') System.out.println(ch + " is an uppercase letter"); else if (ch >= 'a' && ch <= 'z') System.out.println(ch + " is a lowercase letter"); else if (ch >= '0' && ch <= '9') System.out.println(ch + " is a numeric character");
For convenience, Java provides the following methods in the Character class for testing characters as listed in Table 4.6. The Character class is defined in the java.lang package.
Method Description
isDigit(ch) Returns true if the specified character is a digit.
isLetter(ch) Returns true if the specified character is a letter.
isLetterOrDigit(ch) Returns true if the specified character is a letter or digit.
isLowerCase(ch) Returns true if the specified character is a lowercase letter.
isUpperCase(ch) Returns true if the specified character is an uppercase letter.
toLowerCase(ch) Returns the lowercase of the specified character.
toUpperCase(ch) Returns the uppercase of the specified character.
Table 4.6 Methods in the Character Class
For example,
System.out.println("isDigit('a') is " + Character.isDigit('a')); System.out.println("isLetter('a') is " + Character.isLetter('a')); System.out.println("isLowerCase('a') is " + Character.isLowerCase('a')); System.out.println("isUpperCase('a') is " + Character.isUpperCase('a')); System.out.println("toLowerCase('T') is " + Character.toLowerCase('T')); System.out.println("toUpperCase('q') is " + Character.toUpperCase('q'));
displays
isDigit('a') is false isLetter('a') is true
M04_LIAN1878_11_GE_C04.indd 150 12/29/17 3:57 PM
4.3 Character Data Type and Operations 151
isLowerCase('a') is true isUpperCase('a') is false toLowerCase('T') is t toUpperCase('q') is Q
4.3.1 Use print statements to find out the ASCII code for '1', 'A', 'B', 'a', and 'b'. Use print statements to find out the character for the decimal codes 40, 59, 79, 85, and 90. Use print statements to find out the character for the hexadecimal code 40, 5A, 71, 72, and 7A.
4.3.2 Which of the following are correct literals for characters?
'1', '\u345dE', '\u3fFa', '\b', '\t'
4.3.3 How do you display the characters \ and "? 4.3.4 Evaluate the following:
int i = '1'; int j = '1' + '2' * ('4' − '3') + 'b' / 'a'; int k = 'a'; char c = 90;
4.3.5 Can the following conversions involving casting be allowed? If so, find the converted result.
char c = 'A'; int i = (int)c;
float f = 1000.34f; int i = (int)f;
double d = 1000.34; int i = (int)d;
int i = 97; char c = (char)i;
4.3.6 Show the output of the following program:
public class Test { public static void main(String[] args) { char x = 'a'; char y = 'c'; System.out.println(++x); System.out.println(y++); System.out.println(x − y); } }
4.3.7 Write the code that generates a random lowercase letter. 4.3.8 Show the output of the following statements:
System.out.println('a' < 'b'); System.out.println('a' <= 'A'); System.out.println('a' > 'b'); System.out.println('a' >= 'A'); System.out.println('a' == 'a'); System.out.println('a' != 'b');
Point Check
M04_LIAN1878_11_GE_C04.indd 151 12/29/17 3:57 PM
152 Chapter 4 Mathematical Functions, Characters, and Strings
4.4 The String Type A string is a sequence of characters.
The char type represents only one character. To represent a string of characters, use the data type called String. For example, the following code declares message to be a string with the value “Welcome to Java".
String message = "Welcome to Java";
String is a predefined class in the Java library, just like the classes System and Scanner. The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. The variable declared by a reference type is known as a reference variable that references an object. Here, message is a reference variable that references a string object with contents Welcome to Java.
Reference data types will be discussed in detail in Chapter 9, Objects and Classes. For the time being, you need to know only how to declare a String variable, how to assign a string to the variable, and how to use the methods in the String class. More details on using strings will be covered in Chapter 10.
Table 4.7 lists the String methods for obtaining string length, for accessing characters in the string, for concatenating string, for converting string to uppercases or lowercases, and for trimming a string.
Point Key
VideoNote
Introduce strings and objects
Method Description
length() Returns the number of characters in this string.
charAt(index) Returns the character at the specified index from this string.
concat(s1) Returns a new string that concatenates this string with string s1.
toUpperCase() Returns a new string with all letters in uppercase.
toLowerCase() Returns a new string with all letters in lowercase.
trim() Returns a new string with whitespace characters trimmed on both sides.
Table 4.7 Simple Methods for String Objects
Strings are objects in Java. The methods listed in Table 4.7 can only be invoked from a specific string instance. For this reason, these methods are called instance methods. A noninstance method is called a static method. A static method can be invoked without using an object. All the methods defined in the Math class are static methods. They are not tied to a specific object instance. The syntax to invoke an instance method is referenceVariable.methodName(arguments). A method may have many arguments or no arguments. For example, the charAt(index)method has one argument, but the length() method has no arguments. Recall that the syntax to invoke a static method is ClassName.methodName(arguments). For example, the pow method in the Math class can be invoked using Math.pow(2, 2.5).
4.4.1 Getting String Length You can use the length() method to return the number of characters in a string. For example, the following code
String message = "Welcome to Java"; System.out.println("The length of " + message + " is " + message.length());
instance method static method
M04_LIAN1878_11_GE_C04.indd 152 12/29/17 3:57 PM
4.4 The String Type 153
displays
The length of Welcome to Java is 15
Note When you use a string, you often know its literal value. For convenience, Java allows you to use the string literal to refer directly to strings without creating new variables. Thus, "Welcome to Java".length() is correct and returns 15. Note that "" denotes an empty string and "".length() is 0.
4.4.2 Getting Characters from a String The s.charAt(index) method can be used to retrieve a specific character in a string s, where the index is between 0 and s.length()–1. For example, message.charAt(0) returns the character W, as shown in Figure 4.1. Note that the index for the first character in the string is 0.
string literal
empty string
charAt(index)
Figure 4.1 The characters in a String object can be accessed using its index.
Indices message
message.charAt(0) message.charAt(14)message.length() is 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
W e l c o m e t o J a v a
Caution Attempting to access characters in a string s out of bounds is a common pro gramming error. To avoid it, make sure that you do not use an index beyond s.length()–1. For example, s.charAt(s.length()) would cause a StringIndexOutOfBoundsException.
4.4.3 Concatenating Strings You can use the concat method to concatenate two strings. The statement given below, for example, concatenates strings s1 and s2 into s3:
String s3 = s1.concat(s2);
Because string concatenation is heavily used in programming, Java provides a convenient way to accomplish it. You can use the plus (+) operator to concatenate two strings, so the previous statement is equivalent to
String s3 = s1 + s2;
The following code combines the strings message, " and ", and "HTML" into one string:
String myString = message + " and " + "HTML";
Recall that the + operator can also concatenate a number with a string. In this case, the number is converted into a string then concatenated. Note at least one of the operands must be a string in order for concatenation to take place. If one of the operands is a nonstring (e.g., a
string index range
s1.concat(s2)
s1 + s2
concatenate strings and numbers
M04_LIAN1878_11_GE_C04.indd 153 12/29/17 3:57 PM
154 Chapter 4 Mathematical Functions, Characters, and Strings
number), the nonstring value is converted into a string and concatenated with the other string. Here are some examples:
// Three strings are concatenated String message = "Welcome " + "to " + "Java";
// String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2
// String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB
If neither of the operands is a string, the plus sign (+) is the addition operator that adds two numbers.
The augmented += operator can also be used for string concatenation. For example, the following code appends the string " and Java is fun" with the string "Welcome to Java" in message.
message += " and Java is fun";
So the new message is "Welcome to Java and Java is fun." If i = 1 and j = 2, what is the output of the following statement?
System.out.println("i + j is " + i + j);
The output is "i + j is 12" because "i + j is" is concatenated with the value of i first. To force i + j to be executed first, enclose i + j in the parentheses, as follows:
System.out.println("i + j is " + (i + j));
4.4.4 Converting Strings The toLowerCase() method returns a new string with all lowercase letters, and the toUpperCase() method returns a new string with all uppercase letters. For example,
"Welcome".toLowerCase() returns a new string welcome. "Welcome".toUpperCase() returns a new string WELCOME.
The trim() method returns a new string by eliminating whitespace characters from both ends of the string. The characters ' ', \t, \f, \r, or \n are known as whitespace characters. For example,
"\t Good Night \n".trim() returns a new string Good Night.
4.4.5 Reading a String from the Console To read a string from the console, invoke the next() method on a Scanner object. For exam- ple, the following code reads three strings from the keyboard:
Scanner input = new Scanner(System.in); System.out.print("Enter three words separated by spaces: "); String s1 = input.next(); String s2 = input.next(); String s3 = input.next(); System.out.println("s1 is " + s1); System.out.println("s2 is " + s2); System.out.println("s3 is " + s3);
toLowerCase() toUpperCase()
whitespace character
trim()
read strings
M04_LIAN1878_11_GE_C04.indd 154 12/29/17 3:57 PM
4.4 The String Type 155
The next() method reads a string that ends with a whitespace character. You can use the nextLine() method to read an entire line of text. The nextLine() method reads a string that ends with the Enter key pressed. For example, the following statements read a line of text:
Scanner input = new Scanner(System.in); System.out.println("Enter a line: "); String s = input.nextLine(); System.out.println("The line entered is " + s);
Enter three words separated by spaces: Welcome to Java
s1 is Welcome s2 is to s3 is Java
Enter a line: Welcome to Java
The line entered is Welcome to Java
Method Description
equals(s1) Returns true if this string is equal to string s1.
equalsIgnoreCase(s1) Returns true if this string is equal to string s1; it is case insensitive.
compareTo(s1) Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1.
compareToIgnoreCase(s1) Same as compareTo except that the comparison is case insensitive.
startsWith(prefix) Returns true if this string starts with the specified prefix.
endsWith(suffix) Returns true if this string ends with the specified suffix.
contains(s1) Returns true if s1 is a substring in this string.
Table 4.8 Comparison Methods for String Objects
For convenience, we call the input using the methods next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), and nextDouble() the token-based input, because they read individual elements separated by whitespace characters rather than an entire line. The nextLine() method is called a line-based input.
Important Caution To avoid input errors, do not use a line-based input after a token-based input in the program. The reasons will be explained in Section 12.11.4, "How Does Scanner Work?"
4.4.6 Reading a Character from the Console To read a character from the console, use the nextLine() method to read a string and then invoke the charAt(0) method on the string to return a character. For example, the following code reads a character from the keyboard:
Scanner input = new Scanner(System.in); System.out.print("Enter a character: "); String s = input.nextLine(); char ch = s.charAt(0); System.out.println("The character entered is " + ch);
4.4.7 Comparing Strings The String class contains the methods, as listed in Table 4.8, for comparing two strings.
token-based input
line-based input
avoid input errors
M04_LIAN1878_11_GE_C04.indd 155 12/29/17 3:57 PM
156 Chapter 4 Mathematical Functions, Characters, and Strings
How do you compare the contents of two strings? You might attempt to use the == operator, as follows:
if (string1 == string2) System.out.println("string1 and string2 are the same object"); else System.out.println("string1 and string2 are different objects");
However, the == operator checks only whether string1 and string2 refer to the same object; it does not tell you whether they have the same contents. Therefore, you cannot use the == operator to find out whether two string variables have the same contents. Instead, you should use the equals method. The following code, for instance, can be used to compare two strings:
if (string1.equals(string2)) System.out.println("string1 and string2 have the same contents"); else System.out.println("string1 and string2 are not equal");
For example, the following statements display true then false:
String s1 = "Welcome to Java"; String s2 = "Welcome to Java"; String s3 = "Welcome to C++"; System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false
The compareTo method can also be used to compare two strings. For example, consider the following code:
s1.compareTo(s2)
The method returns the value 0 if s1 is equal to s2, a value less than 0 if s1 is lexicographically (i.e., in terms of Unicode ordering) less than s2, and a value greater than 0 if s1 is lexicographically greater than s2.
The actual value returned from the compareTo method depends on the offset of the first two distinct characters in s1 and s2 from left to right. For example, suppose s1 is abc and s2 is abg, and s1.compareTo(s2) returns −4. The first two characters (a vs. a) from s1 and s2 are compared. Because they are equal, the second two characters (b vs. b) are compared. Because they are also equal, the third two characters (c vs. g) are compared. Since the character c is 4 less than g, the comparison returns −4.
Caution Syntax errors will occur if you compare strings by using relational operators >, >=, <, or <=. Instead, you have to use s1.compareTo(s2).
Note The equals method returns true if two strings are equal, and false if they are not. The compareTo method returns 0, a positive integer, or a negative integer, depending on whether one string is equal to, greater than, or less than the other string.
The String class also provides the equalsIgnoreCase and compareToIgnoreCase methods for comparing strings. The equalsIgnoreCase and compareToIgnoreCase methods ignore the case of the letters when comparing two strings. You can also use str.startsWith(prefix) to check whether string str starts with a specified prefix, str.endsWith(suffix) to check whether string str ends with a specified suffix, and str.contains(s1) to check whether string str contains string s1. For example,
"Welcome to Java".startsWith("We") returns true. "Welcome to Java".startsWith("we") returns false. "Welcome to Java".endsWith("va") returns true.
==
string1.equals(string2)
s1.compareTo(s2)
M04_LIAN1878_11_GE_C04.indd 156 12/29/17 3:57 PM
4.4 The String Type 157
"Welcome to Java".endsWith("v") returns false. "Welcome to Java".contains("to") returns true. "Welcome to Java".contains("To") returns false.
Listing 4.2 gives a program that prompts the user to enter two cities and displays them in alphabetical order.
lisTing 4.2 OrderTwoCities.java 1 import java.util.Scanner; 2 3 public class OrderTwoCities { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter two cities 8 System.out.print("Enter the first city: "); 9 String city1 = input.nextLine(); 10 System.out.print("Enter the second city: "); 11 String city2 = input.nextLine(); 12 13 if (city1.compareTo(city2) < 0) 14 System.out.println("The cities in alphabetical order are " + 15 city1 + " " + city2); 16 else 17 System.out.println("The cities in alphabetical order are " + 18 city2 + " " + city1); 19 } 20 }
Enter the first city: New York
Enter the second city: Boston
The cities in alphabetical order are Boston New York
The program reads two strings for two cities (lines 9 and 11). If input.nextLine() is replaced by input.next() (line 9), you cannot enter a string with spaces for city1. Since a city name may contain multiple words separated by spaces, the program uses the nextLine method to read a string (lines 9 and 11). Invoking city1.compareTo(city2) compares two strings city1 with city2 (line 13). A negative return value indicates that city1 is less than city2.
4.4.8 Obtaining Substrings You can obtain a single character from a string using the charAt method. You can also obtain a substring from a string using the substring method (see Figure 4.2) in the String class, as given in Table 4.9.
For example,
String message = "Welcome to Java"; String message = message.substring(0,11) + "HTML"; The string message now becomes Welcome to HTML.
input city1
input city2
compare two cities
Method Description
substring(beginIndex) Returns this string’s substring that begins with the character at the specified beginIndex and extends to the end of the string, as shown in Figure 4.2.
substring(beginIndex,
endIndex) Returns this string’s substring that begins at the specified beginIndex and extends to the character at index endIndex – 1, as shown in Figure 4.2. Note the character at endIndex is not part of the substring.
Table 4.9 The String Class Contains the Methods for Obtaining Substrings
M04_LIAN1878_11_GE_C04.indd 157 12/29/17 3:57 PM
158 Chapter 4 Mathematical Functions, Characters, and Strings
Note If beginIndex is endIndex, substring(beginIndex, endIndex) returns an empty string with length 0. If beginIndex > endIndex, it would be a runtime error.
4.4.9 Finding a Character or a Substring in a String The String class provides several versions of indexOf and lastIndexOf methods to find a character or a substring in a string, as listed in Table 4.10.
beginIndex <= endIndex
Figure 4.2 The substring method obtains a substring from a string.
Indices message
message.substring(0, 11) message.substring(11)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
W e l c o m e t o J a v a
Method Description
index Of (ch) Returns the index of the first occurrence of ch in the string. Returns −1 if not matched.
indexOf(ch, fromIndex) Returns the index of the first occurrence of ch after fromIndex in the string. Returns −1 if not matched.
indexOf(s) Returns the index of the first occurrence of string s in this string. Returns −1 if not matched.
indexOf(s, fromIndex) Returns the index of the first occurrence of string s in this string after fromIndex. Returns −1 if not matched.
lastIndexOf(ch) Returns the index of the last occurrence of ch in the string. Returns −1 if not matched.
lastIndexOf(ch, fromIndex) Returns the index of the last occurrence of ch before fromIndex in this string. Returns −1 if not matched.
lastIndexOf(s) Returns the index of the last occurrence of string s. Returns −1 if not matched.
lastIndexOf(s, fromIndex) Returns the index of the last occurrence of string s before fromIndex. Returns −1 if not matched.
Table 4.10 The String Class Contains the Methods for Finding Substrings
For example,
"Welcome to Java".indexOf('W') returns 0. "Welcome to Java".indexOf('o') returns 4. "Welcome to Java".indexOf('o', 5) returns 9. "Welcome to Java".indexOf("come") returns 3. "Welcome to Java".indexOf("Java", 5) returns 11. "Welcome to Java".indexOf("java", 5) returns −1.
"Welcome to Java".lastIndexOf('W') returns 0. "Welcome to Java".lastIndexOf('o') returns 9. "Welcome to Java".lastIndexOf('o', 5) returns 4. "Welcome to Java".lastIndexOf("come") returns 3. "Welcome to Java".lastIndexOf("Java", 5) returns −1. "Welcome to Java".lastIndexOf("Java") returns 11.
Suppose that a string s contains the first name and last name separated by a space. You can use the following code to extract the first name and last name from the string:
int k = s.indexOf(' '); String firstName = s.substring(0, k); String lastName = s.substring(k + 1);
indexOf
lastIndexOf
M04_LIAN1878_11_GE_C04.indd 158 12/29/17 3:57 PM
4.4 The String Type 159
For example, if s is Kim Jones, the following diagram illustrates how the first name and last name are extracted.
012345678
k is 3
s.substring(0, k) is Kim
s.substring(k + 1) is Jones
Kim Jones
4.4.10 Conversion between Strings and Numbers You can convert a numeric string into a number. To convert a string into an int value, use the Integer.parseInt method, as follows:
int intValue = Integer.parseInt(intString);
where intString is a numeric string such as "123". To convert a string into a double value, use the Double.parseDouble method, as
follows:
double doubleValue = Double.parseDouble(doubleString);
where doubleString is a numeric string such as "123.45". If the string is not a numeric string, the conversion would cause a runtime error. The
Integer and Double classes are both included in the java.lang package, and thus they are automatically imported.
You can convert a number into a string; simply use the string concatenating operator as follows:
String s = number + "";
4.4.1 Suppose s1, s2, and s3 are three strings, given as follows:
String s1 = "Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java";
What are the results of the following expressions?
(a) s1 == s2
(b) s2 == s3
(c) s1.equals(s2)
(d) s2.equals(s3)
(e) s1.compareTo(s2)
(f) s2.compareTo(s3)
(g) s2.compareTo(s2)
(h) s1.charAt(0)
(i) s1.indexOf('j')
(j) s1.indexOf("to")
(k) s1.lastIndexOf('a')
(l) s1.lastIndexOf("o", 15)
(m) s1.length()
(n) s1.substring(5)
(o) s1.substring(5, 11)
(p) s1.startsWith("Wel")
(q) s1.endsWith("Java")
(r) s1.toLowerCase()
(s) s1.toUpperCase()
(t) s1.concat(s2)
(u) s1.contain(s2)
(v) "\t Wel \t".trim()
Integer.parseInt method
Double.parseDouble method
number to string
Point Check
M04_LIAN1878_11_GE_C04.indd 159 12/29/17 3:57 PM
160 Chapter 4 Mathematical Functions, Characters, and Strings
4.4.2 Suppose s1 and s2 are two strings. Which of the following statements or expres- sions are incorrect?
String s = "Welcome to Java"; String s3 = s1 + s2; String s3 = s1 − s2; s1 == s2; s1 >= s2; s1.compareTo(s2); int i = s1.length(); char c = s1(0); char c = s1.charAt(s1.length());
4.4.3 Show the output of the following statements (write a program to verify your results):
System.out.println("1" + 1); System.out.println('1' + 1); System.out.println("1" + 1 + 1); System.out.println("1" + (1 + 1)); System.out.println('1' + 1 + 1);
4.4.4 Evaluate the following expressions (write a program to verify your results):
1 + "Welcome " + 1 + 1 1 + "Welcome " + (1 + 1) 1 + "Welcome " + ('\u0001' + 1) 1 + "Welcome " + 'a' + 1
4.4.5 Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements:
(a) Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual.
(b) Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual.
(c) Compare s1 with s2 and assign the result to an int variable x.
(d) Compare s1 with s2, ignoring case, and assign the result to an int variable x.
(e) Check whether s1 has the prefix AAA and assign the result to a Boolean variable b.
(f) Check whether s1 has the suffix AAA and assign the result to a Boolean variable b.
(g) Assign the length of s1 to an int variable x.
(h) Assign the first character of s1 to a char variable x.
(i) Create a new string s3 that combines s1 with s2.
(j) Create a substring of s1 starting from index 1.
(k) Create a substring of s1 from index 1 to index 4.
(l) Create a new string s3 that converts s1 to lowercase.
(m) Create a new string s3 that converts s1 to uppercase.
(n) Create a new string s3 that trims whitespaces on both ends of s1.
M04_LIAN1878_11_GE_C04.indd 160 12/29/17 3:57 PM
4.5 Case Studies 161
(o) Assign the index of the first occurrence of the character e in s1 to an int variable x.
(p) Assign the index of the last occurrence of the string abc in s1 to an int variable x.
4.4.6 Write one statement to return the number of digits in an integer i. 4.4.7 Write one statement to return the number of digits in a double value d.
4.5 Case Studies Strings are fundamental in programming. The ability to write programs using strings is essential in learning Java programming.
You will frequently use strings to write useful programs. This section presents three examples of solving problems using strings.
4.5.1 Case Study: Guessing Birthdays You can find out the date of the month when your friend was born by asking five questions. Each question asks whether the day is in one of the five sets of numbers.
Point Key
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
9 10 11 12 13 14 15 24 25 26 27 28 29 30 31
5 6 7 12 13 14 15 20 21 22 23 28 29 30 31
2 3 6 7 10 11 14 15 18 22 23 26 27 30 31
3 5 7 9 11 13 15
17 19 1921 23 25 27 29 31
Set1 Set2 Set3 Set4 Set5
= 19
841
+
The birthday is the sum of the first numbers in the sets where the day appears. For example, if the birthday is 19, it appears in Set1, Set2, and Set5. The first numbers in these three sets are 1, 2, and 16. Their sum is 19.
Listing 4.3 gives a program that prompts the user to answer whether the day is in Set1 (lines 41–44), in Set2 (lines 50–53), in Set3 (lines 59–62), in Set4 (lines 68–71), and in Set5 (lines 77–80). If the number is in the set, the program adds the first number in the set to day (lines 47, 56, 65, 74, and 83).
lisTing 4.3 GuessBirthday.java 1 import java.util.Scanner; 2 3 public class GuessBirthday { 4 public static void main(String[] args) { 5 String set1 = 6 " 1 3 5 7\n" + 7 " 9 11 13 15\n" + 8 "17 19 21 23\n" + 9 "25 27 29 31"; 10 11 String set2 = 12 " 2 3 6 7\n" +
M04_LIAN1878_11_GE_C04.indd 161 12/29/17 3:57 PM
162 Chapter 4 Mathematical Functions, Characters, and Strings
73 if (answer == 1) 74 day += 8; 75 76 // Prompt the user to answer questions 77 System.out.print("\nIs your birthday in Set5?\n"); 78 System.out.print(set5); 79 System.out.print("\nEnter 0 for No and 1 for Yes: "); 80 answer = input.nextInt(); 81 82 if (answer == 1) 83 day += 16; 84 85 System.out.println("\nYour birthday is " + day + "!"); 86 } 87 }
in Set3?
in Set2?
in Set1?
day to be determined
in Set4?
in Set5?
13 "10 11 14 15\n" + 14 "18 19 22 23\n" + 15 "26 27 30 31"; 16 17 String set3 = 18 " 4 5 6 7\n" + 19 "12 13 14 15\n" + 20 "20 21 22 23\n" + 21 "28 29 30 31"; 22 23 String set4 = 24 " 8 9 10 11\n" + 25 "12 13 14 15\n" + 26 "24 25 26 27\n" + 27 "28 29 30 31"; 28 29 String set5 = 30 "16 17 18 19\n" + 31 "20 21 22 23\n" + 32 "24 25 26 27\n" + 33 "28 29 30 31"; 34 35 int day = 0; 36 37 // Create a Scanner 38 Scanner input = new Scanner(System.in); 39 40 // Prompt the user to answer questions 41 System.out.print("Is your birthday in Set1?\n"); 42 System.out.print(set1); 43 System.out.print("\nEnter 0 for No and 1 for Yes: "); 44 int answer = input.nextInt(); 45 46 if (answer == 1) 47 day += 1; 48 49 // Prompt the user to answer questions 50 System.out.print("\nIs your birthday in Set2?\n"); 51 System.out.print(set2); 52 System.out.print("\nEnter 0 for No and 1 for Yes: "); 53 answer = input.nextInt(); 54 55 if (answer == 1) 56 day += 2; 57 58 // Prompt the user to answer questions 59 System.out.print("\nIs your birthday in Set3?\n"); 60 System.out.print(set3); 61 System.out.print("\nEnter 0 for No and 1 for Yes: "); 62 answer = input.nextInt(); 63 64 if (answer == 1) 65 day += 4; 66 67 // Prompt the user to answer questions 68 System.out.print("\nIs your birthday in Set4?\n"); 69 System.out.print(set4); 70 System.out.print("\nEnter 0 for No and 1 for Yes: "); 71 answer = input.nextInt(); 72
M04_LIAN1878_11_GE_C04.indd 162 12/29/17 3:57 PM
4.5 Case Studies 163
73 if (answer == 1) 74 day += 8; 75 76 // Prompt the user to answer questions 77 System.out.print("\nIs your birthday in Set5?\n"); 78 System.out.print(set5); 79 System.out.print("\nEnter 0 for No and 1 for Yes: "); 80 answer = input.nextInt(); 81 82 if (answer == 1) 83 day += 16; 84 85 System.out.println("\nYour birthday is " + day + "!"); 86 } 87 }
in Set3?
in Set2?
in Set1?
day to be determined
in Set4?
in Set5?
Is your birthday in Set1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set2?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set3?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Is your birthday in Set4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Is your birthday in Set5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 1
Your birthday is 19!
M04_LIAN1878_11_GE_C04.indd 163 12/29/17 3:57 PM
164 Chapter 4 Mathematical Functions, Characters, and Strings
line# day answer output
35 0
44 1
47 1
53 1
56 3
62 0
71 0
80 1
83 19
85 Your birthday is 19!
Figure 4.3 (a) A number between 1 and 31 can be represented using a five-digit binary number. (b) A five-digit binary number can be obtained by adding binary numbers 1, 10, 100, 1000, or 10000.
Decimal Binary
1 00001 2 00010
000113 ... 19 10011 ... 31 11111
10000 10 1+
10011
19 31
10000 1000 100 10
+ 1 11111
0 0 0
0 0
0
b5 b4
b3 b2
b1
b5 b4 b3 b2 b1
0
0 00
+
(a) (b)
4.5.2 Case Study: Converting a Hexadecimal Digit to a Decimal Value
The hexadecimal number system has 16 digits: 0–9, A–F. The letters A, B, C, D, E, and F correspond to the decimal numbers 10, 11, 12, 13, 14, and 15. We now write a program that prompts the user to enter a hex digit and display its corresponding decimal value, as given in Listing 4.4.
This game is easy to program. You may wonder how the game was created. The mathemat- ics behind the game is actually quite simple. The numbers are not grouped together by accident—the way they are placed in the five sets is deliberate. The starting numbers in the five sets are 1, 2, 4, 8, and 16, which correspond to 1, 10, 100, 1000, and 10000 in binary (binary numbers are introduced in Appendix F, Number Systems). A binary number for deci- mal integers between 1 and 31 has at most five digits, as shown in Figure 4.3a. Let it be b5b4b3b2b1. Thus, b5b4b3b2b1 = b5 0000 + b4 000 + b3 00 + b2 0 + b1, as shown in Figure 4.3b. If a day’s binary number has a digit 1 in bk, the number should appear in Setk. For exam- ple, number 19 is binary 10011, so it appears in Set1, Set2, and Set5. It is binary 1 + 10 + 10000 = 10011 or decimal 1 + 2 + 16 = 19. Number 31 is binary 11111, so it appears in Set1, Set2, Set3, Set4, and Set5. It is binary 1 + 10 + 100 + 1000 + 10000 = 11111 or decimal 1 + 2 + 4 + 8 + 16 = 31.
mathematics behind the game
M04_LIAN1878_11_GE_C04.indd 164 12/29/17 3:57 PM
4.5 Case Studies 165
lisTing 4.4 HexDigit2Dec.java 1 import java.util.Scanner; 2 3 public class HexDigit2Dec { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("Enter a hex digit: "); 7 String hexString = input.nextLine(); 8 9 // Check if the hex string has exactly one character 10 if (hexString.length() != 1) { 11 System.out.println("You must enter exactly one character"); 12 System.exit(1); 13 } 14 15 // Display decimal value for the hex digit 16 char ch = Character.toUpperCase(hexString.charAt(0)); 17 if ('A' <= ch && ch <= 'F') { 18 int value = ch − 'A' + 10; 19 System.out.println("The decimal value for hex digit " 20 + ch + " is " + value); 21 } 22 else if (Character.isDigit(ch)) { 23 System.out.println("The decimal value for hex digit " 24 + ch + " is " + ch); 25 } 26 else { 27 System.out.println(ch + " is an invalid input"); 28 } 29 } 30 }
is 0–9?
is A–F?
check length
input string
VideoNote
Convert hex to decimal
Enter a hex digit: AB7C
You must enter exactly one character
Enter a hex digit: B
The decimal value for hex digit B is 11
Enter a hex digit: 8
The decimal value for hex digit 8 is 8
Enter a hex digit: T
T is an invalid input
The program reads a string from the console (line 7) and checks if the string contains a single character (line 10). If not, report an error and exit the program (line 12).
The program invokes the Character.toUpperCase method to obtain the character ch as an uppercase letter (line 16). If ch is between 'A' and 'F' (line 17), the corresponding decimal value is ch – 'A' + 10 (line 18). Note ch – 'A' is 0 if ch is 'A', ch – 'A' is
M04_LIAN1878_11_GE_C04.indd 165 12/29/17 3:57 PM
166 Chapter 4 Mathematical Functions, Characters, and Strings
1 if ch is 'B', and so on. When two characters perform a numerical operation, the characters' Unicodes are used in the computation.
The program invokes the Character.isDigit(ch) method to check if ch is between '0' and '9' (line 22). If so, the corresponding decimal digit is the same as ch (lines 23 and 24).
If ch is not between 'A' and 'F' nor a digit character, the program displays an error mes- sage (line 27).
4.5.3 Case Study: Revising the Lottery Program Using Strings The lottery program in Listing 3.8, Lottery.java, generates a random two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule:
1. If the user input matches the lottery number in the exact order, the award is $10,000.
2. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.
The program in Listing 3.8 uses an integer to store the number. Listing 4.5 gives a new program that generates a random two-digit string instead of a number, and receives the user input as a string instead of a number.
lisTing 4.5 LotteryUsingStrings.java 1 import java.util.Scanner; 2 3 public class LotteryUsingStrings { 4 public static void main(String[] args) { 5 // Generate a lottery as a two-digit string 6 String lottery = "" + (int)(Math.random() * 10) 7 + (int)(Math.random() * 10); 8 9 // Prompt the user to enter a guess 10 Scanner input = new Scanner(System.in); 11 System.out.print("Enter your lottery pick (two digits): "); 12 String guess = input.nextLine(); 13 14 // Get digits from lottery 15 char lotteryDigit1 = lottery.charAt(0); 16 char lotteryDigit2 = lottery.charAt(1); 17 18 // Get digits from guess 19 char guessDigit1 = guess.charAt(0); 20 char guessDigit2 = guess.charAt(1); 21 22 System.out.println("The lottery number is " + lottery); 23 24 // Check the guess 25 if (guess.equals(lottery)) 26 System.out.println("Exact match: you win $10,000"); 27 else if (guessDigit2 == lotteryDigit1 28 && guessDigit1 == lotteryDigit2) 29 System.out.println("Match all digits: you win $3,000"); 30 else if (guessDigit1 == lotteryDigit1 31 || guessDigit1 == lotteryDigit2 32 || guessDigit2 == lotteryDigit1 33 || guessDigit2 == lotteryDigit2) 34 System.out.println("Match one digit: you win $1,000");
match one digit?
match all digits?
exact match?
enter a guess
generate a lottery
M04_LIAN1878_11_GE_C04.indd 166 12/29/17 3:57 PM
4.6 Formatting Console Output 167
35 else 36 System.out.println("Sorry, no match"); 37 } 38 }
Enter your lottery pick (two digits): 00 The lottery number is 00 Exact match: you win $10,000
Enter your lottery pick (two digits): 45
The lottery number is 54 Match all digits: you win $3,000
Enter your lottery pick: 23
The lottery number is 34 Match one digit: you win $1,000
Enter your lottery pick: 23
The lottery number is 14 Sorry: no match
The program generates two random digits and concatenates them into the string lottery (lines 6 and 7). After this, lottery contains two random digits.
The program prompts the user to enter a guess as a two-digit string (line 12) and checks the guess against the lottery number in this order:
■■ First, check whether the guess matches the lottery exactly (line 25).
■■ If not, check whether the reversal of the guess matches the lottery (line 27).
■■ If not, check whether one digit is in the lottery (lines 30–33).
■■ If not, nothing matches and display “Sorry, no match” (line 36).
4.5.1 If you run Listing 4.3 GuessBirthday.java with input 1 for Set1, Set3, and Set4 and 0 for Set2 and Set5, what will be the birthday?
4.5.2 If you enter a lowercase letter such as b, the program in Listing 4.4 displays B is 11. Revise the code as to display b is 11.
4.5.3 What would be wrong if lines 6 and 7 are in Listing 4.5 replaced by the following code? String lottery = "" + (int)(Math.random() * 100);
4.6 Formatting Console Output You can use the System.out.printf method to display formatted output on the console.
Often, it is desirable to display numbers in a certain format. For example, the following code computes interest, given the amount and the annual interest rate:
double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.println("Interest is $" + interest);
Point Check
Point Key
M04_LIAN1878_11_GE_C04.indd 167 12/29/17 3:57 PM
168 Chapter 4 Mathematical Functions, Characters, and Strings
Because the interest amount is currency, it is desirable to display only two digits after the decimal point. To do this, you can write the code as follows:
double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.println("Interest is $" + (int)(interest * 100) / 100.0);
Interest is $16.404674
Interest is $16.4
% 2 f4 .
�eld width
precision
conversion code
format speci�er
However, the format is still not correct. There should be two digits after the decimal point: 16.40 rather than 16.4. You can fix it by using the printf method, as follows:
double amount = 12618.98; double interestRate = 0.0013; double interest = amount * interestRate; System.out.printf("Interest is $%4.2f", interest);
printf
Interest is $16.40
The f in the printf stands for formatted, implying that the method prints an item in some format. The syntax to invoke this method is
System.out.printf(format, item1, item2, ..., itemk);
where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be formatted. An item may be a numeric value,
a character, a Boolean value, or a string. A simple format specifier consists of a percent sign (%) followed by a conversion code. Table 4.11 lists some frequently used simple format specifiers.
format specifier
Format Specifier Output Example
%b A Boolean value True or false
%c A character ‘a’
%d A decimal integer 200
%f A floating-point number 45.460000
%e A number in standard scientific notation 4.556000e+01 %s A string “Java is cool”
Table 4.11 Frequently Used Format Specifiers
Here is an example:
int count = 5; double amount = 45.56; System.out.printf("count is %d and amount is %f", count, amount);
display count is 5 and amount is 45.560000
items
M04_LIAN1878_11_GE_C04.indd 168 12/29/17 3:57 PM
4.6 Formatting Console Output 169
Items must match the format specifiers in order, in number, and in exact type. For example, the format specifier for count is %d and for amount is %f. By default, a floating-point value is displayed with six digits after the decimal point. You can specify the width and precision in a format specifier, as shown in the examples in Table 4.12.
Example Output
%5c Output the character and add four spaces before the character item, because the width is 5.
%6b Output the Boolean value and add one space before the false value and two spaces before the true value.
%5d Output the integer item with width 5. If the number of digits in the item is 6 5, add spaces before the number. If the number of digits in the item is 7 5, the width is auto- matically increased.
%10.2f Output the floating-point item with width 10 including a decimal point and two digits after the point. Thus, there are seven digits allocated before the decimal point. If the number of digits before the decimal point in the item is 6 7, add spaces before the number. If the number of digits before the decimal point in the item is 7 7, the width is automatically increased.
%10.2e Output the floating-point item with width 10 including a decimal point, two digits after the point and the exponent part. If the displayed number in scientific notation has width 6 10, add spaces before the number.
%12s Output the string with width 12 characters. If the string item has fewer than 12 char- acters, add spaces before the string. If the string item has more than 12 characters, the width is automatically increased.
Table 4.12 Examples of Specifying Width and Precision
If an item requires more spaces than the specified width, the width is automatically increased. For example, the following code
System.out.printf("%3d#%2s#%4.2f\n", 1234, "Java", 51.6653);
displays
1234#Java#51.67
The specified width for int item 1234 is 3, which is smaller than its actual size 4. The width is automatically increased to 4. The specified width for string item Java is 2, which is smaller than its actual size 4. The width is automatically increased to 4. The specified width for double item 51.6653 is 4, but it needs width 5 to display 51.67, so the width is automatically increased to 5.
You can display a number with comma separators by adding a comma in front of a number specifier. For example, the following code
System.out.printf("%,8d %,10.1f\n", 12345678, 12345678.263);
displays
12,345,678 12,345,678.3
You can pad a number with leading zeros rather than spaces by adding a 0 in front of a number specifier. For example, the following code
System.out.printf("%08d %08.1f\n", 1234, 5.63);
displays
00001234 000005.6
comma separators
leading zeros
M04_LIAN1878_11_GE_C04.indd 169 12/29/17 3:57 PM
170 Chapter 4 Mathematical Functions, Characters, and Strings
By default, the output is right justified. You can put the minus sign (−) in the format speci- fier to specify that the item is left justified in the output within the specified field. For example, the following statements
System.out.printf("%8d%8s%8.1f\n", 1234, "Java", 5.63); System.out.printf("%−8d%−8s%−8.1f \n", 1234, "Java", 5.63);
display
right justify left justify
8 1234 Java 5.6
1234 Java 5.6
8 8
where the square box (n) denotes a blank space.
Caution The items must match the format specifiers in exact type. The item for the format speci- fier %f or %e must be a floating-point type value such as 40.0, not 40. Thus, an int variable cannot match %f or %e. You can use %.2f to specify a floating-point value with two digits after the decimal point. However, %0.2f would be incorrect.
Tip The % sign denotes a format specifier. To output a literal % in the format string, use %%. For example, the following code
System.out.printf("%.2f%%\n", 75.234);
displays
75.23%
Listing 4.6 gives a program that uses printf to display a table.
lisTing 4.6 FormatDemo.java 1 public class FormatDemo { 2 public static void main(String[] args) { 3 // Display the header of the table 4 System.out.printf("%−10s%−10s%−10s%−10s%−10s\n", "Degrees", 5 "Radians", "Sine", "Cosine", "Tangent"); 6 7 // Display values for 30 degrees 8 int degrees = 30; 9 double radians = Math.toRadians(degrees); 10 System.out.printf("%−10d%−10.4f%−10.4f%−10.4f%−10.4f\n", degrees, 11 radians, Math.sin(radians), Math.cos(radians), 12 Math.tan(radians)); 13 14 // Display values for 60 degrees 15 degrees = 60; 16 radians = Math.toRadians(degrees); 17 System.out.printf("%−10d%−10.4f%−10.4f%−10.4f%−10.4f\n", degrees, 18 radians, Math.sin(radians), Math.cos(radians), 19 Math.tan(radians)); 20 } 21 }
%%
values for 60 degrees
values for 30 degrees
display table header
M04_LIAN1878_11_GE_C04.indd 170 12/29/17 3:57 PM
Chapter Summary 171
The statements in lines 4 and 5 display the column names of the table. The column names are strings. Each string is displayed using the specifier %−10s, which left-justifies the string. The statements in lines 10–12 display the degrees as an integer and four float values. The integer is displayed using the specifier %−10d, and each float is displayed using the specifier %−10.4f, which specifies four digits after the decimal point.
4.6.1 What are the format specifiers for outputting a Boolean value, a character, a decimal integer, a floating-point number, and a string?
4.6.2 What is wrong in the following statements?
(a) System.out.printf("%5d %d", 1, 2, 3);
(b) System.out.printf("%5d %f", 1);
(c) System.out.printf("%5d %f", 1, 2);
(d) System.out.printf("%.2f\n%0.3f\n", 1.23456, 2.34);
(e) System.out.printf("%08s\n", "Java");
4.6.3 Show the output of the following statements:
(a) System.out.printf("amount is %f %e\n", 32.32, 32.32);
(b) System.out.printf("amount is %5.2f%% %5.4e\n", 32.327, 32.32);
(c) System.out.printf("%6b\n", (1 > 2));
(d) System.out.printf("%6s\n", "Java");
(e) System.out.printf("%−6b%s\n", (1 > 2), "Java");
(f) System.out.printf("%6b%−8s\n", (1 > 2), "Java");
(g) System.out.printf("%,5d %,6.1f\n", 312342, 315562.932);
(h) System.out.printf("%05d %06.1f\n", 32, 32.32);
Point Check
Degrees Radians Sine Cosine Tangent
30 0.5236 0.5000 0.8660 0.5774 60 1.0472 0.8660 0.5000 1.7321
Key Terms char type 147 encoding 147 escape character 148 escape sequence 148 format specifier 168 instance method 152 line-based input 155
specific import 152 static method 152 supplementary Unicode 147 token-based input 155 Unicode 147 whitespace character 154
ChapTer summary 1. Java provides the mathematical methods sin, cos, tan, asin, acos, atan, toRadi-
ans, toDegrees, exp, log, log10, pow, sqrt, ceil, floor, rint, round, min, max, abs, and random in the Math class for performing mathematical functions.
2. The character type char represents a single character.
M04_LIAN1878_11_GE_C04.indd 171 12/29/17 3:57 PM
172 Chapter 4 Mathematical Functions, Characters, and Strings
3. An escape sequence consists of a backslash (\) followed by a character or a combina- tion of digits.
4. The character \ is called the escape character.
5. The characters ' ', \t, \f, \r, and \n are known as the whitespace characters.
6. Characters can be compared based on their Unicode using the relational operators.
7. The Character class contains the methods isDigit, isLetter, isLetterOrDigit, isLowerCase, and isUpperCase for testing whether a character is a digit, letter, low- ercase, or uppercase. It also contains the toLowerCase and toUpperCase methods for returning a lowercase or uppercase letter.
8. A string is a sequence of characters. A string value is enclosed in matching double quotes ("). A character value is enclosed in matching single quotes (').
9. Strings are objects in Java. A method that can only be invoked from a specific object is called an instance method. A noninstance method is called a static method, which can be invoked without using an object.
10. You can get the length of a string by invoking its length() method, retrieve a char- acter at the specified index in the string using the charAt(index) method, and use the indexOf and lastIndexOf methods to find a character or a substring in a string.
11. You can use the concat method to concatenate two strings or the plus (+) operator to concatenate two or more strings.
12. You can use the substring method to obtain a substring from the string.
13. You can use the equals and compareTo methods to compare strings. The equals method returns true if two strings are equal, and false if they are not equal. The compareTo method returns 0, a positive integer, or a negative integer, depending on whether one string is equal to, greater than, or less than the other string.
14. The printf method can be used to display a formatted output using format specifiers.
Quiz Answer the quiz for this chapter online at the Companion Website.
programming exerCises
Section 4.2 4.1 (Geometry: area of a pentagon) Write a program that prompts the user to enter
the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure.
r
M04_LIAN1878_11_GE_C04.indd 172 12/29/17 3:57 PM
Programming Exercises 173
*4.2 (Geometry: great circle distance) The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula:
d = radius * arccos(sin (x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))
Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average radius of the earth is 6,371.01 km. Note you need to convert the degrees into radi- ans using the Math.toRadians method since the Java trigonometric methods use radians. The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees. Here is a sample run:
VideoNote
Compute great circle distance
Enter the length from the center to a vertex: 5.5 The area of the pentagon is 71.92
The formula for computing the area of a pentagon is Area = 5 * s2
4 * tan ap 5 b
, where
s is the length of a side. The side can be computed using the formula s = 2r sin p
5 ,
where r is the length from the center of a pentagon to a vertex. Round up two digits after the decimal point. Here is a sample run:
Enter point 1 (latitude and longitude) in degrees: 39.55 −116.25 Enter point 2 (latitude and longitude) in degrees: 41.5 87.37 The distance between the two points is 10691.79183231593 km
*4.3 (Geography: estimate areas) Use the GPS locations for Atlanta, Georgia; Orlando, Florida; Savannah, Georgia; and Charlotte, North Carolina in the figure in Section 4.1 to compute the estimated area enclosed by these four cities. (Hint: Use the formula in Programming Exercise 4.2 to compute the distance between two cities. Divide the polygon into two triangles and use the formula in Programming Exercise 2.19 to compute the area of a triangle.)
4.4 (Geometry: area of a hexagon) The area of a hexagon can be computed using the following formula (s is the length of a side):
Area = 6 * s2
4 * tan( p
6 )
Write a program that prompts the user to enter the side of a hexagon and displays its area. Here is a sample run:
Enter the side: 5.5 The area of the hexagon is 78.59
M04_LIAN1878_11_GE_C04.indd 173 12/29/17 3:57 PM
174 Chapter 4 Mathematical Functions, Characters, and Strings
*4.5 (Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is
Area = n * s2
4 * tan( p
n )
Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:
Enter the number of sides: 5 Enter the side: 6.5 The area of the polygon is 72.69017017488385
*4.6 (Random points on a circle) Write a program that generates three random points on a circle centered at (0, 0) with radius 40 and displays three angles in a triangle formed by these three points, as shown in Figure 4.4a. (Hint: Generate a random angle a in radians between 0 and 2π, as shown in Figure 4.4b and the point deter- mined by this angle is (rxcos (a), rxsin (a)).)
Figure 4.4 (a) A triangle is formed from three random points on the circle. (b) A random point on the circle can be generated using a random angle a. (c) A pentagon is centered at (0, 0) with one point at the 0 o’clock position.
65
55
60
0 o’clock positionx = r × cos(a) and y = r ×sin(a)
(a) (b) (c)
(0, 0)
p2
p1
p5p4
p3
(x, y)
a
r r
*4.7 (Corner point coordinates) Suppose a pentagon is centered at (0, 0) with one point at the 0 o’clock position, as shown in Figure 4.4c. Write a program that prompts the user to enter the radius of the bounding circle of a pentagon and displays the coordinates of the five corner points on the pentagon from p1 to p5 in this order.
M04_LIAN1878_11_GE_C04.indd 174 12/29/17 3:57 PM
Programming Exercises 175
Sections 4.3–4.6 *4.8 (Find the character of an ASCII code) Write a program that receives a character
and displays its ASCII code (an integer between 0 and 127). Here is a sample run:
Enter the radius of the bounding circle: 100.52 The coordinates of five points on the pentagon are (95.60, 31.06) (0.00, 100.52) (−95.60, 31.06) (−58.08, −81.32) (59.08, −81.32)
Use console format to display two digits after the decimal point. Here is a sample run:
Enter a character: E The ASCII code for character E is 69
*4.9 (Find the Unicode of a character) Write a program that receives a character and displays its Unicode. Here is a sample run:
Enter a character: E The Unicode for the character E is 69
Enter binary digits (0000 to 1111): 0111 The decimal value is 7
Enter binary digits (0000 to 1111): 1001 The decimal value is 9
Enter binary digits (0000 to 1111): 1100 The decimal value is 12
*4.10 (Guess birthday) Rewrite Listing 4.3, GuessBirthday.java, to prompt the user to enter the character Y for Yes and N for No, rather than entering 1 for Yes and 0 for No.
*4.11 (Binary to decimal) Write a program that prompts the user to enter binary digits and displays its corresponding decimal value. Here are some sample runs:
M04_LIAN1878_11_GE_C04.indd 175 12/29/17 3:57 PM
176 Chapter 4 Mathematical Functions, Characters, and Strings
4.12 (Hex to binary) Write a program that prompts the user to enter a hex digit and displays its corresponding binary number. For an incorrect input, display invalid input. Here is a sample run:
VideoNote
Convert hex to binary
Enter a hex digit: B
The binary value is 1011
Enter a hex digit: G
G is an invalid input
Enter a letter: B B is a consonant
Enter a letter: a a is a vowel
Enter a letter: #
# is an invalid input
*4.13 (Vowel or consonant?) Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant. For a nonletter input, display invalid input. Here is a sample run:
*4.14 (Convert letter grade to number) Write a program that prompts the user to enter a letter grade A, B, C, D, or F and displays its corresponding numeric value 4, 3, 2, 1, or 0. For other input, display invalid grade. Here is a sample run:
Enter a letter grade: B The numeric value for grade B is 3
Enter a letter grade: T
T is an invalid grade
*4.15 (Phone key pads) The international standard letter/number mapping found on the telephone is shown below:
M04_LIAN1878_11_GE_C04.indd 176 12/29/17 3:57 PM
Programming Exercises 177
Write a program that prompts the user to enter a lowercase or uppercase letter and displays its corresponding number. For a nonletter input, display invalid input.
Enter a letter: A The corresponding number is 2
Enter a letter: a The corresponding number is 2
Enter a letter: +
+ is an invalid input
4.16 (Random character) Write a program that displays a random lowercase letter using the Math.random() method.
*4.17 (Days of a month) Write a program that prompts the user to enter the year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month. If the input for month is incorrect, display a mes- sage as presented in the following sample runs:
Enter a year: 2001 Enter a month: Jan Jan 2001 has 31 days
Enter a year: 2016 Enter a month: jan jan is not a correct month name
Enter two characters: I1 Information Management Freshman
*4.18 (Student major and status) Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following characters are used to denote the majors:
I: Information Management C: Computer Science A: Accounting
Here is a sample run:
M04_LIAN1878_11_GE_C04.indd 177 12/29/17 3:57 PM
178 Chapter 4 Mathematical Functions, Characters, and Strings
4.19 (Business: check ISBN-10) Rewrite Programming Exercise 3.9 by entering the ISBN number as a string.
4.20 (Process a string) Write a program that prompts the user to enter a string and dis- plays its length and its last character.
*4.21 (Check SSN) Write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid. Here are sample runs:
Enter two characters: T3
Invalid input
Enter a SSN: 232−23−5435
232−23−5435 is a valid social security number
Enter a SSN: 23−23−5435
23−23−5435 is an invalid social security number
Enter string s1: ABCD Enter string s2: BC BC is a substring of ABCD
4.22 (Check substring) Write a program that prompts the user to enter two strings, and reports whether the second string is a substring of the first string.
*4.23 (Financial application: payroll) Write a program that reads the following informa- tion and prints a payroll statement:
Employee’s name (e.g., Smith) Number of hours worked in a week (e.g., 10) Hourly pay rate (e.g., 9.75)
Enter string s1: ABCD
Enter string s2: BDC BDC is not a substring of ABCD
Enter two characters: A3
Accounting Junior
M04_LIAN1878_11_GE_C04.indd 178 12/29/17 3:57 PM
Programming Exercises 179
Federal tax withholding rate (e.g., 20%) State tax withholding rate (e.g., 9%)
A sample run is as follows:
Enter employee’s name: Smith
Enter number of hours worked in a week: 10 Enter hourly pay rate: 9.75
Enter federal tax withholding rate: 0.20 Enter state tax withholding rate: 0.09
Employee Name: Smith
Hours Worked: 10.0
Pay Rate: $9.75
Gross Pay: $97.5
Deductions:
Federal Withholding (20.0%): $19.5
State Withholding (9.0%): $8.77
Total Deduction: $28.27
Net Pay: $69.22
*4.24 (Enter three countries) Write a program that prompts the user to enter three coun- tries and displays them in descending order. Here is a sample run:
Enter the first country: Germany
Enter the second country: France
Enter the third country: Switzerland The three countries in descending order are Switzerland Germany France
*4.25 (Generate vehicle plate numbers) Assume that a vehicle plate number consists of three uppercase letters followed by four digits. Write a program to generate a plate number.
*4.26 (Financial application: monetary units) Rewrite Listing 2.10, ComputeChange.java, to fix the possible loss of accuracy when converting a float value to an int value. Read the input as a string such as "11.56". Your program should extract the dollar amount before the decimal point, and the cents after the decimal amount using the indexOf and substring methods.
Note More than 200 additional programming exercises with solutions are provided to the instructors on the Instructor Resource Website.
M04_LIAN1878_11_GE_C04.indd 179 12/29/17 3:57 PM
A01_GIPE3898_09_SE_FM.indd 3 12/20/17 5:56 PM
This page intentionally left blank
Loops
Objectives ■■ To write programs for executing statements repeatedly using a while
loop (§5.2).
■■ To write loops for the guessing number problem (§5.3).
■■ To follow the loop design strategy to develop loops (§5.4).
■■ To control a loop with the user confirmation or a sentinel value (§5.5).
■■ To obtain large input from a file using input redirection rather than typing from the keyboard (§5.5).
■■ To write loops using do-while statements (§5.6).
■■ To write loops using for statements (§5.7).
■■ To discover the similarities and differences of three types of loop statements (§5.8).
■■ To write nested loops (§5.9).
■■ To learn the techniques for minimizing numerical errors (§5.10).
■■ To learn loops from a variety of examples (GCD, FutureTuition, and Dec2Hex) (§5.11).
■■ To implement program control with break and continue (§5.12).
■■ To process characters in a string using a loop in a case study for checking palindrome (§5.13).
■■ To write a program that displays prime numbers (§5.14).
Chapter
5
M05_LIAN1878_11_GE_C05.indd 181 1/2/18 11:16 PM
182 Chapter 5 Loops
5.1 Introduction A loop can be used to tell a program to execute statements repeatedly.
Suppose you need to display a string (e.g., Welcome to Java!) a hundred times. It would be tedious to have to write the following statement a hundred times:Point
Keyproblem
100 times
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); ... System.out.println("Welcome to Java!");
So, how do you solve this problem? Java provides a powerful construct called a loop that controls how many times an operation
or a sequence of operations is performed in succession. Using a loop statement, you can simply tell the computer to display a string a hundred times without having to code the print statement a hundred times, as follows:
int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; }
The variable count is initially 0. The loop checks whether count < 100 is true. If so, it executes the loop body to display the message Welcome to Java! and increments count by 1. It repeatedly executes the loop body until count < 100 becomes false. When count < 100 is false (i.e., when count reaches 100), the loop terminates, and the next statement after the loop statement is executed.
Loops are constructs that control repeated executions of a block of statements. The concept of looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for loops.
5.2 The while Loop A while loop executes statements repeatedly while the condition is true.
The syntax for the while loop is as follows:
while (loop-continuation-condition) { // Loop body Statement(s); }
Figure 5.1a shows the while loop flowchart. The part of the loop that contains the state- ments to be repeated is called the loop body. A one-time execution of a loop body is referred to as an iteration (or repetition) of the loop. Each loop contains a loop-continuation- condition, a Boolean expression that controls the execution of the body. It is evaluated each time to determine if the loop body is executed. If its evaluation is true, the loop body is executed; if its evaluation is false, the entire loop terminates and the program control turns to the statement that follows the while loop.
The loop for displaying Welcome to Java! a hundred times introduced in the preceding section is an example of a while loop. Its flowchart is shown in Figure 5.1b.
loop
Point Key
while loop
VideoNote
Use while loop loop body iteration loop-continuation- condition
M05_LIAN1878_11_GE_C05.indd 182 1/2/18 11:16 PM
5.2 The while Loop 183
The loop-continuation-condition is count < 100 and the loop body contains two statements in the following code:
Figure 5.1 The while loop repeatedly executes the statements in the loop body when the loop-continuation-condition evaluates to true.
loop- continuation- condition?
true
false
(a)
(count < 100)?
true
false
(b)
count = 0;
System.out.println("Welcome to Java!"); count++;
Statement(s) (loop body)
Statement(s) Before loop
loop-continuation-condition
int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); loop body count++; }
In this example, you know exactly how many times the loop body needs to be executed because the control variable count is used to count the number of iterations. This type of loop is known as a counter-controlled loop.
Note The loop-continuation-condition must always appear inside the parenthe- ses. The braces enclosing the loop body can be omitted only if the loop body contains one or no statement.
Here is another example to help understand how a loop works.
int sum = 0, i = 1; while (i < 10) { sum = sum + i; i++; } System.out.println("sum is " + sum); // sum is 45
If i < 10 is true, the program adds i to sum. Variable i is initially set to 1, then is incre- mented to 2, 3, and up to 10. When i is 10, i < 10 is false, so the loop exits. Therefore, the sum is 1 + 2 + 3 + ... + 9 = 45.
counter-controlled loop
M05_LIAN1878_11_GE_C05.indd 183 1/2/18 11:16 PM
184 Chapter 5 Loops
What happens if the loop is mistakenly written as follows?
int sum = 0, i = 1; while (i < 10) { sum = sum + i; }
This loop is infinite, because i is always 1 and i < 10 will always be true.
Note Make sure that the loop-continuation-condition eventually becomes false so that the loop will terminate. A common programming error involves infinite loops (i.e., the loop runs forever). If your program takes an unusually long time to run and does not stop, it may have an infinite loop. If you are running the program from the command window, press CTRL+C to stop it.
Caution Programmers often make the mistake of executing a loop one more or less time. This is commonly known as the off-by-one error. For example, the following loop displays Welcome to Java 101 times rather than 100 times. The error lies in the condition, which should be count < 100 rather than count <= 100.
int count = 0; while (count <= 100) { System.out.println("Welcome to Java!"); count++; }
Recall that Listing 3.1, AdditionQuiz.java, gives a program that prompts the user to enter an answer for a question on addition of two single digits. Using a loop, you can now rewrite the program to let the user repeatedly enter a new answer until it is correct, as given in Listing 5.1.
Listing 5.1 RepeatAdditionQuiz.java 1 import java.util.Scanner; 2 3 public class RepeatAdditionQuiz { 4 public static void main(String[] args) { 5 int number1 = (int)(Math.random() * 10); 6 int number2 = (int)(Math.random() * 10); 7 8 // Create a Scanner 9 Scanner input = new Scanner(System.in); 10 11 System.out.print( 12 "What is " + number1 + " + " + number2 + "? "); 13 int answer = input.nextInt(); 14 15 while (number1 + number2 != answer) { 16 System.out.print("Wrong answer. Try again. What is " 17 + number1 + " + " + number2 + "? "); 18 answer = input.nextInt(); 19 } 20 21 System.out.println("You got it!"); 22 } 23 }
infinite loop
off-by-one error
generate number1 generate number2
show question
get first answer
check answer
read an answer
What is 5 + 9? 12
Wrong answer. Try again. What is 5 + 9? 34
Wrong answer. Try again. What is 5 + 9? 14
You got it!
M05_LIAN1878_11_GE_C05.indd 184 1/2/18 11:16 PM
5.3 Case Study: Guessing Numbers 185
The loop in lines 15–19 repeatedly prompts the user to enter an answer when number1 + number2 != answer is true. Once number1 + number2 != answer is false, the loop exits.
5.2.1 Analyze the following code. Is count < 100 always true, always false, or sometimes true or sometimes false at Point A, Point B, and Point C?
int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C
5.2.2 How many times are the following loop bodies repeated? What is the output of each loop?
Point Check
Guess a magic number between 0 and 100
Enter your guess: 50
Your guess is too high
Enter your guess: 25
Your guess is too low
Enter your guess: 42
Your guess is too high
Enter your guess: 39
Yes, the number is 39
(c)
int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i);
int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i++);
int i = 1; while (i < 10) if ((i++) % 2 == 0) System.out.println(i);
(a) (b)
5.2.3 What is the output of the following code? Explain the reason. int x = 80000000;
while (x > 0) x++;
System.out.println("x is " + x);
5.3 Case Study: Guessing Numbers This case study generates a random number and lets the user repeatedly guess a n umber until it is correct.
The problem is to guess what number a computer has in mind. You will write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can make the next guess intelligently. Here is a sample run:
Point Key
VideoNote
Guess a number
M05_LIAN1878_11_GE_C05.indd 185 1/2/18 11:16 PM
186 Chapter 5 Loops
The magic number is between 0 and 100. To minimize the number of guesses, enter 50 first. If your guess is too high, the magic number is between 0 and 49. If your guess is too low, the magic number is between 51 and 100. Thus, you can eliminate half of the numbers from further consideration after one guess.
How do you write this program? Do you immediately begin coding? No. It is important to think before coding. Think how you would solve the problem without writing a program. You need first to generate a random number between 0 and 100, inclusive, then to prompt the user to enter a guess, then to compare the guess with the random number.
It is a good practice to code incrementally one step at a time. For programs involving loops, if you don’t know how to write a loop right away, you may first write the code for executing the loop one time, then figure out how to repeatedly execute the code in a loop. For this pro- gram, you may create an initial draft, as given in Listing 5.2.
Listing 5.2 GuessNumberOneTime.java 1 import java.util.Scanner; 2 3 public class GuessNumberOneTime { 4 public static void main(String[] args) { 5 // Generate a random number to be guessed 6 int number = (int)(Math.random() * 101); 7 8 Scanner input = new Scanner(System.in); 9 System.out.println("Guess a magic number between 0 and 100"); 10 11 // Prompt the user to guess the number 12 System.out.print("\nEnter your guess: "); 13 int guess = input.nextInt(); 14 15 if (guess == number) 16 System.out.println("Yes, the number is " + number); 17 else if (guess > number) 18 System.out.println("Your guess is too high"); 19 else 20 System.out.println("Your guess is too low"); 21 } 22 }
When you run this program, it prompts the user to enter a guess only once. To let the user enter a guess repeatedly, you may wrap the code in lines 11–20 in a loop as follows:
while (true) { // Prompt the user to guess the number System.out.print("\nEnter your guess: "); guess = input.nextInt();
if (guess == number) System.out.println("Yes, the number is " + number); else if (guess > number) System.out.println("Your guess is too high"); else System.out.println("Your guess is too low"); } // End of loop
This loop repeatedly prompts the user to enter a guess. However, this loop is not correct, because it never terminates. When guess matches number, the loop should end. Thus, the loop can be revised as follows:
while (guess != number) { // Prompt the user to guess the number
intelligent guess
think before coding
code incrementally
generate a number
enter a guess
correct guess
too high
too low
M05_LIAN1878_11_GE_C05.indd 186 1/2/18 11:16 PM
5.3 Case Study: Guessing Numbers 187
System.out.print("\nEnter your guess: "); guess = input.nextInt();
if (guess == number) System.out.println("Yes, the number is " + number); else if (guess > number) System.out.println("Your guess is too high"); else System.out.println("Your guess is too low"); } // End of loop
The complete code is given in Listing 5.3.
Listing 5.3 GuessNumber.java 1 import java.util.Scanner; 2 3 public class GuessNumber { 4 public static void main(String[] args) { 5 // Generate a random number to be guessed 6 int number = (int)(Math.random() * 101); 7 8 Scanner input = new Scanner(System.in); 9 System.out.println("Guess a magic number between 0 and 100"); 10 11 int guess = –1; 12 while (guess != number) { 13 // Prompt the user to guess the number 14 System.out.print("\nEnter your guess: "); 15 guess = input.nextInt(); 16 17 if (guess == number) 18 System.out.println("Yes, the number is " + number); 19 else if (guess > number) 20 System.out.println("Your guess is too high"); 21 else 22 System.out.println("Your guess is too low"); 23 } // End of loop 24 } 25 }
generate a number
enter a guess
too high
too low
line# number guess output
6 39
11 −1
iteration 1 b 15 50 20 Your guess is too high
iteration 2 b 15 25 22 Your guess is too low
iteration 3 b 15 42 20 Your guess is too high
iteration 4 b 15 39 18 Yes, the number is 39
The program generates the magic number in line 6 and prompts the user to enter a guess continuously in a loop (lines 12–23). For each guess, the program checks whether the guess is
M05_LIAN1878_11_GE_C05.indd 187 1/2/18 11:16 PM
188 Chapter 5 Loops
correct, too high, or too low (lines 17–22). When the guess is correct, the program exits the loop (line 12). Note that guess is initialized to −1. Initializing it to a value between 0 and 100 would be wrong, because that could be the number to be guessed.
5.3.1 What is wrong if guess is initialized to 0 in line 11 in Listing 5.3?
5.4 Loop Design Strategies The key to designing a loop is to identify the code that needs to be repeated and write a condition for terminating the loop.
Writing a correct loop is not an easy task for novice programmers. Consider three steps when writing a loop.
Step 1: Identify the statements that need to be repeated.
Step 2: Wrap these statements in a loop as follows:
while (true) { Statements; }
Step 3: Code the loop-continuation-condition and add appropriate statements for controlling the loop.
while (loop-continuation-condition) { Statements; Additional statements for controlling the loop; }
The Math subtraction learning tool program in Listing 3.3, SubtractionQuiz.java, gener- ates just one question for each run. You can use a loop to generate questions repeatedly. How do you write the code to generate five questions? Follow the loop design strategy. First, identify the statements that need to be repeated. These are the statements for obtaining two random numbers, prompting the user with a subtraction question, and grading the question. Second, wrap the statements in a loop. Third, add a loop control variable and the loop- continuation-condition to execute the loop five times.
Listing 5.4 gives a program that generates five questions and, after a student answers all five, reports the number of correct answers. The program also displays the time spent on the test and lists all the questions.
Listing 5.4 SubtractionQuizLoop.java 1 import java.util.Scanner; 2 3 public class SubtractionQuizLoop { 4 public static void main(String[] args) { 5 final int NUMBER_OF_QUESTIONS = 5; // Number of questions 6 int correctCount = 0; // Count the number of correct answers 7 int count = 0; // Count the number of questions 8 long startTime = System.currentTimeMillis(); 9 String output = " "; // output string is initially empty 10 Scanner input = new Scanner(System.in); 11 12 while (count < NUMBER_OF_QUESTIONS) { 13 // 1. Generate two random single-digit integers 14 int number1 = (int)(Math.random() * 10); 15 int number2 = (int)(Math.random() * 10); 16 17 // 2. If number1 < number2, swap number1 with number2 18 if (number1 < number2) {
Point Check
Point Key
VideoNote Multiple subtraction quiz
loop
get start time
M05_LIAN1878_11_GE_C05.indd 188 1/2/18 11:16 PM
5.4 Loop Design Strategies 189
19 int temp = number1; 20 number1 = number2; 21 number2 = temp; 22 } 23 24 // 3. Prompt the student to answer "What is number1 – number2?" 25 System.out.print( 26 "What is " + number1 + " – " + number2 + "? "); 27 int answer = input.nextInt(); 28 29 // 4. Grade the answer and display the result 30 if (number1 – number2 == answer) { 31 System.out.println("You are correct!"); 32 correctCount++; // Increase the correct answer count 33 } 34 else 35 System.out.println("Your answer is wrong.\n" + number1 36 + " – " + number2 + " should be " + (number1 — number2)); 37 38 // Increase the question count 39 count++; 40 41 output += "\n" + number1 + "–" + number2 + "=" + answer + 42 ((number1 – number2 == answer) ? " correct": " wrong"); 43 } 44 45 long endTime = System.currentTimeMillis(); 46 long testTime = endTime – startTime; 47 48 System.out.println("Correct count is " + correctCount + 49 "\nTest time is " + testTime / 1000 + " seconds\n" + output); 50 } 51 }
display a question
grade an answer
increase correct count
increase control variable
prepare output
end loop
get end time test time
display result
What is 9 – 2? 7
You are correct!
What is 3 – 0? 3 You are correct!
What is 3 – 2? 1 You are correct!
What is 7 – 4? 4 Your answer is wrong. 7 – 4 should be 3
What is 7 – 5? 4 Your answer is wrong. 7 – 5 should be 2
Correct count is 3 Test time is 1021 seconds
9–2=7 correct 3–0=3 correct 3–2=1 correct 7–4=4 wrong 7–5=4 wrong
M05_LIAN1878_11_GE_C05.indd 189 1/2/18 11:16 PM
190 Chapter 5 Loops
The program uses the control variable count to control the execution of the loop. count is initially 0 (line 7) and is increased by 1 in each iteration (line 39). A subtraction question is displayed and processed in each iteration. The program obtains the time before the test starts in line 8 and the time after the test ends in line 45, then computes the test time in line 46. The test time is in milliseconds and is converted to seconds in line 49.
5.4.1 Revise the code using the System.nanoTime() to measure the time in nano seconds.
5.5 Controlling a Loop with User Confirmation or a Sentinel Value
It is a common practice to use a sentinel value to terminate the input.
The preceding example executes the loop five times. If you want the user to decide whether to continue, you can offer a user confirmation. The template of the program can be coded as follows:
char continueLoop = 'Y'; while (continueLoop == 'Y') { // Execute the loop body once ... // Prompt the user for confirmation System.out.print("Enter Y to continue and N to quit: "); continueLoop = input.getLine().charAt(0); }
You can rewrite the program given in Listing 5.4 with user confirmation to let the user decide whether to advance to the next question.
Another common technique for controlling a loop is to designate a special value when read- ing and processing a set of values. This special input value, known as a sentinel value, signifies the end of the input. A loop that uses a sentinel value to control its execution is called a sentinel-controlled loop.
Listing 5.5 gives a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. Do you need to declare a new variable for each input value? No. Just use one variable named data (line 12) to store the input value, and use a variable named sum (line 15) to store the total. Whenever a value is read, assign it to data and, if it is not zero, add it to sum (line 17).
Listing 5.5 SentinelValue.java 1 import java.util.Scanner; 2 3 public class SentinelValue { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Read an initial data 10 System.out.print( 11 "Enter an integer (the input ends if it is 0): "); 12 int data = input.nextInt(); 13 14 // Keep reading data until the input is 0 15 int sum = 0; 16 while (data != 0) {
Point Check
Point Key
sentinel value
sentinel-controlled loop
loop
input
M05_LIAN1878_11_GE_C05.indd 190 1/2/18 11:16 PM
5.5 Controlling a Loop with User Confirmation or a Sentinel Value 191
17 sum += data; 18 19 // Read the next data 20 System.out.print( 21 "Enter an integer (the input ends if it is 0): "); 22 data = input.nextInt(); 23 } 24 25 System.out.println("The sum is " + sum); 26 } 27 }
end of loop
display result
Enter an integer (the input ends if it is 0): 2
Enter an integer (the input ends if it is 0): 3
Enter an integer (the input ends if it is 0): 4
Enter an integer (the input ends if it is 0): 0
The sum is 9
If data is not 0, it is added to sum (line 17) and the next item of input data is read (lines 20–22). If data is 0, the loop body is no longer executed and the while loop terminates. The input value 0 is the sentinel value for this loop. Note if the first input read is 0, the loop body never executes, and the resulting sum is 0.
Caution Don’t use floating-point values for equality checking in a loop control. Because floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results.
Consider the following code for computing 1 + 0.9 + 0.8 + ... + 0.1:
double item = 1; double sum = 0; while (item != 0) { // No guarantee item will be 0 sum += item; item −= 0.1; } System.out.println(sum);
Variable item starts with 1 and is reduced by 0.1 every time the loop body is executed. The loop should terminate when item becomes 0. However, there is no guarantee that item will be exactly 0, because the floating-point arithmetic is approximated. This loop seems okay on the surface, but it is actually an infinite loop. numeric error
line# data sum output
12 2
15 0
iteration 1 b 17 2 22 3
iteration 2 b 17 5 22 4
iteration 3 b 17 9 22 0
25 The sum is 9
M05_LIAN1878_11_GE_C05.indd 191 1/2/18 11:16 PM
192 Chapter 5 Loops
In the preceding example, if you have a large number of data to enter, it would be cumbersome to type from the keyboard. You can store the data separated by whitespaces in a text file, say input.txt, and run the program using the following command:
java SentinelValue < input.txt
This command is called input redirection. The program takes the input from the file input. txt rather than having the user type the data from the keyboard at runtime. Suppose the contents of the file are as follows:
2 3 4 5 6 7 8 9 12 23 32 23 45 67 89 92 12 34 35 3 1 2 4 0
The program should get sum to be 518. Similarly, there is output redirection, which sends the output to a file rather than displaying
it on the console. The command for output redirection is
java ClassName > output.txt
Input and output redirections can be used in the same command. For example, the following command gets input from input.txt and sends output to output.txt:
java SentinelValue < input.txt > output.txt
Try running the program to see what contents are in output.txt.
5.5.1 Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner;
public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in);
int number, max; number = input.nextInt(); max = number;
while (number != 0) { number = input.nextInt(); if (number > max) max = number; }
System.out.println("max is " + max); System.out.println("number " + number); } }
5.6 The do-while Loop A do-while loop is the same as a while loop except that it executes the loop body first then checks the loop continuation condition.
The do-while loop is a variation of the while loop. Its syntax is as follows:
do { // Loop body; Statement(s); } while (loop-continuation-condition);
Its execution flowchart is shown in Figure 5.2a.
input redirection
output redirection
Point Check
Point Key
VideoNote Use do-while loop do-while loop
M05_LIAN1878_11_GE_C05.indd 192 1/2/18 11:16 PM
The loop body is executed first, then the loop-continuation-condition is evaluated. If the evaluation is true, the loop body is executed again; if it is false, the do-while loop terminates. For example, the following while loop statement
int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; }
can be written using a do-while loop as follows:
int count = 0; do { System.out.println("Welcome to Java!"); count++; } while (count < 100);
The flowchart of this do-while loop is shown in Figure 5.2b. The difference between a while loop and a do-while loop is the order in which the loop-
continuation-condition is evaluated and the loop body is executed. In the case of a do- while loop, the loop body is executed at least once. You can write a loop using either the while loop or the do-while loop. Sometimes one is a more convenient choice than the other. For exam- ple, you can rewrite the while loop in Listing 5.5 using a do-while loop, as given in Listing 5.6.
Listing 5.6 TestDoWhile.java 1 import java.util.Scanner; 2 3 public class TestDoWhile { 4 /** Main method */ 5 public static void main(String[] args) { 6 int data; 7 int sum = 0;
Figure 5.2 The do-while loop executes the loop body first then checks the l oop- continuation-condition to determine whether to continue or terminate the loop.
loop- continuation-
condition?true
(a)
(count < 100)? true
falsefalse
(b)
count = 0;
System.out.println("Welcome to Java!"); count++;
Statement(s) (loop body)
Statement(s) Before loop
5.6 The do-while Loop 193
M05_LIAN1878_11_GE_C05.indd 193 1/2/18 11:16 PM
194 Chapter 5 Loops
8 9 // Create a Scanner 10 Scanner input = new Scanner(System.in); 11 12 // Keep reading data until the input is 0 13 do { 14 // Read the next data 15 System.out.print( 16 "Enter an integer (the input ends if it is 0): "); 17 data = input.nextInt(); 18 19 sum += data; 20 } while (data != 0); 21 22 System.out.println("The sum is " + sum); 23 } 24 }
loop
end loop
Enter an integer (the input ends if it is 0): 3
Enter an integer (the input ends if it is 0): 5
Enter an integer (the input ends if it is 0): 6
Enter an integer (the input ends if it is 0): 0
The sum is 14
Tip Use a do-while loop if you have statements inside the loop that must be executed at least once, as in the case of the do-while loop in the preceding TestDoWhile program. These statements must appear before the loop as well as inside it if you use a while loop.
5.6.1 Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner;
public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in);
int number, max; number = input.nextInt(); max = number;
do { number = input.nextInt(); if (number > max) max = number; } while (number != 0);
System.out.println("max is " + max); System.out.println("number " + number); } }
Point Check
M05_LIAN1878_11_GE_C05.indd 194 1/2/18 11:16 PM
5.7 The for Loop 195
5.6.2 What are the differences between a while loop and a do-while loop? Convert the following while loop into a do-while loop:
Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter an integer " + "(the input ends if it is 0)"); int number = input.nextInt(); while (number != 0) { sum += number; System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); }
5.7 The for Loop A for loop has a concise syntax for writing loops.
Often you write a loop in the following common form:
i = initialValue; // Initialize loop control variable while (i < endValue) { // Loop body ... i++; // Adjust loop control variable }
This loop is intuitive and easy for beginners to grasp. However, programmers often forget to adjust the control variable, which leads to an infinite loop. A for loop can be used to simplify the preceding loop as shown in (a), which is equivalent to (b)
Point Key
for (i = initialValue; i < endValue; i++) {
// Loop body ... }
(a)
i = initialValue;
while (i < endValue) {
// Loop body ... i++;
}
(b)
In general, the syntax of a for loop is as follows:
for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Statement(s); }
The flowchart of the for loop is shown in Figure 5.3a. The for loop statement starts with the keyword for, followed by a pair of parentheses
enclosing the control structure of the loop. This structure consists of initial-action, loop- continuation-condition, and action-after-each-iteration. The control structure is followed by the loop body enclosed inside braces. The initial-action, loop-contin- uation-condition, and action-after-each-iteration are separated by semicolons.
A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. This variable is referred to as a control variable. The initial- action often initializes a control variable, the action-after-each-iteration usually increments or decrements the control variable, and the loop-continuation-condition
for loop
control variable
M05_LIAN1878_11_GE_C05.indd 195 1/2/18 11:16 PM
196 Chapter 5 Loops
tests whether the control variable has reached a termination value. For example, the following for loop prints Welcome to Java! a hundred times:
int i; for (i = 0; i < 100; i++) { System.out.println("Welcome to Java!"); }
The flowchart of the statement is shown in Figure 5.3b. The for loop initializes i to 0, then repeatedly executes the println statement and evaluates i++ while i is less than 100.
The initial-action, i = 0, initializes the control variable, i. The loop- continuation-condition, i < 100, is a Boolean expression. The expression is evaluated right after the initialization and at the beginning of each iteration. If this condition is true, the loop body is executed. If it is false, the loop terminates and the program control turns to the line following the loop.
The action-after-each-iteration, i++, is a statement that adjusts the control v ariable. This statement is executed after each iteration and increments the control variable. Eventually, the value of the control variable should force the loop-continuation-condi- tion to become false; otherwise, the loop is infinite.
The loop control variable can be declared and initialized in the for loop. Here is an example:
for (int i = 0; i < 100; i++) { System.out.println("Welcome to Java!"); }
If there is only one statement in the loop body, as in this example, the braces can be omitted.
Tip The control variable must be declared inside the control structure of the loop or before the loop. If the loop control variable is used only in the loop, and not elsewhere, it is a good programming practice to declare it in the initial-action of the for loop. If
initial-action
action-after-each-iteration
omitting braces
declare control variable
Figure 5.3 A for loop performs an initial action once, then repeatedly executes the statements in the loop body, and performs an action after an iteration when the loop-
continuation-condition evaluates to true.
Statement(s) (loop body)
(a)
Initial-action
action-after-each-iteration
true
false loop-
continuation- condition?
System.out.println( "Welcome to Java!");
(b)
i = 0
i++
true
false (i < 100)?
M05_LIAN1878_11_GE_C05.indd 196 1/2/18 11:16 PM
the variable is declared inside the loop control structure, it cannot be referenced outside the loop. In the preceding code, for example, you cannot reference i outside the for loop, because it is declared inside the for loop.
Note The initial-action in a for loop can be a list of zero or more comma-separated variable declaration statements or assignment expressions. For example:
for (int i = 0, j = 0; i + j < 10; i++, j++) { // Do something }
The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. For example:
for (int i = 1; i < 100; System.out.println(i), i++) ;
This example is correct, but it is a bad example, because it makes the code difficult to read. Normally, you declare and initialize a control variable as an initial action, and increment or decrement the control variable as an action after each iteration.
Note If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus, the statement given below in (a), which is an infinite loop, is the same as in (b). To avoid confusion, though, it is better to use the equivalent loop in (c).
for loop variations
5.7.1 Do the following two loops result in the same value in sum? Point Check
for ( ; ; ) { // Do something }
(a)
for ( ; true; ) { // Do something }
(b)
while (true) { // Do something }
(c) This is better
Equivalent Equivalent
5.7.2 What are the three parts of a for loop control? Write a for loop that prints the numbers from 1 to 100.
5.7.3 Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner;
public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in);
int number, sum = 0, count;
for (count = 0; count < 5; count++) { number = input.nextInt(); sum += number; }
System.out.println("sum is " + sum); System.out.println("count is " + count); } }
for (int i = 0; i < 10; ++i) { sum += i; }
(a)
for (int i = 0; i < 10; i++) { sum += i; }
(b)
5.7 The for Loop 197
M05_LIAN1878_11_GE_C05.indd 197 1/2/18 11:16 PM
198 Chapter 5 Loops
5.7.4 What does the following statement do? for ( ; ; ) { // Do something }
5.7.5 If a variable is declared in a for loop control, can it be used after the loop exits? 5.7.6 Convert the following for loop statement to a while loop and to a do-while loop:
long sum = 0; for (int i = 0; i <= 1000; i++) sum = sum + i;
5.7.7 Count the number of iterations in the following loops.
5.8 Which Loop to Use? You can use a for loop, a while loop, or a do-while loop, whichever is convenient.
The while loop and do-while loop are easier to learn than the for loop. However, you will learn the for loop quickly after some practice. A for loop places control variable initialization, loop continuation condition, and adjustment after each iteration all together. It is more concise and enables you to write the code with less errors than the other two loops.
The while loop and for loop are called pretest loops because the continuation condition is checked before the loop body is executed. The do-while loop is called a posttest loop because the condition is checked after the loop body is executed. The three forms of loop statements—while, do-while, and for—are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the for loop in (b).
Point Key
pretest loop
posttest loop
int count = 0; while (count < n) { count++; }
(a)
for (int count = 0; count <= n; count++) { }
(b)
int count = 5; while (count < n) { count++; }
(c)
int count = 5; while (count < n) { count = count + 3; }
(d)
A for loop in (a) in the next figure can generally be converted into the while loop in (b) except in certain special cases (see CheckPoint Question 5.12.2 in Section 5.12 for such a case).
while (loop-continuation-condition) { // Loop body }
(a)
for ( ; loop-continuation-condition; ) { // Loop body }
(b)
Equivalent
Equivalent
for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; }
(a)
initial-action; while (loop-continuation-condition) { // Loop body; action-after-each-iteration; }
(b)
M05_LIAN1878_11_GE_C05.indd 198 1/2/18 11:16 PM
5.8 Which Loop to Use? 199
Use the loop statement that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known in advance, as, for example, when you need to display a message a hundred times. A while loop may be used if the number of repetitions is not fixed, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before the continuation condition is tested.
Caution Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below in (a). In (a), the semicolon signifies the end of the loop prematurely. The loop body is actually empty, as shown in (b). (a) and (b) are equivalent. Both are incorrect.
Similarly, the loop in (c) is also wrong. (c) is equivalent to (d). Both are incorrect.
for (int i = 0; i < 10; i++); { System.out.println("i is " + i); }
(a)
Error
for (int i = 0; i < 10; i++) { }; { System.out.println("i is " + i); }
(b)
Empty body
Error Empty body
These errors often occur when you use the next-line block style. Using the end-of-line block style can avoid errors of this type.
In the case of the do-while loop, the semicolon is needed to end the loop.
int i = 0; while (i < 10); { System.out.println("i is " + i); i++; }
int i = 0; while (i < 10) { }; { System.out.println("i is " + i); i++; }
(c) (d)
int i = 0; do { System.out.println("i is " + i); i++; } while (i < 10);
Correct
5.8.1 Can you convert a for loop to a while loop? List the advantages of using for loops. 5.8.2 Can you always convert a while loop into a for loop? Convert the following
while loop into a for loop:
int i = 1; int sum = 0; while (sum < 10000) { sum = sum + i; i++; }
Point Check
M05_LIAN1878_11_GE_C05.indd 199 1/2/18 11:16 PM
200 Chapter 5 Loops
5.9 Nested Loops A loop can be nested inside another loop.
Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started anew.
Listing 5.7 presents a program that uses nested for loops to display a multiplication table.
Listing 5.7 MultiplicationTable.java 1 public class MultiplicationTable { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Display the table heading 5 System.out.println(" Multiplication Table"); 6 7 // Display the number title 8 System.out.print(" "); 9 for (int j = 1; j <= 9; j++) 10 System.out.print(" " + j);
Point Key
table title
nested loop
5.8.4 What is wrong with the following programs?
5.8.3 Identify and fix the errors in the following code: 1 public class Test { 2 public void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 sum += i; 5 6 if (i < j); 7 System.out.println(i) 8 else 9 System.out.println(j); 10 11 while (j < 10); 12 { 13 j++; 14 } 15 16 do { 17 j++; 18 } while (j < 10) 19 } 20 }
1 public class ShowErrors { 2 public static void main(String[] args) { 3 int i = 0; 4 do { 5 System.out.println(i + 4); 6 i++; 7 } 8 while (i < 10) 8 } 9 }
1 public class ShowErrors { 2 public static void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 System.out.println(i + 4); 5 } 6 }
(b)(a)
M05_LIAN1878_11_GE_C05.indd 200 1/2/18 11:16 PM
5.9 Nested Loops 201
11 12 System.out.println("\n — — — — — — — — — — — — — — — —— — — — — — — — — — — — —"); 13 14 // Display table body 15 for (int i = 1; i <= 9; i++) { 16 System.out.print(i + " | "); 17 for (int j = 1; j <= 9; j++) { 18 // Display the product and align properly 19 System.out.printf("%4d", i * j); 20 } 21 System.out.println(); 22 } 23 } 24 }
outer loop
inner loop
Multiplication Table
1 2 3 4 5 6 7 8 9
1 | 1 2 3 4 5 6 7 8 9 2 | 2 4 6 8 10 12 14 16 18 3 | 3 6 9 12 15 18 21 24 27 4 | 4 8 12 16 20 24 28 32 36 5 | 5 10 15 20 25 30 35 40 45 6 | 6 12 18 24 30 36 42 48 54 7 | 7 14 21 28 35 42 49 56 63 8 | 8 16 24 32 40 48 56 64 72 9 | 9 18 27 36 45 54 63 72 81
The program displays a title (line 5) on the first line in the output. The first for loop (lines 9 and 10) displays the numbers 1–9 on the second line. A dashed (–) line is displayed on the third line (line 12).
The next loop (lines 15–22) is a nested for loop with the control variable i in the outer loop and j in the inner loop. For each i, the product i * j is displayed on a line in the inner loop, with j being 1, 2, 3, …, 9.
Note Be aware that a nested loop may take a long time to run. Consider the following loop nested in three levels:
for (int i = 0; i < 10000; i++) for (int j = 0; j < 10000; j++) for (int k = 0; k < 10000; k++) Perform an action
The action is performed one trillion times. If it takes 1 microsecond to perform the action, the total time to run the loop would be more than 277 hours. Note 1 microsecond is one-millionth (10-6) of a second.
5.9.1 How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)
Point Check
M05_LIAN1878_11_GE_C05.indd 201 1/2/18 11:16 PM
202 Chapter 5 Loops
5.9.2 Show the output of the following programs. (Hint: Draw a table and list the vari- ables in the columns to trace these programs.)
public class Test { public static void main(String[] args) { for (int i = 1; i < 5; i++) { int j = 0; while (j < i) { System.out.print(j + " "); j++; } } } }
(a)
public class Test { public static void main(String[] args) { int i = 0; while (i < 5) { for (int j = i; j > 1; j––) System.out.print(j + " "); System.out.println("****"); i++; } } }
(b)
(c)
public class Test { public static void main(String[] args) { int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "xxx"); num *= 2; } System.out.println(); i--; } } }
public class Test { public static void main(String[] args) { int i = 1; do { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "G"); num += 2; }
System.out.println(); i++; } while (i <= 5); } }
(d)
5.10 Minimizing Numeric Errors Using floating-point numbers in the loop continuation condition may cause numeric errors.
Numeric errors involving floating-point numbers are inevitable, because floating-point num- bers are represented in approximation in computers by nature. This section discusses how to minimize such errors through an example.
Listing 5.8 presents an example summing a series that starts with 0.01 and ends with 1.0. The numbers in the series will increment by 0.01, as follows: 0.01 + 0.02 + 0.03, and so on.
Listing 5.8 TestSum.java 1 public class TestSum { 2 public static void main(String[] args) { 3 // Initialize sum 4 float sum = 0; 5 6 // Add 0.01, 0.02, ..., 0.99, 1 to sum 7 for (float i = 0.01f; i <= 1.0f; i = i + 0.01f) 8 sum += i; 9 10 // Display result
Point Key
VideoNote
Minimize numeric errors
loop
M05_LIAN1878_11_GE_C05.indd 202 1/2/18 11:16 PM
5.10 Minimizing Numeric Errors 203
The for loop (lines 7 and 8) repeatedly adds the control variable i to sum. This variable, which begins with 0.01, is incremented by 0.01 after each iteration. The loop terminates when i exceeds 1.0.
The for loop initial action can be any statement, but it is often used to initialize a control variable. From this example, you can see a control variable can be a float type. In fact, it can be any data type.
The exact sum should be 50.50, but the answer is 50.499985. The result is imprecise because computers use a fixed number of bits to represent floating-point numbers, and thus they cannot represent some floating-point numbers exactly. If you change float in the pro- gram to double, as follows, you should see a slight improvement in precision, because a double variable holds 64 bits, whereas a float variable holds 32 bits.
// Initialize sum double sum = 0;
// Add 0.01, 0.02, ..., 0.99, 1 to sum for (double i = 0.01; i <= 1.0; i = i + 0.01) sum += i;
However, you will be stunned to see the result is actually 49.50000000000003. What went wrong? If you display i for each iteration in the loop, you will see that the last i is slightly larger than 1 (not exactly 1). This causes the last i not to be added into sum. The fundamental problem is the floating-point numbers are represented by approximation. To fix the problem, use an integer count to ensure all the numbers are added to sum. Here is the new loop:
double currentValue = 0.01;
for (int count = 0; count < 100; count++) { sum += currentValue; currentValue += 0.01; }
After this loop, sum is 50.50000000000003. This loop adds the numbers from smallest to biggest. What happens if you add numbers from biggest to smallest (i.e., 1.0, 0.99, 0.98, . . . , 0.02, 0.01 in this order) is as follows:
double currentValue = 1.0;
for (int count = 0; count < 100; count++) { sum += currentValue; currentValue –= 0.01; }
After this loop, sum is 50.49999999999995. Adding from biggest to smallest is less accurate than adding from smallest to biggest. This phenomenon is an artifact of the finite-precision arithmetic. Adding a very small number to a very big number can have no effect if the result requires more precision than the variable can store. For example, the inaccurate result of 100000000.0 + 0.000000001 is 100000000.0. To obtain more accurate results, carefully select the order of computation. Adding smaller numbers before bigger numbers to sum is one way to minimize errors.
double precision
numeric error
avoiding numeric error
The sum is 50.499985
11 System.out.println("The sum is " + sum); 12 } 13 }
M05_LIAN1878_11_GE_C05.indd 203 1/2/18 11:16 PM
204 Chapter 5 Loops
5.11 Case Studies Loops are fundamental in programming. The ability to write loops is essential in learning Java programming.
If you can write programs using loops, you know how to program! For this reason, this section presents three additional examples of solving problems using loops.
5.11.1 Case Study: Finding the Greatest Common Divisor The greatest common divisor (gcd) of the two integers 4 and 2 is 2. The greatest common divisor of the two integers 16 and 24 is 8. How would you write this program to find the great- est common divisor? Would you immediately begin to write the code? No. It is important to think before you code. Thinking enables you to generate a logical solution for the problem without concern about how to write the code.
Let the two input integers be n1 and n2. You know that number 1 is a common divisor, but it may not be the greatest common divisor. Therefore, you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2. Store the common divisor in a variable named gcd. Initially, gcd is 1. Whenever a new common divisor is found, it becomes the new gcd. When you have checked all the possible common divisors from 2 up to n1 or n2, the value in variable gcd is the greatest common divisor.
Once you have a logical solution, type the code to translate the solution into a Java program as follows:
int gcd = 1; // Initial gcd is 1 int k = 2; // Possible gcd
while (k <= n1 && k <= n2) { if (n1 % k == 0 && n2 % k == 0) gcd = k; // Update gcd k++; // Next possible gcd }
// After the loop, gcd is the greatest common divisor for n1 and n2
Listing 5.9 presents the program that prompts the user to enter two positive integers and finds their greatest common divisor.
Listing 5.9 GreatestCommonDivisor.java 1 import java.util.Scanner; 2 3 public class GreatestCommonDivisor { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter two integers 10 System.out.print("Enter first integer: "); 11 int n1 = input.nextInt(); 12 System.out.print("Enter second integer: "); 13 int n2 = input.nextInt(); 14 15 int gcd = 1; // Initial gcd is 1 16 int k = 2; // Possible gcd 17 while (k <= n1 && k <= n2) { 18 if (n1 % k == 0 && n2 % k == 0) 19 gcd = k; // Update gcd 20 k++;
Point Key
gcd
think before you code
logical solution
check divisor
gcd
input
input
M05_LIAN1878_11_GE_C05.indd 204 1/2/18 11:16 PM
5.11 Case Studies 205
21 } 22 23 System.out.println("The greatest common divisor for " + n1 + 24 " and " + n2 + " is " + gcd); 25 } 26 }
output
Enter first integer: 125
Enter second integer: 2525 The greatest common divisor for 125 and 2525 is 25
Translating a logical solution to Java code is not unique. For example, you could use a for loop to rewrite the code as follows:
for (int k = 2; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; }
A problem often has multiple solutions, and the gcd problem can be solved in many ways. Programming Exercise 5.14 suggests another solution. A more efficient solution is to use the classic Euclidean algorithm (see Section 22.6).
You might think that a divisor for a number n1 cannot be greater than n1 / 2 and would attempt to improve the program using the following loop:
for (int k = 2; k <= n1 / 2 && k <= n2 / 2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; }
This revision is wrong. Can you find the reason? See Checkpoint Question 5.11.1 for the answer.
5.11.2 Case Study: Predicting the Future Tuition Suppose the tuition for a university is $10,000 this year and tuition increases 7% every year. In how many years will the tuition be doubled?
Before you can write a program to solve this problem, first consider how to solve it by hand. The tuition for the second year is the tuition for the first year * 1.07. The tuition for a future year is the tuition of its preceding year * 1.07. Thus, the tuition for each year can be computed as follows:
double tuition = 10000; int year = 0; // Year 0 tuition = tuition * 1.07; year++; // Year 1 tuition = tuition * 1.07; year++; // Year 2 tuition = tuition * 1.07; year++; // Year 3 ...
Keep computing the tuition for a new year until it is at least 20000. By then, you will know how many years it will take for the tuition to be doubled. You can now translate the logic into the following loop:
double tuition = 10000; // Year 0 int year = 0; while (tuition < 20000) { tuition = tuition * 1.07; year++; }
The complete program is given in Listing 5.10.
think before you type
multiple solutions
erroneous solutions
think before you code
M05_LIAN1878_11_GE_C05.indd 205 1/2/18 11:16 PM
206 Chapter 5 Loops
Listing 5.10 FutureTuition.java 1 public class FutureTuition { 2 public static void main(String[] args) { 3 double tuition = 10000; // Year 0 4 int year = 0; 5 while (tuition < 20000) { 6 tuition = tuition * 1.07; 7 year++; 8 } 9 10 System.out.println("Tuition will be doubled in " 11 + year + " years"); 12 System.out.printf("Tuition will be $%.2f in %1d years", 13 tuition, year); 14 } 15 }
loop next year’s tuition
Tuition will be doubled in 11 years
Tuition will be $21048.52 in 11 years
Remainder
h0
Quotient
16 123
112 11
7
h1
16 7
0 7
0
The while loop (lines 5–8) is used to repeatedly compute the tuition for a new year. The loop terminates when the tuition is greater than or equal to 20000.
5.11.3 Case Study: Converting Decimals to Hexadecimals Hexadecimals are often used in computer systems programming (see Appendix F for an intro- duction to number systems). How do you convert a decimal number to a hexadecimal number? To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits hn, hn - 1, hn - 2, c , h2, h1, and h0 such that
d = hn * 16n + hn - 1 * 16n - 1 + hn - 2 * 16n - 2 + g + h2 * 162 + h1 * 161 + h0 * 160
These hexadecimal digits can be found by successively dividing d by 16 until the quotient is 0. The remainders are h0, h1, h2, c , hn - 2, hn - 1, and hn. The hexadecimal digits include the decimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, plus A, which is the decimal value 10; B, which is the decimal value 11; C, which is 12; D, which is 13; E, which is 14; and F, which is 15.
For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows. Divide 123 by 16. The remainder is 11 (B in hexadecimal) and the quotient is 7. Continue to divide 7 by 16. The remainder is 7 and the quotient is 0. Therefore, 7B is the hexadecimal number for 123.
Listing 5.11 gives a program that prompts the user to enter a decimal number and converts it into a hex number as a string.
M05_LIAN1878_11_GE_C05.indd 206 1/2/18 11:16 PM
5.11 Case Studies 207
Listing 5.11 Dec2Hex.java 1 import java.util.Scanner; 2 3 public class Dec2Hex { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter a decimal integer 10 System.out.print("Enter a decimal number: "); 11 int decimal = input.nextInt(); 12 13 // Convert decimal to hex 14 String hex = ""; 15 16 while (decimal != 0) { 17 int hexValue = decimal % 16; 18 19 // Convert a decimal value to a hex digit 20 char hexDigit = (0 <= hexValue && hexValue <= 9) ? 21 (char)(hexValue + '0') : (char)(hexValue – 10 + 'A'); 22 23 hex = hexDigit + hex; 24 decimal = decimal / 16; 25 } 26 27 System.out.println("The hex number is " + hex); 28 } 29 }
get a hex char
decimal to hex
input decimal
add to hex string
Enter a decimal number: 1234 The hex number is 4D2
line# decimal hex hexValue hexDigit
14 1234 ""
iteration 1 c 17 223 "2" 2 24 77
iteration 2 c 17 1323 "D2" D 24 4
iteration 3 c 17 423 "4D2" 4 24 0
The program prompts the user to enter a decimal integer (line 11), converts it to a hex num- ber as a string (lines 14–25), and displays the result (line 27). To convert a decimal to a hex number, the program uses a loop to successively divide the decimal number by 16 and obtain its remainder (line 17). The remainder is converted into a hex character (lines 20 and 21). The character is then appended to the hex string (line 23). The hex string is initially empty (line 14). Divide the decimal number by 16 to remove a hex digit from the number (line 24). The loop ends when the remaining decimal number becomes 0.
M05_LIAN1878_11_GE_C05.indd 207 1/2/18 11:16 PM
208 Chapter 5 Loops
The program converts a hexValue between 0 and 15 into a hex character. If hexValue is between 0 and 9, it is converted to (char)(hexValue +'0') (line 21). Recall that when add- ing a character with an integer, the character’s Unicode is used in the evaluation. For example, if hexValue is 5, (char)(hexValue + '0') returns 5. Similarly, if hexValue is between 10 and 15, it is converted to (char)(hexValue – 10 + 'A') (line 21). For instance, if hexValue is 11, (char),(hexValue – 10 + 'A') returns B.
5.11.1 Will the program work if n1 and n2 are replaced by n1 / 2 and n2 / 2 in line 17 in Listing 5.9?
5.11.2 In Listing 5.11, why is it wrong if you change the code (char)(hexValue + '0') to hexValue + '0' in line 21?
5.11.3 In Listing 5.11, how many times the loop body is executed for a decimal number 245, and how many times the loop body is executed for a decimal number 3245?
5.11.4 What is the hex number after E? What is the hex number after F? 5.11.5 Revise line 27 in Listing 5.11 so the program displays hex number 0 if the input
decimal is 0.
5.12 Keywords break and continue The break and continue keywords provide additional controls in a loop.
Pedagogical Note Two keywords, break and continue, can be used in loop statements to provide additional controls. Using break and continue can simplify programming in some cases. Overusing or improperly using them, however, can make programs difficult to read and debug. (Note to instructors: You may skip this section without affecting students’ understanding of the rest of the book.)
You have used the keyword break in a switch statement. You can also use break in a loop to immediately terminate the loop. Listing 5.12 presents a program to demonstrate the effect of using break in a loop.
Listing 5.12 TestBreak.java 1 public class TestBreak { 2 public static void main(String[] args) { 3 int sum = 0; 4 int number = 0; 5 6 while (number < 20) { 7 number++; 8 sum += number; 9 if (sum >= 100) 10 break; 11 } 12 13 System.out.println("The number is " + number); 14 System.out.println("The sum is " + sum); 15 } 16 }
Point Check
Point Key
break statement
break
The number is 14
The sum is 105
M05_LIAN1878_11_GE_C05.indd 208 1/2/18 11:16 PM
5.12 Keywords break and continue 209
The program in Listing 5.12 adds integers from 1 to 20 in this order to sum until sum is greater than or equal to 100. Without the if statement (line 9), the program calculates the sum of the numbers from 1 to 20. However, with the if statement, the loop terminates when sum becomes greater than or equal to 100. Without the if statement, the output would be as follows:
The number is 20
The sum is 210
The sum is 189
The program in Listing 5.13 adds integers from 1 to 20 except 10 and 11 to sum. With the if statement in the program (line 8), the continue statement is executed when number becomes 10 or 11. The continue statement ends the current iteration so that the rest of the statement in the loop body is not executed; therefore, number is not added to sum when it is 10 or 11. Without the if statement in the program, the output would be as follows:
You can also use the continue keyword in a loop. When it is encountered, it ends the current iteration and program control goes to the end of the loop body. In other words, con- tinue breaks out of an iteration, while the break keyword breaks out of a loop. Listing 5.13 presents a program to demonstrate the effect of using continue in a loop.
Listing 5.13 TestContinue.java 1 public class TestContinue { 2 public static void main(String[] args) { 3 int sum = 0; 4 int number = 0; 5 6 while (number < 20) { 7 number++; 8 if (number == 10 || number == 11) 9 continue; 10 sum += number; 11 } 12 13 System.out.println("The sum is " + sum); 14 } 15 }
continue statement
continue
The sum is 210
In this case, all of the numbers are added to sum, even when number is 10 or 11. Therefore, the result is 210, which is 21 more than it was with the if statement.
Note The continue statement is always inside a loop. In the while and do-while loops, the loop-continuation-condition is evaluated immediately after the continue statement. In the for loop, the action-after-each-iteration is performed, then the loop-continuation-condition is evaluated immediately after the continue statement.
M05_LIAN1878_11_GE_C05.indd 209 1/2/18 11:16 PM
210 Chapter 5 Loops
Note Some programming languages have a goto statement. The goto statement indiscrimi- nately transfers control to any statement in the program and executes it. This makes your program vulnerable to errors. The break and continue statements in Java are different from goto statements. They operate only in a loop or a switch statement. The break statement breaks out of the loop, and the continue statement breaks out of the current iteration in the loop.
You can always write a program without using break or continue in a loop (see Check- Point Question 5.12.3). In general, though, using break and continue is appropriate if it simplifies coding and makes programs easier to read.
Suppose you need to write a program to find the smallest factor other than 1 for an integer n (assume n >= 2). You can write a simple and intuitive code using the break statement as follows:
int factor = 2; while (factor <= n) { if (n % factor == 0) break; factor++; } System.out.println("The smallest factor other than 1 for " + n + " is " + factor);
You may rewrite the code without using break as follows:
boolean found = false; int factor = 2; while (factor <= n && !found) { if (n % factor == 0) found = true; else factor++; } System.out.println("The smallest factor other than 1 for " + n + " is " + factor);
Obviously, the break statement makes this program simpler and easier to read in this case. However, you should use break and continue with caution. Too many break and continue statements will produce a loop with many exit points and make the program dif- ficult to read.
Note Programming is a creative endeavor. There are many different ways to write code. In fact, you can find a smallest factor using a rather simple code as follows:
int factor = 2; while (n % factor != 0) factor++; or for (int factor = 2; n % factor != 0; factor++);
The code here finds the smallest factor for an integer n. Programming Exercise 5.16 writes a program that finds all smallest factors in n.
5.12.1 What is the keyword break for? What is the keyword continue for? Will the fol- lowing programs terminate? If so, give the output.
goto
Point Check
M05_LIAN1878_11_GE_C05.indd 210 1/2/18 11:16 PM
5.13 Case Study: Checking Palindromes 211
5.12.2 The for loop on the left is converted into the while loop on the right. What is wrong? Correct it.
int balance = 10; while (true) { if (balance < 9) break; balance = balance – 9; }
System.out.println("Balance is " + balance);
(a)
int balance = 10; while (true) { if (balance < 9) continue; balance = balance – 9; }
System.out.println("Balance is " + balance);
(b)
int sum = 0; for (int i = 0; i < 4; i++) { if (i % 3 == 0) continue; sum += i; }
Converted
Wrong conversion
int i = 0, sum = 0; while (i < 4) { if (i % 3 == 0) continue; sum += i; i++; }
5.12.3 Rewrite the programs TestBreak and TestContinue in Listings 5.12 and 5.13 without using break and continue.
5.12.4 After the break statement in (a) is executed in the following loop, which state- ment is executed? Show the output. After the continue statement in (b) is exe- cuted in the following loop, which statement is executed? Show the output.
for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { if (i * j > 2) break;
System.out.println(i * j); }
System.out.println(i); }
(a)
for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { if (i * j > 2) continue;
System.out.println(i * j); }
System.out.println(i); }
(b)
5.13 Case Study: Checking Palindromes This section presents a program that checks whether a string is a palindrome.
A string is a palindrome if it reads the same forward and backward. The words “mom,” “dad,” and “noon,” for instance, are all palindromes.
The problem is to write a program that prompts the user to enter a string and reports whether the string is a palindrome. One solution is to check whether the first character in the string is the same as the last character. If so, check whether the second character is the same as the second-to-last
Point Key
M05_LIAN1878_11_GE_C05.indd 211 1/2/18 11:16 PM
212 Chapter 5 Loops
character. This process continues until a mismatch is found or all the characters in the string are checked, except for the middle character if the string has an odd number of characters.
Listing 5.14 gives the program.
Listing 5.14 Palindrome.java 1 import java.util.Scanner; 2 3 public class Palindrome { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter a string 10 System.out.print("Enter a string: "); 11 String s = input.nextLine(); 12 13 // The index of the first character in the string 14 int low = 0; 15 16 // The index of the last character in the string 17 int high = s.length() – 1; 18 19 boolean isPalindrome = true; 20 while (low < high) { 21 if (s.charAt(low) != s.charAt(high)) { 22 isPalindrome = false; 23 break; 24 } 25 26 low++; 27 high––; 28 } 29 30 if (isPalindrome) 31 System.out.println(s + " is a palindrome"); 32 else 33 System.out.println(s + " is not a palindrome"); 34 } 35 }
think before you code
high index
low index
input string
update indices
Enter a string: noon
noon is a palindrome
Enter a string: abcdefgnhgfedcba
abcdefgnhgfedcba is not a palindrome
String s
low
a b c d e f g n h g f e d c b a
high
The program uses two variables, low and high, to denote the positions of the two characters at the beginning and the end in a string s (lines 14 and 17), as shown in the following figure.
M05_LIAN1878_11_GE_C05.indd 212 1/2/18 11:16 PM
5.14 Case Study: Displaying Prime Numbers 213
Initially, low is 0 and high is s.length() – 1. If the two characters at these positions match, increment low by 1 and decrement high by 1 (lines 26–27). This process continues until (low >= high) or a mismatch is found (line 21).
The program uses a boolean variable isPalindrome to denote whether the string s is a palindrome. Initially, it is set to true (line 19). When a mismatch is discovered (line 21), isPal- indrome is set to false (line 22) and the loop is terminated with a break statement (line 23).
5.13.1 What happens to the program if (low < high) in line 20 is changed to (low <= high)?
5.14 Case Study: Displaying Prime Numbers This section presents a program that displays the first 50 prime numbers in 5 lines, each containing 10 numbers.
An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not.
The problem is to display the first 50 prime numbers in 5 lines, each of which contains 10 numbers. The problem can be broken into the following tasks:
■■ Determine whether a given number is prime.
■■ For number = 2, 3, 4, 5, 6, …, test whether it is prime.
■■ Count the prime numbers.
■■ Display each prime number and display 10 numbers per line.
Obviously, you need to write a loop and repeatedly test whether a new number is prime. If the number is prime, increase the count by 1. The count is 0 initially. When it reaches 50, the loop terminates.
Here is the algorithm for the problem:
Set the number of prime numbers to be printed as a constant NUMBER_OF_PRIMES; Use count to track the number of prime numbers and set an initial count to 0; Set an initial number to 2;
while (count < NUMBER_OF_PRIMES) { Test whether number is prime; if number is prime { Display the prime number and increase the count; }
Increment number by 1; }
To test whether a number is prime, check whether it is divisible by 2, 3, 4, and so on up to number/2. If a divisor is found, the number is not a prime. The algorithm can be described as follows:
Use a boolean variable isPrime to denote whether the number is prime; Set isPrime to true initially;
for (int divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) { Set isPrime to false Exit the loop; } }
Point Check
Point Key
M05_LIAN1878_11_GE_C05.indd 213 1/2/18 11:16 PM
214 Chapter 5 Loops
The complete program is given in Listing 5.15.
Listing 5.15 PrimeNumber.java 1 public class PrimeNumber { 2 public static void main(String[] args) { 3 final int NUMBER_OF_PRIMES = 50; // Number of primes to display 4 final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line 5 int count = 0; // Count the number of prime numbers 6 int number = 2; // A number to be tested for primeness 7 8 System.out.println("The first 50 prime numbers are \n"); 9 10 // Repeatedly find prime numbers 11 while (count < NUMBER_OF_PRIMES) { 12 // Assume the number is prime 13 boolean isPrime = true; // Is the current number prime? 14 15 // Test whether number is prime 16 for (int divisor = 2; divisor <= number / 2; divisor++) { 17 if (number % divisor == 0) { // If true, number is not prime 18 isPrime = false; // Set isPrime to false 19 break; // Exit the for loop 20 } 21 } 22 23 // Display the prime number and increase the count 24 if (isPrime) { 25 count++; // Increase the count 26 27 if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { 28 // Display the number and advance to the new line 29 System.out.println(number); 30 } 31 else 32 System.out.print(number + " "); 33 } 34 35 // Check if the next number is prime 36 number++; 37 } 38 } 39 }
count prime numbers
check primeness
exit loop
display if prime
The first 50 prime numbers are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
This is a complex program for novice programmers. The key to developing a programmatic solution for this problem, and for many other problems, is to break it into subproblems and develop solutions for each of them in turn. Do not attempt to develop a complete solution in the first trial. Instead, begin by writing the code to determine whether a given number is prime, then expand the program to test whether other numbers are prime in a loop.
To determine whether a number is prime, check whether it is divisible by a number between 2 and number/2 inclusive (lines 16–21). If so, it is not a prime number (line 18); otherwise, it is a prime number. For a prime number, display it (lines 27–33). If the count is divisible by 10,
subproblem
M05_LIAN1878_11_GE_C05.indd 214 1/2/18 11:16 PM
display the number followed by a newline (lines 27–30). The program ends when the count reaches 50.
The program uses the break statement in line 19 to exit the for loop as soon as the number is found to be a nonprime. You can rewrite the loop (lines 16–21) without using the break statement, as follows:
for (int divisor = 2; divisor <= number / 2 && isPrime; divisor++) { // If true, the number is not prime if (number % divisor == 0) { // Set isPrime to false, if the number is not prime isPrime = false; } }
However, using the break statement makes the program simpler and easier to read in this case. Prime numbers have many applications in computer science. Section 22.7 will study several
efficient algorithms for finding prime numbers.
5.14.1 Simplify the code in lines 27–32 using a conditional operator. Point Check
Key Terms break statement 208 continue statement 209 do-while loop 192 for loop 195 infinite loop 184 input redirection 192 iteration 182 loop 182
loop body 182 nested loop 200 off-by-one error 184 output redirection 192 posttest loop 198 pretest loop 198 sentinel value 190 while loop 182
ChapTer summary 1. There are three types of repetition statements: the while loop, the do-while loop, and
the for loop.
2. The part of the loop that contains the statements to be repeated is called the loop body.
3. A one-time execution of a loop body is referred to as an iteration of the loop.
4. An infinite loop is a loop statement that executes infinitely.
5. In designing loops, you need to consider both the loop control structure and the loop body.
6. The while loop checks the loop-continuation-condition first. If the condition is true, the loop body is executed; if it is false, the loop terminates.
7. The do-while loop is similar to the while loop, except the do-while loop executes the loop body first then checks the loop-continuation-condition to decide whether to continue or to terminate.
8. The while loop and the do-while loop often are used when the number of repetitions is not predetermined.
Chapter Summary 215
M05_LIAN1878_11_GE_C05.indd 215 1/2/18 11:16 PM
216 Chapter 5 Loops
9. A sentinel value is a special value that signifies the end of the loop.
10. The for loop generally is used to execute a loop body a fixed number of times.
11. The for loop control has three parts. The first part is an initial action that often initializes a control variable. The second part, the loop-continuation-condition, determines whether the loop body is to be executed. The third part is executed after each iteration and is often used to adjust the control variable. Usually, the loop control variables are initialized and changed in the control structure.
12. The while loop and for loop are called pretest loops because the continuation condition is checked before the loop body is executed.
13. The do-while loop is called a posttest loop because the condition is checked after the loop body is executed.
14. Two keywords break and continue can be used in a loop.
15. The break keyword immediately ends the innermost loop, which contains the break.
16. The continue keyword only ends the current iteration.
Quiz Answer the quiz for this chapter online at the Companion Website.
programming exerCises
Pedagogical Note Read each problem several times until you understand it. Think how to solve the problem before starting to write code. Translate your logic into a program.
A problem often can be solved in many different ways. Students are encouraged to explore various solutions.
Sections 5.2–5.7 *5.1 (Pass or fail) Write a program that prompts a student to enter a Java score. If the
score is greater or equal to 60, display “you pass the exam”; otherwise, display “you don’t pass the exam”. Your program ends with input -1. Here is a sample run:
read and think before coding
explore solutions
Enter your score: 80 You pass the exam.
Enter your score: 59 You don't pass the exam.
Enter your score: −1
No numbers are entered except 0
M05_LIAN1878_11_GE_C05.indd 216 1/2/18 11:16 PM
5.2 (Repeat multiplications) Listing 5.4, SubtractionQuizLoop.java, generates five random subtraction questions. Revise the program to generate ten random multiplication ques- tions for two integers between 1 and 12. Display the correct count and test time.
5.3 (Conversion from Cº to Fº) Write a program that displays the following table (note that farenheit = celsius * 9/5 + 32):
Celsius Fahrenheit 0 32.0 2 35.6 ... 98 208.4 100 212.0
5.4 (Conversion from inch to centimeter) Write a program that displays the following table (note that 1 inch is 2.54 centimeters):
Inches Centimetres 1 2.54 2 5.08 ... 9 22.86 10 25.4
5.5 (Conversion from Cº to Fº and Fº to Cº) Write a program that displays the following two tables side by side:
Celsius Fahrenheit | Fahrenheit Celsius 0 32.000 | 20 −6.667 2 35.600 | 25 −3.889 ... 98 208.400 | 265 129.444 100 212.000 | 270 132.222
5.6 (Conversion from square meter to ping) Write a program that displays the following two tables side by side (note that 1 ping = 3.305 square meters):
Ping Square meter | Square meter Ping 10 33.050 | 30 9.077 15 49.575 | 35 10.590 ... 75 247.875 | 95 28.744 80 264.400 | 100 30.257
**5.7 (Financial application: compute future tuition) Suppose that the tuition for a uni- versity is $10,000 this year and increases 6% every year. In one year, the tuition will be $10,600. Write a program that computes the tuition in ten years and the total cost of four years’ worth of tuition after the tenth year.
5.8 (Find the highest score) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name of the student with the highest score. Use the next() method in the Scanner class to read a name, rather than using the nextLine() method.
*5.9 (Find the two lowest scores) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the names of the students with the lowest and second-lowest scores.
Programming Exercises 217
M05_LIAN1878_11_GE_C05.indd 217 1/2/18 11:16 PM
218 Chapter 5 Loops
5.10 (Find numbers divisible by 3 and 4) Write a program that displays all the numbers from 100 to 1,000, ten per line, that are divisible by 3 and 4. Numbers are separated by exactly one space.
5.11 (Find numbers divisible by 3 or 4, but not both) Write a program that displays all the numbers from 100 to 200, ten per line, that are divisible by 3 or 4, but not both. Numbers are separated by exactly one space.
5.12 (Find the smallest n such that n3 7 12,000) Use a while loop to find the smallest integer n such that n3 is greater than 12,000.
5.13 (Find the largest n such that n2 6 12,000) Use a while loop to find the largest integer n such that n2 is less than 12,000.
Sections 5.8–5.10 *5.14 (Compute the greatest common divisor) Another solution for Listing 5.9 to find the
greatest common divisor of two integers n1 and n2 is as follows: First find d to be the minimum of n1 and n2, then check whether d, d–1, d–2, …, 2, or 1 is a divi- sor for both n1 and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2. Write a program that prompts the user to enter two positive integers and displays the gcd.
*5.15 (Display the ASCII character table) Write a program that prints the characters in the ASCII character table from ! to ~. Display 10 characters per line. The ASCII table is given in Appendix B. Characters are separated by exactly one space.
*5.16 (Find the factors of an integer) Write a program that reads an integer and displays all its smallest factors in an increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
** 5.17 (Display pyramid) Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run:
Enter the number of lines: 7
7 6 5 4 3 2 1 2 3 4 5 6 7
6 5 4 3 2 1 2 3 4 5 6
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
*5.18 (Display four patterns using loops) Use nested loops that display the following patterns in four separate programs:
Pattern A Pattern B Pattern C Pattern D
* * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * *
M05_LIAN1878_11_GE_C05.indd 218 1/2/18 11:16 PM
**5.19 (Display numbers in a pyramid pattern) Write a nested for loop that prints the following output: 1
1 3 1
1 3 9 3 1
1 3 9 27 9 3 1
1 3 9 27 81 27 9 3 1
1 3 9 27 81 243 81 27 9 3 1
1 3 9 27 81 243 729 243 81 27 9 3 1
1 3 9 27 81 243 729 2187 729 243 81 27 9 3 1
*5.20 (Display prime numbers between 2 and 1,200) Modify Listing 5.15 to display all the prime numbers between 2 and 1,200, inclusive. Display eight prime numbers per line. Numbers are separated by exactly one space.
Comprehensive ** 5.21 (Financial application: compare loans with various interest rates) Write a program
that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 10%, with an increment of 1/4. Here is a sample run:
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.000% 188.71 11322.74
5.250% 189.86 11391.59
5.500% 191.01 11460.70
...
9.750% 211.24 12674.55
10.000% 212.47 12748.23
For the formula to compute monthly payment, see Listing 2.9, ComputeLoan.java.
** 5.22 (Financial application: loan amortization schedule) The monthly payment for a given loan pays the principal and the interest. The monthly interest is computed by multiply- ing the monthly interest rate and the balance (the remaining principal). The principal paid for the month is therefore the monthly payment minus the monthly interest. Write a program that lets the user enter the loan amount, number of years, and interest rate then displays the amortization schedule for the loan. Here is a sample run:
VideoNote
Display loan schedule
Loan Amount: 10000
Number of Years: 1
Annual Interest Rate: 7
Monthly Payment: 865.26
Total Payment: 10383.21
Payment# Interest Principal Balance 1 58.33 806.93 9193.07 2 53.62 811.64 8381.43 ... 11 10.00 855.26 860.27 12 5.01 860.25 0.01
Programming Exercises 219
M05_LIAN1878_11_GE_C05.indd 219 1/2/18 11:16 PM
220 Chapter 5 Loops
Note The balance after the last payment may not be zero. If so, the last payment should be the normal monthly payment plus the final balance.
Hint: Write a loop to display the table. Since the monthly payment is the same for each month, it should be computed before the loop. The balance is initially the loan amount. For each iteration in the loop, compute the interest and principal, and update the balance. The loop may look as follows:
for (i = 1; i <= numberOfYears * 12; i++) { interest = monthlyInterestRate * balance; principal = monthlyPayment – interest; balance = balance – principal; System.out.println(i + "\t\t" + interest + "\t\t" + principal + "\t\t" + balance); }
*5.23 (Demonstrate cancellation errors) A cancellation error occurs when you are manipulating a very large number with a very small number. The large number may cancel out the smaller number. For example, the result of 100000000.0 + 0.000000001 is equal to 100000000.0. To avoid cancellation errors and obtain more accurate results, carefully select the order of computation. For example, in computing the following summation, you will obtain more accurate results by computing from right to left rather than from left to right:
1 + 1 2
+ 1 3
+ c + 1 n
Write a program that compares the results of the summation of the preceding series, computing from left to right and from right to left with n = 50000.
*5.24 (Sum a series) Write a program to compute the following summation:
1 3
+ 3 5
+ 5 7
+ 7 9
+ 9 11
+ 11 13
+ g + 95 97
+ 97 99
** 5.25 (Compute p) You can approximate p by using the following summation:
p = 4 a1 - 1 3
+ 1 5
- 1 7
+ 1 9
- 1 11
+ g + (-)i+ 1
2i - 1 b
Write a program that displays the p value for i = 10000, 20000, …, and 100000.
** 5.26 (Compute e) You can approximate e using the following summation:
e = 1 + 1 1!
+ 1 2!
+ 1 3!
+ 1 4!
+ g + 1 i!
Write a program that displays the e value for i = 10000, 20000, …, and 100000. (Hint: Because i! = i * (i - 1) * c * 2 * 1, then
1 i!
is 1
i(i - 1)!
Initialize e and item to be 1, and keep adding a new item to e. The new item is the previous item divided by i, for i >= 2.)
** 5.27 (Display leap years) Write a program that displays all the leap years, ten per line, from 2014 to 2114, separated by exactly one space. Also display the number of leap years in this period.
VideoNote
Sum a series
M05_LIAN1878_11_GE_C05.indd 220 1/2/18 11:16 PM
** 5.28 (Display the first days of each month) Write a program that prompts the user to enter the year and first day of the year, then displays the first day of each month in the year. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the following output:
January 1, 2013 is Tuesday ... December 1, 2013 is Sunday
** 5.29 (Display calendars) Write a program that prompts the user to enter the year and first day of the year and displays the calendar table for the year on the console. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the calendar for each month in the year, as follows:
January 2013
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
. . .
December 2013
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
* 5.30 (Financial application: compound value) Suppose you save $100 each month in a savings account with annual interest rate 3.75%. The monthly interest rate is 0.0375 / 12 = 0.003125. After the first month, the value in the account becomes
100 * (1 + 0.003125) = 100.3125
After the second month, the value in the account becomes
100 + 100.3125) * (1 + 0.003125) = 200.938
After the third month, the value in the account becomes
(100 + 200.938) * (1 + 0.003125) = 301.878
and so on.
Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 3.75), and the number of months (e.g., 6) and displays the amount in the savings account after the given month.
Programming Exercises 221
M05_LIAN1878_11_GE_C05.indd 221 1/2/18 11:16 PM
222 Chapter 5 Loops
*5.31 (Financial application: compute CD value) Suppose you put $10,000 into a CD with an annual percentage yield of 6.15%. After one month, the CD is worth
10000 + 10000 * 6.15 / 1200 = 10051.25
After two months, the CD is worth
10051.25 + 10051.25 * 6.15 / 1200 = 10102.76
After three months, the CD is worth
10102.76 + 10102.76 * 6.15 / 1200 = 10154.53
and so on.
Write a program that prompts the user to enter an amount (e.g., 10000), the annual percentage yield (e.g., 6.15), and the number of months (e.g., 18) and displays a table as shown in the sample run.
Enter the initial deposit amount: 10000
Enter annual percentage yield: 6.15
Enter maturity period (number of months): 18
Month CD Value 1 10051.25 2 10102.76 ... 17 10907.90 18 10963.81
** 5.32 (Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a loop to continuously generate the second digit until it is different from the first digit.)
** 5.33 (Perfect number) A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers 6 10,000. Write a program to find all these four numbers.
*** 5.34 (Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that plays the scissor-rock-paper game. Revise the program to let the user continuously play until either the user or the computer wins three times more than their opponent.
*5.35 (Summation) Write a program to compute the following summation:
1
1 + 22 + 122 + 23 + 123 + 24 + c + 12999 + 21000 ** 5.36 (Business application: checking ISBN) Use loops to simplify Programming
Exercise 3.9.
** 5.37 ( Decimal to binary) Write a program that prompts the user to enter a decimal integer then displays its corresponding binary value. Don’t use Java’s Integer .toBinaryString(int) in this program.
** 5.38 (Decimal to octal) Write a program that prompts the user to enter a decimal integer and displays its corresponding octal value. Don’t use Java’s Integer. toOctalString(int) in this program.
M05_LIAN1878_11_GE_C05.indd 222 1/2/18 11:16 PM
*5.39 (Financial application: find the sales amount) You have just started a sales job in a department store. Your pay consists of a base salary and a commission. The base salary is $5,000. The scheme shown below is used to determine the commission rate.
Sales Amount Commission Rate
$0.01–$5,000 6 percent
$5,000.01–$10,000 8 percent
$10,000.01 and above 10 percent
Note that this is a graduated rate. The rate for the first $5,000 is at 6%, the next $5000 is at 8%, and the rest is at 10%. If your sales amounts to $25,000, the com- mission is 5,000 * 6% + 5,000 * 8% + 15,000 * 10% = 2,200. Your goal is to earn $30,000 a year. Write a program to find the minimum sales you have to gener- ate in order to make $30,000.
5.40 (Simulation: heads or tails) Write a program that simulates flipping a coin two millions times and displays the number of heads and tails.
*5.41 (Occurrence of max numbers) Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume the input ends with number 0. Sup- pose you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4.
(Hint: Maintain two variables, max and count. max stores the current max number and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1.)
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number is 4
*5.42 (Financial application: find the sales amount) Rewrite Programming Exercise 5.39 as follows:
■■ Use a while loop instead of a do-while loop. ■■ Let the user enter COMMISSION_SOUGHT instead of fixing it as a constant.
*5.43 (Math: combinations) Write a program that displays all possible combinations for picking two numbers from integers 1 to 7. Also display the total number of all combinations.
1 2
1 3 ... ...
The total number of all combinations is 21
Programming Exercises 223
M05_LIAN1878_11_GE_C05.indd 223 1/2/18 11:16 PM
224 Chapter 5 Loops
*5.44 (Computer architecture: bit-level operations) A byte value is stored in 8 bits. Write a program that prompts the user to enter a byte integer and displays the 8 bits for the integer. Here are sample runs:
Enter an integer: 5
The 8 bits are 00000101
Enter an integer: –5
The 8 bits are 11111011
(Hint: You need to use the bitwise right shift operator (>>) and the bitwise AND operator (&), which are covered in Appendix G, Bitwise Operations.)
** 5.45 (Statistics: compute mean and standard deviation) In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you how tightly all the various data are clustered around the mean in a set of data. For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is 0.
Write a program that prompts the user to enter 10 numbers and displays the mean and standard deviations of these numbers using the following formula:
mean = a
n
i = 1 xi
n =
x1 + x2 + g + xn n
deviation = cani = 1xi2 - aa n
i = 1 xib
2
n
n - 1 Here is a sample run:
Enter 10 numbers: 1 2 3 4.5 5.6 6 7 8 9 10
The mean is 5.61
The standard deviation is 2.99794
Enter a string: ABCD
The reversed string is DCBA
*5.46 (Reverse a string) Write a program that prompts the user to enter a string and displays the string in reverse order.
M05_LIAN1878_11_GE_C05.indd 224 1/2/18 11:16 PM
Enter the first 12 digits of an ISBN-13 as a string: 978013213080
The ISBN-13 number is 9780132130806
Enter the first 12 digits of an ISBN-13 as a string: 978013213079
The ISBN-13 number is 9780132130790
Enter the first 12 digits of an ISBN-13 as a string: 97801320
97801320 is an invalid input
*5.47 (Business: check ISBN-13) ISBN-13 is a new standard for identifying books. It uses 13 digits d1d2d3d4d5d6d7d8d9d10d11d12d13. The last digit d13 is a checksum, which is calculated from the other digits using the following formula:
10 - (d1 + 3d2 + d3 + 3d4 + d5 + 3d6 + d7 + 3d8 + d9 + 3d10 + d11 + 3d12),10
If the checksum is 10, replace it with 0. Your program should read the input as a string. Here are sample runs:
Enter a string: Beijing Chicago
ejnhcg
*5.48 (Process string) Write a program that prompts the user to enter a string and displays the characters at even positions. Here is a sample run:
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
*5.49 (Count vowels and consonants) Assume that the letters A, E, I, O, and U are vowels. Write a program that prompts the user to enter a string, and displays the number of vowels and consonants in the string.
Programming Exercises 225
M05_LIAN1878_11_GE_C05.indd 225 1/2/18 11:16 PM
226 Chapter 5 Loops
Enter the first string: Welcome to C++
Enter the second string: Welcome to programming
The common prefix is Welcome to
*5.51 (Longest common prefix) Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. Here are some sample runs:
Enter the first string: Atlanta
Enter the second string: Macon
Atlanta and Macon have no common prefix
*5.50 (Print multiplication table) Write a program that uses for, while, and do-while loop statements to display the multiplication table. Here is a sample run:
Enter a string: Welcome to Java
1*1= 1 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9
1*2= 1 2*2= 2 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
1*3= 3 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
1*4= 4 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
M05_LIAN1878_11_GE_C05.indd 226 1/2/18 11:16 PM
Methods
Objectives ■■ To define methods with formal parameters (§6.2).
■■ To invoke methods with actual parameters (i.e., arguments) (§6.2).
■■ To define methods with a return value (§6.3).
■■ To define methods without a return value and distinguish the differ- ences between void methods and value-returning methods (§6.4).
■■ To pass arguments by value (§6.5).
■■ To develop reusable code that is modular, easy to read, easy to debug, and easy to maintain (§6.6).
■■ To write a method that converts hexadecimals to decimals (§6.7).
■■ To use method overloading and understand ambiguous overloading (§6.8).
■■ To determine the scope of variables (§6.9).
■■ To apply the concept of method abstraction in software development (§6.10).
■■ To design and implement methods using stepwise refinement (§6.11).
Chapter
6
M06_LIAN1878_11_GE_C06.indd 227 1/2/18 10:42 PM
228 Chapter 6 Methods
6.1 Introduction Methods can be used to define reusable code and organize and simplify coding.
Suppose you need to find the sum of integers from 1 to 10, 20 to 37, and 35 to 49, respectively. You may write the code as follows:
int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum);
sum = 0; for (int i = 20; i <= 37; i++) sum += i; System.out.println("Sum from 20 to 37 is " + sum);
sum = 0; for (int i = 35; i <= 49; i++) sum += i; System.out.println("Sum from 35 to 49 is " + sum);
You may have observed that computing these sums from 1 to 10, 20 to 37, and 35 to 49 are very similar, except that the starting and ending integers are different. Wouldn’t it be nice if we could write the common code once and reuse it? We can do so by defining a method and invoking it.
The preceding code can be simplified as follows:
1 public static int sum(int i1, int i2) { 2 int result = 0; 3 for (int i = i1; i <= i2; i++) 4 result += i; 5 6 return result; 7 } 8 9 public static void main(String[] args) { 10 System.out.println("Sum from 1 to 10 is " + sum(1, 10)); 11 System.out.println("Sum from 20 to 37 is " + sum(20, 37)); 12 System.out.println("Sum from 35 to 49 is " + sum(35, 49)); 13 }
Lines 1–7 define the method named sum with two parameters i1 and i2. The statements in the main method invoke sum(1, 10) to compute the sum from 1 to 10, sum(20, 37) to compute the sum from 20 to 37, and sum(35, 49) to compute the sum from 35 to 49.
A method is a collection of statements grouped together to perform an operation. In earlier chap- ters you have used predefined methods such as System.out.println, System.exit, Math .pow, and Math.random. These methods are defined in the Java library. In this chapter, you will learn how to define your own methods and apply method abstraction to solve complex problems.
6.2 Defining a Method A method definition consists of method name, parameters, return value type, and body.
The syntax for defining a method is as follows:
modifier returnValueType methodName(list of parameters) { // Method body; }
Point Key
problem
why methods?
define sum method
main method invoke sum
method
Point Key
M06_LIAN1878_11_GE_C06.indd 228 1/2/18 10:42 PM
6.2 Defining a Method 229
The method header specifies the modifiers, return value type, method name, and parameters of the method. The static modifier is used for all the methods in this chapter. The reason for using it will be discussed in Chapter 9, Objects and Classes.
A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform desired operations without returning a value. In this case, the returnValueType is the keyword void. For example, the returnValueType is void in the main method, as well as in System.exit, and System.out.println. If a method returns a value, it is called a value-returning method; otherwise, it is called a void method.
The variables defined in the method header are known as formal parameters or simply parameters. A parameter is like a placeholder: when a method is invoked, you pass a value to the parameter. This value is referred to as an actual parameter or argument. The parameter list refers to the method’s type, order, and the number of parameters. The method name and the parameter list together constitute the method signature. Parameters are optional; that is, a method may contain no parameters. For example, the Math.random() method has no parameters.
The method body contains a collection of statements that implement the method. The method body of the max method uses an if statement to determine which number is larger and return the value of that number. In order for a value-returning method to return a result, a return statement using the keyword return is required. The method terminates when a return statement is executed.
Note Some programming languages refer to methods as procedures and functions. In those languages, a value-returning method is called a function and a void method is called a procedure.
Caution In the method header, you need to declare each parameter separately. For instance, max(int num1, int num2) is correct, but max(int num1, num2) is wrong.
method header
modifier
value-returning method
void method formal parameter
parameter actual parameter argument
parameter list
method signature
Figure 6.1 A method definition consists of a method header and a method body.
De�ne a method Invoke a method
int z = max(x, y);
actual parameters (arguments)
public static int max(int num1, int num2) {
int result;
if (num1 > num2) result = num1;
else result = num2;
return result;
}
modi�er return value
type method name
formal parameters
return value
method body
method header
parameter list method signature
Let’s look at a method defined to find the larger between two integers. This method, named max, has two int parameters, num1 and num2, the larger of which is returned by the method. Figure 6.1 illustrates the components of this method.
M06_LIAN1878_11_GE_C06.indd 229 1/2/18 10:42 PM
230 Chapter 6 Methods
Note We say “define a method” and “declare a variable.” We are making a subtle distinction here. A definition defines what the defined item is, but a declaration usually involves allocating memory to store data for the declared item.
6.3 Calling a Method Calling a method executes the code in the method.
In a method definition, you define what the method is to do. To execute the method, you have to call or invoke it. The program that calls the function is called a caller. There are two ways to call a method, depending on whether the method returns a value or not.
If a method returns a value, a call to the method is usually treated as a value. For example,
int larger = max(3, 4);
calls max(3, 4) and assigns the result of the method to the variable larger. Another example of a call that is treated as a value is
System.out.println(max(3, 4));
which prints the return value of the method call max(3, 4). If a method returns void, a call to the method must be a statement. For example, the method
println returns void. The following call is a statement:
System.out.println("Welcome to Java!");
Note A value-returning method can also be invoked as a statement in Java. In this case, the caller simply ignores the return value. This is not often done, but it is permissible if the caller is not interested in the return value.
When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method- ending closing brace is reached.
Listing 6.1 presents a complete program that is used to test the max method.
Listing 6.1 TestMax.java 1 public class TestMax { 2 /** Main method */ 3 public static void main(String[] args) { 4 int i = 5; 5 int j = 2; 6 int k = max(i, j); 7 System.out.println("The maximum of " + i + 8 " and " + j + " is " + k); 9 } 10 11 /** Return the max of two numbers */ 12 public static int max(int num1, int num2) { 13 int result; 14 15 if (num1 > num2) 16 result = num1; 17 else 18 result = num2; 19 20 return result; 21 } 22 }
define vs. declare
Point Key
VideoNote
Define/invoke max method
main method
invoke max
define method
caller
M06_LIAN1878_11_GE_C06.indd 230 1/2/18 10:42 PM
6.3 Calling a Method 231
line# i j k num1 num2 result
4 5
Invoking max c 5 212 5 213 undefined 16 5
6 5
The maximum of 5 and 2 is 5
Figure 6.2 When the max method is invoked, the flow of control transfers to it. Once the max method is finished, it returns control back to the caller.
public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum of " + i + " and " + j + " is " + k); }
pass the value j
pass the value i
public static int max(int num1, int num2) { int result;
if (num1 > num2) result = num1; else result = num2;
return result; }
This program contains the main method and the max method. The main method is just like any other method, except that it is invoked by the JVM to start the program.
The main method’s header is always the same. Like the one in this example, it includes the modifiers public and static, return value type void, method name main, and a parameter of the String[] type. String[] indicates the parameter is an array of String, a subject addressed in Chapter 7.
The statements in main may invoke other methods that are defined in the class that contains the main method or in other classes. In this example, the main method invokes max(i, j), which is defined in the same class with the main method.
When the max method is invoked (line 6), variable i’s value 5 is passed to num1 and vari- able j’s value 2 is passed to num2 in the max method. The flow of control transfers to the max method and the max method is executed. When the return statement in the max method is executed, the max method returns the control to its caller (in this case, the caller is the main method). This process is illustrated in Figure 6.2.
main method
max method
Caution A return statement is required for a value-returning method. The method given in (a) is logically correct, but it has a compile error because the Java compiler thinks this method might not return a value.
M06_LIAN1878_11_GE_C06.indd 231 1/2/18 10:42 PM
232 Chapter 6 Methods
To fix this problem, delete if (n < 0) in (a), so the compiler will see a return statement to be reached regardless of how the if statement is evaluated.
Note Methods enable code sharing and reuse. The max method can be invoked from any class, not just TestMax. If you create a new class, you can invoke the max method using ClassName.methodName (i.e., TestMax.max).
Each time a method is invoked, the system creates an activation record (also called an activation frame) that stores parameters and variables for the method and places the activation record in an area of memory known as a call stack. A call stack is also known as an execution stack, runtime stack, or machine stack and it is often shortened to just “the stack.” When a method calls another method, the caller’s activation record is kept intact and a new activation record is created for the new method called. When a method finishes its work and returns to its caller, its activation record is removed from the call stack.
A call stack stores the activation records in a last-in, first-out fashion: The activation record for the method that is invoked last is removed first from the stack. For example, suppose method m1 calls method m2, and m2 calls method m3. The runtime system pushes m1’s activa- tion record into the stack, then m2’s, and then m3’s. After m3 is finished, its activation record is removed from the stack. After m2 is finished, its activation record is removed from the stack. After m1 is finished, its activation record is removed from the stack.
Understanding call stacks helps you to comprehend how methods are invoked. The vari- ables defined in the main method in Listing 6.1 are i, j, and k. The variables defined in the max method are num1, num2, and result. The variables num1 and num2 are defined in the method signature and are parameters of the max method. Their values are passed through method invocation. Figure 6.3 illustrates the activation records for method calls in the stack.
reusing method
activation record
call stack
Figure 6.3 When the max method is invoked, the flow of control transfers to the max method. Once the max method is finished, it returns control back to the caller.
Activation record for the main method
Activation record for the max method
k: j: 2 i: 5
(a) The main method is invoked.
Activation record for the main method
k: j: 2 i: 5
(d) The max method is �nished and the return value is sent to k.
Activation record for the main method
k: 5 j: 2 i: 5
Stack is empty
(b) The max method is invoked.
(e) The main method is �nished.
Activation record for the main method
Activation record for the max method
k: j: 2 i: 5
result: 5 num2: 2 num1: 5
(c) The max method is being executed.
result: num2: 2 num1: 5
(a)
public static int sign(int n) {
if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return −1; }
public static int sign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else return –1; }
(b)
Should be
M06_LIAN1878_11_GE_C06.indd 232 1/2/18 10:42 PM
6.4 void vs. Value-Returning Methods 233
6.4 void vs. Value-Returning Methods A void method does not return a value.
The preceding section gives an example of a value-returning method. This section shows how to define and invoke a void method. Listing 6.2 gives a program that defines a method named printGrade and invokes it to print the grade for a given score.
Listing 6.2 TestVoidMethod.java 1 public class TestVoidMethod { 2 public static void main(String[] args) { 3 System.out.print("The grade is "); 4 printGrade(78.5); 5 6 System.out.print("The grade is "); 7 printGrade(59.5); 8 } 9 10 public static void printGrade(double score) { 11 if (score >= 90.0) { 12 System.out.println('A'); 13 } 14 else if (score >= 80.0) { 15 System.out.println('B'); 16 } 17 else if (score >= 70.0) { 18 System.out.println('C'); 19 } 20 else if (score >= 60.0) { 21 System.out.println('D'); 22 } 23 else { 24 System.out.println('F'); 25 } 26 } 27 }
Point Key
VideoNote
Use void method
main method
invoke printGrade
printGrade method
The grade is C
The grade is F
The printGrade method is a void method because it does not return any value. A call to a void method must be a statement. Therefore, it is invoked as a statement in line 4 in the main method. Like any Java statement, it is terminated with a semicolon.
To see the differences between a void and value-returning method, let’s redesign the printGrade method to return a value. The new method, which we call getGrade, returns the grade as given in Listing 6.3.
Listing 6.3 TestReturnGradeMethod.java 1 public class TestReturnGradeMethod { 2 public static void main(String[] args) { 3 System.out.print("The grade is " + getGrade(78.5)); 4 System.out.print("\nThe grade is " + getGrade(59.5)); 5 } 6
invoke void method
void vs. value-returned
main method
invoke getGrade
M06_LIAN1878_11_GE_C06.indd 233 1/2/18 10:42 PM
234 Chapter 6 Methods
7 public static char getGrade(double score) { 8 if (score >= 90.0) 9 return 'A'; 10 else if (score >= 80.0) 11 return 'B'; 12 else if (score >= 70.0) 13 return 'C'; 14 else if (score >= 60.0) 15 return 'D'; 16 else 17 return 'F'; 18 } 19 }
getGrade method
The grade is C
The grade is F
The getGrade method defined in lines 7–18 returns a character grade based on the numeric score value. The caller invokes this method in lines 3 and 4.
The getGrade method can be invoked by a caller wherever a character may appear. The printGrade method does not return any value, so it must be invoked as a statement.
Note A return statement is not needed for a void method, but it can be used for terminat- ing the method and returning to the method’s caller. The syntax is simply
return;
This is not often done, but sometimes it is useful for circumventing the normal flow of control in a void method. For example, the following code has a return statement to terminate the method when the score is invalid:
public static void printGrade(double score) { if (score < 0 || score > 100) { System.out.println("Invalid score"); return; }
if (score >= 90.0) { System.out.println('A'); } else if (score >= 80.0) { System.out.println('B'); } else if (score >= 70.0) { System.out.println('C'); } else if (score >= 60.0) { System.out.println('D'); } else { System.out.println('F'); } }
return in void method
M06_LIAN1878_11_GE_C06.indd 234 1/2/18 10:42 PM
6.4.1 What are the benefits of using a method? 6.4.2 How do you define a method? How do you invoke a method? 6.4.3 How do you simplify the max method in Listing 6.1 using the conditional operator? 6.4.4 True or false? A call to a method with a void return type is always a statement
itself, but a call to a value-returning method cannot be a statement by itself.
6.4.5 What is the return type of a main method? 6.4.6 What would be wrong with not writing a return statement in a value-returning
method? Can you have a return statement in a void method? Does the return statement in the following method cause syntax errors?
public static void xMethod(double x, double y) { System.out.println(x + y); return x + y; }
6.4.7 Define the terms parameter, argument, and method signature. 6.4.8 Write method headers (not the bodies) for the following methods:
a. Return a sales commission, given the sales amount and the commission rate.
b. Display the calendar for a month, given the month and year.
c. Return a square root of a number.
d. Test whether a number is even, and returning true if it is.
e. Display a message a specified number of times.
f. Return the monthly payment, given the loan amount, number of years, and annual interest rate.
g. Return the corresponding uppercase letter, given a lowercase letter.
6.4.9 Identify and correct the errors in the following program: 1 public class Test { 2 public static method1(int n, m) { 3 n += m; 4 method2(3.4); 5 } 6 7 public static int method2(int n) { 8 if (n > 0) return 1; 9 else if (n == 0) return 0; 10 else if (n < 0) return −1; 11 } 12 }
6.4.10 Reformat the following program according to the programming style and docu- mentation guidelines proposed in Section 1.9, Programming Style and Documen- tation. Use the end-of-line brace style.
public class Test { public static double method(double i, double j) { while (i < j) { j––; }
return j; } }
Point Check
6.4 void vs. Value-Returning Methods 235
M06_LIAN1878_11_GE_C06.indd 235 1/2/18 10:42 PM
236 Chapter 6 Methods
6.5 Passing Parameters by Values The arguments are passed by value to parameters when invoking a method.
The power of a method is its ability to work with parameters. You can use println to print any string, and max to find the maximum of any two int values. When calling a method, you need to provide arguments, which must be given in the same order as their respective param- eters in the method signature. This is known as parameter order association. For example, the following method prints a message n times:
public static void nPrintln(String message, int n) { for (int i = 0; i < n; i++) System.out.println(message); }
You can use nPrintln("Hello", 3) to print Hello three times. The nPrintln("Hello", 3) statement passes the actual string parameter Hello to the parameter message, passes 3 to n, and prints Hello three times. However, the statement nPrintln(3, "Hello") would be wrong. The data type of 3 does not match the data type for the first parameter, message, nor does the second argument, Hello, match the second parameter, n.
Caution The arguments must match the parameters in order, number, and compatible type, as defined in the method signature. Compatible type means you can pass an argument to a parameter without explicit casting, such as passing an int value argument to a double value parameter.
When you invoke a method with an argument, the value of the argument is passed to the parameter. This is referred to as pass-by-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the method. As given in Listing 6.4, the value of x (1) is passed to the parameter n to invoke the increment method (line 5). The parameter n is incremented by 1 in the method (line 10), but x is not changed no matter what the method does.
Listing 6.4 Increment.java 1 public class Increment { 2 public static void main(String[] args) { 3 int x = 1; 4 System.out.println("Before the call, x is " + x); 5 increment(x); 6 System.out.println("After the call, x is " + x); 7 } 8 9 public static void increment(int n) { 10 n++; 11 System.out.println("n inside the method is " + n); 12 } 13 }
Point Key
parameter order association
pass-by-value
invoke increment
increment n
Before the call, x is 1
n inside the method is 2
After the call, x is 1
M06_LIAN1878_11_GE_C06.indd 236 1/2/18 10:42 PM
6.5 Passing Parameters by Values 237
Listing 6.5 gives another program that demonstrates the effect of passing by value. The program creates a method for swapping two variables. The swap method is invoked by passing two arguments. Interestingly, the values of the arguments are not changed after the method is invoked.
Listing 6.5 TestPassByValue.java 1 public class TestPassByValue { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Declare and initialize variables 5 int num1 = 1; 6 int num2 = 2; 7 8 System.out.println("Before invoking the swap method, num1 is " + 9 num1 + " and num2 is " + num2); 10 11 // Invoke the swap method to attempt to swap two variables 12 swap(num1, num2); 13 14 System.out.println("After invoking the swap method, num1 is " + 15 num1 + " and num2 is " + num2); 16 } 17 18 /** Swap two variables */ 19 public static void swap(int n1, int n2) { 20 System.out.println("\tInside the swap method"); 21 System.out.println("\t\tBefore swapping, n1 is " + n1 22 + " and n2 is " + n2); 23 24 // Swap n1 with n2 25 int temp = n1; 26 n1 = n2; 27 n2 = temp; 28 29 System.out.println("\t\tAfter swapping, n1 is " + n1 30 + " and n2 is " + n2); 31 } 32 }
false swap
Before invoking the swap method, num1 is 1 and num2 is 2 Inside the swap method Before swapping, n1 is 1 and n2 is 2 After swapping, n1 is 2 and n2 is 1 After invoking the swap method, num1 is 1 and num2 is 2
Before the swap method is invoked (line 12), num1 is 1 and num2 is 2. After the swap method is invoked, num1 is still 1 and num2 is still 2. Their values have not been swapped. As shown in Figure 6.4, the values of the arguments num1 and num2 are passed to n1 and n2, but n1 and n2 have their own memory locations independent of num1 and num2. Therefore, changes in n1 and n2 do not affect the contents of num1 and num2.
Another twist is to change the parameter name n1 in swap to num1. What effect does this have? No change occurs, because it makes no difference whether the parameter and the argu- ment have the same name. The parameter is a variable in the method with its own memory space. The variable is allocated when the method is invoked, and it disappears when the method is returned to its caller.
M06_LIAN1878_11_GE_C06.indd 237 1/2/18 10:42 PM
238 Chapter 6 Methods
Note For simplicity, Java programmers often say passing x to y, which actually means passing the value of argument x to parameter y.
6.5.1 How is an argument passed to a method? Can the argument have the same name as its parameter?
6.5.2 Identify and correct the errors in the following program:
1 public class Test { 2 public static void main(String[] args) { 3 nPrintln(5, "Welcome to Java!"); 4 } 5 6 public static void nPrintln(String message, int n) { 7 int n = 1; 8 for (int i = 0; i < n; i++) 9 System.out.println(message); 10 } 11 }
6.5.3 What is pass-by-value? Show the result of the following programs.
Point Check
Figure 6.4 The values of the variables are passed to the method’s parameters.
Space required for the main method
Space required for the swap method
The main method is invoked.
Space required for the main method
Space required for the main method
num2: 2 num1: 1
num2: 2 num1: 1
Space required for the swap method
num2: 2 num1: 1
The swap method is invoked.
The swap method is executed.
Space required for the main method
num2: 2 num1: 1
The swap method is �nished.
Stack is empty
The main method is �nished.
The values of num1 and num2 are passed to n1 and n2.
The values for n1 and n2 are swapped, but it does not affect num1 and num2.
n2: 2 n1: 1
temp: n2: 1 n1: 2
temp:
public class Test { public static void main(String[] args) { int max = 0; max(1, 2, max); System.out.println(max); }
public static void max( int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; } }
(a)
public class Test { public static void main(String[] args) { int i = 1; while (i <= 6) { method1(i, 2); i++; } }
public static void method1( int i, int num) { for (int j = 1; j <= i; j++) { System.out.print(num + " "); num *= 2; }
System.out.println(); } }
(b)
M06_LIAN1878_11_GE_C06.indd 238 1/2/18 10:42 PM
6.6 Modularizing Code 239
6.5.4 For (a) in the preceding question, show the contents of the activation records in the call stack just before the method max is invoked, just as max is entered, just before max is returned, and right after max is returned.
6.6 Modularizing Code Modularizing makes the code easy to maintain and debug and enables the code to be reused.
Methods can be used to reduce redundant code and enable code reuse. Methods can also be used to modularize code and improve the quality of the program.
Listing 5.9 gives a program that prompts the user to enter two integers and displays their greatest common divisor. You can rewrite the program using a method, as given in Listing 6.6.
Listing 6.6 GreatestCommonDivisorMethod.java 1 import java.util.Scanner; 2 3 public class GreatestCommonDivisorMethod { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter two integers 10 System.out.print("Enter first integer: "); 11 int n1 = input.nextInt(); 12 System.out.print("Enter second integer: "); 13 int n2 = input.nextInt(); 14
Point Key
VideoNote
Modularize code
public class Test { public static void main(String[] args) { // Initialize times int times = 3; System.out.println("Before the call," + " variable times is " + times);
// Invoke nPrintln and display times nPrintln("Welcome to Java!", times); System.out.println("After the call," + " variable times is " + times); }
// Print the message n times public static void nPrintln( String message, int n) { while (n > 0) { System.out.println("n = " + n); System.out.println(message); n––; } } }
(c)
public class Test { public static void main(String[] args) { int i = 0; while (i <= 4) { method1(i); i++; }
System.out.println("i is " + i); }
public static void method1(int i) { do { if (i % 3 != 0) System.out.print(i + " "); i––; } while (i >= 1);
System.out.println(); } }
(d)
M06_LIAN1878_11_GE_C06.indd 239 1/2/18 10:42 PM
240 Chapter 6 Methods
15 System.out.println("The greatest common divisor for " + n1 + 16 " and " + n2 + " is " + gcd(n1, n2)); 17 } 18 19 /** Return the gcd of two integers */ 20 public static int gcd(int n1,int n2) { 21 int gcd = 1; // Initial gcd is 1 22 int k = 2; // Possible gcd 23 24 while (k <= n1 && k <= n2) { 25 if (n1 % k == 0 && n2 % k == 0) 26 gcd = k; // Update gcd 27 k++; 28 } 29 30 return gcd; // Return gcd 31 } 32 }
invoke gcd
compute gcd
return gcd
Enter first integer: 45
Enter second integer: 75
The greatest common divisor for 45 and 75 is 15
By encapsulating the code for obtaining the gcd in a method, this program has several advantages:
1. It isolates the problem for computing the gcd from the rest of the code in the main method. Thus, the logic becomes clear, and the program is easier to read.
2. The errors on computing the gcd are confined in the gcd method, which narrows the scope of debugging.
3. The gcd method now can be reused by other programs.
Listing 6.7 applies the concept of code modularization to improve Listing 5.15, Prime- Number.java.
Listing 6.7 PrimeNumberMethod.java 1 public class PrimeNumberMethod { 2 public static void main(String[] args) { 3 System.out.println("The first 50 prime numbers are \n"); 4 printPrimeNumbers(50); 5 } 6 7 public static void printPrimeNumbers(int numberOfPrimes) { 8 final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line 9 int count = 0; // Count the number of prime numbers 10 int number = 2; // A number to be tested for primeness 11 12 // Repeatedly find prime numbers 13 while (count < numberOfPrimes) { 14 // Print the prime number and increase the count 15 if (isPrime(number)) { 16 count++; // Increase the count 17
invoke printPrimeNumbers
printPrimeNumbers method
invoke isPrime
M06_LIAN1878_11_GE_C06.indd 240 1/2/18 10:42 PM
6.7 Case Study: Converting Hexadecimals to Decimals 241
18 if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { 19 // Print the number and advance to the new line 20 System.out.printf("%−5d\n", number); 21 } 22 else 23 System.out.printf("%−5d", number); 24 } 25 26 // Check whether the next number is prime 27 number++; 28 } 29 } 30 31 /** Check whether number is prime */ 32 public static boolean isPrime(int number) { 33 for (int divisor = 2; divisor <= number / 2; divisor++) { 34 if (number % divisor == 0) { // If true, number is not prime 35 return false; // Number is not a prime 36 } 37 } 38 39 return true; // Number is prime 40 } 41 }
isPrime method
The first 50 prime numbers are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229
We divided a large problem into two subproblems: determining whether a number is a prime, and printing the prime numbers. As a result, the new program is easier to read and easier to debug. Moreover, the methods printPrimeNumbers and isPrime can be reused by other programs.
6.6.1 Trace the gcd method to find the return value for gcd(4, 6). 6.6.2 Trace the isPrime method to find the return value for isPrime(25).
6.7 Case Study: Converting Hexadecimals to Decimals This section presents a program that converts a hexadecimal number into a decimal number.
Listing 5.11, Dec2Hex.java, gives a program that converts a decimal to a hexadecimal. How would you convert a hex number into a decimal?
Given a hexadecimal number hnhn - 1hn - 2 c h2h 1h 0 , the equivalent decimal value is
hn * 16n + hn - 1 * 16n - 1 + hn - 2 * 16n - 2 + g + h2 * 162 + h1 * 161 + h0 * 160
For example, the hex number AB8C is
10 * 163 + 11 * 162 + 8 * 161 + 12 * 160 = 43916
Our program will prompt the user to enter a hex number as a string and convert it into a decimal using the following method:
public static int hexToDecimal(String hex)
Point Check
Point Key
M06_LIAN1878_11_GE_C06.indd 241 1/2/18 10:42 PM
242 Chapter 6 Methods
A brute-force approach is to convert each hex character into a decimal number, multiply it by 16i for a hex digit at the i’s position, and then add all the items together to obtain the equivalent decimal value for the hex number.
Note that
hn * 16n + hn - 1 * 16n - 1 + hn - 2 * 16n - 2 + g + h1 * 161 + h0 * 160
= ( c ((hn * 16 + hn - 1) * 16 + hn - 2) * 16 + g + h1) * 16 + h0
This observation, known as the Horner’s algorithm, leads to the following efficient code for converting a hex string to a decimal number:
int decimalValue = 0; for (int i = 0; i < hex.length(); i++) { char hexChar = hex.charAt(i); decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar); }
Here is a trace of the algorithm for hex number AB8C:
i hexChar hexCharToDecimal (hexChar) decimalValue
Before the loop 0
After the 1st iteration 0 A 10 10
After the 2nd iteration 1 B 11 10 * 16 + 11
After the 3rd iteration 2 8 8 (10 * 16 + 11) * 16 + 8
After the 4th iteration 3 C 12 ((10 * 16 + 11) * 16 + 8) * 16 + 12
Listing 6.8 gives the complete program.
Listing 6.8 Hex2Dec.java 1 import java.util.Scanner; 2 3 public class Hex2Dec { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter a string 10 System.out.print("Enter a hex number: "); 11 String hex = input.nextLine(); 12 13 System.out.println("The decimal value for hex number " 14 + hex + " is " + hexToDecimal(hex.toUpperCase())); 15 } 16 17 public static int hexToDecimal(String hex) { 18 int decimalValue = 0; 19 for (int i = 0; i < hex.length(); i++) { 20 char hexChar = hex.charAt(i); 21 decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
input string
hex to decimal
M06_LIAN1878_11_GE_C06.indd 242 1/2/18 10:42 PM
6.8 Overloading Methods 243
22 } 23 24 return decimalValue; 25 } 26 27 public static int hexCharToDecimal(char ch) { 28 if (ch >= 'A' && ch <= 'F') 29 return 10 + ch – 'A'; 30 else // ch is '0', '1', ..., or '9' 31 return ch − '0'; 32 } 33 }
hex char to decimal check uppercase
Enter a hex number: AB8C
The decimal value for hex number AB8C is 43916
Enter a hex number: af71
The decimal value for hex number af71 is 44913
The program reads a string from the console (line 11) and invokes the hexToDecimal method to convert a hex string to decimal number (line 14). The characters can be in either lowercase or uppercase. They are converted to uppercase before invoking the hexToDecimal method.
The hexToDecimal method is defined in lines 17–25 to return an integer. The length of the string is determined by invoking hex.length() in line 19.
The hexCharToDecimal method is defined in lines 27–32 to return a decimal value for a hex character. The character can be in either lowercase or uppercase. Recall that to subtract two characters is to subtract their Unicodes. For example, '5' – '0' is 5.
6.7.1 What is hexCharToDecimal('B'))? What is hexCharToDecimal('7'))? What is hexToDecimal("A9"))?
6.8 Overloading Methods Overloading methods enable you to define the methods with the same name as long as their parameter lists are different.
The max method used earlier works only with the int data type. But what if you need to determine which of the two floating-point numbers has the maximum value? The solution is to create another method with the same name but different parameters, as shown in the following code:
public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }
If you call max with int parameters, the max method that expects int parameters will be invoked; and if you call max with double parameters, the max method that expects double parameters will be invoked. This is referred to as method overloading; that is, two methods have the same name but different parameter lists within one class. The Java compiler determines which method to use based on the method signature.
Point Check
Point Key
method overloading
M06_LIAN1878_11_GE_C06.indd 243 1/2/18 10:42 PM
244 Chapter 6 Methods
Listing 6.9 is a program that creates three methods. The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. All three methods are named max.
Listing 6.9 TestMethodOverloading.java 1 public class TestMethodOverloading { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Invoke the max method with int parameters 5 System.out.println("The maximum of 3 and 4 is " 6 + max(3, 4)); 7 8 // Invoke the max method with the double parameters 9 System.out.println("The maximum of 3.0 and 5.4 is " 10 + max(3.0, 5.4)); 11 12 // Invoke the max method with three double parameters 13 System.out.println("The maximum of 3.0, 5.4, and 10.14 is " 14 + max(3.0, 5.4, 10.14)); 15 } 16 17 /** Return the max of two int values */ 18 public static int max(int num1, int num2) { 19 if (num1 > num2) 20 return num1; 21 else 22 return num2; 23 } 24 25 /** Find the max of two double values */ 26 public static double max(double num1, double num2) { 27 if (num1 > num2) 28 return num1; 29 else 30 return num2; 31 } 32 33 /** Return the max of three double values */ 34 public static double max(double num1, double num2, double num3) { 35 return max(max(num1, num2), num3); 36 } 37 }
overloaded max
overloaded max
overloaded max
The maximum of 3 and 4 is 4 The maximum of 3.0 and 5.4 is 5.4 The maximum of 3.0, 5.4, and 10.14 is 10.14
When calling max(3, 4) (line 6), the max method for finding the maximum of two integers is invoked. When calling max(3.0, 5.4) (line 10), the max method for finding the maximum of two doubles is invoked. When calling max(3.0, 5.4, 10.14) (line 14), the max method for finding the maximum of three double values is invoked.
Can you invoke the max method with an int value and a double value, such as max(2, 2.5)? If so, which of the max methods is invoked? The answer to the first question is yes. The answer to the second question is that the max method for finding the maximum of two double values is invoked. The argument value 2 is automatically converted into a double value and passed to this method.
M06_LIAN1878_11_GE_C06.indd 244 1/2/18 10:42 PM
6.8 Overloading Methods 245
You may be wondering why the method max(double, double) is not invoked for the call max(3, 4). Both max(double, double) and max(int, int) are possible matches for max(3, 4). The Java compiler finds the method that best matches a method invocation. Since the method max(int, int) is a better match for max(3, 4) than max(double, double), max(int, int) is used to invoke max(3, 4).
Tip Overloading methods can make programs clearer and more readable. Methods that perform the same function with different types of parameters should be given the same name.
Note Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types.
Note Sometimes there are two or more possible matches for the invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation causes a compile error. Consider the following code:
public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); }
public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; }
public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } }
Both max(int, double) and max(double, int) are possible candidates to match max(1, 2). Because neither is more specific than the other, the invocation is ambiguous, resulting in a compile error.
6.8.1 What is method overloading? Is it permissible to define two methods that have the same name but different parameter types? Is it permissible to define two methods in a class that have identical method names and parameter lists, but different return value types or different modifiers?
6.8.2 What is wrong in the following program?
public class Test { public static void method(int x) { }
public static int method(int y) {
ambiguous invocation
Point Check
M06_LIAN1878_11_GE_C06.indd 245 1/2/18 10:42 PM
246 Chapter 6 Methods
return y; } }
6.8.3 Given two method definitions,
public static double m(double x, double y)
public static double m(int x, double y)
tell which of the two methods is invoked for:
a. double z = m(4, 5);
b. double z = m(4, 5.4);
c. double z = m(4.5, 5.4);
6.9 The Scope of Variables The scope of a variable is the part of the program where the variable can be referenced.
Section 2.5 introduced the scope of a variable. This section discusses the scope of variables in detail. A variable defined inside a method is referred to as a local variable. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared and assigned a value before it can be used.
A parameter is actually a local variable. The scope of a method parameter covers the entire method. A variable declared in the initial-action part of a for-loop header has its scope in the entire loop. However, a variable declared inside a for-loop body has its scope limited in the loop body from its declaration to the end of the block that contains the variable, as shown in Figure 6.5.
Point Key
scope of variables local variable
Figure 6.5 A variable declared in the initial-action part of a for-loop header has its scope in the entire loop.
The scope of j
The scope of i
public static void method1() {
.
.
.
.
.
.
.
for (int i = 1; i < 10; i++) {
int j;
} }
You can declare a local variable with the same name in different blocks in a method, but you cannot declare a local variable twice in the same block or in nested blocks, as shown in Figure 6.6.
M06_LIAN1878_11_GE_C06.indd 246 1/2/18 10:42 PM
6.10 Case Study: Generating Random Characters 247
Caution A common mistake is to declare a variable in a for loop and then attempt to use it outside the loop. As shown in the following code, i is declared in the for loop, but it is accessed from the outside of the for loop, which causes a syntax error.
for (int i = 0; i < 10; i++) { }
System.out.println(i); // Causes a syntax error on i
The last statement would cause a syntax error, because variable i is not defined outside of the for loop.
6.9.1 What is a local variable? 6.9.2 What is the scope of a local variable?
6.10 Case Study: Generating Random Characters A character is coded using an integer. Generating a random character is to generate an integer.
Computer programs process numerical data and characters. You have seen many examples that involve numerical data. It is also important to understand characters and how to process them. This section presents an example of generating random characters.
As introduced in Section 4.3, every character has a unique Unicode between 0 and FFFF in hexadecimal (65535 in decimal). To generate a random character is to generate a ran- dom integer between 0 and 65535 using the following expression (note since 0 <= Math .random() < 1.0, you have to add 1 to 65535):
(int)(Math.random() * (65535 + 1))
Now let’s consider how to generate a random lowercase letter. The Unicodes for lowercase letters are consecutive integers starting from the Unicode for a, then for b, c, . . . , and z. The Unicode for a is
(int)'a'
Thus, a random integer between (int)'a' and (int)'z' is
(int)((int)'a' + Math.random() * ((int)'z' – (int)'a' + 1))
Point Check
Point Key
Figure 6.6 A variable can be declared multiple times in nonnested blocks, but only once in nested blocks.
It is �ne to declare i in two nonnested blocks.
It is wrong to declare i in two nested blocks.
public static void method1() { int x = 1; int y = 1;
for (int i = 1; i < 10; i++) { x += i;
}
for (int i = 1; i < 10; i++) { y += i;
} }
public static void method2() {
int i = 1; int sum = 0;
for (int i = 1; i < 10; i++) { sum += i;
}
}
M06_LIAN1878_11_GE_C06.indd 247 1/2/18 10:42 PM
248 Chapter 6 Methods
As discussed in Section 4.3.3, all numeric operators can be applied to the char operands. The char operand is cast into a number if the other operand is a number or a character. Therefore, the preceding expression can be simplified as follows:
'a' + Math.random() * ('z' – 'a' + 1)
and a random lowercase letter is
(char)('a' + Math.random() * ('z' – 'a' + 1))
Hence, a random character between any two characters ch1 and ch2 with ch1 < ch2 can be generated as follows:
(char)(ch1 + Math.random() * (ch2 – ch1 + 1))
This is a simple but useful discovery. Listing 6.10 defines a class named RandomCharacter with overloaded methods to get a certain type of random character. You can use these methods in your future projects.
Listing 6.10 RandomCharacter.java 1 public class RandomCharacter { 2 /** Generate a random character between ch1 and ch2 */ 3 public static char getRandomCharacter(char ch1, char ch2) { 4 return (char)(ch1 + Math.random() * (ch2 – ch1 + 1)); 5 } 6 7 /** Generate a random lowercase letter */ 8 public static char getRandomLowerCaseLetter() { 9 return getRandomCharacter('a', 'z'); 10 } 11 12 /** Generate a random uppercase letter */ 13 public static char getRandomUpperCaseLetter() { 14 return getRandomCharacter('A', 'Z'); 15 } 16 17 /** Generate a random digit character */ 18 public static char getRandomDigitCharacter() { 19 return getRandomCharacter('0', '9'); 20 } 21 22 /** Generate a random character */ 23 public static char getRandomCharacter() { 24 return getRandomCharacter('\u0000', '\uFFFF'); 25 } 26 }
Listing 6.11 gives a test program that displays 175 random lowercase letters.
Listing 6.11 TestRandomCharacter.java 1 public class TestRandomCharacter { 2 /** Main method */ 3 public static void main(String[] args) { 4 final int NUMBER_OF_CHARS = 175; 5 final int CHARS_PER_LINE = 25; 6 7 // Print random characters between 'a' and 'z', 25 chars per line 8 for (int i = 0; i < NUMBER_OF_CHARS; i++) { 9 char ch = RandomCharacter.getRandomLowerCaseLetter(); 10 if ((i + 1) % CHARS_PER_LINE == 0) 11 System.out.println(ch);
getRandomCharacter()
getRandomLower CaseLetter()
getRandomUpper CaseLetter()
getRandomDigit Character()
getRandomCharacter
constants
lowercase letter
M06_LIAN1878_11_GE_C06.indd 248 1/2/18 10:42 PM
6.11 Method Abstraction and Stepwise Refinement 249
12 else 13 System.out.print(ch); 14 } 15 } 16 }
gmjsohezfkgtazqgmswfclrao pnrunulnwmaztlfjedmpchcif lalqdgivxkxpbzulrmqmbhikr lbnrjlsopfxahssqhwuuljvbe xbhdotzhpehbqmuwsfktwsoli cbuwkzgxpmtzihgatdslvbwbz bfesoklwbhnooygiigzdxuqni
Line 9 invokes getRandomLowerCaseLetter() defined in the RandomCharacter class. Note getRandomLowerCaseLetter() does not have any parameters, but you still have to use the parentheses when defining and invoking the method.
6.11 Method Abstraction and Stepwise Refinement The key to developing software is to apply the concept of abstraction.
You will learn many levels of abstraction from this book. Method abstraction is achieved by separating the use of a method from its implementation. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is also known as information hiding or encapsulation. If you decide to change the implementation, the client program will not be affected, provided that you do not change the method signature. The implementation of the method is hidden from the client in a “black box,” as shown in Figure 6.7.
parentheses required
Point Key
method abstraction
information hiding
VideoNote
Stepwise refinement
Figure 6.7 The method body can be thought of as a black box that contains the detailed implementation for the method.
Method Header
Black box
Optional arguments for input
Optional return value
Method Body
You have already used the System.out.print method to display a string and the max method to find the maximum number. You know how to write the code to invoke these methods in your program, but as a user of these methods, you are not required to know how they are implemented.
The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the divide-and-conquer strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.
Suppose that you write a program that displays the calendar for a given month of the year. The program prompts the user to enter the year and the month, and then displays the entire calendar for the month, as presented in the following sample run:
divide and conquer stepwise refinement
M06_LIAN1878_11_GE_C06.indd 249 1/2/18 10:42 PM
250 Chapter 6 Methods
Let us use this example to demonstrate the divide-and-conquer approach.
6.11.1 Top-Down Design How would you get started on such a program? Would you immediately start coding? Begin- ning programmers often start by trying to work out the solution to every detail. Although details are important in the final program, concern for detail in the early stages may block the problem-solving process. To make problem solving flow as smoothly as possible, this example begins by using method abstraction to isolate details from design and only later implements the details.
For this example, the problem is first broken into two subproblems: get input from the user, and print the calendar for the month. At this stage, you should be concerned with what the subproblems will achieve, not with how to get input and print the calendar for the month. You can draw a structure chart to help visualize the decomposition of the problem (see Figure 6.8a).
Enter full year (e.g., 2012): 2012
Enter month as number between 1 and 12: 3
March 2012
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
Figure 6.8 The structure chart shows the printCalendar problem is divided into two subproblems, readInput and printMonth in (a), and printMonth is divided into two smaller subproblems, printMonthTitle and printMonth Body in (b).
printCalendar (main)
(a) (b)
printMonthBodyprintMonthTitlereadInput printMonth
printMonth
You can use Scanner to read input for the year and the month. The problem of printing the calendar for a given month can be broken into two subproblems: print the month title, and print the month body, as shown in Figure 6.8b. The month title consists of three lines: month and year, a dashed line, and the names of the seven days of the week. You need to get the month name (e.g., January) from the numeric month (e.g., 1). This is accomplished in getMonthName (see Figure 6.9a).
In order to print the month body, you need to know which day of the week is the first day of the month (getStartDay) and how many days the month has (getNumberOfDaysInMonth),
M06_LIAN1878_11_GE_C06.indd 250 1/2/18 10:42 PM
6.11 Method Abstraction and Stepwise Refinement 251
as shown in Figure 6.9b. For example, December 2013 has 31 days, and December 1, 2013 is a Sunday.
How would you get the start day for the first date in a month? There are several ways to do so. For now, we’ll use an alternative approach. Assume you know that the start day for January 1, 1800 was a Wednesday (START_DAY_FOR_JAN_1_1800 = 3). You could compute the total number of days (totalNumberOfDays) between January 1, 1800 and the first date of the calendar month. The start day for the calendar month is (totalNumberOfDays + START_ DAY_FOR_JAN_1_1800) % 7, since every week has seven days. Thus, the getStartDay problem can be further refined as getTotalNumberOfDays, as shown in Figure 6.10a.
Figure 6.9 (a) To printMonthTitle, you need getMonthName. (b) The printMonthBody problem is refined into several smaller problems.
(b)(a)
getNumberOfDaysInMonthgetStartDaygetMonthName
printMonthTitle
printMonthBody
Figure 6.10 (a) To getStartDay, you need getTotalNumberOfDays. (b) The getTotalNumberOfDays problem is refined into two smaller problems.
getTotalNumberOfDays
getStartDay
isLeapYear
(a) (b)
getNumberOfDaysInMonth
getTotalNumberOfDays
To get the total number of days, you need to know whether the year is a leap year and the number of days in each month. Thus, getTotalNumberOfDays can be further refined into two subproblems: isLeapYear and getNumberOfDaysInMonth, as shown in Figure 6.10b. The complete structure chart is shown in Figure 6.11.
6.11.2 Top-Down and/or Bottom-Up Implementation Now we turn our attention to implementation. In general, a subproblem corresponds to a method in the implementation, although some are so simple that this is unnecessary. You would need to decide which modules to implement as methods and which to combine with other methods. Decisions of this kind should be based on whether the overall program will be easier to read as a result of your choice. In this example, the subproblem readInput can be simply implemented in the main method.
You can use either a “top-down” or a “bottom-up” approach. The top-down approach imple- ments one method in the structure chart at a time from the top to the bottom. Stubs—a simple but incomplete version of a method—can be used for the methods waiting to be implemented. The use of stubs enables you to quickly build the framework of the program. Implement the main
stub
top-down approach
M06_LIAN1878_11_GE_C06.indd 251 1/2/18 10:42 PM
252 Chapter 6 Methods
method first then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin as follows:
public class PrintCalendar { /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in);
// Prompt the user to enter year System.out.print("Enter full year (e.g., 2012): "); int year = input.nextInt();
// Prompt the user to enter month System.out.print("Enter month as a number between 1 and 12: "); int month = input.nextInt();
// Print calendar for the month of the year printMonth(year, month); }
/** A stub for printMonth may look like this */ public static void printMonth(int year, int month) { System.out.print(month + " " + year); }
/** A stub for printMonthTitle may look like this */ public static void printMonthTitle(int year, int month) { }
/** A stub for printMonthBody may look like this */ public static void printMonthBody(int year, int month) { }
Figure 6.11 The structure chart shows the hierarchical relationship of the subproblems in the program.
printCalendar (main)
getTotalNumberOfDays
getNumberOfDaysInMonth
isLeapYear
printMonthreadInput
printMonthTitle
getMonthName
printMonthBody
getStartDay
M06_LIAN1878_11_GE_C06.indd 252 1/2/18 10:42 PM
6.11 Method Abstraction and Stepwise Refinement 253
/** A stub for getMonthName may look like this */ public static String getMonthName(int month) { return "January"; // A dummy value }
/** A stub for getStartDay may look like this */ public static int getStartDay(int year, int month) { return 1; // A dummy value }
/** A stub for getTotalNumberOfDays may look like this */ public static int getTotalNumberOfDays(int year, int month) { return 10000; // A dummy value }
/** A stub for getNumberOfDaysInMonth may look like this */ public static int getNumberOfDaysInMonth(int year, int month) { return 31; // A dummy value }
/** A stub for isLeapYear may look like this */ public static boolean isLeapYear(int year) { return true; // A dummy value } }
Compile and test the program, and fix any errors. You can now implement the printMonth method. For methods invoked from the printMonth method, you can again use stubs.
The bottom-up approach implements one method in the structure chart at a time from the bottom to the top. For each method implemented, write a test program, known as the driver, to test it. The top-down and bottom-up approaches are equally good: Both approaches imple- ment methods incrementally, help to isolate programming errors, and make debugging easy. They can be used together.
6.11.3 Implementation Details The isLeapYear(int year) method can be implemented using the following code from Section 3.11:
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
Use the following facts to implement getTotalNumberOfDaysInMonth(int year, int month):
■■ January, March, May, July, August, October, and December have 31 days.
■■ April, June, September, and November have 30 days.
■■ February has 28 days during a regular year, and 29 days during a leap year. A regular year, therefore, has 365 days, and a leap year has 366 days.
To implement getTotalNumberOfDays(int year, int month), you need to compute the total number of days (totalNumberOfDays) between January 1, 1800 and the first day of the calendar month. You could find the total number of days between the year 1800 and the calendar year then figure out the total number of days prior to the calendar month in the calendar year. The sum of these two totals is totalNumberOfDays.
To print a body, first pad some space before the start day then print the lines for every week. The complete program is given in Listing 6.12.
bottom-up approach driver
M06_LIAN1878_11_GE_C06.indd 253 1/2/18 10:42 PM
254 Chapter 6 Methods
Listing 6.12 PrintCalendar.java 1 import java.util.Scanner; 2 3 public class PrintCalendar { 4 /** Main method */ 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter year 9 System.out.print("Enter full year (e.g., 2012): "); 10 int year = input.nextInt(); 11 12 // Prompt the user to enter month 13 System.out.print("Enter month as a number between 1 and 12: "); 14 int month = input.nextInt(); 15 16 // Print calendar for the month of the year 17 printMonth(year, month); 18 } 19 20 /** Print the calendar for a month in a year */ 21 public static void printMonth(int year, int month) { 22 // Print the headings of the calendar 23 printMonthTitle(year, month); 24 25 // Print the body of the calendar 26 printMonthBody(year, month); 27 } 28 29 /** Print the month title, e.g., March 2012 */ 30 public static void printMonthTitle(int year, int month) { 31 System.out.println(" " + getMonthName(month) 32 + " " + year); 33 System.out.println("−−−−−−−−−−−−−−−−−−−−−−−−−−−−−"); 34 System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); 35 } 36 37 /** Get the English name for the month */ 38 public static String getMonthName(int month) { 39 String monthName = ""; 40 switch (month) { 41 case 1: monthName = "January"; break; 42 case 2: monthName = "February"; break; 43 case 3: monthName = "March"; break; 44 case 4: monthName = "April"; break; 45 case 5: monthName = "May"; break; 46 case 6: monthName = "June"; break; 47 case 7: monthName = "July"; break; 48 case 8: monthName = "August"; break; 49 case 9: monthName = "September"; break; 50 case 10: monthName = "October"; break; 51 case 11: monthName = "November"; break; 52 case 12: monthName = "December"; 53 } 54 55 return monthName; 56 } 57 58 /** Print month body */
printMonth
printMonthTitle
getMonthName
M06_LIAN1878_11_GE_C06.indd 254 1/2/18 10:42 PM
6.11 Method Abstraction and Stepwise Refinement 255
59 public static void printMonthBody(int year, int month) { 60 // Get start day of the week for the first date in the month 61 int startDay = getStartDay(year, month); 62 63 // Get number of days in the month 64 int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month); 65 66 // Pad space before the first day of the month 67 int i = 0; 68 for (i = 0; i < startDay; i++) 69 System.out.print(" "); 70 71 for (i = 1; i <= numberOfDaysInMonth; i++) { 72 System.out.printf("%4d", i); 73 74 if ((i + startDay) % 7 == 0) 75 System.out.println(); 76 } 77 78 System.out.println(); 79 } 80 81 /** Get the start day of month/1/year */ 82 public static int getStartDay(int year, int month) { 83 final int START_DAY_FOR_JAN_1_1800 = 3; 84 // Get total number of days from 1/1/1800 to month/1/year 85 int totalNumberOfDays = getTotalNumberOfDays(year, month); 86 87 // Return the start day for month/1/year 88 return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7; 89 } 90 91 /** Get the total number of days since January 1, 1800 */ 92 public static int getTotalNumberOfDays(int year, int month) { 93 int total = 0; 94 95 // Get the total days from 1800 to 1/1/year 96 for (int i = 1800; i < year; i++) 97 if (isLeapYear(i)) 98 total = total + 366; 99 else 100 total = total + 365; 101 102 // Add days from Jan to the month prior to the calendar month 103 for (int i = 1; i < month; i++) 104 total = total + getNumberOfDaysInMonth(year, i); 105 106 return total; 107 } 108 109 /** Get the number of days in a month */ 110 public static int getNumberOfDaysInMonth(int year, int month) { 111 if (month == 1 || month == 3 || month == 5 || month == 7 || 112 month == 8 || month == 10 || month == 12) 113 return 31; 114 115 if (month == 4 || month == 6 || month == 9 || month == 11) 116 return 30; 117 118 if (month == 2) return isLeapYear(year) ? 29 : 28;
getStartDay
printMonthBody
getTotalNumberOfDays
getNumberOfDaysInMonth
M06_LIAN1878_11_GE_C06.indd 255 1/2/18 10:42 PM
256 Chapter 6 Methods
119 120 return 0; // If month is incorrect 121 } 122 123 /** Determine if it is a leap year */ 124 public static boolean isLeapYear(int year) { 125 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); 126 } 127 }
The program does not validate user input. For instance, if the user enters either a month not in the range between 1 and 12 or a year before 1800, the program displays an erroneous calen- dar. To avoid this error, add an if statement to check the input before printing the calendar.
This program prints calendars for a month, but could easily be modified to print calendars for a whole year. Although it can print months only after January 1800, it could be modified to print months before 1800.
6.11.4 Benefits of Stepwise Refinement Stepwise refinement breaks a large problem into smaller manageable subproblems. Each sub- problem can be implemented using a method. This approach makes the program easier to write, reuse, debug, test, modify, and maintain.
Simpler Program The print calendar program is long. Rather than writing a long sequence of statements in one method, stepwise refinement breaks it into smaller methods. This simplifies the program and makes the whole program easier to read and understand.
Reusing Methods Stepwise refinement promotes code reuse within a program. The isLeapYear method is defined once and invoked from the getTotalNumberOfDays and getNumberOfDaysInMonth methods. This reduces redundant code.
Easier Developing, Debugging, and Testing Since each subproblem is solved in a method, a method can be developed, debugged, and tested individually. This isolates the errors and makes developing, debugging, and testing easier.
When implementing a large program, use the top-down and/or bottom-up approach. Do not write the entire program at once. Using these approaches seems to take more development time (because you repeatedly compile and run the program), but it actually saves time and makes debugging easier.
Better Facilitating Teamwork When a large problem is divided into subprograms, subproblems can be assigned to different programmers. This makes it easier for programmers to work in teams.
isLeapYear
incremental development and testing
Key terms actual parameter 229 ambiguous invocation 245 argument 229 divide and conquer 249 formal parameter (i.e., parameter) 229 information hiding 249 method 228 method abstraction 249
method overloading 243 method signature 229 modifier 229 parameter 229 pass-by-value 236 scope of a variable 246 stepwise refinement 249 stub 251
M06_LIAN1878_11_GE_C06.indd 256 1/2/18 10:42 PM
ChapTer summary 1. Making programs modular and reusable is one of the central goals in software engineer-
ing. Java provides many powerful constructs that help to achieve this goal. Methods are one such construct.
2. The method header specifies the modifiers, return value type, method name, and param- eters of the method. The static modifier is used for all the methods in this chapter.
3. A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void.
4. The parameter list refers to the type, order, and number of a method’s parameters. The method name and the parameter list together constitute the method signature. Parameters are optional; that is, a method doesn’t need to contain any parameters.
5. A return statement can also be used in a void method for terminating the method and returning to the method’s caller. This is useful occasionally for circumventing the normal flow of control in a method.
6. The arguments that are passed to a method should have the same number, type, and order as the parameters in the method signature.
7. When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed, or when its method-ending closing brace is reached.
8. A value-returning method can also be invoked as a statement in Java. In this case, the caller simply ignores the return value.
9. A method can be overloaded. This means that two methods can have the same name, as long as their method parameter lists differ.
10. A variable declared in a method is called a local variable. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared and initialized before it is used.
11. Method abstraction is achieved by separating the use of a method from its implementa- tion. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as information hiding or encapsulation.
12. Method abstraction modularizes programs in a neat, hierarchical manner. Programs written as collections of concise methods are easier to write, debug, maintain, and modify than would otherwise be the case. This writing style also promotes method reusability.
13. When implementing a large program, use the top-down and/or bottom-up coding approach. Do not write the entire program at once. This approach may seem to take more time for coding (because you are repeatedly compiling and running the program), but it actually saves time and makes debugging easier.
Chapter Summary 257
M06_LIAN1878_11_GE_C06.indd 257 1/2/18 10:42 PM
258 Chapter 6 Methods
Quiz Answer the quiz for this chapter online at the Companion Website.
programming exerCiSeS
Note A common error for the exercises in this chapter is that students don’t implement the methods to meet the requirements even though the output from the main program is correct. For an example of this type of error, see liveexample.pearsoncmg.com/etc/ CommonMethodErrorJava.pdf.
Sections 6.2–6.9 6.1 (Math: pentagonal numbers) A pentagonal number is defined as n(3n-1)/2 for
n = 1 , 2 , …, and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . . . Write a method with the following header that returns a pentagonal number:
public static int getPentagonalNumber(int n)
For example, getPentagonalNumber(1) returns 1 and getPentagonal Number(2) returns 5. Write a test program that uses this method to display the first 100 pentagonal numbers with 10 numbers on each line. Use the %7d format to display each number.
*6.2 (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header:
public static int sumDigits(long n)
For example, sumDigits(234) returns 9 (= 2 + 3 + 4). (Hint: Use the % opera- tor to extract digits and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4 ). To remove 4 from 234, use 234 / 10 (= 2 3 ). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer then displays the sum of all its digits.
**6.3 (Palindrome integer) Write the methods with the following headers: // Return the reversal of an integer, e.g., reverse(456) returns 654 public static int reverse(int number)
// Return true if number is a palindrome public static boolean isPalindrome(int number)
Use the reverse method to implement isPalindrome. A number is a palin- drome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.
*6.4 (Display an integer reversed) Write a method with the following header to display an integer in reverse order:
public static void reverse(int number)
For example, reverse(3456) displays 6543. Write a test program that prompts the user to enter an integer then displays its reversal.
VideoNote
Reverse an integer
M06_LIAN1878_11_GE_C06.indd 258 1/2/18 10:42 PM
*6.5 (Sort three numbers) Write a method with the following header to display three numbers in decreasing order:
public static void displaySortedNumbers( double num1, double num2, double num3)
Write a test program that prompts the user to enter three numbers and invokes the method to display them in decreasing order.
*6.6 (Display patterns) Write a method to display a pattern as follows:
1 1 2 1 2 3 ... n n−1 ... 3 2 1
The method header is
public static void displayPattern(int n)
*6.7 (Financial application: compute the future investment value) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula in Programming Exercise 2.21.
Use the following method header:
public static double futureInvestmentValue( double investmentAmount, double monthlyInterestRate,int years)
For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59.
Write a test program that prompts the user to enter the investment amount (e.g., 1,000) and the interest rate (e.g., 9%) and prints a table that displays future value for the years from 1 to 30, as shown below:
The amount invested: 1000
Annual interest rate: 9 Years Future Value 1 1093.80 2 1196.41 ... 29 13467.25 30 14730.57
6.8 (Conversions between mile and kilometer) Write a class that contains the follow- ing two methods:
/** Convert from Mile to Kilometer */ public static double mileToKilometer(double mile)
/** Convert from Kilometer to Mile */ public static double kilometerToMile(double kilometer)
Programming Exercises 259
M06_LIAN1878_11_GE_C06.indd 259 1/2/18 10:42 PM
260 Chapter 6 Methods
The formula for the conversion is:
1 mile = 1.6 kilometers
Write a test program that invokes these methods to display the following tables:
Miles Kilometers | Kilometers Miles
1 1.609 | 20 12.430
2 3.218 | 25 15.538
...
9 14.481 | 60 37.290
10 16.090 | 65 40.398
6.9 (Conversions between pounds and kilograms) Write a class that contains the following two methods:
/** Convert from pounds to kilograms */ public static double poundToKilogram(double pound)
/** Convert from kilograms to pounds */ public static double kilogramToPound(double kilogram)
The formula for the conversion is:
pound = 0.453 * kilogram kilogram = 2.204 * pound
Write a test program that invokes these methods to display the following tables:
Kilograms Pounds | Pounds Kilograms
1 2.2 | 20 9.09 3 6.6 | 25 11.36 ... 197 433.4 | 510 231.82 199 437.8 | 515 234.09
6.10 (Use the isPrime Method) Listing 6.7, PrimeNumberMethod.java, provides the isPrime(int number) method for testing whether a number is prime. Use this method to find the number of prime numbers less than 10000.
6.11 (Financial application: compute commissions) Write a method that computes the commission, using the scheme in Programming Exercise 5.39. The header of the method is as follows:
public static double computeCommission(double salesAmount)
Write a test program that displays the following table:
Sales Amount Commission
10000 900.0
15000 1500.0
...
95000 11100.0
100000 11700.0
M06_LIAN1878_11_GE_C06.indd 260 1/2/18 10:42 PM
6.12 (Display numbers) Write a method that prints numbers using the following header:
public static void printNumbers(int num1, int num2, int numberPerLine)
This method prints the characters between num1 and num2 with the specified numbers per line. Write a test program that prints ten characters per line from 1 to 100. Numbers are separated by exactly one space.
*6.13 (Sum series) Write a method to compute the following series:
m(i) = 1 3
+ 2 4
+ g + i
i + 2
Write a test program that displays the following table:
I m(i)
1 0.3333 2 0.8333 ... 19 14.7093 20 15.6184
*6.14 (Estimate p) p can be computed using the following summation:
m (i) = 4a1 - 1 3
+ 1 5
- 1 7
+ 1 9
- 1
1 1 + g +
(-1 )i + 1
2 i - 1 b
Write a method that returns m(i) for a given i and write a test program that displays the following table:
i m(i)
1 4.0000
101 3.1515
201 3.1466
301 3.1449
401 3.1441
501 3.1436
601 3.1433
701 3.1430
801 3.1428
901 3.1427
*6.15 (Financial application: print a tax table) Listing 3.5 gives a program to compute tax. Write a method for computing tax using the following header:
public static double computeTax(int status, double taxableIncome)
VideoNote
Estimate p
Programming Exercises 261
M06_LIAN1878_11_GE_C06.indd 261 1/2/18 10:42 PM
262 Chapter 6 Methods
Use this method to write a program that prints a tax table for taxable income from $50,000 to $70,000 with intervals of $100 for all the following statuses:
Taxable Income
Single Married Joint or Qualifying Widow(er)
Married Separate
Head of a House
50000 8688 6665 8688 7353 50100 8713 6680 8713 7378 ... 69900 13663 9850 12328 9840 70000 13688 9875 12353 9853
Hint: round the tax into integers using Math.round (i.e., Math .round(computeTax(status, taxableIncome))).
*6.16 (Number of days in a year) Write a method that returns the number of days in a year using the following header:
public static int numberOfDaysInAYear(int year)
Write a test program that displays the number of days in the years between 2014 and 2034.
Sections 6.10 and 6.11 *6.17 (Display matrix of 0s and 1s) Write a method that displays an n-by-n matrix using
the following header:
public static void printMatrix(int n)
Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run:
Enter n: 3 0 1 0 0 0 0 1 1 1
**6.18 (Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows:
■■ A password must have at least ten characters. ■■ A password consists of only letters and digits. ■■ A password must contain at least three digits.
Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise.
*6.19 (Triangles) Implement the following two methods:
/** Return true if the sum of every two sides is * greater than the third side. */ public static boolean isValid( double side1, double side2, double side3)
M06_LIAN1878_11_GE_C06.indd 262 1/2/18 10:42 PM
/** Return the area of the triangle. */ public static double area( double side1, double side2, double side3)
Write a test program that reads three sides for a triangle and uses the isValid method to test if the input is valid and uses the area method to obtain the area. The program displays the area if the input is valid. Otherwise, it displays that the input is invalid. The formula for computing the area of a triangle is given in Programming Exercise 2.19.
*6.20 (Count the letters in a string) Write a method that counts the number of letters in a string using the following header:
public static int countLetters(String s)
Write a test program that prompts the user to enter a string and displays the num- ber of letters in the string.
*6.21 (Phone keypads) The international standard letter/number mapping for tele- phones is given in Programming Exercise 4.15. Write a method that returns a number, given an uppercase letter, as follows:
public static int getNumber(char uppercaseLetter)
Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact. Here are sample runs of the program:
Enter a string: 1-800-Flowers 1-800-3569377
Enter a string: 1800flowers 18003569377
**6.22 (Math: approximate the square root) There are several techniques for implement- ing the sqrt method in the Math class. One such technique is known as the Babylonian method. It approximates the square root of a number, n, by repeat- edly performing the calculation using the following formula:
nextGuess = (lastGuess + n / lastGuess) / 2
When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. The initial guess can be any positive value (e.g., 1). This value will be the starting value for lastGuess. If the difference between nextGuess and lastGuess is less than a very small number, such as 0.0001, you can claim that nextGuess is the approximated square root of n. If not, next Guess becomes lastGuess and the approximation process continues. Implement the following method that returns the square root of n:
public static double sqrt(long n)
*6.23 (Occurrences of a specified character) Write a method that finds the number of occurrences of a specified character in a string using the following header:
public static int count(String str, char a)
Programming Exercises 263
M06_LIAN1878_11_GE_C06.indd 263 1/2/18 10:42 PM
264 Chapter 6 Methods
For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string followed by a character then displays the number of occurrences of the character in the string.
Sections 6.10–6.12 **6.24 (Display current date and time) Listing 2.7, ShowCurrentTime.java, displays the
current time. Revise this example to display the current date and time. The calen- dar example in Listing 6.12, PrintCalendar.java, should give you some ideas on how to find the year, month, and day.
**6.25 (Convert milliseconds to hours, minutes, and seconds) Write a method that con- verts milliseconds to hours, minutes, and seconds using the following header:
public static String convertMillis(long millis)
The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
Comprehensive **6.26 (Palindromic prime) A palindromic prime is a prime number and also palindromic.
For example, 131 is a prime and also a palindromic prime, as are 313 and 757. Write a program that displays the first 120 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows:
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
**6.27 (Emirp) An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are emirps. Write a program that displays the first 120 emirps. Display 10 numbers per line, separated by exactly one space, as follows:
13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389 ...
**6.28 (Mersenne prime) A prime number is called a Mersenne prime if it can be written in the form 2 p - 1 for some positive integer p. Write a program that finds all Mersenne primes with p … 31 and displays the output as follows:
p 2^p – 1
2 3
3 7
5 31
...
**6.29 (Twin primes) Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are twin primes. Write a program to find all twin primes less than 1,200. Display the output as follows:
(3, 5) (5, 7) ...
M06_LIAN1878_11_GE_C06.indd 264 1/2/18 10:42 PM
**6.30 (Game: craps) Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respec- tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.
Your program acts as a single player. Here are some sample runs.
You rolled 5 + 6 = 11 You win
You rolled 1 + 2 = 3 You lose
You rolled 4 + 4 = 8 point is 8 You rolled 6 + 2 = 8 You win
You rolled 3 + 2 = 5 point is 5 You rolled 2 + 5 = 7 You lose
**6.31 (Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with
■■ 4 for Visa cards ■■ 5 for Master cards ■■ 37 for American Express cards ■■ 6 for Discover cards
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly, or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustra- tion, consider the card number 4388576018402626):
1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
4388576018402626
2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12 (1 + 2 = 3) 5 * 2 = 10 (1 + 0 = 1) 8 * 2 = 16 (1 + 6 = 7) 4 * 2 = 8
Programming Exercises 265
M06_LIAN1878_11_GE_C06.indd 265 1/2/18 10:42 PM
266 Chapter 6 Methods
2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 3 7 3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 3 8 4. Sum the results from Step 2 and Step 3.
3 7 + 3 8 = 7 5 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods:
/** Return true if the card number is valid */ public static boolean isValid(long number)
/** Get the result from Step 2 */ public static int sumOfDoubleEvenPlace(long number)
/** Return this number if it is a single digit, otherwise, * return the sum of the two digits */ public static int getDigit(int number)
/** Return sum of odd-place digits in number */ public static int sumOfOddPlace(long number)
/** Return true if the number d is a prefix for number */ public static boolean prefixMatched(long number, int d)
/** Return the number of digits in d */ public static int getSize(long d)
/** Return the first k number of digits from number. If the * number of digits in number is less than k, return number. */ public static long getPrefix(long number, int k)
Here are sample runs of the program: (You may also implement this program by reading the input as a string and processing the string to validate the credit card.)
Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid
Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid
Current date and time is May 16, 2012 10:34:23
**6.32 (Game: chance of winning at craps) Revise Exercise 6.30 to run it 15,000 times and display the number of winning games.
**6.33 (Current date and time) Invoking System.currentTimeMillis() returns the elapsed time in milliseconds since midnight of January 1, 1970. Write a program that displays the date and time. Here is a sample run:
M06_LIAN1878_11_GE_C06.indd 266 1/2/18 10:42 PM
**6.34 (Print calendar) Programming Exercise 3.21 uses Zeller’s congruence to calcu- late the day of the week. Simplify Listing 6.12, PrintCalendar.java, using Zeller’s algorithm to get the start day of the month.
6.35 (Geometry: area of a pentagon) The area of a pentagon can be computed using the following formula:
Area = 5 * s 2
4 * tan ap 5 b
Write a method that returns the area of a pentagon using the following header:
public static double area(double side)
Write a main method that prompts the user to enter the side of a pentagon and displays its area. Here is a sample run:
Enter the side: 5.5 The area of the pentagon is 52.04444136781625
*6.36 (Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is
Area = n * s 2
4 * tan ap n b
Write a method that returns the area of a regular polygon using the following header:
public static double area(int n, double side)
Write a main method that prompts the user to enter the number of sides and the side of a regular polygon and displays its area. Here is a sample run:
Enter the number of sides: 5 Enter the side: 6.5 The area of the polygon is 72.69017017488385
6.37 (Format an integer) Write a method with the following header to format the inte- ger with the specified width.
public static String format(int number, int width)
The method returns a string for the number with one or more prefix 0s. The size of the string is the width. For example, format(34, 4) returns 0034 and format(34, 5) returns 00034. If the number is longer than the width, the method returns the string representation for the number. For example, format(34, 1) returns 34.
Write a test program that prompts the user to enter a number and its width, and displays a string returned by invoking format(number, width).
Programming Exercises 267
M06_LIAN1878_11_GE_C06.indd 267 1/2/18 10:42 PM
268 Chapter 6 Methods
*6.38 (Generate random characters) Use the methods in RandomCharacter in Listing 6.10 to print 200 uppercase letters and then 200 single digits, printing ten per line.
6.39 (Geometry: point position) Programming Exercise 3.32 shows how to test whether a point is on the left side of a directed line, on the right, or on the same line. Write the methods with the following headers:
/** Return true if point (x2, y2) is on the left side of the * directed line from (x0, y0) to (x1, y1) */ public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2)
/** Return true if point (x2, y2) is on the same * line from (x0, y0) to (x1, y1) */ public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2)
/** Return true if point (x2, y2) is on the * line segment from (x0, y0) to (x1, y1) */ public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2)
Write a program that prompts the user to enter the three points for p0, p1, and p2 and displays whether p2 is on the left side of the line from p0 to p1, right side, the same line, or on the line segment. Here are some sample runs:
Enter three points for p0, p1, and p2: 1 1 2 2 1.5 1.5 (1.5, 1.5) is on the line segment from (1.0, 1.0) to (2.0, 2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 3 3 (3.0, 3.0) is on the same line from (1.0, 1.0) to (2.0, 2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 1 1.5 (1.0, 1.5) is on the left side of the line from (1.0, 1.0) to (2.0, 2.0)
Enter three points for p0, p1, and p2: 1 1 2 2 1 –1 (1.0, −1.0) is on the right side of the line from (1.0, 1.0) to (2.0, 2.0)
M06_LIAN1878_11_GE_C06.indd 268 1/2/18 10:42 PM
Single-Dimensional Arrays
Objectives ■■ To describe why arrays are necessary in programming (§7.1).
■■ To declare array reference variables and create arrays (§§7.2.1 and 7.2.2).
■■ To obtain array size using arrayRefVar.length and know default values in an array (§7.2.3).
■■ To access array elements using indexes (§7.2.4).
■■ To declare, create, and initialize an array using an array initializer (§7.2.5).
■■ To program common array operations (displaying arrays, summing all elements, finding the minimum and maximum elements, random shuf- fling, and shifting elements) (§7.2.6).
■■ To simplify programming using the foreach loops (§7.2.7).
■■ To apply arrays in application development (AnalyzeNumbers, and DeckOfCards) (§§7.3 and 7.4).
■■ To copy contents from one array to another (§7.5).
■■ To develop and invoke methods with array arguments and return values (§§7.6–7.8).
■■ To define a method with a variable-length argument list (§7.9).
■■ To search elements using the linear (§7.10.1) or binary (§7.10.2) search algorithm.
■■ To sort an array using the selection sort approach (§7.11).
■■ To use the methods in the java.util.Arrays class (§7.12).
■■ To pass arguments to the main method from the command line (§7.13).
Chapter
7
M07_LIAN1878_11_GE_C07.indd 269 1/3/18 12:10 AM
270 Chapter 7 Single-Dimensional Arrays
7.1 Introduction A single array variable can reference a large collection of data.
Often you will have to store a large number of values during the execution of a program. Suppose, for instance, that you need to read 100 numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, then compares each number with the average to determine whether it is above the average. In order to accomplish this task, the numbers must all be stored in variables. You have to declare 100 variables and repeatedly write almost identical code 100 times. Writing a program this way would be impractical. So, how do you solve this problem?
An efficient, organized approach is needed. Java and most other high-level languages pro- vide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. In the present case, you can store all 100 numbers into an array and access them through a single array variable.
This chapter introduces single-dimensional arrays. The next chapter will introduce two- dimensional and multidimensional arrays.
7.2 Array Basics Once an array is created, its size is fixed. An array reference variable is used to access the elements in an array using an index.
An array is used to store a collection of data, but often we find it more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, . . . , and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], . . . , and numbers[99] to represent individual variables. This section introduces how to declare array variables, create arrays, and process arrays using indexes.
7.2.1 Declaring Array Variables To use an array in a program, you must declare a variable to reference the array and specify the array’s element type. Here is the syntax for declaring an array variable:
elementType[] arrayRefVar;
or
elementType arrayRefVar[]; // Allowed, but not preferred
The elementType can be any data type, and all elements in the array will have the same data type. For example, the following code declares a variable myList that references an array of double elements.
double[] myList;
or
double myList[]; // Allowed, but not preferred
Note You can use elementType arrayRefVar[] to declare an array variable. This style comes from the C/C+ + language and was adopted in Java to accommodate C/C+ + programmers. The style elementType[] arrayRefVar is preferred.
Point Key
problem why array?
Point Key
index
element type
preferred syntax
M07_LIAN1878_11_GE_C07.indd 270 1/3/18 12:10 AM
7.2 Array Basics 271
7.2.2 Creating Arrays Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. It creates only a storage location for the refer- ence to an array. If a variable does not contain a reference to an array, the value of the variable is null. You cannot assign elements to an array unless it has already been created. After an array variable is declared, you can create an array by using the new operator and assign its reference to the variable with the following syntax:
arrayRefVar = new elementType[arraySize];
This statement does two things: (1) it creates an array using new elementType[arraySize] and (2) it assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement as
elementType[] arrayRefVar = new elementType[arraySize];
or
elementType arrayRefVar[] = new elementType[arraySize];
Here is an example of such a statement:
double[] myList = new double[10];
This statement declares an array variable, myList, creates an array of 10 elements of double type, and assigns its reference to myList. To assign values to the elements, use the syntax
arrayRefVar[index] = value;
For example, the following code initializes the array:
myList[0] = 5.6; myList[1] = 4.5; myList[2] = 3.3; myList[3] = 13.2; myList[4] = 4.0; myList[5] = 34.33; myList[6] = 34.0; myList[7] = 45.45; myList[8] = 99.993; myList[9] = 11123;
This array is illustrated in Figure 7.1.
null
new operator
Figure 7.1 The array myList has 10 elements of double type and int indices from 0 to 9.
double[] myList = new double[10];
myList reference myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[6]
myList[5]
myList[7]
myList[8]
myList[9]
Array reference variable
Array element at index 5
5.6
4.5
3.3
13.2
4.0
34.33
34.0
45.45
99.993
11123
Element value
M07_LIAN1878_11_GE_C07.indd 271 1/3/18 12:10 AM
272 Chapter 7 Single-Dimensional Arrays
Note An array variable that appears to hold an array actually contains a reference to that array. Strictly speaking, an array variable and an array are different, but most of the time the distinction can be ignored. Thus, it is all right to say, for simplicity, that myList is an array, instead of stating, at greater length, that myList is a variable that contains a refer- ence to an array of double elements.
7.2.3 Array Size and Default Values When space for an array is allocated, the array size must be given, specifying the number of ele- ments that can be stored in it. The size of an array cannot be changed after the array is created. Size can be obtained using arrayRefVar.length. For example, myList.length is 10.
When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, \u0000 for char types, and false for boolean types.
7.2.4 Accessing Array Elements The array elements are accessed through the index. Array indices are 0 based; that is, they range from 0 to arrayRefVar.length − 1. In the example in Figure 7.1, myList holds 10 double values, and the indices are from 0 to 9.
Each element in the array is represented using the following syntax, known as an indexed variable:
arrayRefVar[index];
For example, myList[9] represents the last element in the array myList.
Caution Some programming languages use parentheses to reference an array element, as in myList(9), but Java uses brackets, as in myList[9].
An indexed variable can be used in the same way as a regular variable. For example, the following code adds the values in myList[0] and myList[1] to myList[2]:
myList[2] = myList[0] + myList[1];
The following loop assigns 0 to myList[0], 1 to myList[1], . . . , and 9 to myList[9]:
for (int i = 0; i < myList.length; i++) { myList[i] = i; }
7.2.5 Array Initializers Java has a shorthand notation, known as the array initializer, which combines the declaration, creation, and initialization of an array in one statement using the following syntax:
elementType[] arrayRefVar = {value0, value1, ..., valuek};
For example, the statement
double[] myList = {1.9, 2.9, 3.4, 3.5};
declares, creates, and initializes the array myList with four elements, which is equivalent to the following statements:
double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9;
array vs. array variable
array length
default values
0 based index
indexed variable
array initializer
M07_LIAN1878_11_GE_C07.indd 272 1/3/18 12:10 AM
7.2 Array Basics 273
myList[2] = 3.4; myList[3] = 3.5;
Caution The new operator is not used in the array-initializer syntax. Using an array initializer, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. Thus, the next statement is wrong:
double[] myList; myList = {1.9, 2.9, 3.4, 3.5}; // Wrong
7.2.6 Processing Arrays When processing array elements, you will often use a for loop for one of two reasons:
1. All of the elements in an array are of the same type. They are evenly processed in the same fashion repeatedly using a loop.
2. Since the size of the array is known, it is natural to use a for loop.
Assume that the array is created as follows:
double[] myList = new double[10];
The following are some examples of processing arrays:
1. Initializing arrays with input values: The following loop initializes the array myList with user input values:
java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter " + myList.length + " values: "); for (int i = 0; i < myList.length; i++) myList[i] = input.nextDouble();
2. Initializing arrays with random values: The following loop initializes the array myList with random values between 0.0 and 100.0, but less than 100.0:
for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }
3. Displaying arrays: To print an array, you have to print each element in the array using a loop such as the following:
for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); }
Tip For an array of the char[] type, it can be printed using one print statement. For example, the following code displays Dallas:
char[] city = {'D', 'a', 'l', 'l', 'a', 's'}; System.out.println(city);
4. Summing all elements: Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop such as the following:
double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; }
print character array
M07_LIAN1878_11_GE_C07.indd 273 1/3/18 12:10 AM
274 Chapter 7 Single-Dimensional Arrays
5. Finding the largest element: Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element with max, and update max if the element is greater than max.
double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; }
6. Finding the smallest index of the largest element: Often you need to locate the largest element in an array. If an array has multiple elements with the same largest value, find the smallest index of such an element. Suppose that the array myList is {1, 5, 3, 4, 5, 5}. The largest element is 5, and the smallest index for 5 is 1. Use a variable named max to store the largest element, and a variable named indexOfMax to denote the index of the largest element. Initially max is myList[0] and indexOfMax is 0. Compare each element in myList with max and update max and indexOfMax if the element is greater than max.
double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } }
7. Random shuffling: In many applications, you need to randomly reorder the elements in an array. This is called shuffling. To accomplish this, for each element myList[i], randomly generate an index j and swap myList[i] with myList[j], as follows:
double temp = myList[0]; // Retain the �rst element
// Shift elements left for (int i = 1; i < myList.length; i++) { myList[i - 1] = myList[i]; }
// Move the �rst element to �ll in the last position myList[myList.length - 1] = temp;
myList
8. Shifting elements: Sometimes you need to shift the elements left or right. Here is an example of shifting the elements one position to the left and filling the last element with the first element:
9. Simplifying coding: Arrays can be used to greatly simplify coding for certain tasks. For example, suppose you wish to obtain the English name of a given month by its number. If the month names are stored in an array, the month name for a given month can be
swap
myList i
[1]
[i]
for (int i = 0; i < myList.length – 1; i++) { // Generate an index j randomly int j = (int)(Math.random() * myList.length);
// Swap myList[i] with myList[j] double temp = myList[i]; myList[i] = myList[j]; myList[j] = temp; }
.
.[0]
A random index [j]
random shuffling
VideoNote
Random shuffling
M07_LIAN1878_11_GE_C07.indd 274 1/3/18 12:10 AM
7.2 Array Basics 275
accessed simply via the index. The following code prompts the user to enter a month number and displays its month name:
String[] months = {"January", "February",..., "December"}; System.out.print("Enter a month number (1 to 12): "); int monthNumber = input.nextInt(); System.out.println("The month is " + months[monthNumber − 1]);
If you didn’t use the months array, you would have to determine the month name using a lengthy multiway if−else statement as follows:
if (monthNumber == 1) System.out.println("The month is January"); else if (monthNumber == 2) System.out.println("The month is February"); ... else System.out.println("The month is December");
7.2.7 Foreach Loops Java supports a convenient for loop, known as a foreach loop, which enables you to traverse the array sequentially without using an index variable. For example, the following code dis- plays all the elements in the array myList:
for (double e: myList) { System.out.println(e); }
You can read the code as “for each element e in myList, do the following.” Note that the variable, e, must be declared as the same type as the elements in myList.
In general, the syntax for a foreach loop is
for (elementType element: arrayRefVar) { // Process the element }
You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array.
Caution Accessing an array out of bounds is a common programming error that throws a runtime ArrayIndexOutOfBoundsException. To avoid it, make sure you do not use an index beyond arrayRefVar.length − 1 or simply using a foreach loop if possible.
Programmers often mistakenly reference the first element in an array with index 1, but it should be 0. This is called the off-by-one error. Another common off-by-one error in a loop is using <= where < should be used. For example, the following loop is wrong:
for (int i = 0; i <= list.length; i++) System.out.print(list[i] + " ");
The <= should be replaced by <. Using a foreach loop can avoid the off-by-one error in this case.
7.2.1 How do you declare an array reference variable and how do you create an array? 7.2.2 When is the memory allocated for an array?
ArrayIndexOutOfBounds- Exception
off-by-one error
Point Check
M07_LIAN1878_11_GE_C07.indd 275 1/3/18 12:10 AM
276 Chapter 7 Single-Dimensional Arrays
7.2.3 What is the output of the following code? int x = 30; int[] numbers = new int[x]; x = 60; System.out.println("x is " + x); System.out.println("The size of numbers is " + numbers.length);
7.2.4 Indicate true or false for the following statements: a. Every element in an array has the same type. b. The array size is fixed after an array reference variable is declared. c. The array size is fixed after it is created. d. The elements in an array must be of a primitive data type.
7.2.5 Which of the following statements are valid? a. int i = new int(30); b. double d[] = new double[30]; c. char[] r = new char(1..30); d. int i[] = (3, 4, 3, 2); e. float f[] = {2.3, 4.5, 6.6}; f. char[] c = new char();
7.2.6 How do you access elements in an array? 7.2.7 What is the array index type? What is the lowest index? What is the representation
of the third element in an array named a?
7.2.8 Write statements to do the following:
a. Create an array to hold 10 double values.
b. Assign the value 5.5 to the last element in the array.
c. Display the sum of the first two elements.
d. Write a loop that computes the sum of all elements in the array.
e. Write a loop that finds the minimum element in the array.
f. Randomly generate an index and display the element of this index in the array.
g. Use an array initializer to create another array with the initial values 3.5, 5.5, 4.52, and 5.6.
7.2.9 What happens when your program attempts to access an array element with an invalid index?
7.2.10 Identify and fix the errors in the following code:
1 public class Test { 2 public static void main(String[] args) { 3 double[100] r; 4 5 for (int i = 0; i < r.length(); i++); 6 r(i) = Math.random * 100; 7 } 8 }
7.2.11 What is the output of the following code?
1 public class Test { 2 public static void main(String[] args) { 3 int list[] = {1, 2, 3, 4, 5, 6};
M07_LIAN1878_11_GE_C07.indd 276 1/3/18 12:10 AM
7.3 Case Study: Analyzing Numbers 277
4 for (int i = 1; i < list.length; i++) 5 list[i] = list[i − 1]; 6 7 for (int i = 0; i < list.length; i++) 8 System.out.print(list[i] + " "); 9 } 10 }
7.3 Case Study: Analyzing Numbers The problem is to write a program that finds the number of items above the average of all items.
Now you can write a program using arrays to solve the problem proposed at the beginning of this chapter. The problem is to read 100 numbers, get the average of these numbers, and find the number of the items greater than the average. To be flexible for handling any number of inputs, we will let the user enter the number of inputs, rather than fixing it to 100. Listing 7.1 gives a solution.
Point Key
Enter the number of items: 10
Enter the numbers: 3.4 5 6 1 6.5 7.8 3.5 8.5 6.3 9.5
Average is 5.75
Number of elements above the average is 6
Listing 7.1 AnalyzeNumbers.java 1 public class AnalyzeNumbers { 2 public static void main(String[] args) { numbers[0]: 3 java.util.Scanner input = new java.util.Scanner(System.in); numbers[1]: 4 System.out.print("Enter the number of items: "); numbers[2]: 5 int n = input.nextInt(); 6 double[] numbers = new double[n]; . 7 double sum = 0; . 8 numbers[i]: . 9 System.out.print("Enter the numbers: "); 10 for (int i = 0; i < n; i++) { numbers[n-3]: 11 numbers[i] = input.nextDouble(); numbers[n-2]: 12 sum += numbers[i]; numbers[n-1]: 13 } 14 15 double average = sum / n; 16 17 int count = 0; // The number of elements above average 18 for (int i = 0; i < n; i++) 19 if (numbers[i] > average) 20 count++; 21 22 System.out.println("Average is " + average); 23 System.out.println("Number of elements above the average is " 24 + count); 25 } 26 }
create array
store number in array
get average
above average?
The program prompts the user to enter the array size (line 5) and creates an array with the specified size (line 6). The program reads the input, stores numbers into the array (line 11), adds each number to sum in line 11, and obtains the average (line 15). It then compares
M07_LIAN1878_11_GE_C07.indd 277 1/3/18 12:10 AM
278 Chapter 7 Single-Dimensional Arrays
each number in the array with the average to count the number of values above the average (lines 7–20).
7.4 Case Study: Deck of Cards The problem is to create a program that will randomly select four cards from a deck of cards.
Say you want to write a program that will pick four cards at random from a deck of 52 cards. All the cards can be represented using an array named deck, filled with initial values 0–51, as follows:
int[] deck = new int[52];
// Initialize cards for (int i = 0; i < deck.length; i++) deck[i] = i;
Card numbers 0–12, 13–25, 26–38, and 39–51 represent 13 Spades, 13 Hearts, 13 Diamonds, and 13 Clubs, respectively, as shown in Figure 7.2. cardNumber / 13 determines the suit of the card, and cardNumber % 13 determines the rank of the card, as shown in Figure 7.3. After shuffling the array deck, pick the first four cards from deck. The program displays the cards from these four card numbers.
Point Key
VideoNote
Deck of cards
Figure 7.2 52 cards are stored in an array named deck.
0 . . .
12 13 . . .
25 26 . . .
38 39 . . .
51
13 Diamonds ( )
13 Clubs ( )
0 . . .
12 13 . . .
25 26 . . .
38 39 . . .
51
deck [0] . . .
[12] [13]
.
.
. [25] [26]
.
.
. [38] [39]
.
.
. [51]
Random shuf�e
6 48 11 24 . . . . . . . . . . . . . . . .
deck [0] [1] [2] [3] [4] [5] . . .
[25] [26]
.
.
. [38] [39]
.
.
. [51]
Card number 6 is the 7 (6 % 13 = 6) of Spades (6 / 13 is 0)
Card number 48 is the 10 (48 % 13 = 9) of Clubs (48 / 13 is 3)
Card number 11 is the Queen (11 % 13 = 11) of Spades (11 / 13 is 0)
Card number 24 is the Queen (24 % 13 = 11) of Hearts (24 / 13 is 1)
13 Hearts ( )
13 Spades ( )
Figure 7.3 cardNumber identifies a card’s suit and rank number.
cardNumber / 13 =
0
3
2
1
Spades
Hearts
Diamonds
Clubs
cardNumber % 13 =
0
11
10
.
Ace
1 2
.
12
Jack
Queen
King
M07_LIAN1878_11_GE_C07.indd 278 1/3/18 12:10 AM
7.4 Case Study: Deck of Cards 279
Listing 7.2 gives the solution to the problem.
Listing 7.2 DeckOfCards.java 1 public class DeckOfCards { 2 public static void main(String[] args) { 3 int[] deck = new int[52]; 4 String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"}; 5 String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", 6 "10", "Jack", "Queen", "King"}; 7 8 // Initialize the cards 9 for (int i = 0; i < deck.length; i++) 10 deck[i] = i; 11 12 // Shuffle the cards 13 for (int i = 0; i < deck.length; i++) { 14 // Generate an index randomly 15 int index = (int)(Math.random() * deck.length); 16 int temp = deck[i]; 17 deck[i] = deck[index]; 18 deck[index] = temp; 19 } 20 21 // Display the first four cards 22 for (int i = 0; i < 4; i++) { 23 String suit = suits[deck[i] / 13]; 24 String rank = ranks[deck[i] % 13]; 25 System.out.println("Card number " + deck[i] + ": " 26 + rank + " of " + suit); 27 } 28 } 29 }
create array deck array of strings array of strings
initialize deck
shuffle deck
suit of a card rank of a card
Card number 6: 7 of Spades Card number 48: 10 of Clubs Card number 11: Queen of Spades Card number 24: Queen of Hearts
The program creates an array suits for four suits (line 4) and an array ranks for 13 cards in a suit (lines 5 and 6). Each element in these arrays is a string.
The program initializes deck with values 0–51 in lines 9 and 10. The deck value 0 repre- sents the Ace of Spades, 1 represents the card 2 of Spades, 13 represents the Ace of Hearts, and 14 represents the 2 of Hearts.
Lines 13–19 randomly shuffle the deck. After a deck is shuffled, deck[i] contains an arbitrary value. deck[i] / 13 is 0, 1, 2, or 3, which determines the suit (line 23). deck[i] % 13 is a value between 0 and 12, which determines the rank (line 24). If the suits array is not defined, you would have to determine the suit using a lengthy multiway if−else state- ment as follows:
if (deck[i] / 13 == 0) System.out.print("suit is Spades"); else if (deck[i] / 13 == 1) System.out.print("suit is Hearts"); else if (deck[i] / 13 == 2) System.out.print("suit is Diamonds"); else System.out.print("suit is Clubs");
M07_LIAN1878_11_GE_C07.indd 279 1/3/18 12:10 AM
280 Chapter 7 Single-Dimensional Arrays
With suits = {"Spades", "Hearts", "Diamonds", "Clubs"} created in an array, suits[deck[i] / 13] gives the suit for deck[i]. Using arrays greatly simplifies the solu- tion for this program.
7.4.1 Will the program pick four random cards if you replace lines 22–27 in Listing 7.2, DeckOfCards.java, with the following code?
for (int i = 0; i < 4; i++) { int cardNumber = (int)(Math.random() * deck.length); String suit = suits[cardNumber / 13]; String rank = ranks[cardNumber % 13]; System.out.println("Card number " + cardNumber + ": " + rank + " of " + suit); }
7.5 Copying Arrays To copy the contents of one array into another, you have to copy the array’s individ- ual elements into the other array.
Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows:
list2 = list1;
However, this statement does not copy the contents of the array referenced by list1 to list2, but instead merely copies the reference value from list1 to list2. After this statement, list1 and list2 reference the same array, as shown in Figure 7.4. The array previously referenced by list2 is no longer referenced; it becomes garbage, which will be automatically collected by the Java Virtual Machine. This process is called garbage collection.
Point Check
Point Key
copy reference
garbage collection
Figure 7.4 Before the assignment statement, list1 and list2 point to separate memory locations. After the assignment, the reference of the list1 array is passed to list2.
Contents of list1
Contents of list1
Contents of list2
Contents of list2
list1
list2
Before the assignment list2 = list1;
list1
list2
After the assignment list2 = list1;
In Java, you can use assignment statements to copy primitive data type variables, but not arrays. Assigning one array variable to another array variable actually copies one reference to another and makes both variables point to the same memory location.
There are three ways to copy arrays:
1. Use a loop to copy individual elements one by one.
2. Use the static arraycopy method in the System class.
3. Use the clone method to copy arrays; this will be introduced in Chapter 13, Abstract Classes and Interfaces.
M07_LIAN1878_11_GE_C07.indd 280 1/3/18 12:10 AM
7.6 Passing Arrays to Methods 281
You can write a loop to copy every element from the source array to the corresponding element in the target array. The following code, for instance, copies sourceArray to targetArray using a for loop:
int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArray.length; i++) { targetArray[i] = sourceArray[i]; }
Another approach is to use the arraycopy method in the java.lang.System class to copy arrays instead of using a loop. The syntax for arraycopy is:
arraycopy(sourceArray, srcPos, targetArray, tarPos, length);
The parameters srcPos and tarPos indicate the starting positions in sourceArray and targetArray, respectively. The number of elements copied from sourceArray to targetArray is indicated by length. For example, you can rewrite the loop using the following statement:
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
The arraycopy method does not allocate memory space for the target array. The target array must have already been created with its memory space allocated. After the copying takes place, targetArray and sourceArray have the same content but independent memory locations.
Note The arraycopy method violates the Java naming convention. By convention, this method should be named arrayCopy (i.e., with an uppercase C).
7.5.1 Use the arraycopy method to copy the following array to a target array t: int[] source = {3, 4, 5};
7.5.2 Once an array is created, its size cannot be changed. Does the following code resize the array?
int[] myList; myList = new int[10]; // Sometime later you want to assign a new array to myList myList = new int[20];
7.6 Passing Arrays to Methods When passing an array to a method, the reference of the array is passed to the method.
Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }
You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2.
printArray(new int[]{3, 1, 2, 6, 4, 2});
arraycopy method
Point Check
Point Key
M07_LIAN1878_11_GE_C07.indd 281 1/3/18 12:10 AM
282 Chapter 7 Single-Dimensional Arrays
Note The preceding statement creates an array using the following syntax:
new elementType[]{value0, value1, ..., valuek};
There is no explicit reference variable for the array. Such array is called an anonymous array.
Java uses pass-by-value to pass arguments to a method. There are important differences between passing the values of variables of primitive data types and passing arrays.
■■ For an argument of a primitive type, the argument’s value is passed.
■■ For an argument of an array type, the value of the argument is a reference to an array; this reference value is passed to the method. Semantically, it can be best described as pass-by-sharing, that is, the array in the method is the same as the array being passed. Thus, if you change the array in the method, you will see the change outside the method.
Take the following code, for example:
public class TestArrayArguments { public static void main(String[] args) { int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values
m(x, y); // Invoke m with arguments x and y
System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); }
public static void m(int number, int[] numbers) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] } }
anonymous array
pass-by-value
pass-by-sharing
x is 1 y[0] is 5555
Figure 7.5 The primitive type value in x is passed to number, and the reference value in y is passed to numbers.
reference
Activation record for the main method int[] y: int x: 1
Stack
Activation record for method m int[] numbers: int number: 1 An array of
ten int values is stored here
Arrays are stored in a heap.
Heap
reference
You may wonder why after m is invoked, x remains 1, but y[0] becomes 5555. This is because y and numbers, although they are independent variables, reference the same array, as illustrated in Figure 7.5. When m(x, y) is invoked, the values of x and y are passed to number and numbers. Since y contains the reference value to the array, numbers now contains the same reference value to the same array.
M07_LIAN1878_11_GE_C07.indd 282 1/3/18 12:10 AM
7.6 Passing Arrays to Methods 283
Note Arrays are objects in Java (objects are introduced in Chapter 9). The JVM stores the objects in an area of memory called the heap, which is used for dynamic memory allocation.
Listing 7.3 gives another program that shows the difference between passing a primitive data type value and an array reference variable to a method.
The program contains two methods for swapping elements in an array. The first method, named swap, fails to swap two int arguments. The second method, named swapFirst TwoInArray, successfully swaps the first two elements in the array argument.
Listing 7.3 TestPassArray.java 1 public class TestPassArray { 2 /** Main method */ 3 public static void main(String[] args) { 4 int[] a = {1, 2}; 5 6 // Swap elements using the swap method 7 System.out.println("Before invoking swap"); 8 System.out.println("array is {" + a[0] + ", " + a[1] + "}"); 9 swap(a[0], a[1]); 10 System.out.println("After invoking swap"); 11 System.out.println("array is {" + a[0] + ", " + a[1] + "}"); 12 13 // Swap elements using the swapFirstTwoInArray method 14 System.out.println("Before invoking swapFirstTwoInArray"); 15 System.out.println("array is {" + a[0] + ", " + a[1] + "}"); 16 swapFirstTwoInArray(a); 17 System.out.println("After invoking swapFirstTwoInArray"); 18 System.out.println("array is {" + a[0] + ", " + a[1] + "}"); 19 } 20 21 /** Swap two variables */ 22 public static void swap(int n1, int n2) { 23 int temp = n1; 24 n1 = n2; 25 n2 = temp; 26 } 27 28 /** Swap the first two elements in the array */ 29 public static void swapFirstTwoInArray(int[] array) { 30 int temp = array[0]; 31 array[0] = array[1]; 32 array[1] = temp; 33 } 34 }
heap
false swap
swap array elements
Before invoking swap array is {1, 2} After invoking swap array is {1, 2} Before invoking swapFirstTwoInArray array is {1, 2} After invoking swapFirstTwoInArray array is {2, 1}
M07_LIAN1878_11_GE_C07.indd 283 1/3/18 12:10 AM
284 Chapter 7 Single-Dimensional Arrays
As shown in Figure 7.6, the two elements are not swapped using the swap method. However, they are swapped using the swapFirstTwoInArray method. Since the parameters in the swap method are primitive type, the values of a[0] and a[1] are passed to n1 and n2 inside the method when invoking swap(a[0], a[1]). The memory locations for n1 and n2 are independent of the ones for a[0] and a[1]. The contents of the array are not affected by this call.
Figure 7.6 When passing an array to a method, the reference of the array is passed to the method.
Invoke swap(int n1, int n2). The primitive type values in a[0] and a[1] are passed to the swap method.
Invoke swapFirstTwoInArray(int[] array). The reference value in a is passed to the swapFirstTwoInArray method.
The arrays are stored in a heap.
Stack Heap
Activation record for the swap method
Activation record for the main method
n2: 2 n1: 1
int[] a reference reference
reference
Stack
Activation record for the swapFirstTwoInArray method
Activation record for the main method
int[] a
int[] array
a[0]: 1 a[1]: 2
l public static int[] reverse(int[] list) { 2 int[] result = new int[list.length]; 3 4 for (int i = 0, j = result.length - 1; 5 i < list.length; i++, j--) { 6 result[j] = list[i]; 7 } 8 9 return result; 10 }
list
result
The parameter in the swapFirstTwoInArray method is an array. As shown in Figure 7.6, the reference of the array is passed to the method. Thus, the variables a (outside the method) and array (inside the method) both refer to the same array in the same memory location. Therefore, swapping array[0] with array[1] inside the method swapFirstTwoInArray is the same as swapping a[0] with a[1] outside of the method.
7.7 Returning an Array from a Method When a method returns an array, the reference of the array is returned.
You can pass arrays when invoking a method. A method may also return an array. For example, the following method returns an array that is the reversal of another array.Point
Key
create array
return array
Line 2 creates a new array result. Lines 4–7 copy elements from array list to array result. Line 9 returns the array. For example, the following statement returns a new array list2 with elements 6, 5, 4, 3, 2, 1:
int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);
M07_LIAN1878_11_GE_C07.indd 284 1/3/18 12:10 AM
7.8 Case Study: Counting the Occurrences of Each Letter 285
7.7.1 Suppose the following code is written to reverse the contents in an array, explain why it is wrong. How do you fix it?
int[] list = {1, 2, 3, 5, 4};
for (int i = 0, j = list.length − 1; i < list.length; i++, j−−) { // Swap list[i] with list[j] int temp = list[i]; list[i] = list[j]; list[j] = temp; }
7.8 Case Study: Counting the Occurrences of Each Letter
This section presents a program to count the occurrences of each letter in an array of characters.
The program given in Listing 7.4 does the following:
1. Generates 100 lowercase letters randomly and assigns them to an array of characters, as shown in Figure 7.7a. You can obtain a random letter by using the getRandomLower CaseLetter() method in the RandomCharacter class in Listing 6.10.
2. Count the occurrences of each letter in the array. To do so, create an array, say counts, of 26 int values, each of which counts the occurrences of a letter, as shown in Figure 7.7b. That is, counts[0] counts the number of a’s, counts[1] counts the number of b’s, and so on.
Point Check
Point Key
Figure 7.7 The chars array stores 100 characters, and the counts array stores 26 counts, each of which counts the occurrences of a letter.
…
…
chars[0]
chars[1]
…
…
chars[98]
chars[99]
…
…
counts[0]
counts[1]
…
…
counts[24]
counts[25]
(a) (b)
Listing 7.4 CountLettersInArray.java 1 public class CountLettersInArray { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Declare and create an array 5 char[] chars = createArray(); 6 7 // Display the array 8 System.out.println("The lowercase letters are:"); 9 displayArray(chars); 10
create array
pass array
M07_LIAN1878_11_GE_C07.indd 285 1/3/18 12:10 AM
286 Chapter 7 Single-Dimensional Arrays
11 // Count the occurrences of each letter 12 int[] counts = countLetters(chars); 13 14 // Display counts 15 System.out.println(); 16 System.out.println("The occurrences of each letter are:"); 17 displayCounts(counts); 18 } 19 20 /** Create an array of characters */ 21 public static char[] createArray() { 22 // Declare an array of characters and create it 23 char[] chars = new char[100]; 24 25 // Create lowercase letters randomly and assign 26 // them to the array 27 for (int i = 0; i < chars.length; i++) 28 chars[i] = RandomCharacter.getRandomLowerCaseLetter(); 29 30 // Return the array 31 return chars; 32 } 33 34 /** Display the array of characters */ 35 public static void displayArray(char[] chars) { 36 // Display the characters in the array 20 on each line 37 for (int i = 0; i < chars.length; i++) { 38 if ((i + 1) % 20 == 0) 39 System.out.println(chars[i]); 40 else 41 System.out.print(chars[i] + " "); 42 } 43 } 44 45 /** Count the occurrences of each letter */ 46 public static int[] countLetters(char[] chars) { 47 // Declare and create an array of 26 int 48 int[] counts = new int[26]; 49 50 // For each lowercase letter in the array, count it 51 for (int i = 0; i < chars.length; i++) 52 counts[chars[i] - 'a']++; 53 54 return counts; 55 } 56 57 /** Display counts */ 58 public static void displayCounts(int[] counts) { 59 for (int i = 0; i < counts.length; i++) { 60 if ((i + 1) % 10 == 0) 61 System.out.println(counts[i] + " " + (char)(i + 'a')); 62 else 63 System.out.print(counts[i] + " " + (char)(i + 'a') + " "); 64 } 65 } 66 }
return array
pass array
increase count
M07_LIAN1878_11_GE_C07.indd 286 1/3/18 12:10 AM
7.8 Case Study: Counting the Occurrences of Each Letter 287
The createArray method (lines 21–32) generates an array of 100 random lowercase letters. Line 5 invokes the method and assigns the array to chars. What would be wrong if you rewrote the code as follows?
char[] chars = new char[100]; chars = createArray();
You would be creating two arrays. The first line would create an array by using new char[100]. The second line would create an array by invoking createArray() and assign the reference of the array to chars. The array created in the first line would be garbage because it is no longer referenced, and as mentioned earlier, Java automatically collects garbage behind the scenes. Your program would compile and run correctly, but it would create an array unnecessarily.
Invoking getRandomLowerCaseLetter() (line 28) returns a random lowercase letter. This method is defined in the RandomCharacter class in Listing 6.10.
The countLetters method (lines 46–55) returns an array of 26 int values, each of which stores the number of occurrences of a letter. The method processes each letter in the array and increases its count by one. A brute-force approach to count the occurrences of each letter might be as follows:
for (int i = 0; i < chars.length; i++) if (chars[i] == 'a') counts[0]++; else if (chars[i] == 'b') counts[1]++; ...
However, a better solution is given in lines 51 and 52.
for (int i = 0; i < chars.length; i++) counts[chars[i] − 'a']++;
If the letter (chars[i]) is a, the corresponding count is counts['a' − 'a'] (i.e., counts[0]). If the letter is b, the corresponding count is counts['b' − 'a'] (i.e., counts[1]), since the Unicode of b is one more than that of a. If the letter is z, the corresponding count is counts['z' − 'a'] (i.e., counts[25]), since the Unicode of z is 25 more than that of a.
Figure 7.8 shows the call stack and heap during and after executing createArray. See CheckPoint Question 7.8.3 to show the call stack and heap for other methods in the program.
The lowercase letters are: e y l s r i b k j v j h a b z n w b t v s c c k r d w a m p w v u n q a m p l o a z g d e g f i n d x m z o u l o z j v h w i w n t g x w c d o t x h y v z y z q e a m f w p g u q t r e n n w f c r f
The occurrences of each letter are: 5 a 3 b 4 c 4 d 4 e 4 f 4 g 3 h 3 i 3 j 2 k 3 l 4 m 6 n 4 o 3 p 3 q 4 r 2 s 4 t 3 u 5 v 8 w 3 x 3 y 6 z
M07_LIAN1878_11_GE_C07.indd 287 1/3/18 12:10 AM
288 Chapter 7 Single-Dimensional Arrays
7.8.1 True or false? When an array is passed to a method, a new array is created and passed to the method.
7.8.2 Show the output of the following two programs: Point
Check
Figure 7.8 (a) An array of 100 characters is created when executing createArray. (b) This array is returned and assigned to the variable chars in the main method.
Array of 100 characters
Stack
(a) Executing createArray in line 5
(b) After exiting createArray in line 5
Heap
Activation record for the createArray method
Activation record for the main method
Array of 100 characters
Stack Heap
Activation record for the main method
char[] chars: refchar[] chars: ref
char[] chars: ref
public class Test { public static void main(String[] args) { int number = 0; int[] numbers = new int[1];
m(number, numbers);
System.out.println("number is " + number + " and numbers[0] is " + numbers[0]); }
public static void m(int x, int[] y) { x = 3; y[0] = 3; } }
(a)
public class Test { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); }
public static void reverse(int[] list) { int[] newList = new int[list.length];
for (int i = 0; i < list.length; i++) newList[i] = list[list.length − 1 − i];
list = newList; } }
(b)
7.8.3 Where are the arrays stored during execution? Show the contents of the stack and heap during and after executing displayArray, countLetters, and display Counts in Listing 7.4.
7.9 Variable-Length Argument Lists A variable number of arguments of the same type can be passed to a method and treated as an array.
You can pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows:
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (...). Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it.
Point Key
M07_LIAN1878_11_GE_C07.indd 288 1/3/18 12:10 AM
7.10 Searching Arrays 289
Java treats a variable-length parameter as an array. You can pass an array or a variable number of arguments to a variable-length parameter. When invoking a method with a variable number of arguments, Java creates an array and passes the arguments to it. Listing 7.5 presents a method that prints the maximum value in a list of an unspecified number of values.
Listing 7.5 VarArgsDemo.java 1 public class VarArgsDemo { 2 public static void main(String[] args) { 3 printMax(34, 3, 3, 2, 56.5); 4 printMax(new double[]{1, 2, 3}); 5 } 6 7 public static void printMax(double... numbers) { 8 if (numbers.length == 0) { 9 System.out.println("No argument passed"); 10 return; 11 } 12 13 double result = numbers[0]; 14 15 for (int i = 1; i < numbers.length; i++) 16 if (numbers[i] > result) 17 result = numbers[i]; 18 19 System.out.println("The max value is " + result); 20 } 21 }
Line 3 invokes the printMax method with a variable-length argument list passed to the array numbers. If no arguments are passed, the length of the array is 0 (line 8).
Line 4 invokes the printMax method with an array.
7.9.1 What is wrong with each of the following method headers? a. public static void print(String... strings, double... numbers) b. public static void print(double... numbers, String name) c. public static double... print(double d1, double d2)
7.9.2 Can you invoke the printMax method in Listing 7.5 using the following statements? a. printMax(1, 2, 2, 1, 4); b. printMax(new double[]{1, 2, 3}); c. printMax(new int[]{1, 2, 3});
7.10 Searching Arrays If an array is sorted, binary search is more efficient than linear search for finding an element in the array.
Searching is the process of looking for a specific element in an array—for example, discover- ing whether a certain score is included in a list of scores. Searching is a common task in com- puter programming. Many algorithms and data structures are devoted to searching. This section discusses two commonly used approaches, linear search and binary search.
7.10.1 The Linear Search Approach The linear search approach compares the key element key sequentially with each element in the array. It continues to do so until the key matches an element in the array, or the array is exhausted without a match being found. If a match is made, the linear search returns the index
pass variable-length arg list pass an array arg
a variable-length arg parameter
Point Check
Point Key
linear search binary search
M07_LIAN1878_11_GE_C07.indd 289 1/3/18 12:10 AM
290 Chapter 7 Single-Dimensional Arrays
of the element in the array that matches the key. If no match is found, the search returns −1. The linearSearch method in Listing 7.6 gives the solution.
Listing 7.6 LinearSearch.java linear search animation on Companion Website
list
key Compare key with list[i] for i = 0, 1, …
[0] [1] [2] …
1 public class LinearSearch { 2 /** The method for �nding a key in the list */ 3 public static int linearSearch(int[] list, int key) { 4 for (int i = 0; i < list.length; i++) { 5 if (key == list[i]) 6 return i; 7 } 8 return -1; 9 } 10 }
To better understand this method, trace it with the following statements:
1 int[] list = {1, 4, 4, 2, 5, −3, 6, 2}; 2 int i = linearSearch(list, 4); // Returns 1 3 int j = linearSearch(list, −4); // Returns -1 4 int k = linearSearch(list, −3); // Returns 5
The linear search method compares the key with each element in the array. The elements can be in any order. On average, the algorithm will have to examine half of the elements in an array before finding the key, if it exists. Since the execution time of a linear search increases linearly as the number of array elements increases, linear search is inefficient for a large array.
7.10.2 The Binary Search Approach Binary search is the other common search approach for a list of values. For binary search to work, the elements in the array must already be ordered. Assume that the array is in ascending order. The binary search first compares the key with the element in the middle of the array. Consider the following three cases:
1. If the key is less than the middle element, you need to continue to search for the key only in the first half of the array.
2. If the key is equal to the middle element, the search ends with a match.
3. If the key is greater than the middle element, you need to continue to search for the key only in the second half of the array.
Clearly, the binary search method eliminates at least half of the array after each comparison. Sometimes you eliminate half of the elements, and sometimes you eliminate half plus one. Suppose the array has n elements. For convenience, let n be a power of 2. After the first com- parison, n/2 elements are left for further search; after the second comparison, (n/2)/2 ele- ments are left. After the kth comparison, n/2k elements are left for further search. When k = log2n, only one element is left in the array, and you need only one more comparison. There- fore, in the worst case when using the binary search approach, you need log2n+1 comparisons to find an element in the sorted array. In the worst case for a list of 1024 (210) elements, binary search requires only 11 comparisons, whereas a linear search requires 1023 comparisons in the worst case.
The portion of the array being searched shrinks by half after each comparison. Let low and high denote, respectively, the first index and last index of the array that is currently being searched. Initially, low is 0 and high is list.length − 1. Let mid denote the index of the middle element, so mid is (low + high)/2. Figure 7.9 shows how to find key 11 in the list {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79} using binary search.
binary search animation on Companion Website
M07_LIAN1878_11_GE_C07.indd 290 1/3/18 12:10 AM
7.10 Searching Arrays 291
You now know how the binary search works. The next task is to implement it in Java. Don’t rush to give a complete implementation. Implement it incrementally, one step at a time. You may start with the first iteration of the search, as shown in Figure 7.10a. It compares the key with the middle element in the list whose low index is 0 and high index is list.length − 1. If key < list[mid], set the high index to mid − 1; if key == list[mid], a match is found and return mid; if key > list[mid], set the low index to mid + 1.
Next, consider implementing the method to perform the search repeatedly by adding a loop, as shown in Figure 7.10b. The search ends if the key is found, or if the key is not found when low > high.
why not -1?
Figure 7.9 Binary search eliminates half of the list from further consideration after each comparison.
key is 11 low
key , 50
key . 7
[0] [1] [2] [3] [4] [5] [7] [8] [9] [10] [11]
2list 4 7 10 11 45 50 59 60 66 69 70 79
mid
[6]
high
[12]
low
[0] [1] [2] [3] [4] [5]
2list 4 7 10 11 45
mid high
key 55 11
[3] [4] [5]
list 10 11 45
low mid high
Figure 7.10 Binary search is implemented incrementally.
(a) Version 1
public static int binarySearch( int[] list, int key) { int low = 0; int high = list.length − 1;
int mid = (low + high) / 2; if (key < list[mid]) high = mid − 1; else if (key == list[mid]) return mid; else low = mid + 1;
}
public static int binarySearch( int[] list, int key) { int low = 0; int high = list.length − 1;
while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; }
return 1; // Not found }
(b) Version 2
When the key is not found, low is the insertion point where a key would be inserted to maintain the order of the list. It is more useful to return the insertion point than −1. The method must return a negative value to indicate that the key is not in the list. Can it simply return −low? No. If the key is less than list[0], low would be 0. −0 is 0. This would indicate the key matches list[0]. A good choice is to let the method return −low − 1 if the key is not in the list. Returning −low − 1 indicates not only that the key is not in the list, but also where the key would be inserted.
M07_LIAN1878_11_GE_C07.indd 291 1/3/18 12:10 AM
292 Chapter 7 Single-Dimensional Arrays
The complete program is given in Listing 7.7.
Listing 7.7 BinarySearch.java 1 public class BinarySearch { 2 /** Use binary search to find the key in the list */ 3 public static int binarySearch(int[] list, int key) { 4 int low = 0; 5 int high = list.length − 1; 6 7 while (high >= low) { 8 int mid = (low + high) / 2; 9 if (key < list[mid]) 10 high = mid − 1; 11 else if (key == list[mid]) 12 return mid; 13 else 14 low = mid + 1; 15 } 16 17 return –low - 1; // Now high < low, key not found 18 } 19 }
The binary search returns the index of the search key if it is contained in the list (line 12). Otherwise, it returns −low − 1 (line 17).
What would happen if we replaced (high >= low) in line 7 with (high > low)? The search would miss a possible matching element. Consider a list with just one element. The search would miss the element.
Does the method still work if there are duplicate elements in the list? Yes, as long as the elements are sorted in increasing order. The method returns the index of one of the matching elements if the element is in the list.
The precondition for the binary search method is that the list must be sorted in increasing order. The postcondition is that the method returns the index of the element that matches the key if the key is in the list or a negative integer k such that −k 1 is the position for inserting the key. Precondition and postcondition are the terms often used to describe the properties of a method. Preconditions are the things that are true before the method is invoked, and postconditions are the things that are true after the method is returned:
To better understand this method, trace it with the following statements and identify low and high when the method returns.
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; int i = BinarySearch.binarySearch(list, 2); // Returns 0 int j = BinarySearch.binarySearch(list, 11); // Returns 4 int k = BinarySearch.binarySearch(list, 12); // Returns –6 int l = BinarySearch.binarySearch(list, 1); // Returns –1 int m = BinarySearch.binarySearch(list, 3); // Returns –2
Here is the table that lists the low and high values when the method exits, and the value returned from invoking the method.
Method Low High Value Returned
binarySearch(list, 2) 0 1 0 (mid)
binarySearch(list, 11) 3 5 4 (mid)
binarySearch(list, 12) 5 4 −6
binarySearch(list, 1) 0 −1 −1
binarySearch(list, 3) 1 0 −2
first half
second half
precondition
postcondition
M07_LIAN1878_11_GE_C07.indd 292 1/3/18 12:10 AM
7.11 Sorting Arrays 293
Note Linear search is useful for finding an element in a small array or an unsorted array, but it is inefficient for large arrays. Binary search is more efficient, but it requires that the array be presorted.
7.10.1 If high is a very large integer such as the maximum int value 2147483647, (low + high) / 2 may cause overflow. How do you fix it to avoid overflow?
7.10.2 Use Figure 7.9 as an example to show how to apply the binary search approach to a search for key 10 and key 12 in list {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}.
7.10.3 If the binary search method returns −4, is the key in the list? Where should the key be inserted if you wish to insert the key into the list?
7.11 Sorting Arrays Sorting, like searching, is a common task in computer programming. Many different algorithms have been developed for sorting. This section introduces an intuitive sort- ing algorithm: selection sort.
Suppose you want to sort a list in ascending order. Selection sort finds the smallest number in the list and swaps it with the first element. It then finds the smallest number remaining and swaps it with the second element, and so on, until only a single number remains. Figure 7.11 shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort.
binary search benefits
Point Check
Point Key
selection sort
VideoNote
Selection sort
selection sort animation on Companion Website
Figure 7.11 Selection sort repeatedly selects the smallest number and swaps it with the first number in the list.
Select 1 (the smallest) and swap it with 2 (the �rst) in the list.
The number 1 is now in the correct position, and thus no longer needs to be considered.
The number 2 is now in the correct position, and thus no longer needs to be considered.
The number 4 is now in the correct position, and thus no longer needs to be considered.
The number 5 is now in the correct position, and thus no longer needs to be considered.
The number 6 is now in the correct position, and thus no longer needs to be considered.
2
1
1
1
1
1
1
9
9
2
2
2
2
2
5
swap
5
5
4
4
4
4
4
4
4
5
5
5
5
8
8
8
8
8
6
6
1
2
9
9
9
9
8
6
6
6
6
6
8
9 The number 8 is now in the correct position, and thus no longer needs to be considered.
Select 2 (the smallest) and swap it with 9 (the �rst) in the remaining list.
Select 4 (the smallest) and swap it with 5 (the �rst) in the remaining list.
5 is the smallest and in the right position. No swap is necessary.
Select 6 (the smallest) and swap it with 8 (the �rst) in the remaining list.
Select 8 (the smallest) and swap it with 9 (the �rst) in the remaining list.
Since there is only one element remaining in the list, the sort is completed.
swap
swap
swap
swap
You know how the selection-sort approach works. The task now is to implement it in Java. Beginners find it difficult to develop a complete solution on the first attempt. Start by writing the code for the first iteration to find the smallest element in the list and swap it with the first element, then observe what would be different for the second iteration, the third, and so on. The insight this gives will enable you to write a loop that generalizes all the iterations.
M07_LIAN1878_11_GE_C07.indd 293 1/3/18 12:10 AM
294 Chapter 7 Single-Dimensional Arrays
The solution can be described as follows:
for (int i = 0; i < list.length − 1; i++) { select the smallest element in list[i..list.length−1]; swap the smallest with list[i], if necessary; // list[i] is in its correct position. // The next iteration applies on list[i+1..list.length−1] }
Listing 7.8 implements the solution.
Listing 7.8 SelectionSort.java 1 public class SelectionSort { 2 /** The method for sorting the numbers */ 3 public static void selectionSort(double[] list) { 4 for (int i = 0; i < list.length - 1; i++) { 5 // Find the minimum in the list[i..list.length−1] 6 double currentMin = list[i]; 7 int currentMinIndex = i; 8 9 for (int j = i + 1; j < list.length; j++) { 10 if (currentMin > list[j]) { 11 currentMin = list[j]; 12 currentMinIndex = j; 13 } 14 } 15 16 // Swap list[i] with list[currentMinIndex] if necessary 17 if (currentMinIndex != i) { 18 list[currentMinIndex] = list[i]; 19 list[i] = currentMin; 20 } 21 } 22 } 23 }
The selectionSort(double[] list) method sorts any array of double elements. The method is implemented with a nested for loop. The outer loop (with the loop control vari- able i in line 4) is iterated in order to find the smallest element in the list, which ranges from list[i] to list[list.length−1], and exchanges it with list[i].
The variable i is initially 0. After each iteration of the outer loop, list[i] is in the right place. Eventually, all the elements are put in the right place; therefore, the whole list is sorted.
To understand this method better, trace it with the following statements:
double[] list = {1, 9, 4.5, 6.6, 5.7, −4.5}; SelectionSort.selectionSort(list);
7.11.1 Use Figure 7.11 as an example to show how to apply the selection-sort approach to sort {3.4, 5, 3, 3.5, 2.2, 1.9, 2}.
7.11.2 How do you modify the selectionSort method in Listing 7.8 to sort numbers in decreasing order?
7.12 The Arrays Class The java.util.Arrays class contains useful methods for common array operations such as sorting and searching.
select
swap
Point Check
Point Key
M07_LIAN1878_11_GE_C07.indd 294 1/3/18 12:10 AM
7.12 The Arrays Class 295
The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, filling array elements, and returning a string representation of the array. These methods are overloaded for all primitive types.
You can use the sort or parallelSort method to sort a whole array or a partial array. For example, the following code sorts an array of numbers and an array of characters:
double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); // Sort the whole array java.util.Arrays.parallelSort(numbers); // Sort the whole array
char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; java.util.Arrays.sort(chars, 1, 3); // Sort part of the array java.util.Arrays.parallelSort(chars, 1, 3); // Sort part of the array
Invoking sort(numbers) sorts the whole array numbers. Invoking sort(chars, 1, 3) sorts a partial array from chars[1] to chars[3−1]. parallelSort is more efficient if your computer has multiple processors.
You can use the binarySearch method to search for a key in an array. The array must be pre- sorted in increasing order. If the key is not in the array, the method returns −(insertionIndex + 1). For example, the following code searches the keys in an array of integers and an array of characters:
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; System.out.println("1. Index is " + java.util.Arrays.binarySearch(list, 11)); System.out.println("2. Index is " + java.util.Arrays.binarySearch(list, 12));
char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'}; System.out.println("3. Index is " + java.util.Arrays.binarySearch(chars, 'a')); System.out.println("4. Index is " + java.util.Arrays.binarySearch(chars, 't'));
The output of the preceding code is as follows:
Index is 4
Index is -6 Index is 0
Index is -4
You can use the equals method to check whether two arrays are strictly equal. Two arrays are strictly equal if their corresponding elements are the same. In the following code, list1 and list2 are equal, but list2 and list3 are not.
int[] list1 = {2, 4, 7, 10}; int[] list2 = {2, 4, 7, 10}; int[] list3 = {4, 2, 7, 10}; System.out.println(java.util.Arrays.equals(list1, list2)); // true System.out.println(java.util.Arrays.equals(list2, list3)); // false
You can use the fill method to fill in all or part of the array. For example, the following code fills list1 with 5 and fills 8 into elements list2[1] through list2[5−1].
int[] list1 = {2, 4, 7, 10}; int[] list2 = {2, 4, 7, 7, 7, 10}; java.util.Arrays.fill(list1, 5); // Fill 5 to the whole array java.util.Arrays.fill(list2, 1, 5, 8); // Fill 8 to a partial array
sort
parallelSort
binarySearch
equals
fill
M07_LIAN1878_11_GE_C07.indd 295 1/3/18 12:10 AM
296 Chapter 7 Single-Dimensional Arrays
You can also use the toString method to return a string that represents all elements in the array. This is a quick and simple way to display all elements in the array. For example, the following code:
int[] list = {2, 4, 7, 10}; System.out.println(java.util.Arrays.toString(list));
displays [2, 4, 7, 10].
7.12.1 What types of array can be sorted using the java.util.Arrays.sort method? Does this sort method create a new array?
7.12.2 To apply java.util.Arrays.binarySearch(array, key), should the array be sorted in increasing order, in decreasing order, or neither?
7.12.3 Show the output of the following code: int[] list1 = {2, 4, 7, 10}; java.util.Arrays.fill(list1, 7); System.out.println(java.util.Arrays.toString(list1));
int[] list2 = {2, 4, 7, 10}; System.out.println(java.util.Arrays.toString(list2)); System.out.print(java.util.Arrays.equals(list1, list2));
7.13 Command-Line Arguments The main method can receive string arguments from the command line.
Perhaps you have already noticed the unusual header for the main method, which has the parameter args of the String[] type. It is clear that args is an array of strings. The main method is just like a regular method with a parameter. You can call a regular method by passing actual parameters. Can you pass arguments to main? Yes, of course you can. In the following examples, the main method in class TestMain is invoked by a method in A:
toString
Point Check
Point Key
public class A { public static void main(String[] args) { String[] strings = {"New York", "Boston", "Atlanta"}; TestMain.main(strings); } }
public class TestMain { public static void main(String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }
A main method is just like a regular method. Furthermore, you can pass arguments to a main method from the command line.
7.13.1 Passing Strings to the main Method You can pass strings to a main method from the command line when you run the program. The following command line, for example, starts the program TestMain with three strings: arg0, arg1, and arg2:
java TestMain arg0 arg1 arg2
arg0, arg1, and arg2 are strings, but they don’t have to appear in double quotes on the com- mand line. The strings are separated by a space. A string that contains a space must be enclosed in double quotes. Consider the following command line:
java TestMain "First num" alpha 53
M07_LIAN1878_11_GE_C07.indd 296 1/3/18 12:10 AM
7.13 Command-Line Arguments 297
It starts the program with three strings: First num, alpha, and 53. Since First num is a string, it is enclosed in double quotes. Note 53 is actually treated as a string. You can use "53" instead of 53 in the command line.
When the main method is invoked, the Java interpreter creates an array to hold the com- mand-line arguments and pass the array reference to args. For example, if you invoke a program with n arguments, the Java interpreter creates an array such as the one that follows:
args = new String[n];
The Java interpreter then passes args to invoke the main method.
Note If you run the program with no strings passed, the array is created with new String[0]. In this case, the array is empty with length 0. args references to this empty array. Therefore, args is not null, but args.length is 0.
7.13.2 Case Study: Calculator Suppose you are to develop a program that performs arithmetic operations on integers. The program receives an expression. The expression consists of an integer followed by an operator and another integer. For example, to add two integers, use this command:
java Calculator 2 + 3
The program will display the following output:
2 + 3 = 5
Figure 7.12 shows sample runs of the program. The strings passed to the main program are stored in args, which is an array of strings. The
first string is stored in args[0], and args.length is the number of strings passed. Here are the steps in the program:
1. Use args.length to determine whether the expression has been provided as three arguments in the command line. If not, terminate the program using System.exit(1).
2. Perform a binary arithmetic operation on the operands args[0] and args[2] using the operator in args[1].
VideoNote
Command-line arguments
Figure 7.12 The program takes three arguments (operand1 operator operand2) from the command line and displays the expression and the result of the arithmetic operation.
Add
Subtract
Multiply
Divide
M07_LIAN1878_11_GE_C07.indd 297 1/3/18 12:10 AM
298 Chapter 7 Single-Dimensional Arrays
The program is given in Listing 7.9.
Listing 7.9 Calculator.java 1 public class Calculator { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Check number of strings passed 5 if (args.length != 3) { 6 System.out.println( 7 "Usage: java Calculator operand1 operator operand2"); 8 System.exit(1); 9 } 10 11 // The result of the operation 12 int result = 0; 13 14 // Determine the operator 15 switch (args[1].charAt(0)) { 16 case '+': result = Integer.parseInt(args[0]) + 17 Integer.parseInt(args[2]); 18 break; 19 case '−': result = Integer.parseInt(args[0]) − 20 Integer.parseInt(args[2]); 21 break; 22 case '.': result = Integer.parseInt(args[0]) * 23 Integer.parseInt(args[2]); 24 break; 25 case '/': result = Integer.parseInt(args[0]) / 26 Integer.parseInt(args[2]); 27 } 28 29 // Display result 30 System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] 31 + " = " + result); 32 } 33 }
Integer.parseInt(args[0]) (line 16) converts a digital string into an integer. The string must consist of digits. If not, the program will terminate abnormally.
We used the . symbol for multiplication, not the common * symbol. The reason for this is the * symbol refers to all the files in the current directory when it is used on a command line. The following program displays all the files in the current directory when issuing the com- mand java Test *:
public class Test { public static void main(String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }
To circumvent this problem, we will have to use a different symbol for the multiplication operator.
7.13.1 This book declares the main method as public static void main(String[] args)
Can it be replaced by one of the following lines?
a. public static void main(String args[]) b. public static void main(String[] x)
check argument
check operator
Point Check
M07_LIAN1878_11_GE_C07.indd 298 1/3/18 12:10 AM
Chapter Summary 299
c. public static void main(String x[]) d. static void main(String x[])
7.13.2 Show the output of the following program when invoked using
1. java Test I have a dream
2. java Test “1 2 3”
3. java Test
public class Test { public static void main(String[] args) { System.out.println("Number of strings is " + args.length); for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }
Key Terms anonymous array 282 array 270 array initializer 272 binary search 289 garbage collection 280 index 270
indexed variable 272 linear search 289 off-by-one error 275 postcondition 292 precondition 292 selection sort 293
ChapTer summary 1. A variable is declared as an array type using the syntax elementType[] arrayRefVar
or elementType arrayRefVar[]. The style elementType[] arrayRefVar is preferred, although elementType arrayRefVar[] is legal.
2. Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. An array variable is not a primitive data type variable. An array variable contains a reference to an array.
3. You cannot assign elements to an array unless it has already been created. You can create an array by using the new operator with the following syntax: new elementType[arraySize].
4. Each element in the array is represented using the syntax arrayRefVar[index]. An index must be an integer or an integer expression.
5. After an array is created, its size becomes permanent and can be obtained using array RefVar.length. Since the index of an array always begins with 0, the last index is always arrayRefVar.length − 1. An out-of-bounds error will occur if you attempt to reference elements beyond the bounds of an array.
6. Programmers often mistakenly reference the first element in an array with index 1, but it should be 0. This is called the index off-by-one error.
M07_LIAN1878_11_GE_C07.indd 299 1/3/18 12:10 AM
300 Chapter 7 Single-Dimensional Arrays
7. When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, \u0000 for char types, and false for boolean types.
8. Java has a shorthand notation, known as the array initializer, which combines declaring an array, creating an array, and initializing an array in one statement, using the syntax elementType[] arrayRefVar = {value0, value1, . . . , valuek}.
9. When you pass an array argument to a method, you are actually passing the reference of the array; that is, the called method can modify the elements in the caller’s original array.
10. If an array is sorted, binary search is more efficient than linear search for finding an element in the array.
11. Selection sort finds the smallest number in the list and swaps it with the first element. It then finds the smallest number remaining and swaps it with the first element in the remaining list, and so on, until only a single number remains.
Quiz Answer the quiz for this chapter online at the Companion Website.
programming exerCises
Sections 7.2–7.5 *7.1 (Assign grades) Write a program that reads student scores, gets the best score, and
then assigns grades based on the following scheme:
Grade is A if score is Ú best -5 Grade is B if score is Ú best -10; Grade is C if score is Ú best -15; Grade is D if score is Ú best -20; Grade is F otherwise.
The program prompts the user to enter the total number of students, and then prompts the user to enter all of the scores, and concludes by displaying the grades. Here is a sample run:
Enter the number of students: 4
Enter 4 scores: 40 55 70 58 Student 0 score is 40 and grade is F Student 1 score is 55 and grade is C Student 2 score is 70 and grade is A Student 3 score is 58 and grade is C
7.2 (Comparing numbers) Write a program that reads 11 integers, compares each integer with the 11th integer, and displays whether the integers are “greater”, “smaller”, or “equal” to the 11th integer.
M07_LIAN1878_11_GE_C07.indd 300 1/3/18 12:10 AM
Programming Exercises 301
**7.3 (Count occurrence of numbers) Write a program that reads the integers between 1 and 50 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:
Enter the integers between 1 and 50: 2 5 6 5 4 3 23 43 2 0 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time
Enter numbers: 1 2 3 2 1 6 3 4 5 2 3 6 8 9 9 0 The number of odd numbers: 8 The number of even numbers: 7
Note if a number occurs more than one time, the plural word “times” is used in the output.
7.4 (Analyze scores) Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average, and how many scores are below the average. Enter a negative number to signify the end of the input. Assume the maximum number of scores is 100.
**7.5 (The number of even numbers and odd numbers) Write a program that reads ten integers, and then display the number of even numbers and odd numbers. Assume that the input ends with 0. Here is the sample run of the program.
7.6 (The Sieve of Eratosthenes) The Sieve of Eratosthenes is an algorithm that finds all prime numbers up to a given limit, n. It works by creating an array of Booleans, flag, of size n+1, initializing the array to true (assuming every number is a prime number in the beginning), and iteratively traversing the numbers from 2 to n, setting the values of indices that are multiples of other numbers to false. Write a program using the algorithm above, and display all the prime numbers up to 50.
*7.7 (Count single digits) Write a program that generates 200 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)
Sections 7.6–7.8 7.8 (Average an array) Write two overloaded methods that return the average of an
array with the following headers:
public static int average(int[] array) public static double average(double[] array)
Write a test program that prompts the user to enter 10 double values, invokes this method, then displays the average value.
M07_LIAN1878_11_GE_C07.indd 301 1/3/18 12:10 AM
302 Chapter 7 Single-Dimensional Arrays
7.9 (Find the largest element) Write a method that finds the largest element in an array of double values using the following header:
public static double max(double[] array)
Write a test program that prompts the user to enter ten numbers, invokes this method to return the maximum value, and displays the maximum value. Here is a sample run of the program:
Enter ten numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2 The minimum number is: 6
Enter 10 numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2 The mean is 3.11 The standard deviation is 1.55738
7.10 (Find the index of the largest element) Write a method that returns the index of the largest element in an array of integers. If there is more than one element, return the largest index. Use the following header:
public static int indexOfLargestElement(double[] array)
Write a test program that prompts the user to enter 10 numbers, invokes this method to return the index of the largest element, and displays the index and the element itself.
*7.11 (Statistics: compute deviation) Programming Exercise 5.45 computes the standard deviation of numbers. This exercise uses a different but equivalent formula to compute the standard deviation of n numbers.
mean = a
n
i = 1 xi
n =
x1 + x2 + g + xn n
deviation = H ani = 1(xi - mean)2n - 1 To compute the standard deviation with this formula, you have to store the indi-
vidual numbers using an array, so they can be used after the mean is obtained.
Your program should contain the following methods:
/** Compute the deviation of double values */ public static double deviation(double[] x)
/** Compute the mean of an array of double values */ public static double mean(double[] x)
Write a test program that prompts the user to enter 10 numbers and displays the mean and standard deviation, as presented in the following sample run:
*7.12 (Reverse an array) The reverse method in Section 7.7 reverses an array by copying it to a new array. Rewrite the method that reverses the array passed in the argument and returns this array. Write a test program that prompts the user to enter 10 numbers, invokes the method to reverse the numbers, and displays the numbers.
M07_LIAN1878_11_GE_C07.indd 302 1/3/18 12:10 AM
Programming Exercises 303
Section 7.9 *7.13 (Random number selector) Write a method that returns a random number from a
list of numbers passed in the argument. The method header is specified as follows:
public static int getRandom(int... numbers)
7.14 (Computing lcm) Write a method that returns the lcm (least common multiple) of an unspecified number of integers. The method header is specified as follows:
public static int lcm(int... numbers)
Write a test program that prompts the user to enter five numbers, invokes the method to find the lcm of these numbers, and displays the lcm.
Sections 7.10–7.12 7.15 (Eliminate duplicates) Write a method that returns a new array by eliminating the
duplicate values in the array using the following method header:
public static int[] eliminateDuplicates(int[] list)
Write a test program that reads in 10 integers, invokes the method, and displays the distinct numbers separated by exactly one space. Here is a sample run of the program:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2 The distinct numbers are: 1 2 3 6 4 5
7.16 (Execution time) Write a program that creates an array of numbers from 1 to 100,000,000 in ascending order. Display the execution time of invoking the linearSearch method and the binarySearch method in Listings 7.6 and 7.7 respectively. Display the execution time of invoking both searches for the fol- lowing numbers: 1; 25,000,000; 50,000,000; 75,000,000; 100,000,000. You can use the following code template to obtain the execution time:
long startTime = System.nanoTime(); perform the task; long endTime = System.nanoTime(); long executionTime = endTime − startTime;
**7.17 (Sort students) Write a program that prompts the user to enter the number of stu- dents, the students’ names, and their scores and prints student names in decreasing order of their scores. Assume the name is a string without spaces, use the Scan ner’s next() method to read a name.
**7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bub- ble-sort algorithm makes several passes through the array. On each pass, suc- cessive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top, and the larger values “sink” to the bottom. Write a test program that reads in 10 double numbers, invokes the method, and displays the sorted numbers.
M07_LIAN1878_11_GE_C07.indd 303 1/3/18 12:10 AM
304 Chapter 7 Single-Dimensional Arrays
**7.19 (Sorted?) Write the following method that returns true if the list is already sorted in decreasing order.
public static boolean isSorted(int[] list)
Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list.
Enter list: 8 10 1 5 16 61 9 11 1 The list is not sorted
Enter list: 10 21 11 9 7 5 4 4 3 1 1 The list is already sorted
*7.20 (Descending Bubble Sort) Modify the bubble sort method of Programming Exercise 7.18, and sort the elements in the array in descending order instead of ascending order. Write a test program that reads 10 double numbers, invokes the method, and displays the sorted numbers in descending order.
Section 7.13 *7.21 (Normalizing integers) Write a program that prompts the user to enter 10 integers,
and normalizes the integers to be in the range of 0 to 1 by dividing every integer with the largest integer. Display the 10 normalized real-valued numbers.
*7.22 (Count the number of vowels in a string) Write a program that prompts the user to enter a string, and counts and displays the number of both lowercase and uppercase vowels in the string.
Comprehensive **7.23 (Game: locker puzzle) A school has 100 lockers and 100 students. All lockers are
closed on the first day of school. As the students enter, the first student, denoted as S1, opens every locker. Then the second student, S2, begins with the second locker, denoted as L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100.
After all the students have passed through the building and changed the lockers, which lockers are open? Write a program to find your answer and display all open locker numbers separated by exactly one space.
(Hint: Use an array of 100 Boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.)
**7.24 (Simulation: coupon collector’s problem) Coupon collector is a classic statistics problem with many practical applications. The problem is to repeatedly pick objects from a set of objects and find out how many picks are needed for all the objects to be picked at least once. A variation of the problem is to pick cards from a shuffled deck of 52 cards repeatedly, and find out how many picks are needed before you see one of each suit. Assume a picked card is placed back in the deck
VideoNote
Coupon collector’s problem
M07_LIAN1878_11_GE_C07.indd 304 1/3/18 12:10 AM
Programming Exercises 305
before picking another. Write a program to simulate the number of picks needed to get four cards from each suit and display the four cards picked (it is possible a card may be picked twice). Here is a sample run of the program:
Queen of Spades 5 of Clubs Queen of Hearts 4 of Diamonds Number of picks: 12
Enter list: 5 2 5 6 9 10
The list is sorted
7.25 (Algebra: solve quadratic equations) Write a method for solving a quadratic equa- tion using the following header:
public static int solveQuadratic(double[] eqn, double[] roots)
The coefficients of a quadratic equation ax2 + bx + c = 0 are passed to the array eqn and the real roots are stored in roots. The method returns the number of real roots. See Programming Exercise 3.1 on how to solve a quadratic equation.
Write a program that prompts the user to enter values for a, b, and c and displays the number of real roots and all real roots.
7.26 (Check whether array is sorted) An array list is required to be sorted in ascending order. Write a method that returns true if list is sorted, using the following header:
public static Bboolean isSorted(int[] list)
Write a test program that prompts the user to enter a list of integers. Note the first number in the input indicates the number of elements in the list. This number is not part of the list.
Enter list: 5 2 5 6 1 6
The list is not sorted.
7.27 (Check whether the array is sorted with constant interval) An array list is sorted with a constant interval if its elements are arranged in an ascending order and there is a constant difference between adjacent elements. Write a method that returns true if list is sorted with a constant interval, using the following header:
public static boolean isSortedConstantInterval(int[] list)
Write a test program that prompts the user to enter a list of integers. Note the first number in the input indicates the number of elements in the list. This number is not part of the list.
M07_LIAN1878_11_GE_C07.indd 305 1/3/18 12:10 AM
306 Chapter 7 Single-Dimensional Arrays
*7.28 (Math: permutations) Write a program that prompts the user to enter four integers and then displays all possible ways of arranging the four integers.
*7.29 (Game: dice) Suppose three dice are thrown at random. Write a program that shows all possible permutations or configurations of the three dice that yield the sum of nine.
*7.30 (Pattern recognition: consecutive four equal numbers) Write the following method that tests whether the array has four consecutive numbers with the same value:
public static boolean isConsecutiveFour(int[] values)
Write a test program that prompts the user to enter a series of integers and dis- plays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series. Here are sample runs:
VideoNote
Consecutive four
Enter list: 5 2 5 6 9 10
The list is not sorted with a constant interval
Enter list: 5 2 4 6 8 10 The list is sorted with a constant interval.
Enter the number of values: 8
Enter the values: 3 4 5 5 5 5 4 5
The list has consecutive fours
Enter the number of values: 9
Enter the values: 3 4 5 5 6 5 5 4 5
The list has no consecutive fours
Enter list1 size and contents: 5 1 5 16 61 111
Enter list2 size and contents: 4 2 4 5 6
list1 is 1 5 16 61 111
list2 is 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111
**7.31 (Merge two sorted lists) Write the following method that merges two sorted lists into a new sorted list:
public static int[] merge(int[] list1, int[] list2)
Implement the method in a way that takes at most list1.length + list2. length comparisons. See liveexample.pearsoncmg.com/dsanimation/ MergeSortNeweBook.html for an animation of the implementation. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Note the first number in the input indicates the number of the elements in the list. This number is not part of the list.
M07_LIAN1878_11_GE_C07.indd 306 1/3/18 12:10 AM
Programming Exercises 307
** 7.32 (Partition of a list) Write the following method that partitions the list using the first element, called a pivot:
public static int partition(int[] list)
After the partition, the elements in the list are rearranged so all the elements before the pivot are less than or equal to the pivot, and the elements after the pivot are greater than the pivot. The method returns the index where the pivot is located in the new list. For example, suppose the list is {5, 2, 9, 3, 6, 8}. After the partition, the list becomes {3, 2, 5, 9, 6, 8}. Implement the method in a way that takes at most list.length comparisons. See liveexample.pearsoncmg.com/dsanima- tion/QuickSortNeweBook.html for an animation of the implementation. Write a test program that prompts the user to enter the size of the list and the contents of the list and displays the list after the partition. Here is a sample run.
Enter list size: 8
Enter list content: 10 1 5 16 61 9 11 1
After the partition, the list is 9 1 5 1 10 61 11 16
*7.33 (Month name) Write a program that prompts the user to enter an integer between 1 and 12 and then displays the name of the month that corresponds to the integer entered by the user. For example, if the user enters three, the program should display March.
**7.34 (Sort characters in a string) Write a method that returns a sorted string using the following header:
public static String sort(String s)
For example, sort("acb") returns abc.
Write a test program that prompts the user to enter a string and displays the sorted string.
***7.35 (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as presented in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to store words, as follows: // Add any words you wish in this array String[] words = {"write", "that",...};
(Guess) Enter a letter in word ******* > p
(Guess) Enter a letter in word p****** > r
(Guess) Enter a letter in word pr**r** > p
p is already in the word
(Guess) Enter a letter in word pr**r** > o
(Guess) Enter a letter in word pro*r** > g
(Guess) Enter a letter in word progr** > n
n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr*m > a
The word is program. You missed 1 time
Do you want to guess another word? Enter y or n>
M07_LIAN1878_11_GE_C07.indd 307 1/3/18 12:10 AM
308 Chapter 7 Single-Dimensional Arrays
***7.36 (Game: Eight Queens) The classic Eight Queens puzzle is to place eight queens on a chessboard such that no two queens can attack each other (i.e., no two queens are on the same row, same column, or same diagonal). There are many possible solutions. Write a program that displays one such solution. A sample output is shown below:
Figure 7.13 Each ball takes a random path and falls into a slot.
(a) (b) (c)
Balls are dropped from the opening of the board. Every time a ball hits a nail, it has a 50% chance of falling to the left or to the right. The piles of balls are accu- mulated in the slots at the bottom of the board.
Write a program that simulates the bean machine. Your program should prompt the user to enter the number of the balls and the number of the slots in the machine. Simulate the falling of each ball by printing its path. For example, the path for the ball in Figure 7.13b is LLRRLLR and the path for the ball in Figure 7.13c is RLRRLRR. Display the final buildup of the balls in the slots in a histogram. Here is a sample run of the program:
(Hint: Create an array named slots. Each element in slots stores the number of balls in a slot. Each ball falls into a slot via a path. The number of Rs in a path is the position of the slot where the ball falls. For example, for the path LRLR- LRR, the ball falls into slots[4], and for the path RRLLLLL, the ball falls into slots[2].)
|Q| | | | | | | | | | | | |Q| | | | | | | | | | | |Q| | | | | | |Q| | | | | |Q| | | | | | | | | | | | |Q| | | |Q| | | | | | | | | | |Q| | | | |
*** 7.37 (Game: bean machine) The bean machine, also known as a quincunx or the Gal- ton box, is a device for statistics experiments named after English scientist Sir Francis Galton. It consists of an upright board with evenly spaced nails (or pegs) in a triangular form, as shown in Figure 7.13.
M07_LIAN1878_11_GE_C07.indd 308 1/3/18 12:10 AM
Programming Exercises 309
Enter the number of balls to drop: 5
Enter the number of slots in the bean machine: 8
LRLRLRR
RRLLLRR
LLRLLRR
RRLLLLL
LRLRRLR
O
O
OOO
M07_LIAN1878_11_GE_C07.indd 309 1/3/18 12:10 AM
A01_GIPE3898_09_SE_FM.indd 3 12/20/17 5:56 PM
This page intentionally left blank
Objectives ■■ To give examples of representing data using two-dimensional arrays
(§8.1).
■■ To declare variables for two-dimensional arrays, create arrays, and access array elements in a two-dimensional array using row and column indices (§8.2).
■■ To program common operations for two-dimensional arrays ( displaying arrays, summing all elements, finding the minimum and maximum elements, and random shuffling) (§8.3).
■■ To pass two-dimensional arrays to methods (§8.4).
■■ To write a program for grading multiple-choice questions using two-dimensional arrays (§8.5).
■■ To solve the closest pair problem using two-dimensional arrays (§8.6).
■■ To check a Sudoku solution using two-dimensional arrays (§8.7).
■■ To use multidimensional arrays (§8.8).
Multidimensional Arrays
Chapter
8
M08_LIAN1878_11_GE_C08.indd 311 12/29/17 4:06 PM
312 Chapter 8 Multidimensional Arrays
8.1 Introduction Data in a table or a matrix can be represented using a two-dimensional array.
The preceding chapter introduced how to use one-dimensional arrays to store linear collections of elements. You can use a two-dimensional array to store a matrix or a table. For example, the following table that lists the distances between cities can be stored using a two-dimensional array named distances.
Point Key
problem
Distance Table (in miles)
Chicago Boston New York Atlanta Miami Dallas Houston
Chicago 0 983 787 714 1375 967 1087
Boston 983 0 214 1102 1763 1723 1842
New York 787 214 0 888 1549 1548 1627
Atlanta 714 1102 888 0 661 781 810
Miami 1375 1763 1549 661 0 1426 1187
Dallas 967 1723 1548 781 1426 0 239
Houston 1087 1842 1627 810 1187 239 0
double[][] distances = { {0, 983, 787, 714, 1375, 967, 1087}, {983, 0, 214, 1102, 1763, 1723, 1842}, {787, 214, 0, 888, 1549, 1548, 1627}, {714, 1102, 888, 0, 661, 781, 810}, {1375, 1763, 1549, 661, 0, 1426, 1187}, {967, 1723, 1548, 781, 1426, 0, 239}, {1087, 1842, 1627, 810, 1187, 239, 0}, };
8.2 Two-Dimensional Array Basics An element in a two-dimensional array is accessed through a row and a column index.
How do you declare a variable for two-dimensional arrays? How do you create a two- dimensional array? How do you access elements in a two-dimensional array? This section will address these issues.
8.2.1 Declaring Variables of Two-Dimensional Arrays and Creating Two-Dimensional Arrays
The syntax for declaring a two-dimensional array is as follows:
elementType[][] arrayRefVar;
or
elementType arrayRefVar[][]; // Allowed, but not preferred
As an example, here is how you would declare a two-dimensional array variable matrix of int values:
int[][] matrix;
Point Key
M08_LIAN1878_11_GE_C08.indd 312 12/29/17 4:06 PM
8.2 Two-Dimensional Array Basics 313
or
int matrix[][]; // This style is allowed, but not preferred
You can create a two-dimensional array of 5-by-5 int values and assign it to matrix using this syntax:
matrix = new int[5][5];
Two subscripts are used in a two-dimensional array: one for the row, and the other for the column. As in a one-dimensional array, the index for each subscript is of the int type and starts from 0, as shown in Figure 8.1a.
Figure 8.1 The index of each subscript of a two-dimensional array is an int value, starting from 0.
[4][3][2][1][0]
[4]
[3]
[2]
[1]
[0]
[4][3][2][1][0]
[4]
[3]
[2]
[1]
[0]
[2][1][0]
[3]
[2]
[1]
[0] 1
matrix = new int[5][5]; matrix[2][1] = 7;
2 3
4 5 6
7 8 9
10 11 12
7
0000 0
0000 0
0000 0
0000 0
0000 0
0000 0
0000 0
000 0
0000 0
0000 0 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
(a) (b) (c)
To assign the value 7 to a specific element at row index 2 and column index 1, as shown in Figure 8.1b, you can use the following syntax:
matrix[2][1] = 7;
Caution It is a common mistake to use matrix[2, 1] to access the element at row 2 and column 1. In Java, each subscript must be enclosed in a pair of square brackets.
You can also use an array initializer to declare, create, and initialize a two-dimensional array. For example, the following code in (a) creates an array with the specified initial values, as shown in Figure 8.1c. This is equivalent to the code in (b).
int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
Equivalent
int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
(a) (b)
8.2.2 Obtaining the Lengths of Two-Dimensional Arrays A two-dimensional array is actually an array in which each element is a one-dimensional array. The length of an array x is the number of elements in the array, which can be obtained using x. length. x[0], x[1], . . . , and x[x.length − 1] are arrays. Their lengths can be obtained using x[0].length, x[1].length, . . . , and x[x.length − 1].length.
M08_LIAN1878_11_GE_C08.indd 313 12/29/17 4:06 PM
314 Chapter 8 Multidimensional Arrays
For example, suppose that x = new int[3][4], x[0], x[1], and x[2] are one- dimensional arrays and each contains four elements, as shown in Figure 8.2. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4.
Figure 8.2 A two-dimensional array is a one-dimensional array in which each element is another one-dimensional array.
x
x[0]
x[1]
x[2]
x[0][0] x[0][1] x[0][3]
x[1][0] x[1][1] x[1][2] x[1][3]
x[2][0] x[2][1] x[2][2] x[2][3] x.length is 3
x[0].length is 4
x[1].length is 4
x[2].length is 4
x[0][2]
int[][] triangleArray = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };
1 2 3 4 5
2 3 4 5
5
4 5
3 4 5
8.2.3 Ragged Arrays Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array. Here is an example of creating a ragged array:
ragged array
As you can see, triangleArray[0].length is 5, triangleArray[1].length is 4, triangleArray[2].length is 3, triangleArray[3].length is 2, and triangle− Array[4].length is 1.
If you don’t know the values in a ragged array in advance, but do know the sizes—say, the same as in the preceding figure—you can create a ragged array using the following syntax:
int[][] triangleArray = new int[5][]; triangleArray[0] = new int[5]; triangleArray[1] = new int[4]; triangleArray[2] = new int[3]; triangleArray[3] = new int[2]; triangleArray[4] = new int[1];
You can now assign values to the array. For example,
triangleArray[0][3] = 4; triangleArray[4][0] = 5;
Note The syntax new int[5][] for creating an array requires the first index to be specified. The syntax new int[][] would be wrong.
M08_LIAN1878_11_GE_C08.indd 314 12/29/17 4:06 PM
8.3 Processing Two-Dimensional Arrays 315
8.2.1 Declare an array reference variable for a two-dimensional array of int values, create a 4-by-5 int matrix, and assign it to the variable.
8.2.2 Can the rows in a two-dimensional array have different lengths? 8.2.3 What is the output of the following code?
int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]);
8.2.4 Which of the following statements are valid? int[][] r = new int[2]; int[] x = new int[]; int[][] y = new int[3][]; int[][] z = {{1, 2}}; int[][] m = {{1, 2}, {2, 3}}; int[][] n = {{1, 2}, {2, 3}, };
8.3 Processing Two-Dimensional Arrays Nested for loops are often used to process a two-dimensional array.
Suppose an array matrix is created as follows:
int[][] matrix = new int[10][10];
The following are some examples of processing two-dimensional arrays.
1. Initializing arrays with input values. The following loop initializes the array with user input values:
java.util.Scanner input = new java.util.Scanner(System.in); System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextInt(); } }
2. Initializing arrays with random values. The following loop initializes the array with random values between 0 and 99:
for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = (int)(Math.random() * 100); } }
3. Printing arrays. To print a two-dimensional array, you have to print each element in the array using a loop like the following loop:
for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); }
System.out.println(); }
Point Check
Point Key
M08_LIAN1878_11_GE_C08.indd 315 12/29/17 4:06 PM
316 Chapter 8 Multidimensional Arrays
4. Summing all elements. Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this:
int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { total += matrix[row][column]; } }
5. Summing elements by column. For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this:
for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is " + total); }
6. Which row has the largest sum? Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow and indexOfMaxRow if the new sum is greater.
int maxRow = 0; int indexOfMaxRow = 0;
// Get sum of the first row in maxRow for (int column = 0; column < matrix[0].length; column++) { maxRow += matrix[0][column]; }
for (int row = 1; row < matrix.length; row++) { int totalOfThisRow = 0; for (int column = 0; column < matrix[row].length; column++) totalOfThisRow += matrix[row][column];
if (totalOfThisRow > maxRow) { maxRow = totalOfThisRow; indexOfMaxRow = row; } }
System.out.println("Row " + indexOfMaxRow + " has the maximum sum of " + maxRow);
7. Random shuffling. Shuffling the elements in a one-dimensional array was introduced in Section 7.2.6. How do you shuffle all the elements in a two-dimensional array? To accomplish this, for each element matrix[i][j], randomly generate indices i1 and j1 and swap matrix[i][j] with matrix[i1][j1], as follows:
for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { int i1 = (int)(Math.random() * matrix.length); int j1 = (int)(Math.random() * matrix[i].length);
// Swap matrix[i][j] with matrix[i1][j1]
VideoNote
Find the row with the largest sum
M08_LIAN1878_11_GE_C08.indd 316 12/29/17 4:06 PM
8.4 Passing Two-Dimensional Arrays to Methods 317
int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; } }
8.3.1 Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; for (int i = array.length − 1; i >= 0; i——) { for (int j = array[i].length − 1; j >= 0; j——) System.out.print(array[i][j] + " "); System.out.println(); }
8.3.2 Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; int sum = 0; for (int i = 0; i < array.length; i++) sum += array[i][0]; System.out.println(sum);
8.4 Passing Two-Dimensional Arrays to Methods When passing a two-dimensional array to a method, the reference of the array is passed to the method.
You can pass a two-dimensional array to a method just as you pass a one-dimensional array. You can also return an array from a method. Listing 8.1 gives an example with two meth- ods. The first method, getArray(), returns a two-dimensional array and the second method, sum(int[][] m), returns the sum of all the elements in a matrix.
Listing 8.1 PassTwoDimensionalArray.java
1 import java.util.Scanner; 2 3 public class PassTwoDimensionalArray { 4 public static void main(String[] args) { 5 int[][] m = getArray(); // Get an array 6 7 // Display sum of elements 8 System.out.println("\nSum of all elements is " + sum(m)); 9 } 10 11 public static int[][] getArray() { 12 // Create a Scanner 13 Scanner input = new Scanner(System.in); 14 15 // Enter array values 16 int[][] m = new int[3][4]; 17 System.out.println("Enter " + m.length + " rows and " 18 + m[0].length + " columns: "); 19 for (int i = 0; i < m.length; i++) 20 for (int j = 0; j < m[i].length; j++) 21 m[i][j] = input.nextInt(); 22
Point Check
Point Key
get array
pass array
getArray method
M08_LIAN1878_11_GE_C08.indd 317 12/29/17 4:06 PM
318 Chapter 8 Multidimensional Arrays
23 return m; 24 } 25 26 public static int sum(int[][] m) { 27 int total = 0; 28 for (int row = 0; row < m.length; row++) { 29 for (int column = 0; column < m[row].length; column++) { 30 total += m[row][column]; 31 } 32 } 33 34 return total; 35 } 36 }
return array
sum method
Enter 3 rows and 4 columns:
1 2 3 4 5 6 7 8 9 10 11 12 Sum of all elements is 78
The method getArray prompts the user to enter values for the array (lines 11–24) and returns the array (line 23).
The method sum (lines 26–35) has a two-dimensional array argument. You can obtain the number of rows using m.length (line 28), and the number of columns in a specified row using m[row].length (line 29).
8.4.1 Show the output of the following code: public class Test { public static void main(String[] args) { int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}}; System.out.println(m1(array)[0]); System.out.println(m1(array)[1]); }
public static int[] m1(int[][] m) { int[] result = new int[2]; result[0] = m.length; result[1] = m[0].length; return result; } }
8.5 Case Study: Grading a Multiple-Choice Test The problem is to write a program that will grade multiple-choice tests.
Suppose you need to write a program that grades multiple-choice tests. Assume there are eight students and ten questions, and the answers are stored in a two-dimensional array. Each row records a student’s answers to the questions, as shown in the following array:
Point Check
Point Key
VideoNote
Grade multiple-choice test
M08_LIAN1878_11_GE_C08.indd 318 12/29/17 4:06 PM
8.5 Case Study: Grading a Multiple-Choice Test 319
Students’ Answers to the Questions:
0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D Student 1 D B A B C A E E A D Student 2 E D D A C B E E A D Student 3 C B A E D C E E A D Student 4 A B D C C D E E A D Student 5 B B E C C D E E A D Student 6 B B A C C D E E A D Student 7 E B E C C D E E A D
The key is stored in a one-dimensional array:
Key to the Questions:
0 1 2 3 4 5 6 7 8 9
Key D B D C C D A E A D
Your program grades the test and displays the result. It compares each student’s answers with the key, counts the number of correct answers, and displays it. Listing 8.2 gives the program.
Listing 8.2 GradeExam.java 1 public class GradeExam { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Students' answers to the questions 5 char[][] answers = { 6 {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, 7 {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, 8 {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, 9 {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, 10 {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, 11 {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, 12 {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, 13 {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}}; 14 15 // Key to the questions 16 char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'}; 17 18 // Grade all answers 19 for (int i = 0; i < answers.length; i++) { 20 // Grade one student 21 int correctCount = 0; 22 for (int j = 0; j < answers[i].length; j++) { 23 if (answers[i][j] == keys[j]) 24 correctCount++; 25 } 26 27 System.out.println("Student " + i + "'s correct count is " + 28 correctCount); 29 } 30 } 31 }
2-D array
1-D array
compare with key
M08_LIAN1878_11_GE_C08.indd 319 12/29/17 4:06 PM
320 Chapter 8 Multidimensional Arrays
The statement in lines 5–13 declares, creates, and initializes a two-dimensional array of characters and assigns the reference to answers of the char[][] type.
The statement in line 16 declares, creates, and initializes an array of char values and assigns the reference to keys of the char[] type.
Each row in the array answers stores a student’s answer, which is graded by comparing it with the key in the array keys. The result is displayed immediately after a student's answer is graded.
8.5.1 How do you modify the code so it also displays the highest count and the student with the highest count?
8.6 Case Study: Finding the Closest Pair This section presents a geometric problem for finding the closest pair of points.
Given a set of points, the closest-pair problem is to find the two points that are nearest to each other. In Figure 8.3, for example, points (1, 1) and (2, 0.5) are closest to each other. There are several ways to solve this problem. An intuitive approach is to compute the distances between all pairs of points and find the one with the minimum distance, as implemented in Listing 8.3.
Point Check
Point Key
closest-pair animation on the Companion Website
Figure 8.3 Points can be represented in a two-dimensional array.
(1, 1)
(–1, –1)
(–1, 3)
(2, 0.5)
(3, 3)
–1 3
x y
–1 –1 1 1 2 0.5 2 –1 3 3 4 2 4
0 1 2 3 4 5 6 7 –0.5
(4, 2)
(2, –1) (4, –0.5)
Student 0's correct count is 7
Student 1's correct count is 6
Student 2's correct count is 5
Student 3's correct count is 4
Student 4's correct count is 8
Student 5's correct count is 7
Student 6's correct count is 7
Student 7's correct count is 7
Listing 8.3 FindNearestPoints.java 1 import java.util.Scanner; 2 3 public class FindNearestPoints { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("Enter the number of points: "); 7 int numberOfPoints = input.nextInt(); 8 9 // Create an array to store points
number of points
M08_LIAN1878_11_GE_C08.indd 320 12/29/17 4:06 PM
8.6 Case Study: Finding the Closest Pair 321
10 double[][] points = new double[numberOfPoints][2]; 11 System.out.print("Enter " + numberOfPoints + " points: "); 12 for (int i = 0; i < points.length; i++) { 13 points[i][0] = input.nextDouble(); 14 points[i][1] = input.nextDouble(); 15 } 16 17 // p1 and p2 are the indices in the points' array 18 int p1 = 0, p2 = 1; // Initial two points 19 double shortestDistance = distance(points[p1][0], points[p1][1], 20 points[p2][0], points[p2][1]); // Initialize shortestDistance 21 22 // Compute distance for every two points 23 for (int i = 0; i < points.length; i++) { 24 for (int j = i + 1; j < points.length; j++) { 25 double distance = distance(points[i][0], points[i][1], 26 points[j][0], points[j][1]); // Find distance 27 28 if (shortestDistance > distance) { 29 p1 = i; // Update p1 30 p2 = j; // Update p2 31 shortestDistance = distance; // Update shortestDistance 32 } 33 } 34 } 35 36 // Display result 37 System.out.println("The closest two points are " + 38 "(" + points[p1][0] + ", " + points[p1][1] + ") and (" + 39 points[p2][0] + ", " + points[p2][1] + ")"); 40 } 41 42 /** Compute the distance between two points (x1, y1) and (x2, y2)*/ 43 public static double distance( 44 double x1, double y1, double x2, double y2) { 45 return Math.sqrt((x2 − x1) * (x2 − x1) + (y2 − y1) * (y2 − y1)); 46 } 47 }
read points
2-D array
track shortestDistance
for each point i for each point j
track two points
distance between two points
update shortestDistance
distance between i and j
Enter the number of points: 8
Enter 8 points: −1 3 −1 −1 1 1 2 0.5 2 −1 3 3 4 2 4 −0.5
The closest two points are (1, 1) and (2, 0.5)
The program prompts the user to enter the number of points (lines 6 and 7). The points are read from the console and stored in a two-dimensional array named points (lines 12–15). The program uses the variable shortestDistance (line 19) to store the distance between the two nearest points, and the indices of these two points in the points array are stored in p1 and p2 (line 18).
For each point at index i, the program computes the distance between points[i] and points[j] for all j > i (lines 23–34). Whenever a shorter distance is found, the variable shortestDistance and p1 and p2 are updated (lines 28–32).
The distance between two points (x1, y1) and (x2, y2) can be computed using the formula 2(x2 - x1)2 + (y2 - y1)2 (lines 43–46).
The program assumes the plane has at least two points. You can easily modify the program to handle the case if the plane has zero or one point.
M08_LIAN1878_11_GE_C08.indd 321 12/29/17 4:06 PM
322 Chapter 8 Multidimensional Arrays
Note that there might be more than one closest pair of points with the same minimum dis- tance. The program finds one such pair. You may modify the program to find all closest pairs in Programming Exercise 8.8.
Tip It is cumbersome to enter all points from the keyboard. You may store the input in a file, say FindNearestPoints.txt, and run the program using the following command:
java FindNearestPoints < FindNearestPoints.txt
8.6.1 What happens if the input has only one point?
8.7 Case Study: Sudoku The problem is to check whether a given Sudoku solution is correct.
This section presents an interesting problem of a sort that appears in the newspaper every day. It is a number-placement puzzle, commonly known as Sudoku. This is a very challeng- ing problem. To make it accessible to the novice, this section presents a simplified version of the Sudoku problem, which is to verify whether a Sudoku solution is correct. The complete program for finding a Sudoku solution is presented in Supplement VI.C.
Sudoku is a 9 * 9 grid divided into smaller 3 * 3 boxes (also called regions or blocks), as shown in Figure 8.4a. Some cells, called fixed cells, are populated with numbers from 1 to 9. The objective is to fill the empty cells, also called free cells, with the numbers 1 to 9 so every row, every column, and every 3 * 3 box contains the numbers 1 to 9, as shown in Figure 8.4b.
multiple closest pairs
input file
Point Check
Point Key
VideoNote
Sudoku
fixed cells
free cells
Figure 8.4 The Sudoku puzzle in (a) is solved in (b).
5 3 7
6 1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
6
4 1 9 5
8 7 9
(a) Puzzle
Solution
(b) Solution
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Figure 8.5 A grid can be represented using a two-dimensional array.
5 3 0 0 0 0 0 0
0 0
0 0
0 0
0 0
0 0
0 0
0
0
00
0
00 0
0 0
0 0
0
0 00
0 080 00
0 00
0 06
7
6 0 0
0 0
0 0
0 0 0
1 9 5
9 8 6
8 6 3
4 8 3 1
7 2 6
4 1 9 5
7 9
(a) (b)
int[][] grid = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9} };
For convenience, we use value 0 to indicate a free cell, as shown in Figure 8.5a. The grid can be naturally represented using a two-dimensional array, as shown in Figure 8.5b.representing a grid
M08_LIAN1878_11_GE_C08.indd 322 12/29/17 4:06 PM
8.7 Case Study: Sudoku 323
To find a solution for the puzzle, we must replace each 0 in the grid with an appropriate number from 1 to 9. For the solution to the puzzle in Figure 8.5, the grid should be as shown in Figure 8.6.
Once a solution to a Sudoku puzzle is found, how do you verify that it is correct? Here are two approaches:
1. Check if every row has numbers from 1 to 9, every column has numbers from 1 to 9, and every small box has numbers from 1 to 9.
2. Check each cell. Each cell must be a number from 1 to 9 and the cell must be unique on every row, every column, and every small box.
Figure 8.6 A solution is stored in grid.
A solution grid is {{5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, {1, 9, 8, 3, 4, 2, 5, 6, 7}, {8, 5, 9, 7, 6, 1, 4, 2, 3}, {4, 2, 6, 8, 5, 3, 7, 9, 1}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 6, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9} };
The program in Listing 8.4 prompts the user to enter a solution and reports whether it is valid. We use the second approach in the program to check whether the solution is correct.
Listing 8.4 CheckSudokuSolution.java 1 import java.util.Scanner; 2 3 public class CheckSudokuSolution { 4 public static void main(String[] args) { 5 // Read a Sudoku solution 6 int[][] grid = readASolution(); 7 8 System.out.println(isValid(grid) ? "Valid solution" : 9 "Invalid solution"); 10 } 11 12 /** Read a Sudoku solution from the console */ 13 public static int[][] readASolution() { 14 // Create a Scanner 15 Scanner input = new Scanner(System.in); 16 17 System.out.println("Enter a Sudoku puzzle solution:"); 18 int[][] grid = new int[9][9]; 19 for (int i = 0; i < 9; i++) 20 for (int j = 0; j < 9; j++) 21 grid[i][j] = input.nextInt(); 22 23 return grid; 24 } 25 26 /** Check whether a solution is valid */ 27 public static boolean isValid(int[][] grid) {
read input
solution valid?
read solution
check solution
M08_LIAN1878_11_GE_C08.indd 323 12/29/17 4:06 PM
324 Chapter 8 Multidimensional Arrays
28 for (int i = 0; i < 9; i++) 29 for (int j = 0; j < 9; j++) 30 if (grid[i][j] < 1 || grid[i][j] > 9 31 || !isValid(i, j, grid)) 32 return false; 33 return true; // The solution is valid 34 } 35 36 /** Check whether grid[i][j] is valid in the grid */ 37 public static boolean isValid(int i, int j, int[][] grid) { 38 // Check whether grid[i][j] is unique in i's row 39 for (int column = 0; column < 9; column++) 40 if (column != j && grid[i][column] == grid[i][j]) 41 return false; 42 43 // Check whether grid[i][j] is unique in j's column 44 for (int row = 0; row < 9; row++) 45 if (row != i && grid[row][j] == grid[i][j]) 46 return false; 47 48 // Check whether grid[i][j] is unique in the 3−by−3 box 49 for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) 50 for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) 51 if (!(row == i && col == j) && grid[row][col] == grid[i][j]) 52 return false; 53 54 return true; // The current value at grid[i][j] is valid 55 } 56 }
check small boxes
check columns
check rows
Enter a Sudoku puzzle solution:
9 6 3 1 7 4 2 5 8 1 7 8 3 2 5 6 4 9 2 5 4 6 8 9 7 3 1
8 2 1 4 3 7 5 9 6
4 9 6 8 5 2 3 1 7
7 3 5 9 6 1 8 2 4
5 8 9 7 1 3 4 6 2 3 1 7 2 4 6 9 8 5 6 4 2 5 9 8 1 7 3 Valid solution
The program invokes the readASolution() method (line 6) to read a Sudoku solution and return a two-dimensional array representing a Sudoku grid.
The isValid(grid) method checks whether the values in the grid are valid by verifying that each value is between 1 and 9, and that each value is valid in the grid (lines 27–34).
The isValid(i, j, grid) method checks whether the value at grid[i][j] is valid. It checks whether grid[i][j] appears more than once in row i (lines 39–41), in column j (lines 44–46), and in the 3 * 3 box (lines 49–52).
How do you locate all the cells in the same box? For any grid[i][j], the starting cell of the 3 * 3 box that contains it is grid[(i / 3) * 3][(j / 3) * 3], as illustrated in Figure 8.7.
isValid method
overloaded isValid method
M08_LIAN1878_11_GE_C08.indd 324 12/29/17 4:06 PM
8.8 Multidimensional Arrays 325
With this observation, you can easily identify all the cells in the box. For instance, if grid[r][c] is the starting cell of a 3 * 3 box, the cells in the box can be traversed in a nested loop as follows:
// Get all cells in a 3−by−3 box starting at grid[r][c] for (int row = r; row < r + 3; row++) for (int col = c; col < c + 3; col++) // grid[row][col] is in the box
It is cumbersome to enter 81 numbers from the console. When you test the program, you may store the input in a file, say CheckSudokuSolution.txt (see liveexample.pearsoncmg. com/data/CheckSudokuSolution.txt) and run the program using the following command:
java CheckSudokuSolution < CheckSudokuSolution.txt
8.7.1 What happens if the code in line 51 in Listing 8.4 is changed to if (row != i && col != j && grid[row][col] == grid[i][j])
8.8 Multidimensional Arrays A two-dimensional array is an array of one-dimensional arrays, and a three-dimen- sional array is an array of two-dimensional arrays.
In the preceding section, you used a two-dimensional array to represent a matrix or a table. Occasionally, you will need to represent n-dimensional data structures. In Java, you can create n-dimensional arrays for any positive integer n.
The way to declare two-dimensional array variables and create two-dimensional arrays can be generalized to declare n-dimensional array variables and create n-dimensional arrays for n 7 = 3. For example, you may use a three-dimensional array to store exam scores for a class of six students with five exams, and each exam has two parts (multiple-choice and essay type questions). The following syntax declares a three-dimensional array variable scores, creates an array, and assigns its reference to scores.
double[][][] scores = new double[6][5][2];
You can also use the array initializer to create and initialize the array as follows:
double[][][] scores = { {{7.5, 20.5}, {9.0, 22.5}, {15, 33.5}, {13, 21.5}, {15, 2.5}}, {{4.5, 21.5}, {9.0, 22.5}, {15, 34.5}, {12, 20.5}, {14, 9.5}}, {{6.5, 30.5}, {9.4, 10.5}, {11, 33.5}, {11, 23.5}, {10, 2.5}}, {{6.5, 23.5}, {9.4, 32.5}, {13, 34.5}, {11, 20.5}, {16, 7.5}}, {{8.5, 26.5}, {9.4, 52.5}, {13, 36.5}, {13, 24.5}, {16, 2.5}}, {{9.5, 20.5}, {9.4, 42.5}, {13, 31.5}, {12, 20.5}, {16, 6.5}}};
input file
Point Check
Point Key
Figure 8.7 The location of the first cell in a 3 * 3 box determines the locations of other cells in the box.
grid[0][6]
grid[6][3]
For any grid[i][j] in this 3 by 3 box, its starting cell is grid[3×(i/3)] [3×(j/3)](i.e., grid[6][3]). For example, for grid[8][5], i=8 and j=5, 3×(i/3)=6 and 3×(j/3)=3.
For any grid[i][j] in this 3 by 3 box, its starting cell is grid[3×(i/3)][3×(j/3)] (i.e., grid[0][6]). For example, for grid[2][8], i=2 and j=8, 3×(i/3)=0 and 3×(j/3)=6.
grid[0][0]
M08_LIAN1878_11_GE_C08.indd 325 12/29/17 4:06 PM
326 Chapter 8 Multidimensional Arrays
scores[0][1][0] refers to the multiple-choice score for the first student’s second exam, which is 9.0. scores[0][1][1] refers to the essay score for the first student’s second exam, which is 22.5. This is depicted in the following figure:
scores [i] [j] [k]
Which student Which exam Multiple-choice or essay score
A multidimensional array is actually an array in which each element is another array. A three- dimensional array is an array of two-dimensional arrays. A two-dimensional array is an array of one-dimensional arrays. For example, suppose that x = new int[2][2][5] and x[0] and x[1] are two-dimensional arrays. x[0][0], x[0][1], x[1][0], and x[1][1] are one- dimensional arrays and each contains five elements. x.length is 2, x[0].length and x[1]. length are 2, and x[0][0].length, x[0][1].length, x[1][0].length, and x[1][1]. length are 5.
8.8.1 Case Study: Daily Temperature and Humidity Suppose a meteorology station records the temperature and humidity every hour of every day, and stores the data for the past 10 days in a text file named Weather.txt (see liveexample .pearsoncmg.com/data/Weather.txt). Each line of the file consists of four numbers that indi- cate the day, hour, temperature, and humidity. The contents of the file may look like those in (a).
Day Hour
Temperature Humidity
Day Hour
Temperature Humidity
1 1 76.4 0.92 10 24 98.7 0.74 1 2 77.7 0.93 1 2 77.7 0.93
... ... 10 23 97.7 0.71 10 23 97.7 0.71 10 24 98.7 0.74 1 1 76.4 0.92
(a) (b)
Note the lines in the file are not necessarily in increasing order of day and hour. For example, the file may appear as shown in (b).
Your task is to write a program that calculates the average daily temperature and humid- ity for the 10 days. You can use the input redirection to read the file and store the data in a three-dimensional array named data. The first index of data ranges from 0 to 9 and represents 10 days, the second index ranges from 0 to 23 and represents 24 hours, and the third index ranges from 0 to 1 and represents temperature and humidity, as depicted in the following figure:
Which day Which hour Temperature or humidity
data [ i ] [ j ] [ k ]
M08_LIAN1878_11_GE_C08.indd 326 12/29/17 4:06 PM
8.8 Multidimensional Arrays 327
Note the days are numbered from 1 to 10 and the hours from 1 to 24 in the file. Because the array index starts from 0, data[0][0][0] stores the temperature in day 1 at hour 1 and data[9][23][1] stores the humidity in day 10 at hour 24.
The program is given in Listing 8.5.
Listing 8.5 Weather.java 1 import java.util.Scanner; 2 3 public class Weather { 4 public static void main(String[] args) { 5 final int NUMBER_OF_DAYS = 10; 6 final int NUMBER_OF_HOURS = 24; 7 double[][][] data 8 = new double[NUMBER_OF_DAYS][NUMBER_OF_HOURS][2]; 9 10 Scanner input = new Scanner(System.in); 11 // Read input using input redirection from a file 12 for (int k = 0; k < NUMBER_OF_DAYS * NUMBER_OF_HOURS; k++) { 13 int day = input.nextInt(); 14 int hour = input.nextInt(); 15 double temperature = input.nextDouble(); 16 double humidity = input.nextDouble(); 17 data[day − 1][hour − 1][0] = temperature; 18 data[day − 1][hour − 1][1] = humidity; 19 } 20 21 // Find the average daily temperature and humidity 22 for (int i = 0; i < NUMBER_OF_DAYS; i++) { 23 double dailyTemperatureTotal = 0, dailyHumidityTotal = 0; 24 for (int j = 0; j < NUMBER_OF_HOURS; j++) { 25 dailyTemperatureTotal += data[i][j][0]; 26 dailyHumidityTotal += data[i][j][1]; 27 } 28 29 // Display result 30 System.out.println("Day " + i + "'s average temperature is " 31 + dailyTemperatureTotal / NUMBER_OF_HOURS); 32 System.out.println("Day " + i + "'s average humidity is " 33 + dailyHumidityTotal / NUMBER_OF_HOURS); 34 } 35 } 36 }
three-dimensional array
Day 0's average temperature is 77.7708 Day 0's average humidity is 0.929583 Day 1's average temperature is 77.3125 Day 1's average humidity is 0.929583 ... Day 9's average temperature is 79.3542 Day 9's average humidity is 0.9125
You can use the following command to run the program:
java Weather < Weather.txt
A three-dimensional array for storing temperature and humidity is created in line 8. The loop in lines 12–19 reads the input to the array. You can enter the input from the keyboard, but
M08_LIAN1878_11_GE_C08.indd 327 12/29/17 4:06 PM
328 Chapter 8 Multidimensional Arrays
doing so will be awkward. For convenience, we store the data in a file and use input redirection to read the data from the file. The loop in lines 24–27 adds all temperatures for each hour in a day to dailyTemperatureTotal, and all humidity for each hour to dailyHumidityTotal. The average daily temperature and humidity are displayed in lines 30–33.
8.8.2 Case Study: Guessing Birthdays Listing 4.3, GuessBirthday.java, gives a program that guesses a birthday. The program can be simplified by storing the numbers in five sets in a three-dimensional array and it prompts the user for the answers using a loop, as given in Listing 8.6. The sample run of the program can be the same as given in Listing 4.3.
Listing 8.6 GuessBirthdayUsingArray.java 1 import java.util.Scanner; 2 3 public class GuessBirthdayUsingArray { 4 public static void main(String[] args) { 5 int day = 0; // Day to be determined 6 int answer; 7 8 int[][][] dates = { 9 {{ 1, 3, 5, 7}, 10 { 9, 11, 13, 15}, 11 {17, 19, 21, 23}, 12 {25, 27, 29, 31}}, 13 {{ 2, 3, 6, 7}, 14 {10, 11, 14, 15}, 15 {18, 19, 22, 23}, 16 {26, 27, 30, 31}}, 17 {{ 4, 5, 6, 7}, 18 {12, 13, 14, 15}}, 19 {20, 21, 22, 23}, 20 {28, 29, 30, 31}}, 21 {{ 8, 9, 10, 11}, 22 {12, 13, 14, 15}, 23 {24, 25, 26, 27}, 24 {28, 29, 30, 31}}, 25 {{16, 17, 18, 19}, 26 {20, 21, 22, 23}, 27 {24, 25, 26, 27}, 28 {28, 29, 30, 31}}}; 29 30 // Create a Scanner 31 Scanner input = new Scanner(System.in); 32 33 for (int i = 0; i < 5; i++) { 34 System.out.println("Is your birthday in Set" + (i + 1) + "?"); 35 for (int j = 0; j < 4; j++) { 36 for (int k = 0; k < 4; k++) 37 System.out.printf("%4d", dates[i][j][k]); 38 System.out.println(); 39 } 40 41 System.out.print("\nEnter 0 for No and 1 for Yes: "); 42 answer = input.nextInt(); 43 44 if (answer == 1) 45 day += dates[i][0][0];
three-dimensional array
Set i
add to day
M08_LIAN1878_11_GE_C08.indd 328 12/29/17 4:06 PM
Programming Exercises 329
46 } 47 48 System.out.println("Your birthday is " + day); 49 } 50 }
A three-dimensional array dates is created in lines 8–28. This array stores five sets of numbers. Each set is a 4-by-4 two-dimensional array.
The loop starting from line 33 displays the numbers in each set and prompts the user to answer whether the birthday is in the set (lines 41 and 42). If the day is in the set, the first number (dates[i][0][0]) in the set is added to variable day (line 45).
8.8.1 Declare an array variable for a three-dimensional array, create a 4 * 6 * 5 int array, and assign its reference to the variable.
8.8.2 Assume char[][][] x = new char[12][5][2], how many elements are in the array? What are x.length, x[2].length, and x[0][0].length?
8.8.3 Show the output of the following code: int[][][] array = {{{1, 2}, {3, 4}}, {{5, 6},{7, 8}}}; System.out.println(array[0][0][0]); System.out.println(array[1][1][1]);
ChapTer summary 1. A two-dimensional array can be used to store a table.
2. A variable for two-dimensional arrays can be declared using the syntax: elementType[][] arrayVar.
3. A two-dimensional array can be created using the syntax: new elementType[ROW_SIZE] [COLUMN_SIZE].
4. Each element in a two-dimensional array is represented using the syntax: arrayVar[rowIndex][columnIndex].
5. You can create and initialize a two-dimensional array using an array initializer with the syntax: elementType[][] arrayVar = {{row values}, . . ., {row values}}.
6. You can use arrays of arrays to form multidimensional arrays. For example, a variable for three-dimensional arrays can be declared as elementType[][][] arrayVar and a three-dimensional array can be created using new elementType[size1][size2] [size3].
Quiz Answer the quiz for this chapter online at the book Companion Website.
programming exerCises *8.1 (Sum elements row by row) Write a method that returns the sum of all the ele-
ments in a specified row in a matrix using the following header:
public static double sumRow(double[][] m, int rowIndex)
Point Check
M08_LIAN1878_11_GE_C08.indd 329 12/29/17 4:06 PM
330 Chapter 8 Multidimensional Arrays
Write a test program that reads a 3-by-4 matrix and displays the sum of each row. Here is a sample run:
Enter a 3−by−4 matrix row by row:
1.5 2 3 4
5.5 6 7 8
9.5 1 3 1 Sum of the elements at row 0 is 10.5 Sum of the elements at row 1 is 26.5 Sum of the elements at row 2 is 14.5
Enter a 4−by−4 matrix row by row:
1 2 3 4.0 5 6.5 7 8 9 10 11 12 13 14 15 16 Average of the elements in the major diagonal is 8.625
*8.2 (Average the major diagonal in a matrix) Write a method that averages all the numbers in the major diagonal in an n * n matrix of double values using the following header:
public static double averageMajorDiagonal(double[][] m)
Write a test program that reads a 4-by-4 matrix and displays the average of all its elements on the major diagonal. Here is a sample run:
*8.3 (Sort students on grades) Rewrite Listing 8.2, GradeExam.java, to display stu- dents in decreasing order of the number of correct answers.
**8.4 (Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the fol- lowing array stores the work hours for eight employees. Write a program that displays employees and their total hours in increasing order of the total hours.
Su M T W Th F Sa
Employee 0 2 4 3 4 5 8 8
Employee 1 7 3 4 3 3 4 4
Employee 2 3 3 4 3 3 2 2
Employee 3 9 3 4 7 3 4 1
Employee 4 3 5 4 3 6 3 8
Employee 5 3 4 4 6 3 4 4
Employee 6 3 7 4 8 3 8 4
Employee 7 6 3 5 9 2 7 9
8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows:
public static double[][] addMatrix(double[][] a, double[][] b)
M08_LIAN1878_11_GE_C08.indd 330 12/29/17 4:06 PM
Programming Exercises 331
In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each ele- ment cij is aij + bij. For example, for two 2 * 2 matrices a and b, c is
aa11 a12 a21 a22
b + ab11 b12 b21 b22
b = aa11 + b11 a12 + b12 a21 + b21 a22 + b22
b
Write a test program that prompts the user to enter two 2 * 2 matrices and displays their sum. Here is a sample run:
Enter matrix1: 1 2 3 4
Enter matrix2: 0 2 4 1
The matrices are added as follows
1.0 2.0 0.0 2.0 1.0 4.0
3.0 4.0 + 4.0 1.0 = 7.0 5.0
**8.6 (Algebra: multiply two matrices) Write a method to multiply two matrices. The header of the method is:
public static double[][] multiplyMatrix(double[][] a, double[][] b)
To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element cij is ai1 * b1j + ai2 * b2j + g + ain * bnj. For example, for two 3 * 3 matrices a and b, c is£a11 a12 a13a21 a22 a23
a31 a32 a33
≥ * £b11 b12 b13b21 b22 b23 b31 b32 b33
≥ = £c11 c12 c13c21 c22 c23 c31 c32 c33
≥ where cij = ai1 * b1j + ai2 * b2j + ai3 * b3j. Write a test program that prompts the user to enter two 3 * 3 matrices and
displays their product. Here is a sample run:
VideoNote
Multiply two matrices
Enter matrix1: 1 2 3 4 5 6 7 8 9
Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2
The multiplication of the matrices is
1 2 3 0 2.0 4.0 5.3 23.9 24
4 5 6 * 1 4.5 2.2 = 11.6 56.3 58.2
7 8 9 1.1 4.3 5.2 17.9 88.7 92.4
*8.7 (Points nearest to each other) Listing 8.3 gives a program that finds two points in a two-dimensional space nearest to each other. Revise the program so it finds two points in a three-dimensional space nearest to each other. Use a two-dimensional array to represent the points. Test the program using the following points:
double[][] points = {{−1, 0, 3}, {−1, −1, −1}, {4, 1, 1}, {2, 0.5, 9}, {3.5, 2, −1}, {3, 1.5, 3}, {−1.5, 4, 2}, {5.5, 4, −0.5}};
M08_LIAN1878_11_GE_C08.indd 331 12/29/17 4:06 PM
332 Chapter 8 Multidimensional Arrays
The formula for computing the distance between two points (x1, y1, z1) and (x2, y2, z2) is 2(x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2.
**8.8 (All closest pairs of points) Revise Listing 8.3, FindNearestPoints.java, to display all closest pairs of points with the same minimum distance. Here is a sample run:
Enter the number of points: 8 Enter 8 points: 0 0 1 1 −1 −1 2 2 −2 −2 −3 −3 −4 −4 5 5 The closest two points are (0.0, 0.0) and (1.0, 1.0) The closest two points are (0.0, 0.0) and (−1.0, −1.0) The closest two points are (1.0, 1.0) and (2.0, 2.0) The closest two points are (−1.0, −1.0) and (−2.0, −2.0) The closest two points are (−2.0, −2.0) and (−3.0, −3.0) The closest two points are (−3.0, −3.0) and (−4.0, −4.0) Their distance is 1.4142135623730951
| | | | | | | | | | | | Enter a row (0, 1, or 2) for player X: 1
Enter a column (0, 1, or 2) for player X: 1
| | | | | | X | | | | | | Enter a row (0, 1, or 2) for player O: 1
Enter a column (0, 1, or 2) for player O: 2
| | | | | | X | O | | | | |
***8.9 (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 3 * 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diago- nal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create a program for playing a tic-tac-toe game.
The program prompts two players to alternately enter an X token and O token. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:
M08_LIAN1878_11_GE_C08.indd 332 12/29/17 4:06 PM
Programming Exercises 333
*8.10 (Largest row and column) Write a program that randomly fills in 0s and 1s into a 5-by-5 matrix, prints the matrix, and finds the first row and column with the most 1s. Here is a sample run of the program:
Enter a row (0, 1, or 2) for player X:
. . .
| X | | | | O | X | O | | | | X | X player won
Enter a number between 0 and 511: 7
H H H
H H H
T T T
01101 01011 10010 11111 00101 The largest row index: 3 The largest column index: 4
The user entered 7, which corresponds to 000000111. Since 0 stands for H and 1 for T, the output is correct.
** 8.12 (Financial application: compute tax) Rewrite Listing 3.5, ComputeTax.java, using arrays. For each filing status, there are six tax rates. Each rate is applied to a certain amount of taxable income. For example, from the taxable income of $400,000 for a single filer, $8,350 is taxed at 10%, (33,950 - 8,350) at 15%,
**8.11 (Game: nine heads and tails) Nine coins are placed in a 3-by-3 matrix with some face up and some face down. You can represent the state of the coins using a 3-by-3 matrix with values 0 (heads) and 1 (tails). Here are some examples:
0 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0
Each state can also be represented using a binary number. For example, the pre- ceding matrices correspond to the numbers
000010000 101001100 110100001 101110100 100111110
There are a total of 512 possibilities, so you can use decimal numbers 0, 1, 2, 3, . . . , and 511 to represent all states of the matrix. Write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding matrix with the characters H and T. Here is a sample run:
M08_LIAN1878_11_GE_C08.indd 333 12/29/17 4:06 PM
334 Chapter 8 Multidimensional Arrays
(82,250 - 33,950) at 25%, (171,550 - 82,550) at 28%, (372,550 - 82,250) at 33%, and (400,000 - 372,950) at 36%. The six rates are the same for all filing statuses, which can be represented in the following array:
double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
The brackets for each rate for all the filing statuses can be represented in a two- dimensional array as follows:
int[][] brackets = { {8350, 33950, 82250, 171550, 372950}, // Single filer {16700, 67900, 137050, 20885, 372950}, // Married jointly // −or qualifying widow(er) {8350, 33950, 68525, 104425, 186475}, // Married separately {11950, 45500, 117450, 190200, 372950} // Head of household
};
Suppose the taxable income is $400,000 for single filers. The tax can be com- puted as follows:
tax = brackets[0][0] * rates[0] + (brackets[0][1] – brackets[0][0]) * rates[1] + (brackets[0][2] – brackets[0][1]) * rates[2] + (brackets[0][3] – brackets[0][2]) * rates[3] + (brackets[0][4] – brackets[0][3]) * rates[4] + (400000 – brackets[0][4]) * rates[5];
*8.13 (Locate the smallest element) Write the following method that returns the location of the smallest element in a two-dimensional array.
public static int[] locateSmallest(double[][] a)
The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the smallest element in the two-dimensional array. Write a test program that prompts the user to enter a two- dimensional array and displays the location of the smallest element in the array. Here is a sample run:
Enter the number of rows and columns of the array: 3 4
Enter the array:
23.5 35 2 10 4.5 3 45 3.5
35 44 5.5 9.6
The location of the smallest element is at (0, 2)
** 8.14 (Explore matrix) Write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix, prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Here is a sample run of the program:
M08_LIAN1878_11_GE_C08.indd 334 12/29/17 4:06 PM
Programming Exercises 335
*8.15 (Geometry: same line?) Programming Exercise 6.39 gives a method for testing whether three points are on the same line.
Write the following method to test whether all the points in the array points are on the same line:
public static boolean sameLine(double[][] points)
Write a program that prompts the user to enter five points and displays whether they are on the same line. Here are sample runs:
Enter the size for the matrix: 4
0111
0000
0100
1111
All 0s on row 2
All 1s on row 4
No same numbers on a column
No same numbers on the major diagonal
No same numbers on the sub−diagonal
Enter five points: 3.4 2 6.5 9.5 2.3 2.3 5.5 5 −5 4
The five points are not on the same line
Enter five points: 1 1 2 2 3 3 4 4 5 5
The five points are on the same line
*8.16 (Sort two-dimensional array) Write a method to sort a two-dimensional array using the following header:
public static void sort(int m[][])
The method performs a primary sort on rows, and a secondary sort on columns. For example, the following array
{{4, 2},{1, 7},{4, 5},{1, 2},{1, 1},{4, 1}}
will be sorted to
{{4, 5},{4, 2},{4, 1},{1, 7},{1, 2},{1, 1}}.
***8.17 (Financial tsunami) Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan. A bank’s total assets are its current balance plus its loans to other banks. The diagram in Figure 8.8 shows five banks. The banks’ current balances are 25, 125, 175, 75, and 181 million dol- lars, respectively. The directed edge from node 1 to node 2 indicates that bank 1 lends 40 million dollars to bank 2.
M08_LIAN1878_11_GE_C08.indd 335 12/29/17 4:06 PM
336 Chapter 8 Multidimensional Arrays
If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to the lender, and the lender cannot count the loan in its total assets. Consequently, the lender may also be unsafe, if its total assets are under the limit. Write a program to find all the unsafe banks. Your program reads the input as follows. It first reads two integers n and limit, where n indicates the number of banks and limit is the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with IDs from 0 to n−1.
The first number in the line is the bank’s balance, the second number indicates the number of banks that borrowed money from the bank, and the rest are pairs of two numbers. Each pair describes a borrower. The first number in the pair is the borrower’s ID and the second is the amount borrowed. For example, the input for the five banks in Figure 8.8 is as follows (note the limit is 201):
5 201 25 2 1 100.5 4 320.5 125 2 2 40 3 85 175 2 0 125 3 75 75 1 0 125 181 1 2 125
The total assets of bank 3 are (75 + 125), which is under 201, so bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 fall below (125 + 40). Thus, bank 1 is also unsafe. The output of the program should be
Unsafe banks are 3 1
(Hint: Use a two-dimensional array borrowers to represent loans. borrowers[i][j] indicates the loan that bank i provides to bank j. Once bank j becomes unsafe, borrowers[i][j] should be set to 0.)
*8.18 (Shuffle rows) Write a method that shuffles the rows in a two-dimensional int array using the following header:
public static void shuffle(int[][] m)
Write a test program that shuffles the following matrix:
int[][] m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
**8.19 (Pattern recognition: four consecutive equal numbers) Write the following method that tests whether a two-dimensional array has four consecutive numbers of the same value, either horizontally, vertically, or diagonally:
public static boolean isConsecutiveFour(int[][] values)
Figure 8.8 Banks lend money to each other.
1
2
3
4
0
100.5
125
85
40 75
175125
125
125
320.5
181
25 75
M08_LIAN1878_11_GE_C08.indd 336 12/29/17 4:06 PM
Programming Exercises 337
Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional array then the values in the array, and displays true if the array contains four consecutive numbers with the same value. Otherwise, the program displays false. Here are some examples of the true cases:
0 1 0 3 1 6 1
0 1 6 8 6 0 1
9 6 2 1 8 2 9
6 9 6 1 1 9 1
1 3 9 1 4 0 7
3 3 3 9 4 0 7
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 6 2 1 8 2 9
6 5 6 1 1 9 1
1 3 6 1 4 0 7
3 3 3 3 4 0 7
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 5 2 1 8 2 9
6 5 6 1 1 9 1
1 5 6 1 4 0 7
3 5 3 3 4 0 7
0 1 0 3 1 6 1
0 1 6 8 6 0 1
5 6 2 1 6 2 9
6 5 6 6 1 9 1
1 3 6 1 4 0 7
3 6 3 3 4 0 7
***8.20 (Game: connect four) Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown below.
The objective of the game is to connect four same-colored disks in a row, a col- umn, or a diagonal before your opponent can do likewise. The program prompts two players to drop a red or yellow disk alternately. In the preceding figure, the red disk is shown in a dark color and the yellow in a light color. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:
| | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Drop a red disk at column (0–6): 0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |R| | | | | | |
M08_LIAN1878_11_GE_C08.indd 337 12/29/17 4:06 PM
338 Chapter 8 Multidimensional Arrays
*8.21 (Central city) Given a set of cities, the central city is the city that has the shortest total distance to all other cities. Write a program that prompts the user to enter the number of cities and the locations of the cities (coordinates), and finds the central city and its total distance to all other cities.
Enter the number of cities: 5
Enter the coordinates of the cities:
2.5 5 5.1 3 1 9 5.4 54 5.5 2.1
The central city is at (2.5, 5.0)
The total distance to all other cities is 60.81
Drop a yellow disk at column (0–6): 3 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |R| | |Y| | | |
. . . . . . . . .
Drop a yellow disk at column (0–6): 6
| | | | | | | | | | | | | | | | | | | |R| | | | | | | |Y|R|Y| | | | |R|Y|Y|Y|Y| |R|Y|R|Y|R|R|R| The yellow player won
*8.22 (Even number of 1s) Write a program that generates a 6-by-6 two-dimensional matrix filled with 0s and 1s, displays the matrix, and checks if every row and every column have an even number of 1s.
*8.23 (Game: find the flipped cell) Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first row r and first column c where the even number of the 1s property is violated (i.e., the number of 1s is not even). The flipped cell is at (r, c). Here is a sample run:
VideoNote
Even number of 1s
Enter a 6−by−6 matrix row by row:
1 1 1 0 1 1 1 1 1 1 0 0
0 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 0 0 0 1
The flipped cell is at (0, 1)
M08_LIAN1878_11_GE_C08.indd 338 12/29/17 4:06 PM
Programming Exercises 339
*8.24 (Check Sudoku solution) Listing 8.4 checks whether a solution is valid by check- ing whether every number is valid in the board. Rewrite the program by checking whether every row, every column, and every small box has the numbers 1 to 9.
*8.25 (Markov matrix) An n * n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following method to check whether a matrix is a Markov matrix:
public static boolean isMarkovMatrix(double[][] m)
Write a test program that prompts the user to enter a 3 * 3 matrix of double values and tests whether it is a Markov matrix. Here are sample runs:
Enter a 3−by−3 matrix row by row:
0.15 0.875 0.375
0.55 0.005 0.225
0.30 0.12 0.4
It is a Markov matrix
Enter a 3−by−3 matrix row by row:
0.95 −0.875 0.375 0.65 0.005 0.225
0.30 0.22 −0.4
It is not a Markov matrix
Enter a 4−by−4 matrix row by row:
0.15 0.875 0.375 0.225
0.55 0.005 0.225 0.015
0.30 0.12 0.4 0.008
0.07 0.021 0.14 0.2 The row−sorted array is
0.15 0.225 0.375 0.875
0.005 0.015 0.225 0.55
0.008 0.12 0.30 0.4
0.021 0.07 0.14 0.2
*8.26 (Row sorting) Implement the following method to sort the rows in a two- dimensional array. A new array is returned and the original array is intact.
public static double[][] sortRows(double[][] m)
Write a test program that prompts the user to enter a 4 * 4 matrix of double values and displays a new row-sorted matrix. Here is a sample run:
*8.27 (Column sorting) Implement the following method to sort the columns in a two dimensional array. A new array is returned and the original array is intact.
public static double[][] sortColumns(double[][] m)
M08_LIAN1878_11_GE_C08.indd 339 12/29/17 4:06 PM
340 Chapter 8 Multidimensional Arrays
Write a test program that prompts the user to enter a 4 * 4 matrix of double values and displays a new column-sorted matrix. Here is a sample run:
Enter a 4−by−4 matrix row by row:
0.15 0.875 0.375 0.225
0.55 0.005 0.225 0.015
0.30 0.12 0.4 0.008 0.07 0.021 0.14 0.2 The column−sorted array is
0.07 0.005 0.14 0.008
0.15 0.021 0.225 0.015
0.30 0.12 0.375 0.2
0.55 0.875 0.4 0.225
Enter list1: 51 22 25 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are strictly identical
Enter list1: 51 25 22 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are not strictly identical
Enter list1: 51 25 22 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are identical
Enter list1: 51 5 22 6 1 4 24 54 6
Enter list2: 51 22 25 6 1 4 24 54 6
The two arrays are not identical
8.28 (Strictly identical arrays) The two-dimensional arrays m1 and m2 are strictly identical if their corresponding elements are equal. Write a method that returns true if m1 and m2 are strictly identical, using the following header:
public static boolean equals(int[][] m1, int[][] m2)
Write a test program that prompts the user to enter two 3 * 3 arrays of integers and displays whether the two are strictly identical. Here are the sample runs:
8.29 (Identical arrays) The two-dimensional arrays m1 and m2 are identical if they have the same contents. Write a method that returns true if m1 and m2 are identi- cal, using the following header:
public static boolean equals(int[][] m1, int[][] m2)
Write a test program that prompts the user to enter two 3 * 3 arrays of integers and displays whether the two are identical. Here are the sample runs:
M08_LIAN1878_11_GE_C08.indd 340 12/29/17 4:06 PM
Programming Exercises 341
*8.30 (Algebra: solve linear equations) Write a method that solves the following 2 * 2 system of linear equations:
a00x + a01y = b0 a10x + a11y = b1
x = b0a11 - b1a01
a00a11 - a01a10 y =
b1a00 - b0a10 a00a11 - a01a10
The method header is:
public static double[] linearEquation(double[][] a, double[] b)
The method returns null if a00a11 - a01a10 is 0. Write a test program that prompts the user to enter a00, a01, a10, a11, b0, and b1 and displays the result. If a00a11 - a01a10 is 0, report that “The equation has no solution.” A sample run is similar to Programming Exercise 3.3.
*8.31 (Geometry: intersecting point) Write a method that returns the intersecting point of two lines. The intersecting point of the two lines can be found by using the formula given in Programming Exercise 3.25. Assume that (x1, y1) and (x2, y2) are the two points on line 1 and (x3, y3) and (x4, y4) are on line 2. The method header is:
public static double[] getIntersectingPoint(double[][] points)
The points are stored in a 4-by-2 two-dimensional array points with (points [0][0], points[0][1]) for (x1, y1). The method returns the intersecting point or null if the two lines are parallel. Write a program that prompts the user to enter four points and displays the intersecting point. See Programming Exercise 3.25 for a sample run.
*8.32 (Geometry: area of a triangle) Write a method that returns the area of a triangle using the following header:
public static double getTriangleArea(double[][] points)
The points are stored in a 3-by-2 two-dimensional array points with points [0][0] and points[0][1] for (x1, y1). The triangle area can be computed using the formula in Programming Exercise 2.19. The method returns 0 if the three points are on the same line. Write a program that prompts the user to enter three points of a triangle and displays the triangle’s area. Here are the sample runs:
Enter x1, y1, x2, y2, x3, y3: 2.5 2 5 −1.0 4.0 2.0
The area of the triangle is 2.25
Enter x1, y1, x2, y2, x3, y3: 2 2 4.5 4.5 6 6
The three points are on the same line
*8.33 (Geometry: polygon subareas) A convex four-vertex polygon is divided into four triangles, as shown in Figure 8.9.
Write a program that prompts the user to enter the coordinates of four vertices and displays the areas of the four triangles in increasing order. Here is a sample run:
Enter x1, y1, x2, y2, x3, y3, x4, y4:
−2.5 2 4 4 3 −2 −2 −3.5
The areas are 6.17 7.96 8.08 10.42
M08_LIAN1878_11_GE_C08.indd 341 12/29/17 4:06 PM
342 Chapter 8 Multidimensional Arrays
*8.34 (Geometry: rightmost lowest point) In computational geometry, often you need to find the rightmost lowest point in a set of points. Write the following method that returns the rightmost lowest point in a set of points:
public static double[] getRightmostLowestPoint(double[][] points)
Write a test program that prompts the user to enter the coordinates of six points and displays the rightmost lowest point. Here is a sample run:
Figure 8.9 A four-vertex polygon is defined by four vertices.
v2 (x2, y2)
v3 (x3, y3)
v4 (x4, y4)
v1 (x1, y1)
Enter 6 points: 1.5 2.5 −3 4.5 5.6 −7 6.5 −7 8 1 10 2.5
The rightmost lowest point is (6.5, −7.0)
Enter the number of rows in the matrix: 5 Enter the matrix row by row:
1 0 1 0 1
1 1 1 0 1
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
The maximum square submatrix is at (2, 2) with size 3
Your program should implement and use the following method to find the maxi- mum square submatrix:
public static int[] findLargestBlock(int[][] m)
The return value is an array that consists of three values. The first two values are the row and column indices for the first element in the submatrix, and the third value is the number of the rows in the submatrix.
**8.36 (Latin square) A Latin square is an n-by-n array filled with n different Latin let- ters, each occurring exactly once in each row and once in each column. Write a
**8.35 (Largest block) Given a square matrix with the elements 0 or 1, write a program to find a maximum square submatrix whose elements are all 1s. Your program should prompt the user to enter the number of rows in the matrix. The program then dis- plays the location of the first element in the maximum square submatrix and the number of rows in the submatrix. Here is a sample run:
M08_LIAN1878_11_GE_C08.indd 342 12/29/17 4:06 PM
Programming Exercises 343
program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square. The characters are the first n characters starting from A.
Enter number n: 4
Enter 4 rows of letters separated by spaces:
A B C D
B A D C
C D B A
D C A B
The input array is a Latin square
Enter number n: 3
Enter 3 rows of letters separated by spaces:
A F D
Wrong input: the letters must be from A to C
What is the capital of Alabama? Montogomery
The correct answer should be Montgomery
What is the capital of Alaska? Juneau
Your answer is correct
What is the capital of Arizona? ...
...
The correct count is 35
**8.37 (Guess the capitals) Write a program that repeatedly prompts the user to enter a capital for a state. Upon receiving the user input, the program reports whether the answer is correct. Assume that 50 states and their capitals are stored in a two-dimensional array, as shown in Figure 8.10. The program prompts the user to answer all states’ capitals and displays the total correct count. The user’s answer is not case-sensitive.
Alabama Montgomery
Alaska Juneau
Arizona Phoenix
... ...
... ...
Figure 8.10 A two-dimensional array stores states and their capitals.
Here is a sample run:
M08_LIAN1878_11_GE_C08.indd 343 12/29/17 4:06 PM
A01_GIPE3898_09_SE_FM.indd 3 12/20/17 5:56 PM
This page intentionally left blank
Objects and Classes
Objectives ■■ To describe objects and classes, and use classes to model objects (§9.2).
■■ To use UML graphical notation to describe classes and objects (§9.2).
■■ To demonstrate how to define classes and create objects (§9.3).
■■ To create objects using constructors (§9.4).
■■ To access objects via object reference variables (§9.5).
■■ To define a reference variable using a reference type (§9.5.1).
■■ To access an object’s data and methods using the object member access operator (.) (§9.5.2).
■■ To define data fields of reference types and assign default values for an object’s data fields (§9.5.3).
■■ To distinguish between object reference variables and primitive-data- type variables (§9.5.4).
■■ To use the Java library classes Date, Random, and Point2D (§9.6).
■■ To distinguish between instance and static variables and methods (§9.7).
■■ To define private data fields with appropriate getter and setter methods (§9.8).
■■ To encapsulate data fields to make classes easy to maintain (§9.9).
■■ To develop methods with object arguments and differentiate between primitive-type arguments and object-type arguments (§9.10).
■■ To store and process objects in arrays (§9.11).
■■ To create immutable objects from immutable classes to protect the contents of objects (§9.12).
■■ To determine the scope of variables in the context of a class (§9.13).
■■ To use the keyword this to refer to the calling object itself (§9.14).
Chapter
9
M09_LIAN1878_11_GE_C09.indd 345 12/29/17 4:11 PM
346 Chapter 9 Objects and Classes
9.1 Introduction Object-oriented programming enables you to develop large-scale software and GUIs effectively.
Object-oriented programming is essentially a technology for developing reusable soft- ware. Having learned the material in the preceding chapters, you are able to solve many pro- gramming problems using selections, loops, methods, and arrays. However, these Java features are not sufficient for developing graphical user interfaces and large-scale software systems. Suppose you want to develop a graphical user interface (GUI, pronounced goo-ee) as shown in Figure 9.1. How would you program it?
Point Key
why OOP?
Figure 9.1 The GUI objects are created from classes.
Button Text Field Radio Button Combo BoxLabel Check Box
This chapter introduces object-oriented programming, which you can use to develop GUI and large-scale software systems.
9.2 Defining Classes for Objects A class defines the properties and behaviors for objects.
Object-oriented programming (OOP) involves programming using objects. An object repre- sents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behavior.
■■ The state of an object (also known as its properties or attributes) is represented by data fields with their current values. A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object, for example, has the data fields width and height, which are the properties that characterize a rectangle.
■■ The behavior of an object (also known as its actions) is defined by methods. To invoke a method on an object is to ask the object to perform an action. For example, you may define methods named getArea() and getPerimeter() for circle objects. A circle object may invoke getArea() to return its area and getPerimeter() to return its perimeter. You may also define the setRadius(radius) method. A circle object can invoke this method to change its radius.
Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object’s data fields and methods will be. An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. The relationship between classes and objects is analogous to that between an apple-pie recipe and apple pies: You can make as many apple pies as you want from a single recipe. Figure 9.2 shows a class named Circle and its three objects.
A Java class uses variables to define data fields and methods to define actions. In addition, a class provides methods of a special type, known as constructors, which are invoked to create a new object. A constructor can perform any action, but constructors are designed to perform initializing actions, such as initializing the data fields of objects. Figure 9.3 shows an example of defining the class for circle objects.
Point Key
VideoNote Define classes and objects
object state of an object
properties attributes
data fields behavior actions
class
contract
instantiation
instance
data field method
constructors
M09_LIAN1878_11_GE_C09.indd 346 12/29/17 4:11 PM
9.2 Defining Classes for Objects 347
The Circle class is different from all of the other classes you have seen thus far. It does not have a main method, and therefore cannot be run; it is merely a definition for circle objects. The class that contains the main method will be referred to in this book, for convenience, as the main class.
The illustration of class templates and objects in Figure 9.2 can be standardized using Unified Modeling Language (UML) notation. This notation, as shown in Figure 9.4, is called a UML class diagram, or simply a class diagram. In the class diagram, the data field is denoted as
dataFieldName: dataFieldType
The constructor is denoted as
ClassName(parameterName: parameterType)
main class Unified Modeling Language
(UML)
class diagram
Figure 9.2 A class is a template for creating objects.
Class Name: Circle
Data Fields: radius is _____
Methods: getArea getPerimeter setRadius
Circle Object 1
Data Fields: radius is 1
Circle Object 2
Data Fields: radius is 25
Circle Object 3
Data Fields: radius is 125
A class template
Three objects of the Circle class
Figure 9.3 A class is a construct that defines objects of the same type.
Data �eld
Constructors
Method
class Circle { /** The radius of this circle */ double radius = 1;
/** Construct a circle object */ Circle() { }
/** Construct a circle object */ Circle(double newRadius) { radius = newRadius; }
/** Return the area of this circle */ double getArea() { return radius * radius * Math.PI; }
/** Return the perimeter of this circle */ double getPerimeter() { return 2 * radius * Math.PI; }
/** Set a new radius for this circle */ void setRadius(double newRadius) { radius = newRadius; } }
M09_LIAN1878_11_GE_C09.indd 347 12/29/17 4:11 PM
348 Chapter 9 Objects and Classes
The method is denoted as
methodName(parameterName: parameterType): returnType
9.3 Example: Defining Classes and Creating Objects Classes are definitions for objects and objects are created from classes.
This section gives two examples of defining classes and uses the classes to create objects. Listing 9.1 is a program that defines the Circle class and uses it to create objects. The pro- gram constructs three circle objects with radius 1, 25, and 125 and displays the radius and area of each of the three circles. It then changes the radius of the second object to 100 and displays its new radius and area.
Listing 9.1 TestCircle.java 1 public class TestCircle { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create a circle with radius 1 5 Circle circle1 = new Circle(); 6 System.out.println("The area of the circle of radius " 7 + circle1.radius + " is " + circle1.getArea()); 8 9 // Create a circle with radius 25 10 Circle circle2 = new Circle(25); 11 System.out.println("The area of the circle of radius " 12 + circle2.radius + " is " + circle2.getArea()); 13 14 // Create a circle with radius 125 15 Circle circle3 = new Circle(125); 16 System.out.println("The area of the circle of radius " 17 + circle3.radius + " is " + circle3.getArea()); 18 19 // Modify circle radius 20 circle2.radius = 100; // or circle2.setRadius(100) 21 System.out.println("The area of the circle of radius " 22 + circle2.radius + " is " + circle2.getArea()); 23 } 24 } 25
Point Key
main class
main method
create object
create object
create object
Figure 9.4 Classes and objects can be represented using UML notation.
Class nameCircle
radius: double
Circle()
Circle(newRadius: double)
getArea(): double
getPerimeter(): double
setRadius(newRadius: double): void
Data �elds
Constructors and methods
UML Class Diagram
UML notation for objects
circle2: Circle
radius = 25
circle3: Circle
radius = 125
circle1: Circle
radius = 1
M09_LIAN1878_11_GE_C09.indd 348 12/29/17 4:11 PM
9.3 Example: Defining Classes and Creating Objects 349
26 // Define the circle class with two constructors 27 class Circle { 28 double radius; 29 30 /** Construct a circle with radius 1 */ 31 Circle() { 32 radius = 1; 33 } 34 35 /** Construct a circle with a specified radius */ 36 Circle(double newRadius) { 37 radius = newRadius; 38 } 39 40 /** Return the area of this circle */ 41 double getArea() { 42 return radius * radius * Math.PI; 43 } 44 45 /** Return the perimeter of this circle */ 46 double getPerimeter() { 47 return 2 * radius * Math.PI; 48 } 49 50 /** Set a new radius for this circle */ 51 void setRadius(double newRadius) { 52 radius = newRadius; 53 } 54 }
class Circle data field
no-arg constructor
second constructor
getArea
getPerimeter
setRadius
The area of the circle of radius 1.0 is 3.141592653589793
The area of the circle of radius 25.0 is 1963.4954084936207
The area of the circle of radius 125.0 is 49087.385212340516
The area of the circle of radius 100.0 is 31415.926535897932
Figure 9.5 Each class in the source code file is compiled into a .class file.
Java Compilercompiled
by
generates
generates
// File TestCircle.java
public class TestCircle { … }
class Circle { … }
TestCircle.class
Circle.class
The program contains two classes. The first of these, TestCircle, is the main class. Its sole purpose is to test the second class, Circle. Such a program that uses the class is often referred to as a client of the class. When you run the program, the Java runtime system invokes the main method in the main class.
You can put the two classes into one file, but only one class in the file can be a public class. Furthermore, the public class must have the same name as the file name. Therefore, the file name is TestCircle.java, since TestCircle is public. Each class in the source code is com- piled into a .class file. When you compile TestCircle.java, two class files TestCircle.class and Circle.class are generated, as shown in Figure 9.5.
client
public class
M09_LIAN1878_11_GE_C09.indd 349 12/29/17 4:11 PM
350 Chapter 9 Objects and Classes
The main class contains the main method (line 3) that creates three objects. As in creating an array, the new operator is used to create an object from the constructor: new Circle() creates an object with radius 1 (line 5), new Circle(25) creates an object with radius 25 (line 10), and new Circle(125) creates an object with radius 125 (line 15).
These three objects (referenced by circle1, circle2, and circle3) have different data but the same methods. Therefore, you can compute their respective areas by using the getArea() method. The data fields can be accessed via the reference of the object using circle1.radius, circle2.radius, and circle3.radius, respectively. The object can invoke its method via the reference of the object using circle1.getArea(), circle2. getArea(), and circle3.getArea(), respectively.
These three objects are independent. The radius of circle2 is changed to 100 in line 20. The object’s new radius and area are displayed in lines 21 and 22.
There are many ways to write Java programs. For instance, you can combine the two classes in the preceding example into one, as given in Listing 9.2.
Listing 9.2 Circle.java (AlternativeCircle.java) 1 public class Circle { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create a circle with radius 1 5 Circle circle1 = new Circle(); 6 System.out.println("The area of the circle of radius " 7 + circle1.radius + " is " + circle1.getArea()); 8 9 // Create a circle with radius 25 10 Circle circle2 = new Circle(25); 11 System.out.println("The area of the circle of radius " 12 + circle2.radius + " is " + circle2.getArea()); 13 14 // Create a circle with radius 125 15 Circle circle3 = new Circle(125); 16 System.out.println("The area of the circle of radius " 17 + circle3.radius + " is " + circle3.getArea()); 18 19 // Modify circle radius 20 circle2.radius = 100; 21 System.out.println("The area of the circle of radius " 22 + circle2.radius + " is " + circle2.getArea()); 23 } 24 25 double radius; 26 27 /** Construct a circle with radius 1 */ 28 Circle() { 29 radius = 1; 30 } 31 32 /** Construct a circle with a specified radius */ 33 Circle(double newRadius) { 34 radius = newRadius; 35 } 36 37 /** Return the area of this circle */ 38 double getArea() { 39 return radius * radius * Math.PI; 40 } 41
main method
data field
no-arg constructor
second constructor
method
M09_LIAN1878_11_GE_C09.indd 350 12/29/17 4:11 PM
9.3 Example: Defining Classes and Creating Objects 351
42 /** Return the perimeter of this circle */ 43 double getPerimeter() { 44 return 2 * radius * Math.PI; 45 } 46 47 /** Set a new radius for this circle */ 48 void setRadius(double newRadius) { 49 radius = newRadius; 50 } 51 }
Since the combined class has a main method, it can be executed by the Java interpreter. The main method is the same as that in Listing 9.1. This demonstrates that you can test a class by simply adding a main method in the same class.
As another example, consider television sets. Each TV is an object with states (current channel, current volume level, and power on or off) and behaviors (change channels, adjust volume, and turn on/off). You can use a class to model TV sets. The UML diagram for the class is shown in Figure 9.6.
Figure 9.6 The TV class models TV sets.
TV
The current channel (1–120) of this TV.
The current volume level (1–7) of this TV.
Indicates whether this TV is on/off.
channel: int
volumeLevel: int
on: boolean
+TV()
+turnOn(): void
+turnOf f(): void
+setChannel(newChannel: int): void
+setVolume(newVolumeLevel: int): void
+channelUp(): void
+channelDown(): void
+volumeUp(): void
+volumeDown(): void
Constructs a default TV object.
Turns on this TV.
Turns off this TV.
Sets a new channel for this TV.
Sets a new volume level for this TV.
Increases the channel number by 1.
Decreases the channel number by 1.
Increases the volume level by 1.
Decreases the volume level by 1.
The + sign indicates public modi�er
Listing 9.3 gives a program that defines the TV class.
Listing 9.3 TV.java 1 public class TV { 2 int channel = 1; // Default channel is 1 3 int volumeLevel = 1; // Default volume level is 1 4 boolean on = false; // TV is off 5 6 public TV() { 7 } 8 9 public void turnOn() { 10 on = true; 11 } 12 13 public void turnOff() {
data fields
constructor
turn on TV
turn off TV
M09_LIAN1878_11_GE_C09.indd 351 12/29/17 4:11 PM
352 Chapter 9 Objects and Classes
14 on = false; 15 } 16 17 public void setChannel(int newChannel) { 18 if (on && newChannel >= 1 && newChannel <= 120) 19 channel = newChannel; 20 } 21 22 public void setVolume(int newVolumeLevel) { 23 if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7) 24 volumeLevel = newVolumeLevel; 25 } 26 27 public void channelUp() { 28 if (on && channel < 120) 29 channel++; 30 } 31 32 public void channelDown() { 33 if (on && channel > 1) 34 channel—–; 35 } 36 37 public void volumeUp() { 38 if (on && volumeLevel < 7) 39 volumeLevel++; 40 } 41 42 public void volumeDown() { 43 if (on && volumeLevel > 1) 44 volumeLevel—–; 45 } 46 }
The constructor and methods in the TV class are defined public so they can be accessed from other classes. Note the channel and volume level are not changed if the TV is not on. Before either of these is changed, its current value is checked to ensure it is within the correct range.
Listing 9.4 gives a program that uses the TV class to create two objects.
Listing 9.4 TestTV.java 1 public class TestTV { 2 public static void main(String[] args) { 3 TV tv1 = new TV(); 4 tv1.turnOn(); 5 tv1.setChannel(30); 6 tv1.setVolume(3); 7 8 TV tv2 = new TV(); 9 tv2.turnOn(); 10 tv2.channelUp(); 11 tv2.channelUp(); 12 tv2.volumeUp(); 13 14 System.out.println("tv1's channel is " + tv1.channel 15 + " and volume level is " + tv1.volumeLevel); 16 System.out.println("tv2's channel is " + tv2.channel 17 + " and volume level is " + tv2.volumeLevel); 18 } 19 }
set a new channel
set a new volume
increase channel
decrease channel
increase volume
decrease volume
main method create a TV turn on set a new channel set a new volume
create a TV turn on increase channel
increase volume
display state
M09_LIAN1878_11_GE_C09.indd 352 12/29/17 4:11 PM
9.4 Constructing Objects Using Constructors 353
The program creates two objects in lines 3 and 8 and invokes the methods on the objects to perform actions for setting channels and volume levels and for increasing channels and vol- umes. The program displays the state of the objects in lines 14–17. The methods are invoked using syntax such as tv1.turnOn() (line 4). The data fields are accessed using syntax such as tv1.channel (line 14).
These examples have given you a glimpse of classes and objects. You may have many ques- tions regarding constructors, objects, reference variables, accessing data fields, and invoking object’s methods. The sections that will follow discuss these issues in detail.
9.3.1 Describe the relationship between an object and its defining class. 9.3.2 How do you define a class? 9.3.3 How do you declare an object’s reference variable? 9.3.4 How do you create an object?
9.4 Constructing Objects Using Constructors A constructor is invoked to create an object using the new operator.
Constructors are a special kind of method. They have three peculiarities:
■■ A constructor must have the same name as the class itself.
■■ Constructors do not have a return type—not even void.
■■ Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
The constructor has exactly the same name as its defining class. Like regular methods, con- structors can be overloaded (i.e., multiple constructors can have the same name but different signatures), making it easy to construct objects with different initial data values.
It is a common mistake to put the void keyword in front of a constructor. For example,
public void Circle() { }
In this case, Circle() is a method, not a constructor. Constructors are used to construct objects. To construct an object from a class, invoke a
constructor of the class using the new operator, as follows:
new ClassName(arguments);
For example, new Circle() creates an object of the Circle class using the first construc- tor defined in the Circle class, and new Circle(25) creates an object using the second constructor defined in the Circle class.
A class normally provides a constructor without arguments (e.g., Circle()). Such a con- structor is referred to as a no-arg or no-argument constructor.
A class may be defined without constructors. In this case, a public no-arg constructor with an empty body is implicitly defined in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class.
9.4.5 What are the differences between constructors and methods? 9.4.6 When will a class have a default constructor?
Point Check
Point Key
constructor’s name
no return type
new operator
overloaded constructors
no void
constructing objects
no-arg constructor
default constructor
Point Check
tv1's channel is 30 and volume level is 3
tv2's channel is 3 and volume level is 2
M09_LIAN1878_11_GE_C09.indd 353 12/29/17 4:11 PM
354 Chapter 9 Objects and Classes
9.5 Accessing Objects via Reference Variables An object’s data and methods can be accessed through the dot (.) operator via the object’s reference variable.
Newly created objects are allocated in the memory. They can be accessed via reference variables.
9.5.1 Reference Variables and Reference Types Objects are accessed via the object’s reference variables, which contain references to the objects. Such variables are declared using the following syntax:
ClassName objectRefVar;
A class is essentially a programmer-defined type. A class is a reference type, which means that a variable of the class type can reference an instance of the class. The following statement declares the variable myCircle to be of the Circle type:
Circle myCircle;
The variable myCircle can reference a Circle object. The next statement creates an object and assigns its reference to myCircle:
myCircle = new Circle();
You can write a single statement that combines the declaration of an object reference variable, the creation of an object, and the assigning of an object reference to the variable with the fol- lowing syntax:
ClassName objectRefVar = new ClassName();
Here is an example:
Circle myCircle = new Circle();
The variable myCircle holds a reference to a Circle object.
Note An object reference variable that appears to hold an object actually contains a reference to that object. Strictly speaking, an object reference variable and an object are different, but most of the time the distinction can be ignored. Therefore, it is fine, for simplicity, to say that myCircle is a Circle object rather than use the long-winded description that myCircle is a variable that contains a reference to a Circle object.
Note Arrays are treated as objects in Java. Arrays are created using the new operator. An array variable is actually a variable that contains a reference to an array.
9.5.2 Accessing an Object’s Data and Methods In OOP terminology, an object’s member refers to its data fields and methods. After an object is created, its data can be accessed and its methods can be invoked using the dot operator (.), also known as the object member access operator:
■■ objectRefVar.dataField references a data field in the object.
■■ objectRefVar.method(arguments) invokes a method on the object.
Point Key
reference variable
reference type
object vs. object reference variable
array object
dot operator (.)
M09_LIAN1878_11_GE_C09.indd 354 12/29/17 4:11 PM
9.5 Accessing Objects via Reference Variables 355
For example, myCircle.radius references the radius in myCircle and myCircle .getArea() invokes the getArea method on myCircle. Methods are invoked as operations on objects.
The data field radius is referred to as an instance variable because it is dependent on a specific instance. For the same reason, the method getArea is referred to as an instance method because you can invoke it only on a specific instance. The object on which an instance method is invoked is called a calling object.
Caution Recall that you use Math.methodName(arguments) (e.g., Math.pow(3, 2.5)) to invoke a method in the Math class. Can you invoke getArea() using Circle. getArea()? The answer is no. All the methods in the Math class are static methods, which are defined using the static keyword. However, getArea() is an instance method, and thus nonstatic. It must be invoked from an object using objectRefVar. methodName(arguments) (e.g., myCircle.getArea()). Further explanation will be given in Section 9.7, Static Variables, Constants, and Methods.
Note Usually you create an object and assign it to a variable, then later you can use the variable to reference the object. Occasionally, an object does not need to be referenced later. In this case, you can create an object without explicitly assigning it to a variable using the syntax:
new Circle();
or
System.out.println("Area is " + new Circle(5).getArea());
The former statement creates a Circle object. The latter creates a Circle object and invokes its getArea method to return its area. An object created in this way is known as an anonymous object.
9.5.3 Reference Data Fields and the null Value The data fields can be of reference types. For example, the following Student class contains a data field name of the String type. String is a predefined Java class.
class Student { String name; // name has the default value null int age; // age has the default value 0 boolean isScienceMajor; // isScienceMajor has default value false char gender; // gender has default value '\u0000' }
If a data field of a reference type does not reference any object, the data field holds a special Java value, null. null is a literal just like true and false. While true and false are Boolean literals, null is a literal for a reference type.
The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and \u0000 for a char type. However, Java assigns no default value to a local variable inside a method. The following code displays the default values of the data fields name, age, isScienceMajor, and gender for a Student object:
class TestStudent { public static void main(String[] args) { Student student = new Student(); System.out.println("name? " + student.name);
instance variable
instance method
calling object
invoking methods
anonymous object
reference data fields
null value
default field values
M09_LIAN1878_11_GE_C09.indd 355 12/29/17 4:11 PM
356 Chapter 9 Objects and Classes
System.out.println("age? " + student.age); System.out.println("isScienceMajor? " + student.isScienceMajor); System.out.println("gender? " + student.gender); } }
The following code has a compile error, because the local variables x and y are not initialized:
class TestLocalVariables { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } }
Caution NullPointerException is a common runtime error. It occurs when you invoke a method on a reference variable with a null value. Make sure you assign an object refer- ence to the variable before invoking the method through the reference variable (see CheckPoint Question 9.5.5c).
9.5.4 Differences between Variables of Primitive Types and Reference Types
Every variable represents a memory location that holds a value. When you declare a variable, you are telling the compiler what type of value the variable can hold. For a variable of a primi- tive type, the value is of the primitive type. For a variable of a reference type, the value is a reference to where an object is located. For example, as shown in Figure 9.7, the value of int variable i is int value 1, and the value of Circle object c holds a reference to where the contents of the Circle object are stored in memory.
NullPointerException
Figure 9.7 A variable of a primitive type holds a value of the primitive type, and a variable of a reference type holds a reference to where an object is stored in memory.
Primitive type
Object type
int i = 1
Circle c
i
c reference
1
Created using new Circle()
radius = 1
c: Circle
Figure 9.8 Primitive variable j is copied to variable i.
Primitive type assignment i = j
Before i = j After i = j
2
2
i
j
1
2
i
j
When you assign one variable to another, the other variable is set to the same value. For a variable of a primitive type, the real value of one variable is assigned to the other variable. For a variable of a reference type, the reference of one variable is assigned to the other vari- able. As shown in Figure 9.8, the assignment statement i = j copies the contents of j into i
M09_LIAN1878_11_GE_C09.indd 356 12/29/17 4:11 PM
9.5 Accessing Objects via Reference Variables 357
Note As illustrated in Figure 9.9, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer useful and therefore is now known as garbage. Garbage occupies memory space, so the Java runtime system detects garbage and automatically reclaims the space it occupies. This process is called garbage collection.
Tip If you know that an object is no longer needed, you can explicitly assign null to a refer- ence variable for the object. The JVM will automatically collect the space if the object is not referenced by any reference variable.
9.5.1 Which operator is used to access a data field or invoke a method from an object? 9.5.2 What is an anonymous object? 9.5.3 What is NullPointerException? 9.5.4 Is an array an object or a primitive-type value? Can an array contain elements of an
object type? Describe the default value for the elements of an array.
9.5.5 What is wrong with each of the following programs?
1 public class ShowErrors { 2 public static void main(String[] args) { 3 ShowErrors t = new ShowErrors(5); 4 } 5 }
1 public class ShowErrors { 2 public static void main(String[] args) { 3 ShowErrors t = new ShowErrors(); 4 t.x(); 5 } 6 }
(a) (b)
1 public class ShowErrors { 2 public void method1() { 3 Circle c; 4 System.out.println("What is radius " 5 + c.getRadius()); 6 c = new Circle(); 7 } 8 }
1 public class ShowErrors { 2 public static void main(String[] args) { 3 C c = new C(5.0); 4 System.out.println(c.value); 5 } 6 } 7 8 class C { 9 int value = 2; 10 }
(c) (d)
garbage
garbage collection
Point Check
for primitive variables. As shown in Figure 9.9, the assignment statement c1 = c2 copies the reference of c2 into c1 for reference variables. After the assignment, variables c1 and c2 refer to the same object.
Figure 9.9 Reference variable c2 is copied to variable c1.
Object type assignment c1 = c2
c1
After c1 = c2
c2
Before c1 = c2
c1
c2
radius = 9
c2: Circle
radius = 5
c1: Circle
radius = 5
c1: Circle
radius = 9
c2: Circle
M09_LIAN1878_11_GE_C09.indd 357 12/29/17 4:11 PM
358 Chapter 9 Objects and Classes
9.5.6 What is wrong in the following code? 1 class Test { 2 public static void main(String[] args) { 3 A a = new A(); 4 a.print(); 5 } 6 } 7 8 class A { 9 String s; 10 11 A(String newS) { 12 s = newS; 13 } 14 15 public void print() { 16 System.out.print(s); 17 } 18 }
9.5.7 What is the output of the following code? public class A { boolean x;
public static void main(String[] args) { A a = new A(); System.out.println(a.x); } }
9.6 Using Classes from the Java Library The Java API contains a rich set of classes for developing Java programs.
Listing 9.1 defined the Circle class and created objects from the class. You will frequently use the classes in the Java library to develop programs. This section gives some examples of the classes in the Java library.
9.6.1 The Date Class In Listing 2.7, ShowCurrentTime.java, you learned how to obtain the current time using System.currentTimeMillis(). You used the division and remainder operators to extract the current second, minute, and hour. Java provides a system-independent encapsulation of date and time in the java.util.Date class, as shown in Figure 9.10.
Point Key
VideoNote
Use classes
java.util.Date class
Figure 9.10 A Date object represents a specific date and time.
java.util.Date
+Date()
+Date(elapseTime: long)
+toString(): String
+getTime(): long
+setTime(elapseTime: long): void
Returns a string representing the date and time.
Returns the number of milliseconds since January 1,
Sets a new elapse time in the object.
Constructs a Date object for the current time.
Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.
1970, GMT.
M09_LIAN1878_11_GE_C09.indd 358 12/29/17 4:11 PM
9.6 Using Classes from the Java Library 359
You can use the no-arg constructor in the Date class to create an instance for the current date and time, the getTime() method to return the elapsed time in milliseconds since January 1, 1970, GMT, and the toString() method to return the date and time as a string. For example, the following code
java.util.Date date = new java.util.Date(); System.out.println("The elapsed time since Jan 1, 1970 is " + date.getTime() + " milliseconds"); System.out.println(date.toString());
displays the output as follows:
The elapsed time since Jan 1, 1970 is 1324903419651 milliseconds Mon Dec 26 07:43:39 EST 2011
The Date class has another constructor, Date(long elapseTime), which can be used to construct a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.
9.6.2 The Random Class You have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). Another way to generate random numbers is to use the java.util.Random class, as shown in Figure 9.11, which can generate a random int, long, double, float, and boolean value.
create object
get elapsed time invoke toString
Figure 9.11 A Random object can be used to generate random values.
+Random()
+Random(seed: long)
+nextInt(): int
+nextInt(n: int): int
+nextLong(): long
+nextDouble(): double
+nextFloat(): float
+nextBoolean(): boolean
Constructs a Random object with the current time as its seed.
Constructs a Random object with a speci�ed seed.
Returns a random int value.
Returns a random int value between 0 and n (excluding n).
Returns a random long value.
Returns a random double value between 0.0 and 1.0 (excluding 1.0).
Returns a random float value between 0.0F and 1.0F (excluding 1.0F).
Returns a random boolean value.
java.util.Random
When you create a Random object, you have to specify a seed or use the default seed. A seed is a number used to initialize a random number generator. The no-arg constructor creates a Random object using the current elapsed time as its seed. If two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed, 3:
Random generator1 = new Random(3); System.out.print("From generator1: "); for (int i = 0; i < 10; i++) System.out.print(generator1.nextInt(1000) + " ");
Random generator2 = new Random(3); System.out.print("\nFrom generator2: "); for (int i = 0; i < 10; i++) System.out.print(generator2.nextInt(1000) + " ");
The code generates the same sequence of random int values:
From generator1: 734 660 210 581 128 202 549 564 459 961 From generator2: 734 660 210 581 128 202 549 564 459 961
M09_LIAN1878_11_GE_C09.indd 359 12/29/17 4:11 PM
360 Chapter 9 Objects and Classes
Note The ability to generate the same sequence of random values is useful in software testing and many other applications. In software testing, often you need to reproduce the test cases from a fixed sequence of random numbers.
Note You can generate random numbers using the java.security.SecureRandom class rather than the Random class. The random numbers generated from the Random are deterministic and they can be predicated by hackers. The random numbers generated from the SecureRandom class are nondeterministic and are secure.
9.6.3 The Point2D Class Java API has a convenient Point2D class in the javafx.geometry package for representing a point in a two-dimensional plane. The UML diagram for the class is shown in Figure 9.12.
same sequence
SecureRandom
Figure 9.12 A Point2D object represents a point with x- and y-coordinates.
Constructs a Point2D object with the speci�ed x- and y-coordinates.
Returns the distance between this point and the speci�ed point (x, y).
Returns the distance between this point and the speci�ed point p.
Returns the x-coordinate from this point.
Returns the y-coordinate from this point.
Returns the midpoint between this point and point p.
Returns a string representation for the point.
+Point2D(x: double, y: double)
+distance(x: double, y: double): double
+distance(p: Point2D): double
+getX(): double
+getY(): double
+midpoint(p: Point2D): Point2D
+toString(): String
javafx.geometry.Point2D
You can create a Point2D object for a point with the specified x- and y-coordinates, use the distance method to compute the distance from this point to another point, and use the toString() method to return a string representation of the point. Listing 9.5 gives an example of using this class.
Listing 9.5 TestPoint2D.java 1 import java.util.Scanner; 2 import javafx.geometry.Point2D; 3 4 public class TestPoint2D { 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 8 System.out.print("Enter point1's x-, y–coordinates: "); 9 double x1 = input.nextDouble(); 10 double y1 = input.nextDouble(); 11 System.out.print("Enter point2's x-, y–coordinates: "); 12 double x2 = input.nextDouble(); 13 double y2 = input.nextDouble(); 14 15 Point2D p1 = new Point2D(x1, y1); 16 Point2D p2 = new Point2D(x2, y2); 17 System.out.println("p1 is " + p1.toString()); 18 System.out.println("p2 is " + p2.toString()); 19 System.out.println("The distance between p1 and p2 is " + 20 p1.distance(p2));
create an object
invoke toString()
get distance
M09_LIAN1878_11_GE_C09.indd 360 12/29/17 4:11 PM
9.7 Static Variables, Constants, and Methods 361
This program creates two objects of the Point2D class (lines 15 and 16). The toString() method returns a string that describes the object (lines 17 and 18). Invoking p1.distance(p2) returns the distance between the two points (line 20). Invoking p1.midpoint(p2) returns the midpoint between the two points (line 22).
9.6.1 How do you create a Date for the current time? How do you display the current time? 9.6.2 How do you create a Point2D? Suppose p1 and p2 are two instances of Point2D,
how do you obtain the distance between the two points? How do you obtain the midpoint between the two points?
9.6.3 Which packages contain the classes Date, Random, Point2D, System, and Math?
9.7 Static Variables, Constants, and Methods A static variable is shared by all objects of the class. A static method cannot access instance members (i.e., instance data fields and methods) of the class.
The data field radius in the circle class is known as an instance variable. An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. For example, suppose that you create the following objects:
Circle circle1 = new Circle(); Circle circle2 = new Circle(5);
The radius in circle1 is independent of the radius in circle2 and is stored in a different memory location. Changes made to circle1’s radius do not affect circle2’s radius, and vice versa.
If you want all the instances of a class to share data, use static variables, also known as class variables. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected. Java supports static methods as well as static variables. Static methods can be called without creating an instance of the class.
Let’s modify the Circle class by adding a static variable numberOfObjects to count the number of circle objects created. When the first object of this class is created, numberOfObjects is 1. When the second object is created, numberOfObjects becomes 2. The UML of the new circle class is shown in Figure 9.13. The Circle class defines the instance variable radius and the static variable numberOfObjects, the instance methods getRadius, setRadius, and getArea, and the static method getNumberOfObjects. (Note static variables and methods are underlined in the UML class diagram.)
Point Check
Point Key
VideoNote
Static vs. instance
Static vs. instance
instance variable
static variable
static method
Enter point1's x-, y-coordinates: 1.5 5.5
Enter point2's x-, y-coordinates: −5.3 −4.4 p1 is Point2D [x = 1.5, y = 5.5]
p2 is Point2D [x = −5.3, y = −4.4]
The distance between p1 and p2 is 12.010412149464313
The midpoint between p1 and p2 is
Point2D [x = −1.9, y = 0.5499999999999998]
21 System.out.println("The midpoint between p1 and p2 is " + 22 p1.midpoint(p2).toString()); 23 } 24 }
get midpoint
M09_LIAN1878_11_GE_C09.indd 361 12/29/17 4:11 PM
362 Chapter 9 Objects and Classes
To declare a static variable or define a static method, put the modifier static in the vari- able or method declaration. The static variable numberOfObjects and the static method getNumberOfObjects() can be declared as follows:
static int numberOfObjects;
static int getNumberObjects() { return numberOfObjects; }
Constants in a class are shared by all objects of the class. Thus, constants should be declared as final static. For example, the constant PI in the Math class is defined as follows:
final static double PI = 3.14159265358979323846;
The new circle class is defined in Listing 9.6.
Listing 9.6 Circle.java (for CircleWithStaticMembers) 1 public class Circle { 2 /** The radius of the circle */ 3 double radius; 4 5 /** The number of objects created */ 6 static int numberOfObjects = 0; 7 8 /** Construct a circle with radius 1 */ 9 Circle() { 10 radius = 1; 11 numberOfObjects++; 12 } 13 14 /** Construct a circle with a specified radius */ 15 Circle(double newRadius) { 16 radius = newRadius; 17 numberOfObjects++; 18 } 19 20 /** Return numberOfObjects */ 21 static int getNumberOfObjects() { 22 return numberOfObjects; 23 } 24 25 /** Return the area of this circle */
declare static variable
define static method
declare constant
static variable
increase by 1
increase by 1
static method
Figure 9.13 Instance variables belong to the instances and have memory storage independent of one another. Static variables are shared by all the instances of the same class.
radius
numberOfObjects
radius
Memory instantiate
instantiate
Circle
circle2: Circle
radius: double numberOfObjects: int
getNumberOfObjects(): int getArea(): double
radius = 1 numberOfObjects = 2
radius = 5 numberOfObjects = 2
circle1: Circle
UML Notation: underline: static variables or methods
1
2
5
After two Circle Objects were created, numberOfObjects is 2.
M09_LIAN1878_11_GE_C09.indd 362 12/29/17 4:11 PM
9.7 Static Variables, Constants, and Methods 363
26 double getArea() { 27 return radius * radius * Math.PI; 28 } 29 }
Method getNumberOfObjects() in Circle is a static method. All the methods in the Math class are static. The main method is static, too.
Instance methods (e.g., getArea()) and instance data (e.g., radius) belong to instances and can be used only after the instances are created. They are accessed via a reference variable. Static methods (e.g., getNumberOfObjects()) and static data (e.g., numberOfObjects) can be accessed from a reference variable or from their class name.
The program in Listing 9.7 demonstrates how to use instance and static variables and meth- ods and illustrates the effects of using them.
Listing 9.7 TestCircleWithStaticMembers.java 1 public class TestCircleWithStaticMembers { 2 /** Main method */ 3 public static void main(String[] args) { 4 System.out.println("Before creating objects"); 5 System.out.println("The number of Circle objects is " + 6 Circle.numberOfObjects); 7 8 // Create c1 9 Circle c1 = new Circle(); // Use the Circle class in Listing 9.6 10 11 // Display c1 BEFORE c2 is created 12 System.out.println("\nAfter creating c1"); 13 System.out.println("c1: radius (" + c1.radius + 14 ") and number of Circle objects (" + 15 c1.numberOfObjects + ")"); 16 17 // Create c2 18 Circle c2 = new Circle(5); 19 20 // Modify c1 21 c1.radius = 9; 22 23 // Display c1 and c2 AFTER c2 was created 24 System.out.println("\nAfter creating c2 and modifying c1"); 25 System.out.println("c1: radius (" + c1.radius + 26 ") and number of Circle objects (" + 27 c1.numberOfObjects + ")"); 28 System.out.println("c2: radius (" + c2.radius + 29 ") and number of Circle objects (" + 30 c2.numberOfObjects + ")"); 31 } 32 }
static variable
instance variable
static variable
instance variable
static variable
static variable
Before creating objects
The number of Circle objects is 0
After creating c1
c1: radius (1.0) and number of Circle objects (1)
After creating c2 and modifying c1
c1: radius (9.0) and number of Circle objects (2)
c2: radius (5.0) and number of Circle objects (2)
M09_LIAN1878_11_GE_C09.indd 363 12/29/17 4:11 PM
364 Chapter 9 Objects and Classes
When you compile TestCircleWithStaticMembers.java, the Java compiler automati- cally compiles Circle.java if it has not been compiled since the last change.
Static variables and methods can be accessed without creating objects. Line 6 displays the number of objects, which is 0, since no objects have been created.
The main method creates two circles, c1 and c2 (lines 9 and18). The instance variable radius in c1 is modified to become 9 (line 21). This change does not affect the instance variable radius in c2, since these two instance variables are independent. The static vari- able numberOfObjects becomes 1 after c1 is created (line 9), and it becomes 2 after c2 is created (line 18).
Note PI is a constant defined in Math and Math.PI references the constant. c1.numberO- fObjects (line 27) and c2.numberOfObjects (line 30) are better replaced by Circle. numberOfObjects. This improves readability because other programmers can easily rec- ognize the static variable. You can also replace Circle.numberOfObjects with Circle. getNumberOfObjects().
Tip Use ClassName.methodName(arguments) to invoke a static method and Class- Name.staticVariable to access a static variable. This improves readability because this makes static methods and data easy to spot.
An instance method can invoke an instance or static method, and access an instance or static data field. A static method can invoke a static method and access a static data field. However, a static method cannot invoke an instance method or access an instance data field, since static methods and static data fields don’t belong to a particular object. The relationship between static and instance members is summarized in the following diagram:
use class name
An instance method
invoke
access
invoke
access
An instance method
An instance data field
A static method
A static data field
A static method
invoke
access
invoke
access
An instance method
An instance data field
A static method
A static data field
For example, the following code is wrong.
1 public class A { 2 int i = 5; 3 static int k = 2; 4 5 public static void main(String[] args) { 6 int j = i; // Wrong because i is an instance variable 7 m1(); // Wrong because m1() is an instance method 8 } 9 10 public void m1() { 11 // Correct since instance and static variables and methods 12 // can be used in an instance method 13 i = i + k + m2(i, k); 14 } 15 16 public static int m2(int i, int j) { 17 return (int)(Math.pow(i, j)); 18 } 19 }
M09_LIAN1878_11_GE_C09.indd 364 12/29/17 4:11 PM
9.7 Static Variables, Constants, and Methods 365
Note if you replace the preceding code with the following new code, the program would be fine, because the instance data field i and method m1 are now accessed from an object a (lines 7 and 8):
1 public class A { 2 int i = 5; 3 static int k = 2; 4 5 public static void main(String[] args) { 6 A a = new A(); 7 int j = a.i; // OK, a.i accesses the object's instance variable 8 a.m1(); // OK, a.m1() invokes the object's instance method 9 } 10 11 public void m1() { 12 i = i + k + m2(i, k); 13 } 14 15 public static int m2(int i, int j) { 16 return (int)(Math.pow(i, j)); 17 } 18 }
Design Guide How do you decide whether a variable or a method should be instance or static? A variable or a method that is dependent on a specific instance of the class should be an instance variable or method. A variable or a method that is not dependent on a specific instance of the class should be a static variable or method. For example, every circle has its own radius, so the radius is dependent on a specific circle. Therefore, radius is an instance variable of the Circle class. Since the getArea method is dependent on a specific circle, it is an instance method. None of the methods in the Math class, such as random, pow, sin, and cos, is dependent on a specific instance. Therefore, these methods are static methods. The main method is static and can be invoked directly from a class.
Caution It is a common design error to define an instance method that should have been defined as static. For example, the method factorial(int n) should be defined as static, as shown next, because it is independent of any specific instance.
instance or static?
9.7.1 Suppose the class F is defined in (a). Let f be an instance of F. Which of the statements in (b) are correct? Point
Check
public class Test { public int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i;
return result; } }
(a) Wrong design
public class Test { public static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i;
return result; } }
(b) Correct design
common design error
M09_LIAN1878_11_GE_C09.indd 365 12/29/17 4:11 PM
366 Chapter 9 Objects and Classes
9.7.2 Add the static keyword in the place of ? if appropriate. public class Test { int count;
public ? void main(String[] args) { ... }
public ? int getCount() { return count; }
public ? int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i;
return result; } }
9.7.3 Can you invoke an instance method or reference an instance variable from a static method? Can you invoke a static method or reference a static variable from an instance method? What is wrong in the following code?
1 public class C { 2 public static void main(String[] args) { 3 method1(); 4 } 5 6 public void method1() { 7 method2(); 8 } 9 10 public static void method2() { 11 System.out.println("What is radius " + c.getRadius()); 12 } 13 14 Circle c = new Circle(); 15 }
9.8 Visibility Modifiers Visibility modifiers can be used to specify the visibility of a class and its members.
You can use the public visibility modifier for classes, methods, and data fields to denote they can be accessed from any other classes. If no visibility modifier is used, then by default the classes, methods, and data fields are accessible by any class in the same package. This is known as package-private or package-access.
Point Key
package-private (or package-access)
public class F { int i; static String s;
void imethod() { }
static void smethod() { } }
(a)
System.out.println(f.i); System.out.println(f.s); f.imethod(); f.smethod(); System.out.println(F.i); System.out.println(F.s); F.imethod(); F.smethod();
(b)
M09_LIAN1878_11_GE_C09.indd 366 12/29/17 4:11 PM
9.8 Visibility Modifiers 367
Note Packages can be used to organize classes. To do so, you need to add the following line as the first noncomment and nonblank statement in the program:
package packageName;
If a class is defined without the package statement, it is said to be placed in the default package.
Java recommends that you place classes into packages rather than using a default package. For simplicity, however, this book uses default packages. For more information on packages, see Supplement III.E, Packages.
In addition to the public and default visibility modifiers, Java provides the private and protected modifiers for class members. This section introduces the private modifier. The protected modifier will be introduced in Section 11.14, The protected Data and Methods.
The private modifier makes methods and data fields accessible only from within its own class. Figure 9.14 illustrates how a public, default, and private data field or method in class C1 can be accessed from a class C2 in the same package, and from a class C3 in a different package.
using packages
Figure 9.14 The private modifier restricts access to its defining class, the default modifier restricts access to a package, and the public modifier enables unrestricted access.
package p1;
public class C1 { public int x; int y; private int z;
public void m1() { } void m2() { } private void m3() { } }
package p1;
public class C2 { void aMethod() { C1 c1 = new C1(); can access c1.x; can access c1.y; cannot access o.z;
can invoke c1.m1(); can invoke c1.m2(); cannot invoke c1.m3(); } }
package p2;
public class C3 { void aMethod() { C1 c1 = new C1(); can access c1.x; cannot access c1.y; cannot access c1.z;
can invoke c1.m1(); cannot invoke c1.m2(); cannot invoke c1.m3(); } }
If a class is not defined as public, it can be accessed only within the same package. As shown in Figure 9.15, C1 can be accessed from C2, but not from C3.
package p1;
class C1 { ... }
package p1;
public class C2 { can access C1 }
package p2;
public class C3 { cannot access p1.C1; can access p1.C2; }
Figure 9.15 A nonpublic class has package access.
A visibility modifier specifies how data fields and methods in a class can be accessed from outside the class. There is no restriction on accessing data fields and methods from inside the class. As shown in Figure 9.16b, an object c of class C cannot access its private members, because c is in the Test class. As shown in Figure 9.16a, an object c of class C can access its private members, because c is defined inside its own class.
inside access
M09_LIAN1878_11_GE_C09.indd 367 12/29/17 4:11 PM
368 Chapter 9 Objects and Classes
Caution The private modifier applies only to the members of a class. The public modifier can apply to a class or members of a class. Using the modifiers public and private on local variables would cause a compile error.
Note In most cases, the constructor should be public. However, if you want to prohibit the user from creating an instance of a class, use a private constructor. For example, there is no reason to create an instance from the Math class, because all of its data fields and methods are static. To prevent the user from creating objects from the Math class, the constructor in java.lang.Math is defined as follows:
private Math() { }
9.9 Data Field Encapsulation Making data fields private protects data and makes the class easy to maintain.
The data fields radius and numberOfObjects in the Circle class in Listing 9.6 can be modified directly (e.g., c1.radius = 5 or Circle.numberOfObjects = 10). This is not a good practice—for two reasons:
1. Data may be tampered with. For example, numberOfObjects is to count the number of objects created, but it may be mistakenly set to an arbitrary value (e.g., Circle. numberOfObjects = 10).
2. The class becomes difficult to maintain and vulnerable to bugs. Suppose that you want to modify the Circle class to ensure that the radius is nonnegative after other programs have already used the class. You have to change not only the Circle class but also the programs that use it because the clients may have modified the radius directly (e.g., c1.radius = –5).
To prevent direct modifications of data fields, you should declare the data fields private, using the private modifier. This is known as data field encapsulation.
private constructor
Point Key
Data field encapsulation
VideoNote
Data field encapsulation
data field encapsulation
Figure 9.16 An object can access its private members if it is defined in its own class.
public class C { private boolean x;
public static void main(string[] args) { C c = new C(); system.out.println(c.x); system.out.println(c.convert()); }
private int convert() { return x ? 1 : –1; } }
(a) This is okay because object c is used inside the class C.
public class Test { public static void main(string[] args) { C c = new C(); system.out.println(c.x); system.out.println(c.convert()); } }
(b) This is wrong because x and convert are private in class C.
M09_LIAN1878_11_GE_C09.indd 368 12/29/17 4:11 PM
9.9 Data Field Encapsulation 369
A private data field cannot be accessed by an object from outside the class that defines the private field. However, a client often needs to retrieve and modify a data field. To make a private data field accessible, provide a getter method to return its value. To enable a private data field to be updated, provide a setter method to set a new value. A getter method is also referred to as an accessor and a setter to a mutator. A getter method has the following signature:
public returnType getPropertyName()
If the returnType is boolean, the getter method should be defined as follows by convention:
public boolean isPropertyName()
A setter method has the following signature:
public void setPropertyName(dataType propertyValue)
Let’s create a new circle class with a private data-field radius and its associated accessor and mutator methods. The class diagram is shown in Figure 9.17. The new circle class is defined in Listing 9.8:
getter (or accessor) setter (or mutator)
boolean accessor
Figure 9.17 The Circle class encapsulates circle properties and provides getter/setter and other methods.
The - sign indicates a private modi�er
Circle
-radius: double
-numberOfObjects: int
+Circle()
+getArea(): double
+getNumberOfObjects(): int
+setRadius(radius: double): void
+getRadius(): double
+Circle(radius: double)
The radius of this circle (default: 1.0).
Constructs a default circle object.
The number of circle objects created.
Returns the area of this circle.
Returns the number of circle objects created.
Sets a new radius for this circle. Returns the radius of this circle.
Constructs a circle object with the speci�ed radius.
Listing 9.8 Circle.java(for CircleWithPrivateDataFields) 1 public class Circle { 2 /** The radius of the circle */ 3 private double radius = 1; 4 5 /** The number of objects created */ 6 private static int numberOfObjects = 0; 7 8 /** Construct a circle with radius 1 */ 9 public Circle() { 10 numberOfObjects++; 11 } 12 13 /** Construct a circle with a specified radius */ 14 public Circle(double newRadius) { 15 radius = newRadius; 16 numberOfObjects++;
encapsulate radius
encapsulate numberOfObjects
M09_LIAN1878_11_GE_C09.indd 369 12/29/17 4:11 PM
370 Chapter 9 Objects and Classes
17 } 18 19 /** Return radius */ 20 public double getRadius() { 21 return radius; 22 } 23 24 /** Set a new radius */ 25 public void setRadius(double newRadius) { 26 radius = (newRadius >= 0) ? newRadius : 0; 27 } 28 29 /** Return numberOfObjects */ 30 public static int getNumberOfObjects() { 31 return numberOfObjects; 32 } 33 34 /** Return the area of this circle */ 35 public double getArea() { 36 return radius * radius * Math.PI; 37 } 38 }
The getRadius() method (lines 20–22) returns the radius and the setRadius(newRadius) method (lines 25–27) sets a new radius for the object. If the new radius is negative, 0 is set as the radius for the object. Since these methods are the only ways to read and modify the radius, you have total control over how the radius property is accessed. If you have to change the implementation of these methods, you don’t need to change the client programs. This makes the class easy to maintain.
Listing 9.9 gives a client program that uses the Circle class to create a Circle object, and modifies the radius using the setRadius method.
Listing 9.9 TestCircleWithPrivateDataFields.java 1 public class TestCircleWithPrivateDataFields { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create a circle with radius 5.0 5 Circle myCircle = new Circle(5.0); 6 System.out.println("The area of the circle of radius " 7 + myCircle.getRadius() + " is " + myCircle.getArea()); 8 9 // Increase myCircle's radius by 10% 10 myCircle.setRadius(myCircle.getRadius() * 1.1); 11 System.out.println("The area of the circle of radius " 12 + myCircle.getRadius() + " is " + myCircle.getArea()); 13 14 System.out.println("The number of objects created is " 15 + Circle.getNumberOfObjects()); 16 } 17 }
The data field radius is declared private. Private data can be accessed only within their defin- ing class, so you cannot use myCircle.radius in the client program. A compile error would occur if you attempted to access private data from a client.
Since numberOfObjects is private, it cannot be modified. This prevents tampering. For example, the user cannot set numberOfObjects to 100. The only way to make it 100 is to create 100 objects of the Circle class.
accessor method
mutator method
accessor method
invoke public method
invoke public method
invoke public method
M09_LIAN1878_11_GE_C09.indd 370 12/29/17 4:11 PM
9.10 Passing Objects to Methods 371
Suppose you combined TestCircleWithPrivateDataFields and Circle into one class by moving the main method in TestCircleWithPrivateDataFields into Circle. Could you use myCircle.radius in the main method? See CheckPoint Question 9.9.3 for the answer.
Design Guide To prevent data from being tampered with and to make the class easy to maintain, declare data fields private.
Note From now on, all data fields should be declared private, and all constructors and methods should be defined public, unless specified otherwise.
9.9.1 What is an accessor method? What is a mutator method? What are the naming con- ventions for accessor methods and mutator methods?
9.9.2 What are the benefits of data field encapsulation? 9.9.3 In the following code, radius is private in the Circle class, and myCircle is an object
of the Circle class. Does the highlighted code cause any problems? If so, explain why.
public class Circle { private double radius = 1;
/** Find the area of this circle */ public double getArea() { return radius * radius * Math.PI; }
public static void main(String[] args) { Circle myCircle = new Circle(); System.out.println("Radius is " + myCircle.radius); } }
9.10 Passing Objects to Methods Passing an object to a method is to pass the reference of the object.
You can pass objects to methods. Like passing an array, passing an object is actually passing the reference of the object. The following code passes the myCircle object as an argument to the printCircle method:
1 public class Test { 2 public static void main(String[] args) { 3 // Circle is defined in Listing 9.8 4 Circle myCircle = new Circle(5.0); 5 printCircle(myCircle); 6 } 7 8 public static void printCircle(Circle c) { 9 System.out.println("The area of the circle of radius " 10 + c.getRadius() + " is " + c.getArea()); 11 } 12 }
Java uses exactly one mode of passing arguments: pass-by-value. In the preceding code, the value of myCircle is passed to the printCircle method. This value is a reference to a Circle object.
The program in Listing 9.10 demonstrates the difference between passing a primitive-type value and passing a reference value.
Point Check
Point Key
pass an object
pass-by-value
M09_LIAN1878_11_GE_C09.indd 371 12/29/17 4:11 PM
372 Chapter 9 Objects and Classes
Listing 9.10 TestPassObject.java 1 public class TestPassObject { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create a Circle object with radius 1 5 Circle myCircle = 6 new Circle(1); // Use the Circle class in Listing 9.8 7 8 // Print areas for radius 1, 2, 3, 4, and 5. 9 int n = 5; 10 printAreas(myCircle, n); 11 12 // See myCircle.radius and times 13 System.out.println("\n" + "Radius is " + myCircle.getRadius()); 14 System.out.println("n is " + n); 15 } 16 17 /** Print a table of areas for radius */ 18 public static void printAreas(Circle c, int times) { 19 System.out.println("Radius \t\tArea"); 20 while (times >= 1) { 21 System.out.println(c.getRadius() + "\t\t" + c.getArea()); 22 c.setRadius(c.getRadius() + 1); 23 times——; 24 } 25 } 26 }
pass object
object parameter
Radius Area
1.0 3.141592653589793
2.0 12.566370614359172
3.0 28.274333882308138
4.0 50.26548245743669
5.0 78.53981633974483
Radius is 6.0
n is 5
The Circle class is defined in Listing 9.8. The program passes a Circle object myCircle and an integer value from n to invoke printAreas(myCircle, n) (line 10), which prints a table of areas for radii 1, 2, 3, 4, and 5, as presented in the sample output.
Figure 9.18 shows the call stack for executing the methods in the program. Note the objects are stored in a heap (see Section 7.6).
When passing an argument of a primitive data type, the value of the argument is passed. In this case, the value of n (5) is passed to times. Inside the printAreas method, the content of times is changed; this does not affect the content of n.
When passing an argument of a reference type, the reference of the object is passed. In this case, c contains a reference for the object that is also referenced via myCircle. Therefore, changing the properties of the object through c inside the printAreas method has the same effect as doing so outside the method through the variable myCircle. Pass-by-value on refer- ences can be best described semantically as pass-by-sharing; that is, the object referenced in the method is the same as the object being passed.
pass-by-sharing
M09_LIAN1878_11_GE_C09.indd 372 12/29/17 4:11 PM
9.10 Passing Objects to Methods 373
9.10.1 Describe the difference between passing a parameter of a primitive type and passing a parameter of a reference type. Show the output of the following programs: Point
Check
Figure 9.18 The value of n is passed to times, and the reference to myCircle is passed to c in the printAreas method.
Activation record for the main method int n: 5 myCircle:
Stack
Activation record for the printAreas method int times: 5 Circle c:
A Circle object
Heap
reference
reference
Pass-by-value (here the value is the reference for the object)
Pass-by-value (here the value is 5)
public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0;
for (int i = 0; i < 100; i++) increment(myCount, times);
System.out.println("count is " + myCount.count); System.out.println("times is " + times); }
public static void increment(Count c, int times) { c.count++; times++; } }
public class Count {
public int count;
public Count (int c) { count = c; }
public Count () { count = 1; } }
9.10.2 Show the output of the following program: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(1); Circle circle2 = new Circle(2);
swap1(circle1, circle2); System.out.println("After swap1: circle1 = " + circle1.radius + " circle2 = " + circle2.radius);
swap2(circle1, circle2); System.out.println("After swap2: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); }
public static void swap1(Circle x, Circle y) { Circle temp = x; x = y; y = temp; }
M09_LIAN1878_11_GE_C09.indd 373 12/29/17 4:11 PM
374 Chapter 9 Objects and Classes
public static void swap2(Circle x, Circle y) { double temp = x.radius; x.radius = y.radius; y.radius = temp; } }
class Circle { double radius;
Circle(double newRadius) { radius = newRadius; } }
9.10.3 Show the output of the following code:
public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a[0], a[1]); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); }
public static void swap(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } }
(a)
public class Test { public static void main(String[] args) { int[] a = {1, 2}; swap(a); System.out.println("a[0] = " + a[0] + " a[1] = " + a[1]); }
public static void swap(int[] a) { int temp = a[0]; a[0] = a[1]; a[1] = temp; } }
(b)
public class Test { public static void main(String[] args) { T t = new T(); swap(t); System.out.println("e1 = " + t.e1 + " e2 = " + t.e2); }
public static void swap(T t) { int temp = t.e1; t.e1 = t.e2; t.e2 = temp; } }
class T { int e1 = 1; int e2 = 2; }
(c)
public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } }
class T { static int i = 0; int j = 0;
T() { i++; j = 1; } }
(d)
M09_LIAN1878_11_GE_C09.indd 374 12/29/17 4:11 PM
9.11 Array of Objects 375
9.10.4 What is the output of the following programs?
import java.util.Date;
public class Test { public static void main(String[] args) { Date date = null; m1(date); System.out.println(date); }
public static void m1(Date date) { date = new Date(); } }
(a)
import java.util.Date;
public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); }
public static void m1(Date date) { date = new Date(7654321); } }
(b)
9.11 Array of Objects An array can hold objects as well as primitive-type values.
Chapter 7, Single-Dimensional Arrays, described how to create arrays of primitive-type ele- ments. You can also create arrays of objects. For example, the following statement declares and creates an array of 10 Circle objects:
Circle[] circleArray = new Circle[10];
To initialize circleArray, you can use a for loop as follows:
for (int i = 0; i < circleArray.length; i++) { circleArray[i] = new Circle(); }
An array of objects is actually an array of reference variables. Thus, invoking circleArray[1] .getArea() involves two levels of referencing, as shown in Figure 9.19. circleArray references the entire array, and circleArray[1] references a Circle object.
Note When an array of objects is created using the new operator, each element in the array is a reference variable with a default value of null.
Point Key
import java.util.Date;
public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); }
public static void m1(Date date) { date.setTime(7654321); } }
(c)
import java.util.Date;
public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.println(date.getTime()); }
public static void m1(Date date) { date = null; } }
(d)
M09_LIAN1878_11_GE_C09.indd 375 12/29/17 4:11 PM
376 Chapter 9 Objects and Classes
Listing 9.11 gives an example that demonstrates how to use an array of objects. The pro- gram summarizes the areas of an array of circles. The program creates circleArray, an array composed of five Circle objects; it then initializes circle radii with random values and displays the total area of the circles in the array.
Listing 9.11 TotalArea.java 1 public class TotalArea { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Declare circleArray 5 Circle[] circleArray; 6 7 // Create circleArray 8 circleArray = createCircleArray(); 9 10 // Print circleArray and total areas of the circles 11 printCircleArray(circleArray); 12 } 13 14 /** Create an array of Circle objects */ 15 public static Circle[] createCircleArray() { 16 Circle[] circleArray = new Circle[5]; 17 18 for (int i = 0; i < circleArray.length; i++) { 19 circleArray[i] = new Circle(Math.random() * 100); 20 } 21 22 // Return Circle array 23 return circleArray; 24 } 25 26 /** Print an array of circles and their total area */ 27 public static void printCircleArray(Circle[] circleArray) { 28 System.out.printf("%–30s%–15s\n", "Radius", "Area"); 29 for (int i = 0; i < circleArray.length; i++) { 30 System.out.printf("%–30f%–15f\n", circleArray[i].getRadius(), 31 circleArray[i].getArea()); 32 } 33 34 System.out.println("— — — — — — — — — — — — — — — — — — — — — — — — — — — —"); 35 36 // Compute and display the result 37 System.out.printf("%–30s%–15f\n", "The total area of circles is", 38 sum(circleArray)); 39 } 40
array of objects
return array of objects
pass array of objects
Figure 9.19 In an array of objects, an element of the array contains a reference to an object.
…
circleArray[9]
circleArray[1]
circleArray[0]reference Circle object 0
Circle object 1
Circle object 9
circleArray
M09_LIAN1878_11_GE_C09.indd 376 12/29/17 4:11 PM
9.12 Immutable Objects and Classes 377
41 /** Add circle areas */ 42 public static double sum(Circle[] circleArray) { 43 // Initialize sum 44 double sum = 0; 45 46 // Add areas to sum 47 for (int i = 0; i < circleArray.length; i++) 48 sum += circleArray[i].getArea(); 49 50 return sum; 51 } 52 }
pass array of objects
Radius Area
70.577708 15649.941866
44.152266 6124.291736
24.867853 1942.792644
5.680718 101.380949
36.734246 4239.280350
—————————————————————————————————————————————
The total area of circles is 28056.687544
The program invokes createCircleArray() (line 8) to create an array of five circle objects. Several circle classes were introduced in this chapter. This example uses the Circle class introduced in Section 9.9, Data Field Encapsulation.
The circle radii are randomly generated using the Math.random() method (line 19). The createCircleArray method returns an array of Circle objects (line 23). The array is passed to the printCircleArray method, which displays the radius and area of each circle and the total area of the circles.
The sum of the circle areas is computed by invoking the sum method (line 38), which takes the array of Circle objects as the argument and returns a double value for the total area.
9.11.1 What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 java.util.Date[] dates = new java.util.Date[10]; 4 System.out.println(dates[0]); 5 System.out.println(dates[0].toString()); 6 } 7 }
9.12 Immutable Objects and Classes You can define immutable classes to create immutable objects. The contents of immutable objects cannot be changed.
Normally, you create an object and allow its contents to be changed later. However, occasion- ally it is desirable to create an object whose contents cannot be changed once the object has been created. We call such an object as immutable object and its class as immutable class. The String class, for example, is immutable. If you deleted the setter method in the Circle class in Listing 9.8, the class would be immutable because radius is private and cannot be changed without a setter method.
If a class is immutable, then all its data fields must be private and it cannot contain public setter methods for any data fields. A class with all private data fields and no mutators is not
Point Key
immutable class
VideoNote Immutable objects and this keyword
immutable object
M09_LIAN1878_11_GE_C09.indd 377 12/29/17 4:11 PM
378 Chapter 9 Objects and Classes
necessarily immutable. For example, the following Student class has all private data fields and no setter methods, but it is not an immutable class:
1 public class Student { 2 private int id; 3 private String name; 4 private java.util.Date dateCreated; 5 6 public Student(int ssn, String newName) { 7 id = ssn; 8 name = newName; 9 dateCreated = new java.util.Date(); 10 } 11 12 public int getId() { 13 return id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public java.util.Date getDateCreated() { 21 return dateCreated; 22 } 23 }
As shown in the following code, the data field dateCreated is returned using the get- DateCreated() method. This is a reference to a Date object. Through this reference, the content for dateCreated can be changed.
public class Test { public static void main(String[] args) { Student student = new Student(111223333, "John"); java.util.Date dateCreated = student.getDateCreated(); dateCreated.setTime(200000); // Now dateCreated field is changed! } }
For a class to be immutable, it must meet the following requirements:
■■ All data fields must be private.
■■ There can’t be any mutator methods for data fields.
■■ No accessor methods can return a reference to a data field that is mutable.
Interested readers may refer to Supplement III.U for an extended discussion on immutable objects.
9.12.1 If a class contains only private data fields and no setter methods, is the class immutable?
9.12.2 If all the data fields in a class are private and of primitive types, and the class doesn’t contain any setter methods, is the class immutable?
9.12.3 Is the following class immutable? public class A { private int[] values;
public int[] getValues() {
Student class
Point Check
M09_LIAN1878_11_GE_C09.indd 378 12/29/17 4:11 PM
9.13 The Scope of Variables 379
return values; } }
9.13 The Scope of Variables The scope of instance and static variables is the entire class, regardless of where the variables are declared.
Section 6.9 discussed local variables and their scope rules. Local variables are declared and used inside a method locally. This section discusses the scope rules of all the variables in the context of a class.
Instance and static variables in a class are referred to as the class’s variables or data fields. A variable defined inside a method is referred to as a local variable. The scope of a class’s variables is the entire class, regardless of where the variables are declared. A class’s variables and methods can appear in any order in the class, as shown in Figure 9.20a. The exception is when a data field is initialized based on a reference to another data field. In such cases, the other data field must be declared first, as shown in Figure 9.20b. For consistency, this book declares data fields at the beginning of the class.
Point Key
class’s variables
Figure 9.20 Members of a class can be declared in any order, with one exception.
public class Circle { public double getArea() { return radius * radius * Math.PI; }
private double radius = 1; }
(a) The variable radius and method getArea() can be declared in any order.
public class F { private int i; private int j = i + 1; }
(b) i has to be declared before j because j’s initial value is dependent on i.
You can declare a class’s variable only once, but you can declare the same variable name in a method many times in different nonnesting blocks.
If a local variable has the same name as a class’s variable, the local variable takes prece- dence and the class’s variable with the same name is hidden. For example, in the following program, x is defined both as an instance variable and as a local variable in the method:
public class F { private int x = 0; // Instance variable private int y = 0;
public F() { }
public void p() { int x = 1; // Local variable System.out.println("x = " + x); System.out.println("y = " + y); } }
What is the output for f.p(), where f is an instance of F? The output for f.p() is 1 for x and 0 for y. Here is why:
■■ x is declared as a data field with the initial value of 0 in the class, but it is also declared in the method p() with an initial value of 1. The latter x is referenced in the System.out.println statement.
■■ y is declared outside the method p(), but y is accessible inside the method.
hidden variables
M09_LIAN1878_11_GE_C09.indd 379 12/29/17 4:11 PM
380 Chapter 9 Objects and Classes
Tip To avoid confusion and mistakes, do not use the names of instance or static variables as local variable names, except for method parameters. We will discuss hidden data fields by method parameters in the next section.
9.13.1 What is the output of the following program? public class Test { private static int i = 0; private static int j = 0;
public static void main(String[] args) { int i = 2; int k = 3;
{ int j = 3; System.out.println("i + j is " + i + j); }
k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } }
9.14 The this Reference The keyword this refers to the object itself. It can also be used inside a constructor to invoke another constructor of the same class.
The this keyword is the name of a reference that an object can use to refer to itself. You can use the this keyword to reference the object’s instance members. For example, the following code in (a) uses this to reference the object’s radius and invokes its getArea() method explicitly. The this reference is normally omitted for brevity as shown in (b). However, the this reference is needed to reference a data field hidden by a method or constructor parameter, or to invoke an overloaded constructor.
Point Check
Point Key
this keyword
VideoNote
The this keyword
public class Circle { private double radius;
...
public double getArea() { return this.radius * this.radius * Math.PI; }
public String toString() { return "radius: " + this.radius + "area: " + this.getArea(); } }
(a)
public class Circle { private double radius;
...
public double getArea() { return radius * radius * Math.PI; }
public String toString() { return "radius: " + radius + "area: " + getArea(); } }
(b)
Equivalent
M09_LIAN1878_11_GE_C09.indd 380 12/29/17 4:11 PM
9.14 The this Reference 381
9.14.1 Using this to Reference Data Fields It is a good practice to use the data field as the parameter name in a setter method or a con- structor to make the code easy to read and to avoid creating unnecessary names. In this case, you need to use the this keyword to reference the data field in the setter method. For exam- ple, the setRadius method can be implemented as shown in (a). It would be wrong if it is implemented as shown in (b).
The data field radius is hidden by the parameter radius in the setter method. You need to reference the data field name in the method using the syntax this.radius. A hidden static variable can be accessed simply by using the ClassName.staticVariable reference. A hidden instance variable can be accessed by using the keyword this, as shown in Figure 9.21a.
reference data fields
public class F { private int i = 5; private static double k = 0;
public void setI(int i) { this.i = i; }
public static void setK(double k) { F.k = k; }
// other methods omitted }
(a)
Suppose that f1 and f2 are two objects of F.
Invoking f1.setI(10) is to execute
this.i = 10, where this refers f1
Invoking f2.setI(45) is to execute
this.i = 45, where this refers f2
Invoking F.setK(33) is to execute
F.k = 33. setK is a static method
(b)
Figure 9.21 The keyword this refers to the calling object that invokes the method.
The this keyword gives us a way to reference the object that invokes an instance method. To invoke f1.setI(10), this.i = i is executed, which assigns the value of parameter i to the data field i of this calling object f1. The keyword this refers to the object that invokes the instance method setI, as shown in Figure 9.21b. The line F.k = k means the value in parameter k is assigned to the static data field k of the class, which is shared by all the objects of the class.
private double radius;
public void setRadius(double radius) { this.radius = radius; }
private double radius = 1;
public void setRadius(double radius) { radius = radius; }
Refers to data field radius in this object.
Here, radius is the parameter in the method.
(a) this.radius refers the radius data field in this object.
(b) radius is the parameter defined in the method header.
M09_LIAN1878_11_GE_C09.indd 381 12/29/17 4:11 PM
382 Chapter 9 Objects and Classes
The line this(1.0) in the second constructor invokes the first constructor with a double value argument.
Note Java requires that the this(arg-list) statement appear first in the constructor before any other executable statements.
Tip If a class has multiple constructors, it is better to implement them using this(arg-list) as much as possible. In general, a constructor with no or fewer arguments can invoke a constructor with more arguments using this(arg-list). This syntax often simplifies coding and makes the class easier to read and to maintain.
9.14.1 Describe the role of the this keyword. 9.14.2 What is wrong in the following code?
1 public class C { 2 private int p; 3 4 public C() { 5 System.out.println("C's no-arg constructor invoked"); 6 this(0); 7 } 8 9 public C(int p) { 10 p = p; 11 } 12 13 public void setP(int p) { 14 p = p; 15 } 16 }
9.14.3 What is wrong in the following code? public class Test { private int id;
public void m1() { this.id = 45; }
Point Check
public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } The this keyword is used to reference the
data field radius of the object being constructed. public Circle() { this(1.0); } The this keyword is used to invoke another
constructor. ... }
9.14.2 Using this to Invoke a Constructor The this keyword can be used to invoke another constructor of the same class. For example, you can rewrite the Circle class as follows:
M09_LIAN1878_11_GE_C09.indd 382 12/29/17 4:11 PM
Chapter Summary 383
public void m2() { Test.id = 45; } }
Key Terms action 346 anonymous object 355 attribute 346 behavior 346 class 346 class’s variable 379 client 349 constructor 346 data field 346 data field encapsulation 368 default constructor 353 dot operator (.) 354 getter (or accessor) 369 instance 346 instance method 355 instance variable 355 instantiation 346 immutable class 377
immutable object 377 no-arg constructor 349 null value 355 object 346 object-oriented programming (OOP) package-private (or package-access) 366 private constructor 368 property 346 public class 349 reference type 354 reference variable 354 setter (or mutator) 369 state 346 static method 361 static variable 361 this keyword 380 Unified Modeling Language (UML) 347
ChapTer summary 1. A class is a template for objects. It defines the properties of objects and provides con-
structors for creating objects and methods for manipulating them.
2. A class is also a data type. You can use it to declare object reference variables. An object reference variable that appears to hold an object actually contains a reference to that object. Strictly speaking, an object reference variable and an object are different, but most of the time the distinction can be ignored.
3. An object is an instance of a class. You use the new operator to create an object and the dot operator (.) to access members of that object through its reference variable.
4. An instance variable or method belongs to an instance of a class. Its use is associated with individual instances. A static variable is a variable shared by all instances of the same class. A static method is a method that can be invoked without using instances.
5. Every instance of a class can access the class’s static variables and methods. For clar- ity, however, it is better to invoke static variables and methods using ClassName . variable and ClassName.method.
6. Visibility modifiers specify how the class, method, and data are accessed. A public class, method, or data is accessible to all clients. A private method or data is acces- sible only inside the class.
7. You can provide a getter (accessor) method or a setter (mutator) method to enable clients to see or modify the data.
M09_LIAN1878_11_GE_C09.indd 383 12/29/17 4:11 PM
384 Chapter 9 Objects and Classes
8. A getter method has the signature public returnType getPropertyName(). If the returnType is boolean, the getter method should be defined as public boolean isPropertyName(). A setter method has the signature public void setPropertyName(dataType propertyValue).
9. All parameters are passed to methods using pass-by-value. For a parameter of a primi- tive type, the actual value is passed; for a parameter of a reference type, the reference for the object is passed.
10. A Java array is an object that can contain primitive-type values or object-type values. When an array of objects is created, its elements are assigned the default value of null.
11. Once it is created, an immutable object cannot be modified. To prevent users from modi- fying an object, you can define immutable classes.
12. The scope of instance and static variables is the entire class, regardless of where the vari- ables are declared. Instance and static variables can be declared anywhere in the class. For consistency, they are declared at the beginning of the class in this book.
13. The keyword this can be used to refer to the calling object. It can also be used inside a constructor to invoke another constructor of the same class.
Quiz Answer the quiz for this chapter online at the book Companion Website.
programming exerCises
Pedagogical Note The exercises in Chapters 9–13 help you to achieve three objectives:
1. Design classes and draw UML class diagrams. 2. Implement classes from the UML. 3. Use classes to develop applications.
Students can download solutions for the UML diagrams for the even-numbered exercises from the Companion Website and instructors can download all solutions from the same site.
Starting from Section 9.7, all data fields should be declared private and all constructors and methods should be defined public unless specified otherwise.
Sections 9.2–9.5 9.1 (The Rectangle class) Following the example of the Circle class in Section 9.2,
design a class named Rectangle to represent a rectangle. The class contains:
■■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
■■ A no-arg constructor that creates a default rectangle. ■■ A constructor that creates a rectangle with the specified width and height. ■■ A method named getArea() that returns the area of this rectangle. ■■ A method named getPerimeter() that returns the perimeter.
Draw the UML diagram for the class then implement the class. Write a test pro- gram that creates two Rectangle objects—one with width 4 and height 40, and
three objectives
M09_LIAN1878_11_GE_C09.indd 384 12/29/17 4:11 PM
Programming Exercises 385
the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.
9.2 (The Stock class) Following the example of the Circle class in Section 9.2, design a class named Stock that contains:
■■ A string data field named symbol for the stock’s symbol. ■■ A string data field named name for the stock’s name. ■■ A double data field named previousClosingPrice that stores the stock
price for the previous day. ■■ A double data field named currentPrice that stores the stock price for the
current time. ■■ A constructor that creates a stock with the specified symbol and name. ■■ A method named getChangePercent() that returns the percentage changed
from previousClosingPrice to currentPrice.
Draw the UML diagram for the class then implement the class. Write a test pro- gram that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.
Section 9.6 *9.3 (Use the Date class) Write a program that creates a Date object, sets its elapsed
time to 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, and 100000000000, and displays the date and time using the toString() method, respectively.
*9.4 (Use the Random class) Write a program that creates a Random object with seed 1000 and displays the first 50 random integers between 0 and 100 using the nextInt(100) method.
*9.5 (Use the GregorianCalendar class) Java API has the GregorianCalendar class in the java.util package, which you can use to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR), get(GregorianCalendar. MONTH), and get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day. Write a program to perform two tasks:
1. Display the current year, month, and day. 2. The GregorianCalendar class has the setTimeInMillis(long), which
can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.
Sections 9.7–9.9 *9.6 (Stopwatch) Design a class named StopWatch. The class contains:
■■ Private data fields startTime and endTime with getter methods. ■■ A no-arg constructor that initializes startTime with the current time. ■■ A method named start() that resets the startTime to the current time. ■■ A method named stop() that sets the endTime to the current time. ■■ A method named getElapsedTime() that returns the elapsed time for the
stopwatch in milliseconds.
Draw the UML diagram for the class then implement the class. Write a test program that measures the execution time of sorting 100,000 numbers using selection sort.
9.7 (The Account class) Design a class named Account that contains: ■■ A private int data field named id for the account (default 0). ■■ A private double data field named balance for the account (default 0).
M09_LIAN1878_11_GE_C09.indd 385 12/29/17 4:11 PM
386 Chapter 9 Objects and Classes
■■ A private double data field named annualInterestRate that stores the cur- rent interest rate (default 0). Assume that all accounts have the same interest rate.
■■ A private Date data field named dateCreated that stores the date when the account was created.
■■ A no-arg constructor that creates a default account. ■■ A constructor that creates an account with the specified id and initial balance. ■■ The accessor and mutator methods for id, balance, and annualInterestRate. ■■ The accessor method for dateCreated. ■■ A method named getMonthlyInterestRate() that returns the monthly
interest rate. ■■ A method named getMonthlyInterest() that returns the monthly interest. ■■ A method named withdraw that withdraws a specified amount from the
account. ■■ A method named deposit that deposits a specified amount to the account.
Draw the UML diagram for the class then implement the class. (Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInteres- tRate is annualInterestRate / 12. Note annualInterestRate is a per- centage, for example 4.5%. You need to divide it by 100.)
Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the bal- ance, the monthly interest, and the date when this account was created.
9.8 (The Fan class) Design a class named Fan to represent a fan. The class contains: ■■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3
to denote the fan speed. ■■ A private int data field named speed that specifies the speed of the fan (the
default is SLOW). ■■ A private boolean data field named on that specifies whether the fan is on (the
default is false). ■■ A private double data field named radius that specifies the radius of the fan
(the default is 5). ■■ A string data field named color that specifies the color of the fan (the default
is blue). ■■ The accessor and mutator methods for all four data fields. ■■ A no-arg constructor that creates a default fan. ■■ A method named toString() that returns a string description for the fan. If
the fan is on, the method returns the fan speed, color, and radius in one com- bined string. If the fan is not on, the method returns the fan color and radius along with the string “fan is off” in one combined string.
Draw the UML diagram for the class then implement the class. Write a test program that creates two Fan objects. Assign maximum speed, radius 10, color yellow, and turn it on to the first object. Assign medium speed, radius 5, color blue, and turn it off to the second object. Display the objects by invoking their toString method.
**9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equi- lateral and equiangular). Design a class named RegularPolygon that contains:
■■ A private int data field named n that defines the number of sides in the polygon with default value 3.
VideoNote
The Fan class
M09_LIAN1878_11_GE_C09.indd 386 12/29/17 4:11 PM
Programming Exercises 387
■■ A private double data field named side that stores the length of the side with default value 1.
■■ A private double data field named x that defines the x-coordinate of the poly- gon’s center with default value 0.
■■ A private double data field named y that defines the y-coordinate of the poly- gon’s center with default value 0.
■■ A no-arg constructor that creates a regular polygon with default values. ■■ A constructor that creates a regular polygon with the specified number of sides
and length of side, centered at (0, 0). ■■ A constructor that creates a regular polygon with the specified number of sides,
length of side, and x- and y-coordinates. ■■ The accessor and mutator methods for all data fields. ■■ The method getPerimeter() that returns the perimeter of the polygon. ■■ The method getArea() that returns the area of the polygon. The formula for
computing the area of a regular polygon is
Area = n * s2
4 * tanap n b
.
Draw the UML diagram for the class then implement the class. Write a test pro- gram that creates three RegularPolygon objects, created using the no-arg con- structor, using RegularPolygon(6, 4), and using RegularPolygon(10, 4, 5.6, 7.8). For each object, display its perimeter and area.
*9.10 (Algebra: quadratic equations) Design a class named QuadraticEquation for a quadratic equation ax2 + bx + c = 0. The class contains:
■■ Private data fields a, b, and c that represent three coefficients. ■■ A constructor with the arguments for a, b, and c. ■■ Three getter methods for a, b, and c. ■■ A method named getDiscriminant() that returns the discriminant, which
is b2 - 4ac. ■■ The methods named getRoot1() and getRoot2() for returning two roots
of the equation
r1 = -b + 2b2 - 4ac
2a and r2 =
-b - 2b2 - 4ac 2a
These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative.
Draw the UML diagram for the class then implement the class. Write a test pro- gram that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display “The equation has no roots.” See Programming Exercise 3.1 for sample runs.
*9.11 (Algebra: 2 * 2 linear equations) Design a class named LinearEquation for a 2 * 2 system of linear equations:
ax + by = e cx + dy = f
x = ed - bf ad - bc
y = af - ec ad - bc
The class contains:
M09_LIAN1878_11_GE_C09.indd 387 12/29/17 4:11 PM
388 Chapter 9 Objects and Classes
■■ Private data fields a, b, c, d, e, and f. ■■ A constructor with the arguments for a, b, c, d, e, and f. ■■ Six getter methods for a, b, c, d, e, and f. ■■ A method named isSolvable() that returns true if ad - bc is not 0. ■■ Methods getX() and getY() that return the solution for the equation.
Draw the UML diagram for the class then implement the class. Write a test pro- gram that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that “The equation has no solution.” See Programming Exer- cise 3.3 for sample runs.
**9.12 (Geometry: intersecting point) Suppose two line segments intersect. The two end- points for the first line segment are (x1, y1) and (x2, y2) and for the second line segment are (x3, y3) and (x4, y4). Write a program that prompts the user to enter these four endpoints and displays the intersecting point. As discussed in Program- ming Exercise 3.25, the intersecting point can be found by solving a linear equa- tion. Use the LinearEquation class in Programming Exercise 9.11 to solve this equation. See Programming Exercise 3.25 for sample runs.
**9.13 (The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type.
Write the following method that returns the location of the largest element in a two-dimensional array:
public static Location locateLargest(double[][] a)
The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Here is a sample run:
Enter the number of rows and columns in the array: 3 4 Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6 The location of the largest element is 45 at (1, 2)
M09_LIAN1878_11_GE_C09.indd 388 12/29/17 4:11 PM
Objectives ■■ To apply class abstraction to develop software (§10.2).
■■ To explore the differences between the procedural paradigm and object-oriented paradigm (§10.3).
■■ To discover the relationships between classes (§10.4).
■■ To design programs using the object-oriented paradigm (§§10.5 and 10.6).
■■ To create objects for primitive values using the wrapper classes (Byte, Short, Integer, Long, Float, Double, Character, and Boolean) (§10.7).
■■ To simplify programming using automatic conversion between primitive types and wrapper class types (§10.8).
■■ To use the BigInteger and BigDecimal classes for computing very large numbers with arbitrary precisions (§10.9).
■■ To use the String class to process immutable strings (§10.10).
■■ To use the StringBuilder and StringBuffer classes to process mutable strings (§10.11).
Object-Oriented Thinking
Chapter
10
M10_LIAN1878_11_GE_C10.indd 389 12/29/17 4:15 PM
390 Chapter 10 Object-Oriented Thinking
10.1 Introduction The focus of this chapter is on class design and to explore the differences between procedural programming and object-oriented programming.
The preceding chapter introduced objects and classes. You learned how to define classes, create objects, and use objects. This book’s approach is to teach problem solving and funda- mental programming techniques before object-oriented programming. This chapter shows how procedural and object-oriented programming differ. You will see the benefits of object-oriented programming and learn to use it effectively.
Our focus here is on class design. We will use several examples to illustrate the advantages of the object-oriented approach. The examples involve designing new classes and using them in applications and introducing new classes in the Java API.
10.2 Class Abstraction and Encapsulation Class abstraction is separation of class implementation from the use of a class. The details of implementation are encapsulated and hidden from the user. This is known as class encapsulation.
In Chapter 6, you learned about method abstraction and used it in stepwise refinement. Java provides many levels of abstraction, and class abstraction separates class implementation from how the class is used. The creator of a class describes the functions of the class and lets the user know how the class can be used. The collection of public constructors, meth- ods, and fields that are accessible from outside the class, together with the description of how these members are expected to behave, serves as the class’s contract. As shown in Figure 10.1, the user of the class does not need to know how the class is implemented. The details of implementation are encapsulated and hidden from the user. This is called class encapsulation. For example, you can create a Circle object and find the area of the circle without knowing how the area is computed. For this reason, a class is also known as an abstract data type (ADT).
Point Key
Point Key
class abstraction
class’s contract
class encapsulation
abstract data type
Figure 10.1 Class abstraction separates class implementation from the use of the class.
Class Contract (signatures of public
constructors, methods, and data �elds)
Class
Class implementation is like a black box hidden from the clients
Clients use the class through the
contract of the class
Class abstraction and encapsulation are two sides of the same coin. Many real-life examples illustrate the concept of class abstraction. Consider, for instance, building a computer system. Your personal computer has many components—a CPU, memory, disk, motherboard, fan, and so on. Each component can be viewed as an object that has properties and methods. To get the components to work together, you need to know only how each component is used and how it interacts with the others. You don’t need to know how the components work internally. The internal implementation is encapsulated and hidden from you. You can build a computer without knowing how a component is implemented.
The computer-system analogy precisely mirrors the object-oriented approach. Each com- ponent can be viewed as an object of the class for the component. For example, you might have a class that models all kinds of fans for use in a computer, with properties such as fan size and speed and methods such as start and stop. A specific fan is an instance of this class with specific property values.
As another example, consider getting a loan. A specific loan can be viewed as an object of a Loan class. The interest rate, loan amount, and loan period are its data properties and
M10_LIAN1878_11_GE_C10.indd 390 12/29/17 4:15 PM
10.2 Class Abstraction and Encapsulation 391
computing the monthly and total payments are its methods. When you buy a car, a loan object is created by instantiating the class with your loan interest rate, loan amount, and loan period. You can then use the methods to find the monthly payment and total payment of your loan. As a user of the Loan class, you don’t need to know how these methods are implemented.
Listing 2.9, ComputeLoan.java, presented a program for computing loan payments. That program cannot be reused in other programs because the code for computing the payments is in the main method. One way to fix this problem is to define static methods for computing the monthly payment and the total payment. However, this solution has limitations. Suppose that you wish to associate a date with the loan. There is no good way to tie a date with a loan without using objects. The traditional procedural programming paradigm is action-driven, and data are separated from actions. The object-oriented programming paradigm focuses on objects, and actions are defined along with the data in objects. To tie a date with a loan, you can define a loan class with a date along with the loan’s other properties as data fields. A loan object now contains data and actions for manipulating and processing data, and the loan data and actions are integrated in one object. Figure 10.2 shows the UML class diagram for the Loan class.
VideoNote
The Loan class
Figure 10.2 The Loan class models the properties and behaviors of loans.
Loan
The annual interest rate of the loan (default: 2.5).
Returns the annual interest rate of this loan.
Sets a new annual interest rate for this loan.
Sets a new number of years for this loan.
Sets a new amount for this loan.
Returns the monthly payment for this loan.
The number of years for the loan (default: 1).
The loan amount (default: 1000).
The date this loan was created.
Constructs a loan with speci�ed interest rate, years, and loan amount.
Returns the number of years of this loan.
Returns the amount of this loan.
Returns the date of the creation of this loan.
Returns the total payment for this loan.
+Loan(annualInterestRate: double, numberOfYears: int, loanAmount: double)
+getAnnualInterestRate(): double
+setNumberOfYears( numberOfYears: int): void
+setLoanAmount( loanAmount: double): void
+getMonthlyPayment(): double
+setAnnualInterestRate( annualInterestRate: double): void
+Loan()
+getNumberOfYears(): int
+getLoanAmount(): double
+getLoanDate(): java.util.Date
+getTotalPayment(): double
Constructs a default Loan object.
–annualInterestRate: double
–numberOfYears: int
–loanAmount: double
–loanDate: java.util.Date
The UML diagram in Figure 10.2 serves as the contract for the Loan class. Throughout this book, you will play the roles of both class user and class developer. Remember that a class user can use the class without knowing how the class is implemented.
Assume the Loan class is available. The program in Listing 10.1 uses that class.
Listing 10.1 TestLoanClass.java 1 import java.util.Scanner; 2 3 public class TestLoanClass { 4 /** Main method */ 5 public static void main(String[] args) {
M10_LIAN1878_11_GE_C10.indd 391 12/29/17 4:15 PM
392 Chapter 10 Object-Oriented Thinking
6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Enter annual interest rate 10 System.out.print( 11 "Enter annual interest rate, for example, 8.25: "); 12 double annualInterestRate = input.nextDouble(); 13 14 // Enter number of years 15 System.out.print("Enter number of years as an integer: "); 16 int numberOfYears = input.nextInt(); 17 18 // Enter loan amount 19 System.out.print("Enter loan amount, for example, 120000.95: "); 20 double loanAmount = input.nextDouble(); 21 22 // Create a Loan object 23 Loan loan = 24 new Loan(annualInterestRate, numberOfYears, loanAmount); 25 26 // Display loan date, monthly payment, and total payment 27 System.out.printf("The loan was created on %s\n" + 28 "The monthly payment is %.2f\nThe total payment is %.2f\n", 29 loan.getLoanDate().toString(), loan.getMonthlyPayment(), 30 loan.getTotalPayment()); 31 } 32 }
create loan object
invoke instance method invoke instance method
Enter annual interest rate, for example, 8.25: 2.5
Enter number of years as an integer: 5
Enter loan amount, for example, 120000.95: 1000
The loan was created on Sat Jun 16 21:12:50 EDT 2012
The monthly payment is 17.75
The total payment is 1064.84
The main method reads the interest rate, the payment period (in years), and the loan amount; creates a Loan object; then obtains the monthly payment (line 29) and the total payment (line 30) using the instance methods in the Loan class.
The Loan class can be implemented as in Listing 10.2.
Listing 10.2 Loan.java 1 public class Loan { 2 private double annualInterestRate; 3 private int numberOfYears; 4 private double loanAmount; 5 private java.util.Date loanDate; 6 7 /** Default constructor */ 8 public Loan() { 9 this(2.5, 1, 1000); 10 } 11
no-arg constructor
M10_LIAN1878_11_GE_C10.indd 392 12/29/17 4:15 PM
10.2 Class Abstraction and Encapsulation 393
12 /** Construct a loan with specified annual interest rate, 13 number of years, and loan amount 14 */ 15 public Loan(double annualInterestRate, int numberOfYears, 16 double loanAmount) { 17 this.annualInterestRate = annualInterestRate; 18 this.numberOfYears = numberOfYears; 19 this.loanAmount = loanAmount; 20 loanDate = new java.util.Date(); 21 } 22 23 /** Return annualInterestRate */ 24 public double getAnnualInterestRate() { 25 return annualInterestRate; 26 } 27 28 /** Set a new annualInterestRate */ 29 public void setAnnualInterestRate(double annualInterestRate) { 30 this.annualInterestRate = annualInterestRate; 31 } 32 33 /** Return numberOfYears */ 34 public int getNumberOfYears() { 35 return numberOfYears; 36 } 37 38 /** Set a new numberOfYears */ 39 public void setNumberOfYears(int numberOfYears) { 40 this.numberOfYears = numberOfYears; 41 } 42 43 /** Return loanAmount */ 44 public double getLoanAmount() { 45 return loanAmount; 46 } 47 48 /** Set a new loanAmount */ 49 public void setLoanAmount(double loanAmount) { 50 this.loanAmount = loanAmount; 51 } 52 53 /** Find monthly payment */ 54 public double getMonthlyPayment() { 55 double monthlyInterestRate = annualInterestRate / 1200; 56 double monthlyPayment = loanAmount * monthlyInterestRate / (1 – 57 (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12))); 58 return monthlyPayment; 59 } 60 61 /** Find total payment */ 62 public double getTotalPayment() { 63 double totalPayment = getMonthlyPayment() * numberOfYears * 12; 64 return totalPayment; 65 } 66 67 /** Return loan date */ 68 public java.util.Date getLoanDate() { 69 return loanDate; 70 } 71 }
constructor
M10_LIAN1878_11_GE_C10.indd 393 12/29/17 4:15 PM
394 Chapter 10 Object-Oriented Thinking
From a class developer’s perspective, a class is designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through constructors, properties, and methods.
The Loan class contains two constructors, four getter methods, three setter methods, and the methods for finding the monthly payment and the total payment. You can con- struct a Loan object by using the no-arg constructor or the constructor with three param- eters: annual interest rate, number of years, and loan amount. When a loan object is created, its date is stored in the loanDate field. The getLoanDate method returns the date. The methods—getAnnualInterest, getNumberOfYears, and getLoanAmount—return the annual interest rate, payment years, and loan amount, respectively. All the data properties and methods in this class are tied to a specific instance of the Loan class. Therefore, they are instance variables and methods.
Important Pedagogical Tip Use the UML diagram for the Loan class shown in Figure 10.2 to write a test program that uses the Loan class even though you don’t know how the Loan class is imple- mented. This has three benefits:
■ ■■ It demonstrates that developing a class and using a class are two separate tasks.
■ ■■ It enables you to skip the complex implementation of certain classes without inter- rupting the sequence of this book.
■ ■■ It is easier to learn how to implement a class if you are familiar with it by using the class.
For all the class examples from now on, create an object from the class and try using its methods before turning your attention to its implementation.
10.2.1 If you redefine the Loan class in Listing 10.2 without setter methods, is the class immutable?
10.3 Thinking in Objects The procedural paradigm focuses on designing methods. The object-oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects.
Chapters 1 through 8 introduced fundamental programming techniques for problem solving using loops, methods, and arrays. Knowing these techniques lays a solid foundation for object- oriented programming. Classes provide more flexibility and modularity for building reusable software. This section improves the solution for a problem introduced in Chapter 3 using the object-oriented approach. From these improvements, you will gain insight into the differences between procedural and object-oriented programming, and see the benefits of developing reus- able code using objects and classes.
Listing 3.4, ComputeAndInterpretBMI.java, presented a program for computing the body mass index (BMI). The code cannot be reused in other programs, because the code is in the main method. To make it reusable, define a static method to compute body mass index as follows:
public static double getBMI(double weight, double height)
This method is useful for computing body mass index for a specified weight and height. How- ever, it has limitations. Suppose you need to associate the weight and height with a person’s name and birth date. You could declare separate variables to store these values, but these values would not be tightly coupled. The ideal way to couple them is to create an object that contains them all. Since these values are tied to individual objects, they should be stored in instance data fields. You can define a class named BMI as shown in Figure 10.3.
Point Check
Point Key
VideoNote
The BMI class
M10_LIAN1878_11_GE_C10.indd 394 12/29/17 4:15 PM
10.3 Thinking in Objects 395
Figure 10.3 The BMI class encapsulates BMI information.
BMI
–name: String
–age: int –weight: double
–height: double
+BMI(name: String, age: int, weight: double, height: double)
+BMI(name: String, weight: double, height: double)
+getBMI(): double
+getStatus(): String
The name of the person.
The age of the person. The weight of the person in pounds. The height of the person in inches.
Creates a BMI object with the speci�ed name, age, weight, and height.
Creates a BMI object with the speci�ed name, weight, height, and a default age 20.
Returns the BMI.
Returns the BMI status (e.g., normal, overweight, etc.).
The getter methods for these data �elds are provided in the class, but omitted in the UML diagram for brevity.
The BMI for Kim Yang is 20.81 Normal
The BMI for Susan King is 30.85 Obese
Assume the BMI class is available. Listing 10.3 gives a test program that uses this class.
Listing 10.3 UseBMIClass.java 1 public class UseBMIClass { 2 public static void main(String[] args) { 3 BMI bmi1 = new BMI("Kim Yang", 18, 145, 70); 4 System.out.println("The BMI for " + bmi1.getName() + " is " 5 + bmi1.getBMI() + " " + bmi1.getStatus()); 6 7 BMI bmi2 = new BMI("Susan King", 215, 70); 8 System.out.println("The BMI for " + bmi2.getName() + " is " 9 + bmi2.getBMI() + " " + bmi2.getStatus()); 10 } 11 }
create an object
invoke instance method create an object
invoke instance method
Line 3 creates the object bmi1 for Kim Yang, and line 7 creates the object bmi2 for Susan King. You can use the instance methods getName(), getBMI(), and getStatus() to return the BMI information in a BMI object.
The BMI class can be implemented as in Listing 10.4.
Listing 10.4 BMI.java 1 public class BMI { 2 private String name; 3 private int age; 4 private double weight; // in pounds 5 private double height; // in inches 6 public static final double KILOGRAMS_PER_POUND = 0.45359237; 7 public static final double METERS_PER_INCH = 0.0254; 8 9 public BMI(String name, int age, double weight, double height) { 10 this.name = name;
constructor
M10_LIAN1878_11_GE_C10.indd 395 12/29/17 4:15 PM
396 Chapter 10 Object-Oriented Thinking
11 this.age = age; 12 this.weight = weight; 13 this.height = height; 14 } 15 16 public BMI(String name, double weight, double height) { 17 this(name, 20, weight, height); 18 } 19 20 public double getBMI() { 21 double bmi = weight * KILOGRAMS_PER_POUND / 22 ((height * METERS_PER_INCH) * (height * METERS_PER_INCH)); 23 return Math.round(bmi * 100) / 100.0; 24 } 25 26 public String getStatus() { 27 double bmi = getBMI(); 28 if (bmi < 18.5) 29 return "Underweight"; 30 else if (bmi < 25) 31 return "Normal"; 32 else if (bmi < 30) 33 return "Overweight"; 34 else 35 return "Obese"; 36 } 37 38 public String getName() { 39 return name; 40 } 41 42 public int getAge() { 43 return age; 44 } 45 46 public double getWeight() { 47 return weight; 48 } 49 50 public double getHeight() { 51 return height; 52 } 53 }
The mathematical formula for computing the BMI using weight and height is given in Section 3.8. The instance method getBMI() returns the BMI. Since the weight and height are instance data fields in the object, the getBMI() method can use these properties to compute the BMI for the object.
The instance method getStatus() returns a string that interprets the BMI. The interpreta- tion is also given in Section 3.8.
This example demonstrates the advantages of the object-oriented paradigm over the proce- dural paradigm. The procedural paradigm focuses on designing methods. The object-oriented paradigm couples data and methods together into objects. Software design using the object- oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects.
In procedural programming, data and operations on the data are separate, and this meth- odology requires passing data to methods. Object-oriented programming places data and
constructor
getBMI
getStatus
procedural vs. object-oriented paradigms
M10_LIAN1878_11_GE_C10.indd 396 12/29/17 4:15 PM
10.4 Class Relationships 397
the operations that pertain to them in an object. This approach solves many of the problems inherent in procedural programming. The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain. Programming in Java involves thinking in terms of objects; a Java program can be viewed as a collection of cooperating objects.
10.3.1 Is the BMI class defined in Listing 10.4 immutable?
10.4 Class Relationships To design classes, you need to explore the relationships among classes. The common relationships among classes are association, aggregation, composition, and inheritance.
This section explores association, aggregation, and composition. The inheritance relationship will be introduced in Chapter 11.
10.4.1 Association Association is a general binary relationship that describes an activity between two classes. For example, a student taking a course is an association between the Student class and the Course class, and a faculty member teaching a course is an association between the Faculty class and the Course class. These associations can be represented in UML graphical notation, as shown in Figure 10.4.
Point Check
Point Key
association
Figure 10.4 This UML diagram shows that a student may take any number of courses, a faculty member may teach at most three courses, a course may have from 5 to 60 students, and a course is taught by only one faculty member.
Teach
Teacher
Take
FacultyStudent 5..60 0..3 1
Course
An association is illustrated by a solid line between two classes with an optional label that describes the relationship. In Figure 10.4, the labels are Take and Teach. Each relationship may have an optional small black triangle that indicates the direction of the relationship. In this figure, the N indicates that a student takes a course (as opposed to a course taking a student).
Each class involved in the relationship may have a role name that describes the role it plays in the relationship. In Figure 10.4, teacher is the role name for Faculty.
Each class involved in an association may specify a multiplicity, which is placed at the side of the class to specify how many of the class’s objects are involved in the relationship in UML. A multiplicity could be a number or an interval that specifies how many of the class’s objects are involved in the relationship. The character * means an unlimited number of objects, and the interval m..n indicates that the number of objects is between m and n, inclusively. In Figure 10.4, each student may take any number of courses, and each course must have at least 5 and at most 60 students. Each course is taught by only one faculty member, and a faculty member may teach from 0 to 3 courses per semester.
In Java code, you can implement associations by using data fields and methods. For exam- ple, the relationships in Figure 10.4 may be implemented using the classes in Figure 10.5.
multiplicity
M10_LIAN1878_11_GE_C10.indd 397 12/29/17 4:15 PM
398 Chapter 10 Object-Oriented Thinking
The relation “a student takes a course” is implemented using the addCourse method in the Student class and the addStudent method in the Course class. The relation “a faculty teaches a course” is implemented using the addCourse method in the Faculty class and the setFaculty method in the Course class. The Student class may use a list to store the courses that the student is taking, the Faculty class may use a list to store the courses that the faculty is teaching, and the Course class may use a list to store students enrolled in the course and a data field to store the instructor who teaches the course.
Figure 10.6 Each student has a name and an address.
Aggregation
AddressName
Composition
1 1 1..3 1 Student
Figure 10.5 The association relations are implemented using data fields and methods in classes.
public class Student { private Course[] courseList;
public void addCourse( Course c) { ... } }
public class Course { private Student[] classList; private Faculty faculty;
public void addStudent( Student s) { ... }
public void setFaculty( Faculty faculty) { ... } }
public class Faculty { private Course[] courseList;
public void addCourse( Course c) { ... } }
Note There are many possible ways to implement relationships. For example, the student and faculty information in the Course class can be omitted, since they are already in the Student and Faculty class. Likewise, if you don’t need to know the courses a student takes or a faculty member teaches, the data field courseList and the addCourse method in Student or Faculty can be omitted.
10.4.2 Aggregation and Composition Aggregation is a special form of association that represents an ownership relationship between two objects. Aggregation models has-a relationships. The owner object is called an aggregating object, and its class is called an aggregating class. The subject object is called an aggregated object, and its class is called an aggregated class.
We refer aggregation between two objects as composition if the existence of the aggregated object is dependent on the aggregating object. In other words, if a relationship is composition, the aggregated object cannot exist on its own. For example, “a student has a name” is a composition relationship between the Student class and the Name class because Name is dependent on Student, whereas “a student has an address” is an aggregation relationship between the Student class and the Address class because an address can exist by itself. Composition implies exclusive ownership. One object owns another object. When the owner object is destroyed, the dependent object is destroyed as well. In UML, a filled diamond is attached to an aggregating class (in this case, Student) to denote the composition relationship with an aggregated class (Name), and an empty diamond is attached to an aggregating class (Student) to denote the aggregation relationship with an aggregated class (Address), as shown in Figure 10.6.
many possible implementations
aggregation
aggregating object aggregating class aggregated object aggregated class
composition
In Figure 10.6, each student has only one multiplicity—address—and each address can be shared by up to 3 students. Each student has one name, and the name is unique for each student.
M10_LIAN1878_11_GE_C10.indd 398 12/29/17 4:15 PM
10.4 Class Relationships 399
An aggregation relationship is usually represented as a data field in the aggregating class. For example, the relationships in Figure 10.6 may be implemented using the classes in Figure 10.7. The relation “a student has a name” and “a student has an address” are implemented in the data field name and address in the Student class.
Figure 10.8 A person may have a supervisor.
1
1 Supervisor
Person
Figure 10.9 A person can have several supervisors.
Person
Supervisor
1
(a) (b)
m
public class Person { ...
private Person[] supervisors; }
Figure 10.7 The composition relations are implemented using data fields in classes.
public class Name { ... }
public class Address { ... }
public class Student { private Name name; private Address address; ... }
Aggregated class Aggregating class Aggregated class
Aggregation may exist between objects of the same class. For example, a person may have a supervisor. This is illustrated in Figure 10.8.
In the relationship “a person has a supervisor,” a supervisor can be represented as a data field in the Person class, as follows:
public class Person { // The type for the data is the class itself private Person supervisor;
... }
If a person can have several supervisors, as shown in Figure 10.9a, you may use an array to store supervisors, as shown in Figure 10.9b.
Important Note Since aggregation and composition relationships are represented using classes in the same way, we will not differentiate them and call both compositions for simplicity.
10.4.1 What are common relationships among classes? 10.4.2 What is association? What is aggregation? What is composition? 10.4.3 What is UML notation of aggregation and composition? 10.4.4 Why both aggregation and composition are together referred to as composition?
aggregation or composition
Point Check
M10_LIAN1878_11_GE_C10.indd 399 12/29/17 4:15 PM
400 Chapter 10 Object-Oriented Thinking
10.5 Case Study: Designing the Course Class This section designs a class for modeling courses.
This book’s philosophy is teaching by example and learning by doing. The book provides a wide variety of examples to demonstrate object-oriented programming. This section and the next offer additional examples on designing classes.
Suppose you need to process course information. Each course has a name and has students enrolled. You should be able to add/drop a student to/from the course. You can use a class to model the courses, as shown in Figure 10.10.
Point Key
Figure 10.10 The Course class models the courses.
Course
–courseName: String –students: String[]
–numberOfStudents: int
+Course(courseName: String)
+getCourseName(): String
+addStudent(student: String): void
+dropStudent(student: String): void
+getStudents(): String[] +getNumberOfStudents(): int
The name of the course. An array to store the students for the course.
The number of students (default: 0).
Creates a course with the speci�ed name.
Returns the course name.
Adds a new student to the course.
Drops a student from the course. Returns the students for the course. Returns the number of students for the course.
A Course object can be created using the constructor Course(String name) by passing a course name. You can add students to the course using the addStudent(String student) method, drop a student from the course using the dropStudent(String student) method, and return all the students in the course using the getStudents() method. Suppose that the Course class is available; Listing 10.5 gives a test class that creates two courses and adds students to them.
Listing 10.5 TestCourse.java 1 public class TestCourse { 2 public static void main(String[] args) { 3 Course course1 = new Course("Data Structures"); 4 Course course2 = new Course("Database Systems"); 5 6 course1.addStudent("Peter Jones"); 7 course1.addStudent("Kim Smith"); 8 course1.addStudent("Anne Kennedy"); 9 10 course2.addStudent("Peter Jones"); 11 course2.addStudent("Steve Smith"); 12 13 System.out.println("Number of students in course1: " 14 + course1.getNumberOfStudents()); 15 String[] students = course1.getStudents(); 16 for (int i = 0; i < course1.getNumberOfStudents(); i++) 17 System.out.print(students[i] + ", "); 18 19 System.out.println(); 20 System.out.print("Number of students in course2: " 21 + course2.getNumberOfStudents()); 22 } 23 }
create a Course
add a Student
number of students return students
M10_LIAN1878_11_GE_C10.indd 400 12/29/17 4:15 PM
10.5 Case Study: Designing the Course Class 401
The Course class is implemented in Listing 10.6. It uses an array to store the students in the course. For simplicity, assume the maximum course enrollment is 100. The array is created using new String[100] in line 3. The addStudent method (line 10) adds a student to the array. Whenever a new student is added to the course, numberOfStudents is increased (line 12). The getStudents method returns the array. The dropStudent method (line 27) is left as an exercise.
Listing 10.6 Course.java 1 public class Course { 2 private String courseName; 3 private String[] students = new String[100]; 4 private int numberOfStudents; 5 6 public Course(String courseName) { 7 this.courseName = courseName; 8 } 9 10 public void addStudent(String student) { 11 students[numberOfStudents] = student; 12 numberOfStudents++; 13 } 14 15 public String[] getStudents() { 16 return students; 17 } 18 19 public int getNumberOfStudents() { 20 return numberOfStudents; 21 } 22 23 public String getCourseName() { 24 return courseName; 25 } 26 27 public void dropStudent(String student) { 28 // Left as an exercise in Programming Exercise 10.9 29 } 30 }
The array size is fixed to be 100 (line 3), so you cannot have more than 100 students in the course. You can improve the class by automatically increasing the array size in Programming Exercise 10.9.
When you create a Course object, an array object is created. A Course object contains a reference to the array. For simplicity, you can say the Course object contains the array.
The user can create a Course object and manipulate it through the public methods addStudent, dropStudent, getNumberOfStudents, and getStudents. However, the user doesn’t need to know how these methods are implemented. The Course class encapsu- lates the internal implementation. This example uses an array to store students, but you could use a different data structure to store students. The program that uses Course does not need to change as long as the contract of the public methods remains unchanged.
create students
add a course
return students
number of students
Number of students in course1: 3
Peter Jones, Kim Smith, Anne Kennedy,
Number of students in course2: 2
M10_LIAN1878_11_GE_C10.indd 401 12/29/17 4:15 PM
402 Chapter 10 Object-Oriented Thinking
10.5.1 Replace the statement in line 17 in Listing 10.5, TestCourse.java, so the loop dis- plays each student name followed by a comma except the last student name.
10.6 Case Study: Designing a Class for Stacks This section designs a class for modeling stacks.
Recall that a stack is a data structure that holds data in a last-in, first-out fashion, as shown in Figure 10.11.
Point Check
Point Keystack
Figure 10.11 A stack holds data in a last-in, first-out fashion.
Data1
Data2 Data1
Data3
Data3 Data2 Data1
Data1
Data1
Data2
Data1
Data2
Data2 Data1
Data3
Stacks have many applications. For example, the compiler uses a stack to process method invocations. When a method is invoked, its parameters and local variables are pushed into a stack. When a method calls another method, the new method’s parameters and local variables are pushed into the stack. When a method finishes its work and returns to its caller, its associ- ated space is released from the stack.
You can define a class to model stacks. For simplicity, assume the stack holds the int values. Thus, name the stack class StackOfIntegers. The UML diagram for the class is shown in Figure 10.12.
VideoNote
The StackOfIntegers class
Figure 10.12 The StackOfIntegers class encapsulates the stack storage and provides the operations for manipulating the stack.
StackOfIntegers
–elements: int[]
–size: int
+StackOfIntegers()
+StackOfIntegers(capacity: int)
+empty(): boolean +peek(): int
+push(value: int): void
+pop(): int
+getSize(): int
An array to store integers in the stack. The number of integers in the stack.
Constructs an empty stack with a default capacity of 16. Constructs an empty stack with a speci�ed capacity. Returns true if the stack is empty. Returns the integer at the top of the stack without removing it from the stack. Stores an integer into the top of the stack.
Removes the integer at the top of the stack and returns it.
Returns the number of elements in the stack.
Suppose the class is available. The test program in Listing 10.7 uses the class to create a stack (line 3), store 10 integers 0, 1, 2, . . . , and 9 (line 6), and displays them in reverse order (line 9).
Listing 10.7 TestStackOfIntegers.java 1 public class TestStackOfIntegers { 2 public static void main(String[] args) { 3 StackOfIntegers stack = new StackOfIntegers(); create a stack
M10_LIAN1878_11_GE_C10.indd 402 12/29/17 4:15 PM
10.6 Case Study: Designing a Class for Stacks 403
4 5 for (int i = 0; i < 10; i++) 6 stack.push(i); 7 8 while (!stack.empty()) 9 System.out.print(stack.pop() + " "); 10 } 11 }
push to stack
pop from stack
9 8 7 6 5 4 3 2 1 0
Figure 10.13 The StackOfIntegers uses an array to store the elements in a stack.
.
.
.
.
.
.
elements[0] elements[1]
elements[size 2 1] capacity
top
bottom
size
elements[capacity 2 1]
How do you implement the StackOfIntegers class? The elements in the stack are stored in an array named elements. When you create a stack, the array is also created. The no-arg constructor creates an array with the default capacity of 16. The variable size counts the number of elements in the stack, and size – 1 is the index of the element at the top of the stack, as shown in Figure 10.13. For an empty stack, size is 0.
The StackOfIntegers class is implemented in Listing 10.8. The methods empty(), peek(), pop(), and getSize() are easy to implement. To implement push(int value), assign value to elements[size] if size < capacity (line 24). If the stack is full (i.e., size >= capacity), create a new array of twice the current capacity (line 19), copy the con- tents of the current array to the new array (line 20), and assign the reference of the new array to the current array in the stack (line 21). Now you can add the new value to the array (line 24).
Listing 10.8 StackOfIntegers.java 1 public class StackOfIntegers { 2 private int[] elements; 3 private int size; 4 public static final int DEFAULT_CAPACITY = 16; 5 6 /** Construct a stack with the default capacity 16 */ 7 public StackOfIntegers() { 8 this(DEFAULT_CAPACITY); 9 } 10 11 /** Construct a stack with the specified maximum capacity */ 12 public StackOfIntegers(int capacity) { 13 elements = new int[capacity]; 14 } 15
max capacity 16
M10_LIAN1878_11_GE_C10.indd 403 12/29/17 4:15 PM
404 Chapter 10 Object-Oriented Thinking
16 /** Push a new integer to the top of the stack */ 17 public void push(int value) { 18 if (size >= elements.length) { 19 int[] temp = new int[elements.length * 2]; 20 System.arraycopy(elements, 0, temp, 0, elements.length); 21 elements = temp; 22 } 23 24 elements[size++] = value; 25 } 26 27 /** Return and remove the top element from the stack */ 28 public int pop() { 29 return elements[——size]; 30 } 31 32 /** Return the top element from the stack */ 33 public int peek() { 34 return elements[size – 1]; 35 } 36 37 /** Test whether the stack is empty */ 38 public boolean empty() { 39 return size == 0; 40 } 41 42 /** Return the number of elements in the stack */ 43 public int getSize() { 44 return size; 45 } 46 }
10.7 Processing Primitive Data Type Values as Objects A primitive-type value is not an object, but it can be wrapped in an object using a wrapper class in the Java API.
Owing to performance considerations, primitive data type values are not objects in Java. Because of the overhead of processing objects, the language’s performance would be adversely affected if primitive data type values were treated as objects. However, many Java methods require the use of objects as arguments. Java offers a convenient way to incorporate, or wrap, a primitive data type value into an object (e.g., wrapping an int into an Integer object, wrapping a double into a Double object, and wrapping a char into a Character object). By using a wrapper class, you can process primitive data type values as objects. Java provides Boolean, Character, Double, Float, Byte, Short, Integer, and Long wrapper classes in the java.lang package for primitive data types. The Boolean class wraps a Boolean value true or false. This section uses Integer and Double as examples to introduce the numeric wrapper classes.
Note Most wrapper class names for a primitive type are the same as the primitive data type name with the first letter capitalized. The exceptions are Integer for int and Character for char.
Numeric wrapper classes are very similar to each other. Each contains the methods doubleValue(), floatValue(), intValue(), longValue(), shortValue(), and byteValue(). These methods “convert” objects into primitive-type values. The key features of Integer and Double are shown in Figure 10.14.
double the capacity
add to stack
Point Key
why wrapper class?
naming convention
M10_LIAN1878_11_GE_C10.indd 404 12/29/17 4:15 PM
10.7 Processing Primitive Data Type Values as Objects 405
Figure 10.14 The wrapper classes provide constructors, constants, and conversion methods for manipulating various data types.
–value: int
+MAX_VALUE: int
+MIN_VALUE: int
+Integer(value: int)
+Integer(s: String)
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longValue(): long
+floatValue(): float
+doubleValue(): double
+compareTo(o: Integer): int
+toString(): String
+valueOf(s: String): Integer
+valueOf(s: String, radix: int): Integer
+parseInt(s: String): int
+parseInt(s: String, radix: int): int
java.lang.Integer java.lang.Double
–value: double
+MAX_VALUE: double
+MIN_VALUE: double
+Double(value: double)
+Double(s: String)
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longValue(): long
+floatValue(): float
+doubleValue(): double
+compareTo(o: Double): int
+toString(): String
+valueOf(s: String): Double
+valueOf(s: String, radix: int): Double
+parseDouble(s: String): double
+parseDouble(s: String, radix: int): double
You can construct a wrapper object either from a primitive data type value or from a string representing the numeric value—for example, new Double(5.0), new Double("5.0"), new Integer(5), and new Integer("5").
The wrapper classes do not have no-arg constructors. The instances of all wrapper classes are immutable; this means that, once the objects are created, their internal values cannot be changed.
Each numeric wrapper class has the constants MAX_VALUE and MIN_VALUE. MAX_VALUE represents the maximum value of the corresponding primitive data type. For Byte, Short, Integer, and Long, MIN_VALUE represents the minimum byte, short, int, and long values. Float and Double, MIN_VALUE represents the minimum positive float and double values. The following statements display the maximum integer (2,147,483,647), the minimum positive float (1.4E–45), and the maximum double floating-point number (1.79769313486231570e + 308d):
System.out.println("The maximum integer is " + Integer.MAX_VALUE); System.out.println("The minimum positive float is " + Float.MIN_VALUE); System.out.println( "The maximum double-precision floating-point number is " + Double.MAX_VALUE);
Each numeric wrapper class contains the methods doubleValue(), floatValue(), intValue(), longValue(), and shortValue() for returning a double, float, int, long, or short value for the wrapper object. For example,
new Double(12.4).intValue() returns 12; new Integer(12).doubleValue() returns 12.0;
Recall the String class contains the compareTo method for comparing two strings. The numeric wrapper classes contain the compareTo method for comparing two numbers and
constructors
no no-arg constructor immutable
constants
conversion methods
compareTo method
M10_LIAN1878_11_GE_C10.indd 405 12/29/17 4:15 PM
406 Chapter 10 Object-Oriented Thinking
returns 1, 0, or –1, if this number is greater than, equal to, or less than the other number. For example,
new Double(12.4).compareTo(new Double(12.3)) returns 1; new Double(12.3).compareTo(new Double(12.3)) returns 0; new Double(12.3).compareTo(new Double(12.51)) returns –1;
The numeric wrapper classes have a useful static method, valueOf(String s). This method creates a new object initialized to the value represented by the specified string. For example,
Double doubleObject = Double.valueOf("12.4"); Integer integerObject = Integer.valueOf("12");
You have used the parseInt method in the Integer class to parse a numeric string into an int value and the parseDouble method in the Double class to parse a numeric string into a double value. Each numeric wrapper class has two overloaded parsing methods to parse a numeric string into an appropriate numeric value based on 10 (decimal) or any specified radix (e.g., 2 for binary, 8 for octal, and 16 for hexadecimal).
// These two methods are in the Byte class public static byte parseByte(String s) public static byte parseByte(String s, int radix)
// These two methods are in the Short class public static short parseShort(String s) public static short parseShort(String s, int radix)
// These two methods are in the Integer class public static int parseInt(String s) public static int parseInt(String s, int radix)
// These two methods are in the Long class public static long parseLong(String s) public static long parseLong(String s, int radix)
// These two methods are in the Float class public static float parseFloat(String s) public static float parseFloat(String s, int radix)
// These two methods are in the Double class public static double parseDouble(String s) public static double parseDouble(String s, int radix)
For example,
Integer.parseInt("11", 2) returns 3; Integer.parseInt("12", 8) returns 10; Integer.parseInt("13", 10) returns 13; Integer.parseInt("1A", 16) returns 26;
Integer.parseInt("12", 2) would raise a runtime exception because 12 is not a binary number.
Note you can convert a decimal number into a hex number using the format method. For example,
String.format("%x", 26) returns 1A;
static valueOf methods
static parsing methods
converting decimal to hex
M10_LIAN1878_11_GE_C10.indd 406 12/29/17 4:15 PM
10.8 Automatic Conversion between Primitive Types and Wrapper Class Types 407
10.7.1 Describe primitive-type wrapper classes. 10.7.2 Can each of the following statements be compiled?
a. Integer i = new Integer("23");
b. Integer i = new Integer(23);
c. Integer i = Integer.valueOf("23");
d. Integer i = Integer.parseInt("23", 8);
e. Double d = new Double();
f. Double d = Double.valueOf("23.45");
g. int i = (Integer.valueOf("23")).intValue();
h. double d = (Double.valueOf("23.4")).doubleValue();
i. int i = (Double.valueOf("23.4")).intValue();
j. String s = (Double.valueOf("23.4")).toString();
10.7.3 How do you convert an integer into a string? How do you convert a numeric string into an integer? How do you convert a double number into a string? How do you convert a numeric string into a double value?
10.7.4 Show the output of the following code:
public class Test { public static void main(String[] args) { Integer x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }
10.7.5 What is the output of the following code?
public class Test { public static void main(String[] args) { System.out.println(Integer.parseInt("10")); System.out.println(Integer.parseInt("10", 10)); System.out.println(Integer.parseInt("10", 16)); System.out.println(Integer.parseInt("11")); System.out.println(Integer.parseInt("11", 10)); System.out.println(Integer.parseInt("11", 16)); } }
10.8 Automatic Conversion between Primitive Types and Wrapper Class Types
A primitive-type value can be automatically converted to an object using a wrapper class, and vice versa, depending on the context.
Converting a primitive value to a wrapper object is called boxing. The reverse conversion is called unboxing. Java allows primitive types and wrapper classes to be converted automati- cally. The compiler will automatically box a primitive value that appears in a context requiring an object, and unbox an object that appears in a context requiring a primitive value. This is called autoboxing and autounboxing.
Point Check
Point Key
boxing unboxing autoboxing
autounboxing
M10_LIAN1878_11_GE_C10.indd 407 12/29/17 4:15 PM
408 Chapter 10 Object-Oriented Thinking
For instance, the following statement in (a) can be simplified as in (b) using autoboxing.
Consider the following example:
1 Integer[] intArray = {1, 2, 3}; 2 System.out.println(intArray[0] + intArray1] + intArray[2]);
In line 1, the primitive values 1, 2, and 3 are automatically boxed into objects new Integer(1), new Integer(2), and new Integer(3). In line 2, the objects intArray[0], intArray[1], and intArray[2] are automatically unboxed into int values that are added together.
10.8.1 What are autoboxing and autounboxing? Are the following statements correct?
a. Integer x = 3 + new Integer(5);
b. Integer x = 3;
c. Double x = 3;
d. Double x = 3.0;
e. int x = new Integer(3);
f. int x = new Integer(3) + new Integer(4);
10.8.2 Show the output of the following code.
public class Test { public static void main(String[] args) { Double x = 3.5; System.out.println(x.intValue()); System.out.println(x.compareTo(4.5)); } }
10.9 The BigInteger and BigDecimal Classes The BigInteger and BigDecimal classes can be used to represent integers or decimal numbers of any size and precision.
If you need to compute with very large integers or high-precision floating-point values, you can use the BigInteger and BigDecimal classes in the java.math package. Both are immutable. The largest integer of the long type is Long.MAX_VALUE (i.e., 9223372036854775807). An instance of BigInteger can represent an integer of any size. You can use new BigInteger(String) and new BigDecimal(String) to create an instance of BigInteger and BigDecimal, use the add, subtract, multiply, divide, and remainder methods to perform arithmetic operations, and use the compareTo method to compare two big numbers. For example, the following code creates two BigInteger objects and multiplies them:
BigInteger a = new BigInteger("9223372036854775807"); BigInteger b = new BigInteger("2"); BigInteger c = a.multiply(b); // 9223372036854775807 * 2 System.out.println(c);
Point Check
Point Key
VideoNote
Process large numbers
immutable
Integer intObject = new Integer(2); Equivalent Integer intObject = 2;
autoboxing (a) (b)
int i = 1; (a) and (b) are
Equivalent
int i = new Integer(1);
(a) (b)
The following statement in (a) is the same as in (b) due to autounboxing.
M10_LIAN1878_11_GE_C10.indd 408 12/29/17 4:15 PM
10.9 The BigInteger and BigDecimal Classes 409
The output is 18446744073709551614. There is no limit to the precision of a BigDecimal object. The divide method may throw
an ArithmeticException if the result cannot be terminated. However, you can use the over- loaded divide(BigDecimal d, int scale, int roundingMode) method to specify a scale and a rounding mode to avoid this exception, where scale is the maximum number of digits after the decimal point. For example, the following code creates two BigDecimal objects and performs division with scale 20 and rounding mode BigDecimal.ROUND_UP:
BigDecimal a = new BigDecimal(1.0); BigDecimal b = new BigDecimal(3); BigDecimal c = a.divide(b, 20, BigDecimal.ROUND_UP); System.out.println(c);
The output is 0.33333333333333333334. Note the factorial of an integer can be very large. Listing 10.9 gives a method that can return
the factorial of any integer.
Listing 10.9 LargeFactorial.java 1 import java.util.Scanner; 2 import java.math.*; 3 4 public class LargeFactorial { 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 System.out.print("Enter an integer: "); 8 int n = input.nextInt(); 9 System.out.println(n +"! is \n" + factorial(n)); 10 } 11 12 public static BigInteger factorial(long n) { 13 BigInteger result = BigInteger.ONE; 14 for (int i = 1; i <= n; i++) 15 result = result.multiply(new BigInteger(i + "")); 16 17 return result; 18 } 19 }
constant
multiply
Enter an integer: 50 50! is 30414093201713378043612608166064768844377641568960512000000000000
BigInteger.ONE (line 13) is a constant defined in the BigInteger class. BigInteger. ONE is the same as new BigInteger("1").
A new result is obtained by invoking the multiply method (line 15).
10.9.1 What is the output of the following code? public class Test { public static void main(String[] args) { java.math.BigInteger x = new java.math.BigInteger("3"); java.math.BigInteger y = new java.math.BigInteger("7"); java.math.BigInteger z = x.add(y); System.out.println("x is " + x); System.out.println("y is " + y); System.out.println("z is " + z); } }
Point Check
M10_LIAN1878_11_GE_C10.indd 409 12/29/17 4:15 PM
410 Chapter 10 Object-Oriented Thinking
10.10 The String Class A String object is immutable; its contents cannot be changed once the string is created.
Strings were introduced in Section 4.4. You know strings are objects. You can invoke the charAt(index) method to obtain a character at the specified index from a string, the length() method to return the size of a string, the substring method to return a substring in a string, the indexOf and lastIndexOf methods to return the first or last index of a matching character or a substring, the equals and compareTo methods to compare two strings, and the trim() method to trim whitespace characters from the two ends of a string, and the toLowerCase() and toUpperCase() methods to return the lowercase and uppercase from a string. We will take a closer look at strings in this section.
The String class has 13 constructors and more than 40 methods for manipulating strings. Not only is it very useful in programming, but it is also a good example for learning classes and objects.
10.10.1 Constructing a String You can create a string object from a string literal or from an array of characters. To create a string from a string literal, use the syntax:
String newString = new String(stringLiteral);
The argument stringLiteral is a sequence of characters enclosed in double quotes. The fol- lowing statement creates a String object message for the string literal "Welcome to Java":
String message = new String("Welcome to Java");
Java treats a string literal as a String object. Thus, the following statement is valid:
String message = "Welcome to Java";
You can also create a string from an array of characters. For example, the following statements create the string "Good Day":
char[] charArray = {'G', 'o', 'o', 'd', ' ', 'D', 'a', 'y'}; String message = new String(charArray);
Note A String variable holds a reference to a String object that stores a string value. Strictly speaking, the terms String variable, String object, and string value are different, but most of the time the distinctions between them can be ignored. For simplicity, the term string will often be used to refer to String variable, String object, and string value.
10.10.2 Immutable Strings and Interned Strings A String object is immutable; its contents cannot be changed. Does the following code change the contents of the string?
String s = "Java"; s = "HTML";
The answer is no. The first statement creates a String object with the content "Java" and assigns its reference to s. The second statement creates a new String object with the content "HTML" and assigns its reference to s. The first String object still exists after the assignment, but it can no longer be accessed, because variable s now points to the new object, as shown in Figure 10.15.
Point Key
VideoNote
The String class
string literal object
String variable, string object, string value
immutable
M10_LIAN1878_11_GE_C10.indd 410 12/29/17 4:15 PM
10.10 The String Class 411
Because strings are immutable and are ubiquitous in programming, the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string. For example, the following statements: interned string
String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
String s3 = "Welcome to Java";
System.out.println("s1 == s2 is " + (s1 == s2)); System.out.println("s1 == s3 is " + (s1 == s3));
s1
s2
s3 Interned string object for "Welcome to Java"
: String
A string object for "Welcome to Java"
: String
Figure 10.16 The String class contains the methods for replacing and splitting strings.
java.lang.String
Returns a new string that replaces all matching characters in this string with the new character.
Returns a new string that replaces the �rst matching substring in this string with the new substring.
Returns a new string that replaces all matching substrings in this string with the new substring.
Returns an array of strings consisting of the substrings split by the delimiter.
+replace(oldChar: char, newChar: char): String
+replaceFirst(oldString: String, newString: String): String
+replaceAll(oldString: String, newString: String): String
+split(delimiter: String): String[]
display
s1 == s2 is false s1 == s3 is true
In the preceding statements, s1 and s3 refer to the same interned string—"Welcome to Java"—so s1 == s3 is true. However, s1 == s2 is false, because s1 and s2 are two different string objects, even though they have the same contents.
10.10.3 Replacing and Splitting Strings The String class provides the methods for replacing and splitting strings, as shown in Figure 10.16.
Figure 10.15 Strings are immutable; once created, their contents cannot be changed.
After executing String s = "Java"; After executing s = "HTML";
Contents cannot be changed
String object for "Java"
String object for "HTML"
This string object is now unreferenced
s
String object for "Java"
s : String : String
: String
M10_LIAN1878_11_GE_C10.indd 411 12/29/17 4:15 PM
412 Chapter 10 Object-Oriented Thinking
Once a string is created, its contents cannot be changed. The methods replace, replaceFirst, and replaceAll return a new string derived from the original string ( without changing the original string!). Several versions of the replace methods are provided to replace a character or a substring in the string with a new character or a new substring.
For example,
"Welcome".replace('e', 'A') returns a new string, WAlcomA. "Welcome".replaceFirst("e", "AB") returns a new string, WABlcome. "Welcome".replace("e", "AB") returns a new string, WABlcomAB. "Welcome".replace("el", "AB") returns a new string, WABcome.
The split method can be used to extract tokens from a string with the specified delimiters. For example, the following code
String[] tokens = "Java#HTML#Perl".split("#"); for (int i = 0; i < tokens.length; i++) System.out.print(tokens[i] + " ");
displays
Java HTML Perl
10.10.4 Matching, Replacing, and Splitting by Patterns Often you will need to write code that validates user input, such as to check whether the input is a number, a string with all lowercase letters, or a Social Security number. How do you write this type of code? A simple and effective way to accomplish this task is to use the regular expression.
A regular expression (abbreviated regex) is a string that describes a pattern for matching a set of strings. You can match, replace, or split a string by specifying a pattern. This is an extremely useful and powerful feature.
Let us begin with the matches method in the String class. At first glance, the matches method is very similar to the equals method. For example, the following two statements both evaluate to true:
"Java".matches("Java"); "Java".equals("Java");
However, the matches method is more powerful. It can match not only a fixed string, but also a set of strings that follow a pattern. For example, the following statements all evaluate to true:
"Java is fun".matches("Java.*") "Java is cool".matches("Java.*") "Java is powerful".matches("Java.*")
Java.* in the preceding statements is a regular expression. It describes a string pattern that begins with Java followed by any zero or more characters. Here, the substring matches any zero or more characters.
The following statement evaluates to true:
"440–02–4534".matches("\\d{3}–\\d{2}–\\d{4}")
Here, \\d represents a single digit, and \\d{3} represents three digits.
replace
replaceFirst
replace
replace
split
why regular expression?
regular expression regex
matches(regex)
M10_LIAN1878_11_GE_C10.indd 412 12/29/17 4:15 PM
10.10 The String Class 413
The replaceAll, replaceFirst, and split methods can be used with a regular expression. For example, the following statement returns a new string that replaces $, +, or # in a+b$#c with the string NNN.
String s = "a+b$#c".replaceAll("[$+#]", "NNN"); System.out.println(s);
Here, the regular expression [$+#] specifies a pattern that matches $, +, or #. Thus, the output is aNNNbNNNNNNc.
The following statement splits the string into an array of strings delimited by punctuation marks.
String[] tokens = "Java,C?C#,C++".split("[.,:;?]");
for (int i = 0; i < tokens.length; i++) System.out.println(tokens[i]);
In this example, the regular expression [.,:;?] specifies a pattern that matches ., ,, :, ;, or ?. Each of these characters is a delimiter for splitting the string. Thus, the string is split into Java, C, C#, and C++, which are stored in array tokens.
Regular expression patterns are complex for beginning students to understand. For this reason, simple patterns are introduced in this section. Please refer to Appendix H, Regular Expressions, to learn more about these patterns.
10.10.5 Conversion between Strings and Arrays Strings are not arrays, but a string can be converted into an array and vice versa. To convert a string into an array of characters, use the toCharArray method. For example, the following statement converts the string Java to an array:
char[] chars = "Java".toCharArray();
Thus, chars[0] is J, chars[1] is a, chars[2] is v, and chars[3] is a. You can also use the getChars(int srcBegin, int srcEnd, char[] dst, int
dstBegin) method to copy a substring of the string from index srcBegin to index srcEnd–1 into a character array dst starting from index dstBegin. For example, the following code copies a substring "3720" in "CS3720" from index 2 to index 6–1 into the character array dst starting from index 4:
char[] dst = {'J', 'A', 'V', 'A', '1', '3', '0', '1'}; "CS3720".getChars(2, 6, dst, 4);
Thus, dst becomes {'J', 'A', 'V', 'A', '3', '7', '2', '0'}. To convert an array of characters into a string, use the String(char[]) constructor or the
valueOf(char[]) method. For example, the following statement constructs a string from an array using the String constructor:
String str = new String(new char[]{'J', 'a', 'v', 'a'});
The next statement constructs a string from an array using the valueOf method.
String str = String.valueOf(new char[]{'J', 'a', 'v', 'a'});
10.10.6 Converting Characters and Numeric Values to Strings Recall that you can use Double.parseDouble(str) or Integer.parseInt(str) to convert a string to a double value or an int value", and you can convert a character or a number into a string by using the string concatenating operator. Another way of converting a
replaceAll(regex)
split(regex)
further studies
toCharArray
getChars
valueOf
M10_LIAN1878_11_GE_C10.indd 413 12/29/17 4:15 PM
414 Chapter 10 Object-Oriented Thinking
number into a string is to use the overloaded static valueOf method. This method can also be used to convert a character or an array of characters into a string, as shown in Figure 10.17.
overloaded valueOf
Figure 10.17 The String class contains the static methods for creating strings from prim- itive-type values.
java.lang.String
+valueOf(c: char): String
+valueOf(data: char[]): String
+valueOf(d: double): String
+valueOf(f: f loat): String
+valueOf(i: int): String
+valueOf(l: long): String
Returns a string consisting of the character c.
Returns a string consisting of the characters in the array.
Returns a string representing the double value.
Returns a string representing the �oat value.
Returns a string representing the int value.
Returns a string representing the long value.
+valueOf(b: boolean): String Returns a string representing the boolean value.
For example, to convert a double value 5.44 to a string, use String.valueOf(5.44). The return value is a string consisting of the characters '5', '.', '4', and '4'.
10.10.7 Formatting Strings The String class contains the static format method to return a formatted string. The syntax to invoke this method is
String.format(format, item1, item2, ..., itemk);
This method is similar to the printf method except that the format method returns a format- ted string, whereas the printf method displays a formatted string. For example,
String s = String.format("%7.2f%6d%-4s", 45.556, 14, "AB"); System.out.println(s);
displays
45.56 14AB
where the square box ( ) denotes a blank space.
Note
System.out.printf(format, item1, item2, ..., itemk);
is equivalent to
System.out.print( String.format(format, item1, item2, ..., itemk));
10.10.1 Suppose s1, s2, s3, and s4 are four strings, given as follows:
String s1 = "Welcome to Java"; String s2 = s1; String s3 = new String("Welcome to Java"); String s4 = "Welcome to Java";
What are the results of the following expressions?
a. s1 == s2
b. s1 == s3
Point Check
M10_LIAN1878_11_GE_C10.indd 414 12/29/17 4:15 PM
10.10 The String Class 415
c. s1 == s4
d. s1.equals(s3)
e. s1.equals(s4)
f. "Welcome to Java".replace("Java", "HTML")
g. s1.replace('o', 'T')
h. s1.replaceAll("o", "T")
i. s1.replaceFirst("o", "T")
j. s1.toCharArray()
10.10.2 To create the string Welcome to Java, you may use a statement like this:
String s = "Welcome to Java";
or
String s = new String("Welcome to Java");
Which one is better? Why?
10.10.3 What is the output of the following code?
String s1 = "Welcome to Java"; String s2 = s1.replace("o", "abc"); System.out.println(s1); System.out.println(s2);
10.10.4 Let s1 be " Welcome " and s2 be " welcome ". Write the code for the fol- lowing statements:
a. Replace all occurrences of the character e with E in s1 and assign the new string to s3.
b. Split Welcome to Java and HTML into an array tokens delimited by a space and assign the first two tokens into s1 and s2.
10.10.5 Does any method in the String class change the contents of the string? 10.10.6 Suppose string s is created using new String(); what is s.length()? 10.10.7 How do you convert a char, an array of characters, or a number to a string? 10.10.8 Why does the following code cause a NullPointerException?
1 public class Test { 2 private String text; 3 4 public Test(String s) { 5 String text = s; 6 } 7 8 public static void main(String[] args) { 9 Test test = new Test("ABC"); 10 System.out.println(test.text.toLowerCase()); 11 } 12 }
10.10.9 What is wrong in the following program?
1 public class Test { 2 String text; 3
M10_LIAN1878_11_GE_C10.indd 415 12/29/17 4:15 PM
416 Chapter 10 Object-Oriented Thinking
4 public void Test(String s) { 5 text = s; 6 } 7 8 public static void main(String[] args) { 9 Test test = new Test("ABC"); 10 System.out.println(test); 11 } 12 }
10.10.10 Show the output of the following code:
public class Test { public static void main(String[] args) { System.out.println("Hi, ABC, good".matches("ABC ")); System.out.println("Hi, ABC, good".matches(".*ABC.*")); System.out.println("A,B;C".replaceAll(",;", "#")); System.out.println("A,B;C".replaceAll("[,;]", "#"));
String[] tokens = "A,B;C".split("[,;]"); for (int i = 0; i < tokens.length; i++) System.out.print(tokens[i] + " "); } }
10.10.11 Show the output of the following code:
public class Test { public static void main(String[] args) { String s = "Hi, Good Morning"; System.out.println(m(s)); }
public static int m(String s) { int count = 0; for (int i = 0; i < s.length(); i++) if (Character.isUpperCase(s.charAt(i))) count++;
return count; } }
10.11 The StringBuilder and StringBuffer Classes The StringBuilder and StringBuffer classes are similar to the String class except that the String class is immutable.
In general, the StringBuilder and StringBuffer classes can be used wherever a string is used. StringBuilder and StringBuffer are more flexible than String. You can add, insert, or append new contents into StringBuilder and StringBuffer objects, whereas the value of a String object is fixed once the string is created.
The StringBuilder class is similar to StringBuffer except that the methods for modi- fying the buffer in StringBuffer are synchronized, which means that only one task is allowed to execute the methods. Use StringBuffer if the class might be accessed by multiple tasks concurrently, because synchronization is needed in this case to prevent corruptions to
Point Key
StringBuilder
M10_LIAN1878_11_GE_C10.indd 416 12/29/17 4:15 PM
10.11 The StringBuilder and StringBuffer Classes 417
StringBuffer. Concurrent programming will be introduced in Chapter 32. Using String- Builder is more efficient if it is accessed by just a single task, because no synchronization is needed in this case. The constructors and methods in StringBuffer and StringBuilder are almost the same. This section covers StringBuilder. You can replace StringBuilder in all occurrences in this section by StringBuffer. The program can compile and run without any other changes.
The StringBuilder class has three constructors and more than 30 methods for managing the builder and modifying strings in the builder. You can create an empty string builder or a string builder from a string using the constructors, as shown in Figure 10.18.
StringBuilder constructors
Figure 10.18 The StringBuilder class contains the constructors for creating instances of StringBuilder.
java.lang.StringBuilder
+StringBuilder()
+StringBuilder(capacity: int)
+StringBuilder(s: String)
Constructs an empty string builder with capacity 16.
Constructs a string builder with the speci�ed capacity.
Constructs a string builder with the speci�ed string.
10.11.1 Modifying Strings in the StringBuilder You can append new contents at the end of a string builder, insert new contents at a speci- fied position in a string builder, and delete or replace characters in a string builder, using the methods listed in Figure 10.19.
Figure 10.19 The StringBuilder class contains the methods for modifying string builders.
java.lang.StringBuilder
+append(data: char[]): StringBuilder
+append(data: char[], o�set: int, len: int): StringBuilder
+append(v: aPrimitiveType): StringBuilder
+append(s: String): StringBuilder
+delete(startIndex: int, endIndex: int): StringBuilder
+deleteCharAt(index: int): StringBuilder
+insert(index: int, data: char[], o�set: int, len: int): StringBuilder
+insert(o�set: int, data: char[]): StringBuilder
+insert(o�set: int, b: aPrimitiveType): StringBuilder
+insert(o�set: int, s: String): StringBuilder
+replace(startIndex: int, endIndex: int, s: String): StringBuilder
+reverse(): StringBuilder
+setCharAt(index: int, ch: char): void
Appends a char array into this string builder. Appends a subarray in data into this string builder.
Appends a primitive-type value as a string to this builder.
Appends a string to this string builder.
Deletes characters from startIndex to endIndex–1.
Deletes a character at the speci�ed index.
Inserts a subarray of the data in the array into the builder at the speci�ed index.
Inserts data into this builder at the position offset.
Inserts a value converted to a string into this builder.
Inserts a string into this builder at the position offset.
Replaces the characters in this builder from startIndex to endIndex – 1 with the speci�ed string.
Reverses the characters in the builder.
Sets a new character at the speci�ed index in this builder.
M10_LIAN1878_11_GE_C10.indd 417 12/29/17 4:15 PM
418 Chapter 10 Object-Oriented Thinking
The StringBuilder class provides several overloaded methods to append boolean, char, char[], double, float, int, long, and String into a string builder. For example, the following code appends strings and characters into stringBuilder to form a new string, Welcome to Java:
StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Welcome"); stringBuilder.append(' '); stringBuilder.append("to"); stringBuilder.append(' '); stringBuilder.append("Java");
The StringBuilder class also contains overloaded methods to insert boolean, char, char array, double, float, int, long, and String into a string builder. Consider the following code:
stringBuilder.insert(11, "HTML and ");
Suppose stringBuilder contains Welcome to Java before the insert method is applied. This code inserts "HTML and " at position 11 in stringBuilder (just before the J). The new stringBuilder is Welcome to HTML and Java.
You can also delete characters from a string in the builder using the two delete methods, reverse the string using the reverse method, replace characters using the replace method, or set a new character in a string using the setCharAt method.
For example, suppose stringBuilder contains Welcome to Java before each of the following methods is applied:
stringBuilder.delete(8, 11) changes the builder to Welcome Java. stringBuilder.deleteCharAt(8) changes the builder to Welcome o Java. stringBuilder.reverse() changes the builder to avaJ ot emocleW. stringBuilder.replace(11, 15, "HTML") changes the builder to Welcome to HTML. stringBuilder.setCharAt(0, 'w') sets the builder to welcome to Java.
All these modification methods except setCharAt do two things:
1. Change the contents of the string builder
2. Return the reference of the string builder
For example, the following statement:
StringBuilder stringBuilder1 = stringBuilder.reverse();
reverses the string in the builder and assigns the builder’s reference to stringBuilder1. Thus, stringBuilder and stringBuilder1 both point to the same StringBuilder object. Recall that a value-returning method can be invoked as a statement, if you are not interested in the return value of the method. In this case, the return value is simply ignored. For example, in the following statement:
stringBuilder.reverse():
the return value is ignored. Returning the reference of a StringBuilder enables the StringBuilder methods to be invoked in a chain such as the following:
stringBuilder.reverse().delete(8, 11).replace(11, 15, "HTML");
Tip If a string does not require any change, use String rather than StringBuilder. String is more efficient than StringBuilder.
append
insert
delete
deleteCharAt
reverse
replace
setCharAt
ignore return value
String or StringBuilder?
M10_LIAN1878_11_GE_C10.indd 418 12/29/17 4:15 PM
10.11 The StringBuilder and StringBuffer Classes 419
10.11.2 The toString, capacity, length, setLength, and charAt Methods
The StringBuilder class provides the additional methods for manipulating a string builder and obtaining its properties, as shown in Figure 10.20.
Figure 10.20 The StringBuilder class contains the methods for modifying string builders.
java.lang.StringBuilder
+toString(): String
+capacity(): int
+charAt(index: int): char
+length(): int
+setLength(newLength: int): void
+substring(startIndex: int): String
+substring(startIndex: int, endIndex: int): String
+trimToSize(): void
Returns a string object from the string builder.
Returns the capacity of this string builder.
Returns the character at the speci�ed index.
Returns the number of characters in this builder.
Sets a new length in this builder.
Returns a substring starting at startIndex.
Returns a substring from startIndex to endIndex – 1.
Reduces the storage size used for the string builder.
The capacity() method returns the current capacity of the string builder. The capacity is the number of characters the string builder is able to store without having to increase its size.
The length() method returns the number of characters actually stored in the string builder. The setLength(newLength) method sets the length of the string builder. If the newLength argument is less than the current length of the string builder, the string builder is truncated to contain exactly the number of characters given by the newLength argument. If the newLength argument is greater than or equal to the current length, sufficient null characters (\u0000) are appended to the string builder so length becomes the newLength argument. The newLength argument must be greater than or equal to 0.
The charAt(index) method returns the character at a specific index in the string builder. The index is 0 based. The first character of a string builder is at index 0, the next at index 1, and so on. The index argument must be greater than or equal to 0, and less than the length of the string builder.
Note The length of the string builder is always less than or equal to the capacity of the builder. The length is the actual size of the string stored in the builder, and the capacity is the current size of the builder. The builder’s capacity is automatically increased if more characters are added to exceed its capacity. Internally, a string builder is an array of characters, so the builder’s capacity is the size of the array. If the builder’s capacity is exceeded, the array is replaced by a new array. The new array size is 2 * (the previ- ous array size + 1).
Tip You can use new StringBuilder(initialCapacity) to create a String- Builder with a specified initial capacity. By carefully choosing the initial capacity, you can make your program more efficient. If the capacity is always larger than the actual length of the builder, the JVM will never need to reallocate memory for the builder. On the other hand, if the capacity is too large, you will waste memory space. You can use the trimToSize() method to reduce the capacity to the actual size.
capacity()
length()
setLength(int)
charAt(int)
length and capacity
initial capacity
trimToSize()
M10_LIAN1878_11_GE_C10.indd 419 12/29/17 4:15 PM
420 Chapter 10 Object-Oriented Thinking
10.11.3 Case Study: Ignoring Nonalphanumeric Characters When Checking Palindromes
Listing 5.14, Palindrome.java, considered all the characters in a string to check whether it is a palindrome. Write a new program that ignores nonalphanumeric characters in checking whether a string is a palindrome.
Here are the steps to solve the problem:
1. Filter the string by removing the nonalphanumeric characters. This can be done by creating an empty string builder, adding each alphanumeric character in the string to a string builder, and returning the string from the string builder. You can use the isLetterOrDigit(ch) method in the Character class to check whether character ch is a letter or a digit.
2. Obtain a new string that is the reversal of the filtered string. Compare the reversed string with the filtered string using the equals method.
The complete program is shown in Listing 10.10.
Listing 10.10 PalindromeIgnoreNonAlphanumeric.java 1 import java.util.Scanner; 2 3 public class PalindromeIgnoreNonAlphanumeric { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Create a Scanner 7 Scanner input = new Scanner(System.in); 8 9 // Prompt the user to enter a string 10 System.out.print("Enter a string: "); 11 String s = input.nextLine(); 12 13 // Display result 14 System.out.println("Ignoring nonalphanumeric characters, \nis " 15 + s + " a palindrome? " + isPalindrome(s)); 16 } 17 18 /** Return true if a string is a palindrome */ 19 public static boolean isPalindrome(String s) { 20 // Create a new string by eliminating nonalphanumeric chars 21 String s1 = filter(s); 22 23 // Create a new string that is the reversal of s1 24 String s2 = reverse(s1); 25 26 // Check if the reversal is the same as the original string 27 return s2.equals(s1); 28 } 29 30 /** Create a new string by eliminating nonalphanumeric chars */ 31 public static String filter(String s) { 32 // Create a string builder 33 StringBuilder stringBuilder = new StringBuilder(); 34 35 // Examine each char in the string to skip alphanumeric char 36 for (int i = 0; i < s.length(); i++) { 37 if (Character.isLetterOrDigit(s.charAt(i))) { 38 stringBuilder.append(s.charAt(i)); 39 }
add letter or digit
check palindrome
M10_LIAN1878_11_GE_C10.indd 420 12/29/17 4:15 PM
10.11 The StringBuilder and StringBuffer Classes 421
40 } 41 42 // Return a new filtered string 43 return stringBuilder.toString(); 44 } 45 46 /** Create a new string by reversing a specified string */ 47 public static String reverse(String s) { 48 StringBuilder stringBuilder = new StringBuilder(s); 49 stringBuilder.reverse(); // Invoke reverse in StringBuilder 50 return stringBuilder.toString(); 51 } 52 }
Enter a string: ab<c>cb?a
Ignoring nonalphanumeric characters,
is ab<c>cb?a a palindrome? true
Enter a string: abcc><?cab
Ignoring nonalphanumeric characters,
is abcc><?cab a palindrome? false
The filter(String s) method (lines 31–44) examines each character in string s and cop- ies it to a string builder if the character is a letter or a numeric character. The filter method returns the string in the builder. The reverse(String s) method (lines 47–51) creates a new string that reverses the specified string s. The filter and reverse methods both return a new string. The original string is not changed.
The program in Listing 5.14 checks whether a string is a palindrome by comparing pairs of characters from both ends of the string. Listing 10.10 uses the reverse method in the StringBuilder class to reverse the string, then compares whether the two strings are equal to determine whether the original string is a palindrome.
10.11.1 What is the difference between StringBuilder and StringBuffer? 10.11.2 How do you create a string builder from a string? How do you return a string
from a string builder?
10.11.3 Write three statements to reverse a string s using the reverse method in the StringBuilder class.
10.11.4 Write three statements to delete a substring from a string s of 20 characters, starting at index 4 and ending with index 10. Use the delete method in the StringBuilder class.
10.11.5 What is the internal storage for characters in a string and a string builder? 10.11.6 Suppose s1 and s2 are given as follows:
StringBuilder s1 = new StringBuilder("Java"); StringBuilder s2 = new StringBuilder("HTML");
Show the value of s1 after each of the following statements. Assume the statements are independent.
a. s1.append(" is fun");
b. s1.append(s2);
Point Check
M10_LIAN1878_11_GE_C10.indd 421 12/29/17 4:15 PM
422 Chapter 10 Object-Oriented Thinking
c. s1.insert(2, "is fun");
d. s1.insert(1, s2);
e. s1.charAt(2);
f. s1.length();
g. s1.deleteCharAt(3);
h. s1.delete(1, 3);
i. s1.reverse();
j. s1.replace(1, 3, "Computer");
k. s1.substring(1, 3);
l. s1.substring(2);
10.11.7 Show the output of the following program:
public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder builder = new StringBuilder(s); change(s, builder);
System.out.println(s); System.out.println(builder); }
private static void change(String s, StringBuilder builder) { s = s + " and HTML"; builder.append(" and HTML"); } }
Key Terms abstract data type (ADT) 390 aggregation 398 boxing 407 class abstraction 390 class encapsulation 390 class’s contract 390
composition 398 has-a relationship 398 multiplicity 397 stack 402 unboxing 407
ChapTer summary 1. The procedural paradigm focuses on designing methods. The object-oriented paradigm
couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects.
2. Many Java methods require the use of objects as arguments. Java offers a convenient way to incorporate, or wrap, a primitive data type into an object (e.g., wrapping int into the Integer class, and wrapping double into the Double class).
M10_LIAN1878_11_GE_C10.indd 422 12/29/17 4:15 PM
3. Java can automatically convert a primitive-type value to its corresponding wrapper object in the context and vice versa.
4. The BigInteger class is useful for computing and processing integers of any size. The BigDecimal class can be used to compute and process floating-point numbers with any arbitrary precision.
5. A String object is immutable; its contents cannot be changed. To improve efficiency and save memory, the JVM stores two literal strings that have the same character sequence in a unique object. This unique object is called an interned string object.
6. A regular expression (abbreviated regex) is a string that describes a pattern for matching a set of strings. You can match, replace, or split a string by specifying a pattern.
7. The StringBuilder and StringBuffer classes can be used to replace the String class. The String object is immutable, but you can add, insert, or append new contents into StringBuilder and StringBuffer objects. Use String if the string contents do not require any change and use StringBuilder or StringBuffer if they might change.
Quiz Answer the quiz for this chapter online at the book Companion Website.
programming exerCises
Sections 10.2 and 10.3 *10.1 (The Time class) Design a class named Time. The class contains:
■■ The data fields hour, minute, and second that represent a time. ■■ A no-arg constructor that creates a Time object for the current time. (The
values of the data fields will represent the current time.) ■■ A constructor that constructs a Time object with a specified elapsed time
since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.)
■■ A constructor that constructs a Time object with the specified hour, minute, and second.
■■ Three getter methods for the data fields hour, minute, and second, respectively.
■■ A method named setTime(long elapseTime) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10.
Draw the UML diagram for the class then implement the class. Write a test program that creates three Time objects (using new Time(), new Time(555550000), and new Time(5, 23, 55)) and displays their hour, minute, and second in the format hour:minute:second.
(Hint: The first two constructors will extract the hour, minute, and second from the elapsed time. For the no-arg constructor, the current time can be obtained using System.currentTimeMillis(), as shown in Listing 2.7, Show CurrentTime.java. Assume the time is in GMT.)
Programming Exercises 423
M10_LIAN1878_11_GE_C10.indd 423 12/29/17 4:15 PM
424 Chapter 10 Object-Oriented Thinking
10.2 (The BMI class) Add the following new constructor in the BMI class:
/** Construct a BMI with the specified name, age, weight, * feet, and inches */ public BMI(String name, int age, double weight, double feet, double inches)
10.3 (The MyInteger class) Design a class named MyInteger. The class contains: ■■ An int data field named value that stores the int value represented by
this object. ■■ A constructor that creates a MyInteger object for the specified int value. ■■ A getter method that returns the int value. ■■ The methods isEven(), isOdd(), and isPrime() that return true if the
value in this object is even, odd, or prime, respectively. ■■ The static methods isEven(int), isOdd(int), and isPrime(int) that
return true if the specified value is even, odd, or prime, respectively. ■■ The static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
■■ The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value.
■■ A static method parseInt(char[]) that converts an array of numeric characters to an int value.
■■ A static method parseInt(String) that converts a string into an int value.
Draw the UML diagram for the class then implement the class. Write a client program that tests all methods in the class.
10.4 (The MyPoint class) Design a class named MyPoint to represent a point with x- and y-coordinates. The class contains:
■■ The data fields x and y that represent the coordinates with getter methods. ■■ A no-arg constructor that creates a point (0, 0). ■■ A constructor that constructs a point with specified coordinates. ■■ A method named distance that returns the distance from this point to a
specified point of the MyPoint type. ■■ A method named distance that returns the distance from this point to
another point with specified x- and y-coordinates. ■■ A static method named distance that returns the distance from two MyPoint
objects.
Draw the UML diagram for the class then implement the class. Write a test program that creates the two points (0, 0) and (10, 30.5) and displays the dis- tance between them.
Sections 10.4–10.8 *10.5 (Display the prime factors) Write a program that prompts the user to enter a
positive integer and displays all its smallest factors in decreasing order. For example, if the integer is 120, the smallest factors are displayed as 5, 3, 2, 2, 2. Use the StackOfIntegers class to store the factors (e.g., 2, 2, 2, 3, 5) and retrieve and display them in reverse order.
*10.6 (Display the prime numbers) Write a program that displays all the prime num- bers less than 120 in decreasing order. Use the StackOfIntegers class to store the prime numbers (e.g., 2, 3, 5, . . . ) and retrieve and display them in reverse order.
VideoNote
The MyPoint class
M10_LIAN1878_11_GE_C10.indd 424 12/29/17 4:15 PM
**10.7 (Game: ATM machine) Use the Account class created in Programming Exer- cise 9.7 to simulate an ATM machine. Create 10 accounts in an array with id 0, 1, . . . , 9, and an initial balance of $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.
Enter an id: 4
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 100.0
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 2 Enter an amount to withdraw: 3
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 97.0
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 3 Enter an amount to deposit: 10
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 107.0
Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 4
Enter an id:
Programming Exercises 425
M10_LIAN1878_11_GE_C10.indd 425 12/29/17 4:15 PM
426 Chapter 10 Object-Oriented Thinking
***10.8 (Financial: the Tax class) Programming Exercise 8.12 writes a program for computing taxes using arrays. Design a class named Tax to contain the follow- ing instance data fields:
■■ int filingStatus: One of the four tax-filing statuses: 0—single filer, 1—married filing jointly or qualifying widow(er), 2—married filing separately, and 3—head of household. Use the public static constants SINGLE_FILER (0), MARRIED_JOINTLY_OR_QUALIFYING_WIDOW(ER) (1), MARRIED_ SEPARATELY (2), HEAD_OF_HOUSEHOLD (3) to represent the statuses.
■■ int[][] brackets: Stores the tax brackets for each filing status. ■■ double[] rates: Stores the tax rates for each bracket. ■■ double taxableIncome: Stores the taxable income.
Provide the getter and setter methods for each data field and the getTax() method that returns the tax. Also, provide a no-arg constructor and the construc- tor Tax(filingStatus, brackets, rates, taxableIncome).
Draw the UML diagram for the class and then implement the class. Write a test program that uses the Tax class to print the 2001 and 2009 tax tables for taxable income from $50,000 to $60,000 with intervals of $1,000 for all four statuses. The tax rates for the year 2009 were given in Table 3.2. The tax rates for 2001 are shown in Table 10.1.
Tax Rate Single Filers Married—Filing Jointly or Qualifying Widow(er)
Married—Filing Separately Head of Household
15% Up to $27,050 Up to $45,200 Up to $22,600 Up to $36,250
27.5% $27,051–$65,550 $45,201–$109,250 $22,601–$54,625 $36,251–$93,650
30.5% $65,551–$136,750 $109,251–$166,500 $54,626–$83,250 $93,651–$151,650
35.5% $136,751–$297,350 $166,501–$297,350 $83,251–$148,675 $151,651–$297,350
39.1% $297,351 or more $297,351 or more $ 148,676 or more $297,351 or more
tabLe 10.1 2001 U.S. Federal Personal Tax Rates
**10.9 (The Course class) Revise the Course class as follows: ■■ Revise the getStudents() method to return an array whose length is the
same as the number of students in the course. (Hint: create a new array and copy students to it.)
■■ The array size is fixed in Listing 10.6. Revise the addStudent method to automatically increase the array size if there is no room to add more students. This is done by creating a new larger array and copying the contents of the current array to it.
■■ Implement the dropStudent method. ■■ Add a new method named clear() that removes all students from the course.
Write a test program that creates a course, adds three students, removes one, and displays the students in the course.
*10.10 (The Queue class) Section 10.6 gives a class for Stack. Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retrieved in a last-in first-out fashion. In a queue, the elements are retrieved in a first-in first-out fashion. The class contains:
■■ An int[] data field named elements that stores the int values in the queue. ■■ A data field named size that stores the number of elements in the queue. ■■ A constructor that creates a Queue object with default capacity 8. ■■ The method enqueue(int v) that adds v into the queue.
M10_LIAN1878_11_GE_C10.indd 426 12/29/17 4:15 PM
■■ The method dequeue() that removes and returns the element from the queue.
■■ The method empty() that returns true if the queue is empty. ■■ The method getSize() that returns the size of the queue.
Draw an UML diagram for the class. Implement the class with the initial array size set to 8. The array size will be doubled once the number of the elements exceeds the size. After an element is removed from the beginning of the array, you need to shift all elements in the array one position the left. Write a test program that adds 20 numbers from 1 to 20 into the queue then removes these numbers and displays them.
*10.11 (Geometry: the Circle2D class) Define the Circle2D class that contains: ■■ Two double data fields named x and y that specify the center of the circle
with getter methods. ■■ A data field radius with a getter method. ■■ A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1
for radius. ■■ A constructor that creates a circle with the specified x, y, and radius. ■■ A method getArea() that returns the area of the circle. ■■ A method getPerimeter() that returns the perimeter of the circle. ■■ A method contains(double x, double y) that returns true if the
specified point (x, y) is inside this circle (see Figure 10.21a). ■■ A method contains(Circle2D circle) that returns true if the specified
circle is inside this circle (see Figure 10.21b). ■■ A method overlaps(Circle2D circle) that returns true if the specified
circle overlaps with this circle (see Figure 10.21c).
Figure 10.21 (a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another circle.
(a) (b) (c)
p
Draw the UML diagram for the class then implement the class. Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)), displays its area and perimeter, and displays the result of c1.contains(3, 3), c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)).
***10.12 (Geometry: the Triangle2D class) Define the Triangle2D class that contains: ■■ Three points named p1, p2, and p3 of the type MyPoint with getter and
setter methods. MyPoint is defined in Programming Exercise 10.4. ■■ A no-arg constructor that creates a default triangle with the points (0, 0),
(1, 1), and (2, 5). ■■ A constructor that creates a triangle with the specified points. ■■ A method getArea() that returns the area of the triangle. ■■ A method getPerimeter() that returns the perimeter of the triangle. ■■ A method contains(MyPoint p) that returns true if the specified point p is inside this triangle (see Figure 10.22a).
Programming Exercises 427
M10_LIAN1878_11_GE_C10.indd 427 12/29/17 4:15 PM
428 Chapter 10 Object-Oriented Thinking
■■ A method contains(Triangle2D t) that returns true if the specified triangle is inside this triangle (see Figure 10.22b).
■■ A method overlaps(Triangle2D t) that returns true if the specified triangle overlaps with this triangle (see Figure 10.22c).
Figure 10.22 (a) A point is inside the triangle. (b) A triangle is inside another triangle. (c) A triangle overlaps another triangle.
(a) (b) (c)
p
Draw the UML diagram for the class and then implement the class. Write a test program that creates a Triangle2D object t1 using the constructor new Triangle2D(new MyPoint(2.5, 2), new MyPoint(4.2, 3), new MyPoint(5, 3.5)), displays its area and perimeter, and displays the result of t1.contains(3, 3), r1.contains(new Triangle2D(new MyPoint(2.9, 2), new MyPoint(4, 1), MyPoint(1, 3.4))), and t1 .overlaps(new Triangle2D(new MyPoint(2, 5.5), new MyPoint (4, –3), MyPoint(2, 6.5))).
(Hint: For the formula to compute the area of a triangle, see Programming Exer- cise 2.19. To detect whether a point is inside a triangle, draw three dashed lines, as shown in Figure 10.23. If the point is inside a triangle, each dashed line should intersect a side only once. If a dashed line intersects a side twice, then the point must be outside the triangle. For the algorithm of finding the intersecting point of two lines, see Programming Exercise 3.25.)
Figure 10.23 (a) A point is inside the triangle. (b) A point is outside the triangle.
(a) (b)
p
p
*10.13 (Geometry: the MyRectangle2D class) Define the MyRectangle2D class that contains:
■■ Two double data fields named x and y that specify the center of the rectangle with getter and setter methods. (Assume the rectangle sides are parallel to x- or y-axis.)
■■ The data fields width and height with getter and setter methods. ■■ A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and 1 for both width and height.
■■ A constructor that creates a rectangle with the specified x, y, width, and height.
M10_LIAN1878_11_GE_C10.indd 428 12/29/17 4:15 PM
■■ A method getArea() that returns the area of the rectangle. ■■ A method getPerimeter() that returns the perimeter of the rectangle. ■■ A method contains(double x, double y) that returns true if the
specified point (x, y) is inside this rectangle (see Figure 10.24a). ■■ A method contains(MyRectangle2D r) that returns true if the specified
rectangle is inside this rectangle (see Figure 10.24b). ■■ A method overlaps(MyRectangle2D r) that returns true if the specified
rectangle overlaps with this rectangle (see Figure 10.24c).
Figure 10.24 A point is inside the rectangle. (b) A rectangle is inside another rectangle. (c) A rectangle overlaps another rectangle. (d) Points are enclosed inside a rectangle.
(a) (b) (c) (d)
p
Draw the UML diagram for the class then implement the class. Write a test program that creates a MyRectangle2D object r1 (new MyRectangle2D (2, 2, 5.5, 4.9)), displays its area and perimeter, and displays the result of r1.contains(3, 3), r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)), and r1.overlaps(new MyRectangle2D(3, 5, 2.3, 5.4)).
*10.14 (The MyDate class) Design a class named MyDate. The class contains:
■■ The data fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January.
■■ A no-arg constructor that creates a MyDate object for the current date. ■■ A constructor that constructs a MyDate object with a specified elapsed time
since midnight, January 1, 1970, in milliseconds. ■■ A constructor that constructs a MyDate object with the specified year, month,
and day. ■■ Three getter methods for the data fields year, month, and day, respectively. ■■ A method named setDate(long elapsedTime) that sets a new date for
the object using the elapsed time.
Draw the UML diagram for the class then implement the class. Write a test program that creates two MyDate objects (using new MyDate() and new MyDate(34355555133101L)) and displays their year, month, and day.
(Hint: The first two constructors will extract the year, month, and day from the elapsed time. For example, if the elapsed time is 561555550000 milliseconds, the year is 1987, the month is 9, and the day is 18. You may use the GregorianCalendar class discussed in Programming Exercise 9.5 to simplify coding.)
*10.15 (Geometry: the bounding rectangle) A bounding rectangle is the minimum rec- tangle that encloses a set of points in a two-dimensional plane, as shown in Figure 10.24d. Write a method that returns a bounding rectangle for a set of points in a two-dimensional plane, as follows:
public static MyRectangle2D getRectangle(double[][] points)
Programming Exercises 429
M10_LIAN1878_11_GE_C10.indd 429 12/29/17 4:15 PM
430 Chapter 10 Object-Oriented Thinking
The Rectangle2D class is defined in Programming Exercise 10.13. Write a test program that prompts the user to enter five points and displays the bounding rectangle’s center, width, and height. Here is a sample run:
Enter five points: 1.0 2.5 3 4 5 6 7 8 9 10
The bounding rectangle's center (5.0, 6.25), width 8.0, height 7.5
Section 10.9 *10.16 (Divisible by 2 or 3) Find the first 10 numbers with 50 decimal digits that are
divisible by 2 or 3.
*10.17 (Square numbers) Find the first 10 square numbers that are greater than Long. MAX_VALUE. A square number is a number in the form of n2. For example, 4, 9, and 16 are square numbers. Find an efficient approach to run your program fast.
*10.18 (Large prime numbers) Write a program that finds five prime numbers larger than Long.MAX_VALUE.
*10.19 (Mersenne prime) A prime number is called a Mersenne prime if it can be writ- ten in the form 2p - 1 for some positive integer p. Write a program that finds all Mersenne primes with p … 100 and displays the output as shown below. (Hint: You have to use BigInteger to store the number because it is too big to be stored in long. Your program may take several hours to run.)
p 2^p – 1 ---------------------
2 3 3 7 5 31 ...
*10.20 (Approximate e) Programming Exercise 5.26 approximates e using the following series:
e = 1 + 1 1!
+ 1 2!
+ 1 3!
+ 1 4!
+ g + 1 i!
In order to get better precision, use BigDecimal with 25 digits of precision in the computation. Write a program that displays the e value for i = 100, 200, . . . , and 1000.
10.21 (Divisible by 5 or 6) Find the first 10 numbers greater than Long.MAX_VALUE that are divisible by 5 or 6.
Sections 10.10 and 10.11 **10.22 (Implement the String class) The String class is provided in the Java library.
Provide your own implementation for the following methods (name the new class MyString1):
public MyString1(char[] chars); public char charAt(int index); public int length(); public MyString1 substring(int begin, int end); public MyString1 toLowerCase(); public boolean equals(MyString1 s); public static MyString1 valueOf(int i);
M10_LIAN1878_11_GE_C10.indd 430 12/29/17 4:15 PM
**10.23 (Implement the String class) The String class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyString2):
public MyString2(String s); public int compare(String s); public MyString2 substring(int begin); public MyString2 toUpperCase(); public char[] toChars(); public static MyString2 valueOf(boolean b);
10.24 (Implement the Character class) The Character class is provided in the Java library. Provide your own implementation for this class. Name the new class MyCharacter.
**10.25 (New string split method) The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters.
public static String[] split(String s, String regex)
For example, split("ab#12#453", "#") returns ab, #, 12, #, and 453 in an array of String and split("a?b?gf#e", "[?#]") returns a, ?, b, ?, gf, #, and e in an array of String.
*10.26 (Calculator) Revise Listing 7.9, Calculator.java, to accept an expression as a string in which the operands and operator are separated by zero or more spaces. For example, 3+4 and 3 + 4 are acceptable expressions. Here is a sample run:
**10.27 (Implement the StringBuilder class) The StringBuilder class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyStringBuilder1):
public MyStringBuilder1(String s); public MyStringBuilder1 append(MyStringBuilder1 s); public MyStringBuilder1 append(int i); public int length(); public char charAt(int index); public MyStringBuilder1 toLowerCase(); public MyStringBuilder1 substring(int begin, int end); public String toString();
Programming Exercises 431
M10_LIAN1878_11_GE_C10.indd 431 12/29/17 4:15 PM
432 Chapter 10 Object-Oriented Thinking
**10.28 (Implement the StringBuilder class) The StringBuilder class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyStringBuilder2):
public MyStringBuilder2(); public MyStringBuilder2(char[] chars); public MyStringBuilder2(String s); public MyStringBuilder2 insert(int offset, MyStringBuilder2 s); public MyStringBuilder2 reverse(); public MyStringBuilder2 substring(int begin); public MyStringBuilder2 toUpperCase();
M10_LIAN1878_11_GE_C10.indd 432 12/29/17 4:15 PM
Objectives ■■ To define a subclass from a superclass through inheritance (§11.2).
■■ To invoke the superclass’s constructors and methods using the super keyword (§11.3).
■■ To override instance methods in the subclass (§11.4).
■■ To distinguish differences between overriding and overloading (§11.5).
■■ To explore the toString() method in the Object class (§11.6).
■■ To discover polymorphism and dynamic binding (§§11.7 and 11.8).
■■ To describe casting and explain why explicit downcasting is necessary (§11.9).
■■ To explore the equals method in the Object class (§11.10).
■■ To store, retrieve, and manipulate objects in an ArrayList (§11.11).
■■ To construct an array list from an array, to sort and shuffle a list, and to obtain max and min element from a list (§11.12).
■■ To implement a Stack class using ArrayList (§11.13).
■■ To enable data and methods in a superclass accessible from subclasses using the protected visibility modifier (§11.14).
■■ To prevent class extending and method overriding using the final modifier (§11.15).
Inheritance and Polymorphism
Chapter
11
M11_LIAN1878_11_GE_C11.indd 433 12/29/17 4:18 PM
434 Chapter 11 Inheritance and Polymorphism
11.1 Introduction Object-oriented programming allows you to define new classes from existing classes. This is called inheritance.
As discussed in the preceding chapter, the procedural paradigm focuses on designing methods, and the object-oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimen- sion that integrates data with operations into objects.
Inheritance is an important and powerful feature for reusing software. Suppose you need to define classes to model circles, rectangles, and triangles. These classes have many common fea- tures. What is the best way to design these classes so as to avoid redundancy and make the system easy to comprehend and easy to maintain? The answer is to use inheritance.
11.2 Superclasses and Subclasses Inheritance enables you to define a general class (i.e., a superclass) and later extend it to more specialized classes (i.e., subclasses).
You use a class to model objects of the same type. Different classes may have some common properties and behaviors, which can be generalized in a class that can be shared by other classes. You can define a specialized class that extends the generalized class. The specialized classes inherit the properties and methods from the general class.
Consider geometric objects. Suppose you want to design the classes to model geometric objects such as circles and rectangles. Geometric objects have many common properties and behaviors. They can be drawn in a certain color and be filled or unfilled. Thus, a general class GeometricObject can be used to model all geometric objects. This class contains the proper- ties color and filled and their appropriate getter and setter methods. Assume this class also contains the dateCreated property, and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Since a circle is a spe- cial type of geometric object, it shares common properties and methods with other geometric objects. Thus, it makes sense to define the Circle class that extends the GeometricObject class. Likewise, Rectangle can also be defined as a special type of GeometricObject. Figure 11.1 shows the relationship among these classes. A triangular arrow pointing to the gen- eralized class is used to denote the inheritance relationship between the two classes involved.
In Java terminology, a class C1 extended from another class C2 is called a subclass, and C2 is called a superclass. A superclass is also referred to as a parent class or a base class, and a subclass as a child class, an extended class, or a derived class. A subclass inherits accessible data fields and methods from its superclass and may also add new data fields and methods. Therefore, Circle and Rectangle are subclasses of GeometricObject, and GeometricObject is the superclass for Circle and Rectangle. A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype. Therefore, you can say that Circle is a subtype of GeometricObject, and GeometricObject is a supertype for Circle.
The subclass and its superclass are said to form a is-a relationship. A Circle object is a special type of general GeometricObject. The Circle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has a new data field, radius, and its associated getter and setter methods. The Circle class also contains the getArea(), getPerimeter(), and getDiameter() methods for returning the area, perimeter, and diam- eter of the circle.
The Rectangle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has the data fields width and height and their associated getter and setter methods. It also contains the getArea() and getPerimeter()
Point Key
inheritance
why inheritance?
Point Key
VideoNote
Geometric class hierarchy
subclass
superclass
subtype supertype
is-a relationship
width and height
M11_LIAN1878_11_GE_C11.indd 434 12/29/17 4:18 PM
11.2 Superclasses and Subclasses 435
methods for returning the area and perimeter of the rectangle. Note that you may have used the terms width and length to describe the sides of a rectangle in geometry. The common terms used in computer science are width and height, where width refers to the horizontal length, and height to the vertical length.
The GeometricObject, Circle, and Rectangle classes are shown in Listings 11.1, 11.2, and 11.3, respectively.
lisTing 11.1 GeometricObject.java 1 public class GeometricObject { 2 private String color = "white"; 3 private boolean filled; 4 private java.util.Date dateCreated; 5 6 /** Construct a default geometric object */ 7 public GeometricObject() { 8 dateCreated = new java.util.Date(); 9 } 10
data fields
constructor date constructed
Figure 11.1 The GeometricObject class is the superclass for Circle and Rectangle.
The color of the object (default: white).
Indicates whether the object is �lled with a color (default: false).
The date when the object was created.
Creates a GeometricObject.
Returns the color.
Sets a new color.
Returns the filled property.
Sets a new filled property.
Returns the dateCreated.
Returns a string representation of this object.
GeometricObject
–color: String
–filled: boolean
–dateCreated: java.util.Date
+GeometricObject()
Creates a GeometricObject with the speci�ed color and �lled values.
+GeometricObject(color: String, filled: boolean) +getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): java.util.Date
+toString(): String
Circle
–radius: double
+Circle()
+Circle(radius: double)
+Circle(radius: double, color: String, filled: boolean)
+getRadius(): double
+getArea(): double
+setRadius(radius: double): void
+printCircle(): void
+getPerimeter(): double
+getDiameter(): double
Rectangle
–height: double
+Rectangle()
+Rectangle(width: double, height: double)
+Rectangle(width: double, height: double color: String, filled: boolean)
+getWidth(): double
+getHeight(): double
+setHeight(height: double): void
+setWidth(width: double): void
+getArea(): double
+getPerimeter(): double
–width: double
M11_LIAN1878_11_GE_C11.indd 435 12/29/17 4:18 PM
436 Chapter 11 Inheritance and Polymorphism
11 /** Construct a geometric object with the specified color 12 * and filled value */ 13 public GeometricObject(String color, boolean filled) { 14 dateCreated = new java.util.Date(); 15 this.color = color; 16 this.filled = filled; 17 } 18 19 /** Return color */ 20 public String getColor() { 21 return color; 22 } 23 24 /** Set a new color */ 25 public void setColor(String color) { 26 this.color = color; 27 } 28 29 /** Return filled. Since filled is boolean, 30 its getter method is named isFilled */ 31 public boolean isFilled() { 32 return filled; 33 } 34 35 /** Set a new filled */ 36 public void setFilled(boolean filled) { 37 this.filled = filled; 38 } 39 40 /** Get dateCreated */ 41 public java.util.Date getDateCreated() { 42 return dateCreated; 43 } 44 45 /** Return a string representation of this object */ 46 public String toString() { 47 return "created on " + dateCreated + "\ncolor: " + color + 48 " and filled: " + filled; 49 } 50 }
Listing 11.2 Circle.java 1 public class Circle extends GeometricObject { 2 private double radius; 3 4 public Circle() { 5 } 6 7 public Circle(double radius) { 8 this.radius = radius; 9 } 10 11 public Circle(double radius, 12 String color, boolean filled) { 13 this.radius = radius; 14 setColor(color); 15 setFilled(filled); 16 } 17
data fields extends superclass
constructor
M11_LIAN1878_11_GE_C11.indd 436 12/29/17 4:18 PM
11.2 Superclasses and Subclasses 437
18 /** Return radius */ 19 public double getRadius() { 20 return radius; 21 } 22 23 /** Set a new radius */ 24 public void setRadius(double radius) { 25 this.radius = radius; 26 } 27 28 /** Return area */ 29 public double getArea() { 30 return radius * radius * Math.PI; 31 } 32 33 /** Return diameter */ 34 public double getDiameter() { 35 return 2 * radius; 36 } 37 38 /** Return perimeter */ 39 public double getPerimeter() { 40 return 2 * radius * Math.PI; 41 } 42 43 /** Print the circle info */ 44 public void printCircle() { 45 System.out.println("The circle is created " + getDateCreated() + 46 " and the radius is " + radius); 47 } 48 }
The Circle class (Listing 11.2) extends the GeometricObject class (Listing 11.1) using the following syntax:
methods
Subclass Superclass
public class Circle extends GeometricObject
The keyword extends (line 1) tells the compiler that the Circle class extends the GeometricObject class, thus inheriting the methods getColor, setColor, isFilled, setFilled, and toString.
The overloaded constructor Circle(double radius, String color, boolean filled) is implemented by invoking the setColor and setFilled methods to set the color and filled properties (lines 14 and 15). The public methods defined in the superclass GeometricObject are inherited in Circle, so they can be used in the Circle class.
You might attempt to use the data fields color and filled directly in the constructor as follows:
public Circle(double radius, String color, boolean filled) { this.radius = radius; this.color = color; // Illegal this.filled = filled; // Illegal }
private member in superclass
M11_LIAN1878_11_GE_C11.indd 437 12/29/17 4:18 PM
438 Chapter 11 Inheritance and Polymorphism
This is wrong because the private data fields color and filled in the GeometricObject class cannot be accessed in any class other than in the GeometricObject class itself. The only way to read and modify color and filled is through their getter and setter methods.
The Rectangle class (Listing 11.3) extends the GeometricObject class (Listing 11.1) using the following syntax:
Subclass Superclass
public class Rectangle extends GeometricObject
The keyword extends (line 1) tells the compiler the Rectangle class extends the GeometricObject class, thus inheriting the methods getColor, setColor, isFilled, setFilled, and toString.
Listing 11.3 Rectangle.java 1 public class Rectangle extends GeometricObject { 2 private double width; 3 private double height; 4
5 public Rectangle() { 6 } 7 8 public Rectangle(double width, double height) { 9 this.width = width; 10 this.height = height; 11 } 12 13 public Rectangle( 14 double width, double height, String color, boolean filled) { 15 this.width = width; 16 this.height = height; 17 setColor(color); 18 setFilled(filled); 19 } 20 21 /** Return width */
22 public double getWidth() { 23 return width; 24 } 25 26 /** Set a new width */ 27 public void setWidth(double width) { 28 this.width = width; 29 } 30 31 /** Return height */ 32 public double getHeight() { 33 return height; 34 } 35 36 /** Set a new height */ 37 public void setHeight(double height) { 38 this.height = height; 39 } 40
methods
constructor
data fields extends superclass
M11_LIAN1878_11_GE_C11.indd 438 12/29/17 4:18 PM
11.2 Superclasses and Subclasses 439
41 /** Return area */ 42 public double getArea() { 43 return width * height; 44 } 45 46 /** Return perimeter */ 47 public double getPerimeter() { 48 return 2 * (width + height); 49 } 50 }
The code in Listing 11.4 creates objects of Circle and Rectangle and invokes the methods on these objects. The toString() method is inherited from the GeometricObject class and is invoked from a Circle object (line 4) and a Rectangle object (line 11).
Listing 11.4 TestCircleRectangle.java 1 public class TestCircleRectangle { 2 public static void main(String[] args) { 3 Circle circle = new Circle(1); 4 System.out.println("A circle " + circle.toString()); 5 System.out.println("The color is " + circle.getColor()); 6 System.out.println("The radius is " + circle.getRadius()); 7 System.out.println("The area is " + circle.getArea()); 8 System.out.println("The diameter is " + circle.getDiameter()); 9 10 Rectangle rectangle = new Rectangle(2, 4); 11 System.out.println("\nA rectangle " + rectangle.toString()); 12 System.out.println("The area is " + rectangle.getArea()); 13 System.out.println("The perimeter is " + 14 rectangle.getPerimeter()); 15 } 16 }
invoke toString Rectangle object
invoke getColor invoke toString Circle object
A circle created on Thu Feb 10 19:54:25 EST 2011
color: white and filled: false The color is white The radius is 1.0 The area is 3.141592653589793 The diameter is 2.0 A rectangle created on Thu Feb 10 19:54:25 EST 2011 color: white and filled: false The area is 8.0 The perimeter is 12.0
Note the following points regarding inheritance:
■■ Contrary to the conventional interpretation, a subclass is not a subset of its superclass. In fact, a subclass usually contains more information and methods than its superclass.
■■ Private data fields in a superclass are not accessible outside the class. Therefore, they cannot be used directly in a subclass. They can, however, be accessed/mutated through public accessors/mutators if defined in the superclass.
private data fields
more in subclass
M11_LIAN1878_11_GE_C11.indd 439 12/29/17 4:18 PM
440 Chapter 11 Inheritance and Polymorphism
■■ Not all is-a relationships should be modeled using inheritance. For example, a square is a rectangle, but you should not extend a Square class from a Rectangle class, because the width and height properties are not appropriate for a square. Instead, you should define a Square class to extend the GeometricObject class and define the side property for the side of a square.
■■ Inheritance is used to model the is-a relationship. Do not blindly extend a class just for the sake of reusing methods. For example, it makes no sense for a Tree class to extend a Person class, even though they share common properties such as height and weight. A subclass and its superclass must have the is-a relationship.
■■ Some programming languages allow you to derive a subclass from several classes. This capability is known as multiple inheritance. Java, however, does not allow mul- tiple inheritance. A Java class may inherit directly from only one superclass. This restriction is known as single inheritance. If you use the extends keyword to define a subclass, it allows only one parent class. Nevertheless, multiple inheritance can be achieved through interfaces, which will be introduced in Section 13.5.
11.2.1 True or false? A subclass is a subset of a superclass. 11.2.2 What keyword do you use to define a subclass? 11.2.3 What is single inheritance? What is multiple inheritance? Does Java support mul-
tiple inheritance?
11.3 Using the super Keyword The keyword super refers to the superclass and can be used to invoke the superclass's methods and constructors.
A subclass inherits accessible data fields and methods from its superclass. Does it inherit con- structors? Can the superclass’s constructors be invoked from a subclass? This section addresses these questions and their ramifications.
Section 9.14, The this Reference, introduced the use of the keyword this to reference the calling object. The keyword super refers to the superclass of the class in which super appears. It can be used in two ways:
1. To call a superclass constructor
2. To call a superclass method
11.3.1 Calling Superclass Constructors A constructor is used to construct an instance of a class. Unlike properties and methods, the constructors of a superclass are not inherited by a subclass. They can only be invoked from the constructors of the subclasses using the keyword super.
The syntax to call a superclass’s constructor is:
super() or super(arguments);
The statement super() invokes the no-arg constructor of its superclass, and the statement super(arguments) invokes the superclass constructor that matches the arguments. The statement super() or super(arguments) must be the first statement of the subclass’s con- structor; this is the only way to explicitly invoke a superclass constructor. For example, the constructor in lines 11–16 in Listing 11.2 can be replaced by the following code:
public Circle(double radius, String color, boolean filled) { super(color, filled); this.radius = radius; }
nonextensible is-a
no blind extension
multiple inheritance
single inheritance
Point Check
Point Key
M11_LIAN1878_11_GE_C11.indd 440 12/29/17 4:18 PM
11.3 Using the super Keyword 441
Caution You must use the keyword super to call the superclass constructor, and the call must be the first statement in the constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error.
11.3.2 Constructor Chaining A constructor may invoke an overloaded constructor or its superclass constructor. If neither is invoked explicitly, the compiler automatically puts super() as the first statement in the constructor. For example:
public ClassName() { // some statements }
Equivalent
public ClassName() { super(); // some statements }
public ClassName(parameters) { // some statements }
Equivalent
public ClassName(parameters) { super(); // some statements }
In any case, constructing an instance of a class invokes the constructors of all the super- classes along the inheritance chain. When constructing an object of a subclass, the sub- class constructor first invokes its superclass constructor before performing its own tasks. If the superclass is derived from another class, the superclass constructor invokes its parent-class constructor before performing its own tasks. This process continues until the last constructor along the inheritance hierarchy is called. This is called constructor chaining.
Consider the following code:
1 public class Faculty extends Employee { 2 public static void main(String[] args) { 3 new Faculty(); 4 } 5 6 public Faculty() { 7 System.out.println("(4) Performs Faculty's tasks"); 8 } 9 } 10 11 class Employee extends Person { 12 public Employee() { 13 this("(2) Invoke Employee's overloaded constructor"); 14 System.out.println("(3) Performs Employee's tasks "); 15 } 16 17 public Employee(String s) { 18 System.out.println(s); 19 } 20 } 21 22 class Person { 23 public Person() { 24 System.out.println("(1) Performs Person's tasks"); 25 } 26 }
constructor chaining
invoke overloaded constructor
M11_LIAN1878_11_GE_C11.indd 441 12/29/17 4:18 PM
442 Chapter 11 Inheritance and Polymorphism
The program produces the preceding output. Why? Let us discuss the reason. In line 3, new Faculty() invokes Faculty’s no-arg constructor. Since Faculty is a subclass of Employee, Employee’s no-arg constructor is invoked before any statements in Faculty’s constructor are executed. Employee’s no-arg constructor invokes Employee’s second constructor (line 13). Since Employee is a subclass of Person, Person’s no-arg constructor is invoked before any statements in Employee’s second constructor are executed. This process is illustrated in the following figure.
(1) Performs Person's tasks (2) Invoke Employee's overloaded constructor (3) Performs Employee's tasks (4) Performs Faculty's tasks
Faculty() {
Performs Faculty's tasks;
}
Employee() { this("(2) ...");
Performs Employee's tasks;
}
Employee(String s) {
Performs Employee's tasks;
}
Person() {
Performs Person's tasks;
}
Caution If a class is designed to be extended, it is better to provide a no-arg constructor to avoid programming errors. Consider the following code:
1 public class Apple extends Fruit { 2 } 3 4 class Fruit { 5 public Fruit(String name) { 6 System.out.println("Fruit's constructor is invoked"); 7 } 8 }
Since no constructor is explicitly defined in Apple, Apple’s default no-arg constructor is defined implicitly. Since Apple is a subclass of Fruit, Apple’s default constructor automatically invokes Fruit’s no-arg constructor. However, Fruit does not have a no-arg constructor, because Fruit has an explicit constructor defined. Therefore, the program cannot be compiled.
Design Guide If possible, you should provide a no-arg constructor for every class to make the class easy to extend and to avoid errors.
11.3.3 Calling Superclass Methods The keyword super can also be used to reference a method other than the constructor in the superclass. The syntax is
super.method(arguments);
You could rewrite the printCircle() method in the Circle class as follows:
public void printCircle() { System.out.println("The circle is created " + super.getDateCreated() + " and the radius is " + radius); }
no-arg constructor
no-arg constructor
M11_LIAN1878_11_GE_C11.indd 442 12/29/17 4:18 PM
11.4 Overriding Methods 443
It is not necessary to put super before getDateCreated() in this case, however, because getDateCreated is a method in the GeometricObject class and is inherited by the Circle class. Nevertheless, in some cases, as shown in the next section, the keyword super is needed.
11.3.1 What is the output of running the class C in (a)? What problem arises in compiling the program in (b)? Point
Check
class A { public A() { System.out.println( "A's no-arg constructor is invoked"); } }
class B extends A { }
public class C { public static void main(String[] args) { B b = new B(); } }
class A { public A(int x) { } }
class B extends A { public B() { } }
public class C { public static void main(String[] args) { B b = new B(); } }
(a) (b)
11.3.2 How does a subclass invoke its superclass’s constructor? 11.3.3 True or false? When invoking a constructor from a subclass, its superclass’s no-arg
constructor is always invoked.
11.4 Overriding Methods To override a method, the method must be defined in the subclass using the same signature as in its superclass.
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding.
The toString method in the GeometricObject class (lines 46–49 in Listing 11.1) returns the string representation of a geometric object. This method can be overridden to return the string representation of a circle. To override it, add the following new method in the Circle class in Listing 11.2:
1 public class Circle extends GeometricObject { 2 // Other methods are omitted 3 4 // Override the toString method defined in the superclass 5 public String toString() { 6 return super.toString() + "\nradius is " + radius; 7 } 8 }
The toString() method is defined in the GeometricObject class and modified in the Circle class. Both methods can be used in the Circle class. To invoke the toString method defined in the GeometricObject class from the Circle class, use super.toString() (line 6).
Point Key
method overriding
toString in superclass
M11_LIAN1878_11_GE_C11.indd 443 12/29/17 4:18 PM
444 Chapter 11 Inheritance and Polymorphism
Can a subclass of Circle access the toString method defined in the GeometricObject class using syntax such as super.super.toString()? No. This is a syntax error.
Several points are worth noting:
■■ The overriding method must have the same signature as the overridden method and same or compatible return type. Compatible means that the overriding method’s return type is a subtype of the overridden method’s return type.
■■ An instance method can be overridden only if it is accessible. Thus, a private method can- not be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
■■ Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. The hidden static methods can be invoked using the syntax SuperClassName.staticMethodName.
11.4.1 True or false? You can override a private method defined in a superclass. 11.4.2 True or false? You can override a static method defined in a superclass. 11.4.3 How do you explicitly invoke a superclass’s constructor from a subclass? 11.4.4 How do you invoke an overridden superclass method from a subclass?
11.5 Overriding vs. Overloading Overloading means to define multiple methods with the same name but different signatures. Overriding means to provide a new implementation for a method in the subclass.
You learned about overloading methods in Section 6.8. To override a method, the method must be defined in the subclass using the same signature and the same or compatible return type.
Let us use an example to show the differences between overriding and overloading. In (a) below, the method p(double i) in class A overrides the same method defined in class B. In (b), however, the class A has two overloaded methods: p(double i) and p(int i). The method p(double i) is inherited from B.
no super.super.methodName()
override accessible instance method
cannot override static method
Point Check
Point Key
public class TestOverriding { public static void main(String[] args) { A a = new A(); a.p(10); a.p(10.0); } }
class B { public void p(double i) { System.out.println(i * 2); } }
class A extends B { // This method overrides the method in B public void p(double i) { System.out.println(i); } }
public class TestOverloading { public static void main(String[] args) { A a = new A(); a.p(10); a.p(10.0); } }
class B { public void p(double i) { System.out.println(i * 2); } }
class A extends B { // This method overloads the method in B public void p(int i) { System.out.println(i); } }
(a) (b)
M11_LIAN1878_11_GE_C11.indd 444 12/29/17 4:18 PM
11.5 Overriding vs. Overloading 445
When you run the TestOverriding class in (a), both a.p(10) and a.p(10.0) invoke the p(double i) method defined in class A to display 10.0. When you run the TestOverloading class in (b), a.p(10) invokes the p(int i) method defined in class A to display 10 and a.p(10.0) invokes the p(double i) method defined in class B to display 20.0.
Note the following:
■■ Overridden methods are in different classes related by inheritance; overloaded meth- ods can be either in the same class, or in different classes related by inheritance.
■■ Overridden methods have the same signature; overloaded methods have the same name but different parameter lists.
To avoid mistakes, you can use a special Java syntax, called override annotation, to place @Override before the overriding method in the subclass. For example,
1 public class Circle extends GeometricObject { 2 // Other methods are omitted 3 4 @Override 5 public String toString() { 6 return super.toString() + "\nradius is " + radius; 7 } 8 }
This annotation denotes that the annotated method is required to override a method in its superclass. If a method with this annotation does not override its superclass’s method, the compiler will report an error. For example, if toString is mistyped as tostring, a compile error is reported. If the @Override annotation isn’t used, the compiler won’t report an error. Using the @Override annotation avoids mistakes.
11.5.1 Identify the problems in the following code:
1 public class Circle { 2 private double radius; 3 4 public Circle(double radius) { 5 radius = radius; 6 } 7 8 public double getRadius() { 9 return radius; 10 } 11 12 public double getArea() { 13 return radius * radius * Math.PI; 14 } 15 } 16 17 class B extends Circle { 18 private double length; 19 20 B(double radius, double length) { 21 Circle(radius); 22 length = length; 23 } 24 25 @Override 26 public double getArea() { 27 return getArea() * length; 28 } 29 }
override annotation
toString in superclass
Point Check
M11_LIAN1878_11_GE_C11.indd 445 12/29/17 4:18 PM
446 Chapter 11 Inheritance and Polymorphism
11.5.2 Explain the difference between method overloading and method overriding. 11.5.3 If a method in a subclass has the same signature as a method in its superclass with
the same return type, is the method overridden or overloaded?
11.5.4 If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem?
11.5.5 If a method in a subclass has the same name as a method in its superclass with dif- ferent parameter types, is the method overridden or overloaded?
11.5.6 What is the benefit of using the @Override annotation?
11.6 The Object Class and Its toString() Method Every class in Java is descended from the java.lang.Object class.
If no inheritance is specified when a class is defined, the superclass of the class is Object by default. For example, the following two class definitions are the same:Point
Key
public class ClassName { ... }
Equivalent public class ClassName extends Object { ... }
Classes such as String, StringBuilder, Loan, and GeometricObject are implicitly sub- classes of Object (as are all the main classes you have seen in this book so far). It is important to be familiar with the methods provided by the Object class so that you can use them in your classes. This section introduces the toString method in the Object class.
The signature of the toString() method is:
public String toString()
Invoking toString() on an object returns a string that describes the object. By default, it returns a string consisting of a class name of which the object is an instance, an at sign (@), and the object’s memory address in hexadecimal. For example, consider the following code for the Loan class defined in Listing 10.2:
Loan loan = new Loan(); System.out.println(loan.toString());
The output for this code displays something like Loan@15037e5. This message is not very helpful or informative. Usually you should override the toString method so that it returns a descriptive string representation of the object. For example, the toString method in the Object class was overridden in the GeometricObject class in lines 46–49 in Listing 11.1 as follows:
public String toString() { return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; }
Note You can also pass an object to invoke System.out.println(object) or System. out.print(object). This is equivalent to invoking System.out.println(object. toString()) or System.out.print(object.toString()). Thus, you could replace System.out.println(loan.toString()) with System.out.println(loan).
toString()
string representation
print object
M11_LIAN1878_11_GE_C11.indd 446 12/29/17 4:18 PM
11.8 Dynamic Binding 447
11.7 Polymorphism Polymorphism means that a variable of a supertype can refer to a subtype object.
The three pillars of object-oriented programming are encapsulation, inheritance, and polymor- phism. You have already learned the first two. This section introduces polymorphism.
The inheritance relationship enables a subclass to inherit features from its superclass with additional new features. A subclass is a specialization of its superclass; every instance of a subclass is also an instance of its superclass, but not vice versa. For example, every circle is a geometric object, but not every geometric object is a circle. Therefore, you can always pass an instance of a subclass to a parameter of its superclass type. Consider the code in Listing 11.5.
Listing 11.5 PolymorphismDemo.java 1 public class PolymorphismDemo { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Display circle and rectangle properties 5 displayObject(new Circle(1, "red", false)); 6 displayObject(new Rectangle(1, 1, "black", true)); 7 } 8 9 /** Display geometric object properties */ 10 public static void displayObject(GeometricObject object) { 11 System.out.println("Created on " + object.getDateCreated() + 12 ". Color is " + object.getColor()); 13 } 14 }
Point Key
polymorphic call polymorphic call
Created on Mon Mar 09 19:25:20 EDT 2011. Color is red
Created on Mon Mar 09 19:25:20 EDT 2011. Color is black
The method displayObject (line 10) takes a parameter of the GeometricObject type. You can invoke displayObject by passing any instance of GeometricObject (e.g., new Circle(1, "red", false) and new Rectangle(1, 1, "black", true) in lines 5 and 6). An object of a subclass can be used wherever its superclass object is used. This is commonly known as polymorphism (from a Greek word meaning “many forms”). In simple terms, polymorphism means that a variable of a supertype can refer to a subtype object.
11.7.1 What are the three pillars of object-oriented programming? What is polymorphism?
11.8 Dynamic Binding A method can be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime.
A method can be defined in a superclass and overridden in its subclass. For example, the toString() method is defined in the Object class and overridden in GeometricObject. Consider the following code:
Object o = new GeometricObject(); System.out.println(o.toString());
what is polymorphism?
Point Check
Point Key
M11_LIAN1878_11_GE_C11.indd 447 12/29/17 4:18 PM
448 Chapter 11 Inheritance and Polymorphism
Which toString() method is invoked by o? To answer this question, we first introduce two terms: declared type and actual type. A variable must be declared a type. The type that declares a variable is called the variable’s declared type. Here, o’s declared type is Object. A variable of a reference type can hold a null value or a reference to an instance of the declared type. The instance may be created using the constructor of the declared type or its subtype. The actual type of the variable is the actual class for the object referenced by the variable. Here, o’s actual type is GeometricObject, because o references an object created using new GeometricObject(). Which toString() method is invoked by o is determined by o’s actual type. This is known as dynamic binding.
Dynamic binding works as follows: Suppose that an object o is an instance of classes C1, C2, . . . , Cn-1, and Cn, where C1 is a subclass of C2, C2 is a subclass of C3, . . . , and Cn-1 is a subclass of Cn, as shown in Figure 11.2. That is, Cn is the most general class, and C1 is the most specific class. In Java, Cn is the Object class. If o invokes a method p, the JVM searches for the implementation of the method p in C1, C2, . . . , Cn-1, and Cn, in this order, until it is found. Once an implementation is found, the search stops and the first-found implementation is invoked.
declared type
actual type
dynamic binding
Figure 11.2 The method to be invoked is dynamically bound at runtime.
. . . . .
java.lang.Object If o is an instance of C1, o is also an instance of C2, C3, …, Cn-1, and Cn
Cn Cn-1 C2 C1
Listing 11.6 gives an example to demonstrate dynamic binding.
Listing 11.6 DynamicBindingDemo.java 1 public class DynamicBindingDemo { 2 public static void main(String[] args) { 3 m(new GraduateStudent()); 4 m(new Student()); 5 m(new Person()); 6 m(new Object()); 7 } 8 9 public static void m(Object x) { 10 System.out.println(x.toString()); 11 } 12 } 13 14 class GraduateStudent extends Student { 15 } 16 17 class Student extends Person { 18 @Override 19 public String toString() { 20 return "Student"; 21 } 22 } 23 24 class Person extends Object { 25 @Override
VideoNote
Polymorphism and dynamic binding demo
polymorphic call
dynamic binding
override toString()
override toString()
M11_LIAN1878_11_GE_C11.indd 448 12/29/17 4:18 PM
11.8 Dynamic Binding 449
26 public String toString() { 27 return "Person"; 28 } 29 }
Student
Student Person java.lang.Object@130c19b
Method m (line 9) takes a parameter of the Object type. You can invoke m with any object (e.g., new GraduateStudent(), new Student(), new Person(), and new Object()) in lines 3–6).
When the method m(Object x) is executed, the argument x’s toString method is invoked. x may be an instance of GraduateStudent, Student, Person, or Object. The toString method is implemented in Student, Person, and Object. Which implementation is used will be determined by x’s actual type at runtime. Invoking m(new GraduateStudent()) (line 3) causes the toString method defined in the Student class to be invoked.
Invoking m(new Student()) (line 4) causes the toString method defined in the Student class to be invoked; invoking m(new Person()) (line 5) causes the toString method defined in the Person class to be invoked; and invoking m(new Object()) (line 6) causes the toString method defined in the Object class to be invoked.
Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to the parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several classes along the inheritance chain. The JVM dynamically binds the implementation of the method at runtime, decided by the actual type of the variable.
11.8.1 What is dynamic binding? 11.8.2 Describe the difference between method matching and method binding. 11.8.3 Can you assign new int[50], new Integer[50], new String[50], or new
Object[50] into a variable of Object[] type?
11.8.4 What is wrong in the following code?
1 public class Test { 2 public static void main(String[] args) { 3 Integer[] list1 = {12, 24, 55, 1}; 4 Double[] list2 = {12.4, 24.0, 55.2, 1.0}; 5 int[] list3 = {1, 2, 3}; 6 printArray(list1); 7 printArray(list2); 8 printArray(list3); 9 } 10 11 public static void printArray(Object[] list) { 12 for (Object o: list) 13 System.out.print(o + " "); 14 System.out.println(); 15 } 16 }
matching vs. binding
Point Check
M11_LIAN1878_11_GE_C11.indd 449 12/29/17 4:18 PM
450 Chapter 11 Inheritance and Polymorphism
11.8.5 Show the output of the following code:
public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } }
class Student extends Person { @Override public String getInfo() { return "Student"; } }
class Person { public String getInfo() { return "Person"; }
public void printPerson() { System.out.println(getInfo()); } }
public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } }
class Student extends Person { private String getInfo() { return "Student"; } }
class Person { private String getInfo() { return "Person"; }
public void printPerson() { System.out.println(getInfo()); } }
(a) (b)
11.8.6 Show the output of following program:
1 public class Test { 2 public static void main(String[] args) { 3 A a = new A(3); 4 } 5 } 6 7 class A extends B { 8 public A(int t) { 9 System.out.println("A's constructor is invoked"); 10 } 11 } 12 13 class B { 14 public B() { 15 System.out.println("B's constructor is invoked"); 16 } 17 }
Is the no-arg constructor of Object invoked when new A(3) is invoked?
11.8.7 Show the output of following program:
public class Test { public static void main(String[] args) { new A(); new B(); } }
M11_LIAN1878_11_GE_C11.indd 450 12/29/17 4:18 PM
11.9 Casting Objects and the instanceof Operator 451
class A { int i = 7;
public A() { setI(20); System.out.println("i from A is " + i); }
public void setI(int i) { this.i = 2 * i; } }
class B extends A { public B() { System.out.println("i from B is " + i); }
public void setI(int i) { this.i = 3 * i; } }
11.9 Casting Objects and the instanceof Operator One object reference can be typecast into another object reference. This is called cast- ing object.
In the preceding section, the statement
m(new Student());
assigns the object new Student() to a parameter of the Object type. This statement is equivalent to
Object o = new Student(); // Implicit casting m(o);
The statement Object o = new Student(), known as implicit casting, is legal because an instance of Student is an instance of Object.
Suppose you want to assign the object reference o to a variable of the Student type using the following statement:
Student b = o;
In this case a compile error would occur. Why does the statement Object o = new Student() work, but Student b = o doesn’t? The reason is that a Student object is always an instance of Object, but an Object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not clever enough to know it. To tell the compiler o is a Student object, use explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows:
Student b = (Student)o; // Explicit casting
It is always possible to cast an instance of a subclass to a variable of a superclass (known as upcasting) because an instance of a subclass is always an instance of its superclass. When casting an instance of a superclass to a variable of its subclass (known as downcasting), explicit
Point Key
casting object
implicit casting
explicit casting
downcasting upcasting
M11_LIAN1878_11_GE_C11.indd 451 12/29/17 4:18 PM
452 Chapter 11 Inheritance and Polymorphism
casting must be used to confirm your intention to the compiler with the (SubclassName) cast notation. For the casting to be successful, you must make sure the object to be cast is an instance of the subclass. If the superclass object is not an instance of the subclass, a runtime ClassCastException occurs. For example, if an object is not an instance of Student, it cannot be cast into a variable of Student. It is a good practice, therefore, to ensure the object is an instance of another object before attempting a casting. This can be accomplished by using the instanceof operator. Consider the following code:
void someMethod(Object myObjet) { ... // Some lines of code /** Perform casting if myObject is an instance of Circle */ if (myObject instanceof Circle) { System.out.println("The circle diameter is " + ((Circle)myObject).getDiameter()); ... } }
You may be wondering why casting is necessary. The variable myObject is declared Object. The declared type decides which method to match at compile time. Using myObject. getDiameter() would cause a compile error, because the Object class does not have the getDiameter method. The compiler cannot find a match for myObject.getDiameter(). Therefore, it is necessary to cast myObject into the Circle type to tell the compiler that myObject is also an instance of Circle.
Why not declare myObject as a Circle type in the first place? To enable generic programming, it is a good practice to declare a variable with a supertype that can accept an object of any subtype.
Note instanceof is a Java keyword. Every letter in a Java keyword is in lowercase.
Tip To help understand casting, you may also consider the analogy of fruit, apple, and orange, with the Fruit class as the superclass for Apple and Orange. An apple is a fruit, so you can always safely assign an instance of Apple to a variable for Fruit. However, a fruit is not necessarily an apple, so you have to use explicit casting to assign an instance of Fruit to a variable of Apple.
Listing 11.7 demonstrates polymorphism and casting. The program creates two objects (lines 5 and 6), a circle and a rectangle, and invokes the displayObject method to display them (lines 9 and 10). The displayObject method displays the area and diameter if the object is a circle (line 15), and the area if the object is a rectangle (line 21).
Listing 11.7 CastingDemo.java 1 public class CastingDemo { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Create and initialize two objects 5 Object object1 = new Circle(1); 6 Object object2 = new Rectangle(1, 1); 7 8 // Display circle and rectangle 9 displayObject(object1); 10 displayObject(object2); 11 } 12
ClassCastException
instanceof
lowercase keywords
casting analogy
M11_LIAN1878_11_GE_C11.indd 452 12/29/17 4:18 PM
11.9 Casting Objects and the instanceof Operator 453
13 /** A method for displaying an object */ 14 public static void displayObject(Object object) { 15 if (object instanceof Circle) { 16 System.out.println("The circle area is " + 17 ((Circle)object).getArea()); 18 System.out.println("The circle diameter is " + 19 ((Circle)object).getDiameter()); 20 } 21 else if (object instanceof Rectangle) { 22 System.out.println("The rectangle area is " +
23 ((Rectangle)object).getArea()); 24 } 25 } 26 }
polymorphic call
polymorphic call
The circle area is 3.141592653589793 The circle diameter is 2.0 The rectangle area is 1.0
The displayObject(Object object) method is an example of generic programming. It can be invoked by passing any instance of Object.
The program uses implicit casting to assign a Circle object to object1 and a Rectangle object to object2 (lines 5 and 6), then invokes the displayObject method to display the information on these objects (lines 9–10).
In the displayObject method (lines 14–25), explicit casting is used to cast the object to Circle if the object is an instance of Circle, and the methods getArea and getDiameter are used to display the area and diameter of the circle.
Casting can be done only when the source object is an instance of the target class. The program uses the instanceof operator to ensure that the source object is an instance of the target class before performing a casting (line 15).
Explicit casting to Circle (lines 17 and 19) and to Rectangle (line 23) is necessary because the getArea and getDiameter methods are not available in the Object class.
Caution The object member access operator (.) precedes the casting operator. Use parentheses to ensure that casting is done before the . operator, as in
((Circle)object).getArea();
Casting a primitive-type value is different from casting an object reference. Casting a primi- tive-type value returns a new value. For example:
int age = 45; byte newAge = (byte)age; // A new value is assigned to newAge
However, casting an object reference does not create a new object. For example:
Object o = new Circle(); Circle c = (Circle)o; // No new object is created
Now, reference variables o and c point to the same object.
precedes casting
M11_LIAN1878_11_GE_C11.indd 453 12/29/17 4:18 PM
454 Chapter 11 Inheritance and Polymorphism
11.9.1 Indicate true or false for the following statements:
a. You can always successfully cast an instance of a subclass to a superclass.
b. You can always successfully cast an instance of a superclass to a subclass.
11.9.2 For the GeometricObject and Circle classes in Listings 11.1 and 11.2, answer the following questions:
a. Assume that circle and object1 are created as follows: Circle circle = new Circle(1); GeometricObject object1 = new GeometricObject();
Are the following Boolean expressions true or false? (circle instanceof GeometricObject) (object instanceof GeometricObject) (circle instanceof Circle) (object instanceof Circle)
b. Can the following statements be compiled? Circle circle = new Circle(5); GeometricObject object = circle;
c. Can the following statements be compiled? GeometricObject object = new GeometricObject(); Circle circle = (Circle)object;
11.9.3 Suppose Fruit, Apple, Orange, GoldenDelicious, and McIntosh are defined in the following inheritance hierarchy:
Point Check
Fruit
Apple
McIntosh
Orange
GoldenDelicious
Assume the following code is given:
Fruit fruit = new GoldenDelicious(); Orange orange = new Orange();
Answer the following questions:
a. Is fruit instanceof Fruit?
b. Is fruit instanceof Orange?
c. Is fruit instanceof Apple?
d. Is fruit instanceof GoldenDelicious?
e. Is fruit instanceof McIntosh?
f. Is orange instanceof Orange?
M11_LIAN1878_11_GE_C11.indd 454 12/29/17 4:18 PM
11.10 The Object’s equals Method 455
g. Is orange instanceof Fruit?
h. Is orange instanceof Apple?
i. Suppose the method makeAppleCider is defined in the Apple class. Can Fruit invoke this method? Can orange invoke this method?
j. Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? Can Fruit invoke this method?
k. Is the statement Orange p = new Apple() legal?
l. Is the statement McIntosh p = new Apple() legal?
m. Is the statement Apple p = new McIntosh() legal?
11.9.4 What is wrong in the following code?
1 public class Test { 2 public static void main(String[] args) { 3 Object fruit = new Fruit(); 4 Object apple = (Apple)fruit; 5 } 6 } 7 8 class Apple extends Fruit { 9 } 10 11 class Fruit { 12 }
11.10 The Object’s equals Method Like the toString() method, the equals(Object) method is another useful method defined in the Object class.
Another method defined in the Object class that is often used is the equals method. Its signature is
public boolean equals(Object o)
This method tests whether two objects are equal. The syntax for invoking it is
object1.equals(object2);
The default implementation of the equals method in the Object class is
public boolean equals(Object obj) { return this == obj; }
This implementation checks whether two reference variables point to the same object using the == operator. You should override this method in your custom class to test whether two distinct objects have the same content.
The equals method is overridden in many classes in the Java API, such as java.lang.String and java.util.Date, to compare whether the contents of two objects are equal. You have already used the equals method to compare two strings in Section 4.4.7, The String Class. The equals method in the String class is inherited from the Object class, and is overridden in the String class to test whether two strings are identical in content.
Point Key
M11_LIAN1878_11_GE_C11.indd 455 12/29/17 4:18 PM
456 Chapter 11 Inheritance and Polymorphism
You can override the equals method in the Circle class to compare whether two circles are equal based on their radius as follows:
@Override public boolean equals(Object o) { if (o instanceof Circle) return radius == ((Circle)o).radius; else return false; }
Note The == comparison operator is used for comparing two primitive-data-type values or for determining whether two objects have the same references. The equals method is intended to test whether two objects have the same contents, provided the method is overridden in the defining class of the objects. The == operator is stronger than the equals method in that the == operator checks whether the two reference variables refer to the same object.
Caution Using the signature equals(SomeClassName obj) (e.g., equals(Circle c)) to override the equals method in a subclass is a common mistake. You should use equals(Object obj). See CheckPoint Question 11.10.2.
11.10.1 Does every object have a toString method and an equals method? Where do they come from? How are they used? Is it appropriate to override these methods?
11.10.2 When overriding the equals method, a common mistake is mistyping its signa- ture in the subclass. For example, the equals method is incorrectly written as equals(Circle circle), as shown in (a) in the following code; instead, it should be equals(Object circle), as shown in (b). Show the output of running class Test with the Circle class in (a) and in (b), respectively.
== vs. equals
equals(Object)
Point Check
public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } }
class Circle { double radius;
public boolean equals(Circle circle) { return this.radius == circle.radius; } }
(a)
class Circle { double radius;
public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } }
(b)
If Object is replaced by Circle in the Test class, what would be the output to run Test using the Circle class in (a) and (b), respectively?
11.11 The ArrayList Class An ArrayList object can be used to store a list of objects.
Now we are ready to introduce a very useful class for storing objects. You can create an array to store objects. However, once the array is created, its size is fixed. Java provides the ArrayList
Point Key
VideoNote
The ArrayList class
M11_LIAN1878_11_GE_C11.indd 456 12/29/17 4:18 PM
11.11 The ArrayList Class 457
class, which can be used to store an unlimited number of objects. Figure 11.3 shows some methods in ArrayList.
Figure 11.3 An ArrayList stores an unlimited number of objects.
java.util.ArrayList<E>
+ArrayList()
+add(e: E): void
+add(index: int, e: E): void
+clear(): void
+contains(o: Object): boolean
+get(index: int): E
+indexOf(o: Object): int
+isEmpty(): boolean
+lastIndexOf(o: Object): int
+remove(o: Object): boolean
+size(): int
+remove(index: int): E
+set(index: int, e: E): E
Appends a new element e at the end of this list.
Adds a new element e at the speci�ed index in this list.
Removes all elements from this list
Returns true if this list contains the element o.
Returns the element from this list at the speci�ed index.
Returns the index of the �rst matching element in this list.
Returns true if this list contains no elements.
Returns the index of the last matching element in this list.
Removes the �rst element CDT from this list. Returns true
if an element is removed.
Returns the number of elements in this list.
Removes the element at the speci�ed index. Returns
the removed element.
Sets the element at the speci�ed index.
Creates an empty list.
ArrayList is known as a generic class with a generic type E. You can specify a concrete type to replace E when creating an ArrayList. For example, the following statement creates an ArrayList and assigns its reference to variable cities. This ArrayList object can be used to store strings.
ArrayList<String> cities = new ArrayList<String>();
The following statement creates an ArrayList and assigns its reference to variable dates. This ArrayList object can be used to store dates.
ArrayList<java.util.Date> dates = new ArrayList<java.util.Date>();
Note Since JDK 7, the statement
ArrayList <AConcreteType> list = new ArrayList<AConcreteType>();
can be simplified by
ArrayList<AConcreteType> list = new ArrayList<>();
The concrete type is no longer required in the constructor, thanks to a feature called type inference. The compiler is able to infer the type from the variable declaration. More discussions on generics including how to define custom generic classes and methods will be introduced in Chapter 19, Generics.
Listing 11.8 gives an example of using ArrayList to store objects.
type inference
M11_LIAN1878_11_GE_C11.indd 457 12/29/17 4:18 PM
458 Chapter 11 Inheritance and Polymorphism
Listing 11.8 TestArrayList.java 1 import java.util.ArrayList; 2 3 public class TestArrayList { 4 public static void main(String[] args) { 5 // Create a list to store cities 6 ArrayList<String> cityList = new ArrayList<>(); 7 8 // Add some cities in the list 9 cityList.add("London"); 10 // cityList now contains [London] 11 cityList.add("Denver"); 12 // cityList now contains [London, Denver] 13 cityList.add("Paris"); 14 // cityList now contains [London, Denver, Paris] 15 cityList.add("Miami"); 16 // cityList now contains [London, Denver, Paris, Miami] 17 cityList.add("Seoul"); 18 // Contains [London, Denver, Paris, Miami, Seoul] 19 cityList.add("Tokyo"); 20 // Contains [London, Denver, Paris, Miami, Seoul, Tokyo] 21 22 System.out.println("List size? " + cityList.size()); 23 System.out.println("Is Miami in the list? " + 24 cityList.contains("Miami")); 25 System.out.println("The location of Denver in the list? " 26 + cityList.indexOf("Denver")); 27 System.out.println("Is the list empty? " + 28 cityList.isEmpty()); // Print false 29 30 // Insert a new city at index 2 31 cityList.add(2, "Xian"); 32 // Contains [London, Denver, Xian, Paris, Miami, Seoul, Tokyo] 33 34 // Remove a city from the list 35 cityList.remove("Miami"); 36 // Contains [London, Denver, Xian, Paris, Seoul, Tokyo] 37 38 // Remove a city at index 1 39 cityList.remove(1); 40 // Contains [London, Xian, Paris, Seoul, Tokyo] 41 42 // Display the contents in the list 43 System.out.println(cityList.toString()); 44 45 // Display the contents in the list in reverse order 46 for (int i = cityList.size() − 1; i >= 0; i––) 47 System.out.print(cityList.get(i) + " "); 48 System.out.println(); 49 50 // Create a list to store two circles 51 ArrayList<Circle> list = new ArrayList<>(); 52 53 // Add two circles 54 list.add(new Circle(2)); 55 list.add(new Circle(3)); 56 57 // Display the area of the first circle in the list 58 System.out.println("The area of the circle? " +
get element
toString()
remove element
is empty?
add element
create ArrayList
list size
create ArrayList
contains element?
element index
remove element
import ArrayList
M11_LIAN1878_11_GE_C11.indd 458 12/29/17 4:18 PM
11.11 The ArrayList Class 459
Since the ArrayList is in the java.util package, it is imported in line 1. The program creates an ArrayList of strings using its no-arg constructor and assigns the reference to cityList (line 6). The add method (lines 9–19) adds strings to the end of list. Thus, after cityList.add("London") (line 9), the list contains
[London]
After cityList.add("Denver") (line 11), the list contains
[London, Denver]
After adding Paris, Miami, Seoul, and Tokyo (lines 13–19), the list contains
[London, Denver, Paris, Miami, Seoul, Tokyo]
Invoking size() (line 22) returns the size of the list, which is currently 6. Invoking contains("Miami") (line 24) checks whether the object is in the list. In this case, it returns true, since Miami is in the list. Invoking indexOf("Denver") (line 26) returns the index of Denver in the list, which is 1. If Denver were not in the list, it would return –1. The isEmpty() method (line 28) checks whether the list is empty. It returns false, since the list is not empty.
The statement cityList.add(2, "Xian") (line 31) inserts an object into the list at the specified index. After this statement, the list becomes
[London, Denver, Xian, Paris, Miami, Seoul, Tokyo]
The statement cityList.remove("Miami") (line 35) removes the object from the list. After this statement, the list becomes
[London, Denver, Xian, Paris, Seoul, Tokyo]
The statement cityList.remove(1) (line 39) removes the object at the specified index from the list. After this statement, the list becomes
[London, Xian, Paris, Seoul, Tokyo]
The statement in line 43 is same as
System.out.println(cityList);
The toString() method returns a string representation of the list in the form of [e0.toString(), e1.toString(), ..., ek.toString()], where e0, e1, . . . , and ek are the elements in the list.
The get(index) method (line 47) returns the object at the specified index. ArrayList objects can be used like arrays, but there are many differences. Table 11.1 lists
their similarities and differences. Once an array is created, its size is fixed. You can access an array element using the
square-bracket notation (e.g., a[index]). When an ArrayList is created, its size is 0.
add(Object)
size()
add(index, Object)
remove(Object)
remove(index)
toString()
get(index)
array vs. ArrayList
List size? 6
Is Miami in the list? true The location of Denver in the list? 1 Is the list empty? false [London, Xian, Paris, Seoul, Tokyo] Tokyo Seoul Paris Xian London The area of the circle? 12.566370614359172
59 list.get(0).getArea()); 60 } 61 }
M11_LIAN1878_11_GE_C11.indd 459 12/29/17 4:18 PM
460 Chapter 11 Inheritance and Polymorphism
You cannot use the get(index) and set(index, element) methods if the element is not in the list. It is easy to add, insert, and remove elements in a list, but it is rather complex to add, insert, and remove elements in an array. You have to write code to manipulate the array in order to perform these operations. Note you can sort an array using the java.util.Arrays. sort(array) method. To sort an array list, use the java.util. Collections. sort(arraylist) method.
Suppose you want to create an ArrayList for storing integers. Can you use the following code to create a list?
ArrayList<int> listOfIntegers = new ArrayList<>();
No. This will not work because the elements stored in an ArrayList must be of an object type. You cannot use a primitive data type such as int to replace a generic type. However, you can create an ArrayList for storing Integer objects as follows:
ArrayList<Integer> listOfIntegers = new ArrayList<>();
Note the remove(int index) method removes an element at the specified index. To remove an integer value v from listOfIntegers, you need to use listOfIntegers. remove(new Integer(v)). This is not a good design in the Java API because it could easily lead to mistakes. It would be much better if remove(int) is renamed removeAt(int).
Listing 11.9 gives a program that prompts the user to enter a sequence of numbers and dis- plays the distinct numbers in the sequence. Assume the input ends with 0, and 0 is not counted as a number in the sequence.
Listing 11.9 DistinctNumbers.java 1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class DistinctNumbers { 5 public static void main(String[] args) { 6 ArrayList<Integer> list = new ArrayList<>(); 7 8 Scanner input = new Scanner(System.in); 9 System.out.print("Enter integers (input ends with 0): "); 10 int value; 11 12 do { 13 value = input.nextInt(); // Read a value from the input 14
remove(int) vs. remove(Integer)
create an array list
Operation Array ArrayList
Creating an array/ArrayList String[] a = new String[10] ArrayList<String> list = new ArrayList<>();
Accessing an element a[index] list.get(index);
Updating an element a[index] = "London"; list.set(index, "London");
Returning size a.length list.size();
Adding a new element list.add("London");
Inserting a new element list.add(index, "London");
Removing an element list.remove(index);
Removing an element list.remove(Object);
Removing all elements list.clear();
tabLe 11.1 Differences and Similarities between Arrays and ArrayList
M11_LIAN1878_11_GE_C11.indd 460 12/29/17 4:18 PM
11.11 The ArrayList Class 461
15 if (!list.contains(value) && value != 0) 16 list.add(value); // Add the value if it is not in the list 17 } while (value != 0); 18 19 // Display the distinct numbers 20 for (int i = 0; i < list.size(); i++) 21 System.out.print(list.get(i) + " "); 22 } 23 }
contained in list?
add to list
Enter numbers (input ends with 0): 1 2 3 2 1 6 3 4 5 4 5 1 2 3 0
The distinct numbers are: 1 2 3 6 4 5
The program creates an ArrayList for Integer objects (line 6) and repeatedly reads a value in the loop (lines 12–17). For each value, if it is not in the list (line 15), add it to the list (line 16). You can rewrite this program using an array to store the elements rather than using an ArrayList. However, it is simpler to implement this program using an ArrayList for two reasons.
1. The size of an ArrayList is flexible so you don’t have to specify its size in advance. When creating an array, its size must be specified.
2. ArrayList contains many useful methods. For example, you can test whether an element is in the list using the contains method. If you use an array, you have to write additional code to implement this method.
You can traverse the elements in an array using a foreach loop. The elements in an array list can also be traversed using a foreach loop using the following syntax:
for (elementType element: arrayList) { // Process the element }
For example, you can replace the code in lines 20 and 21 using the following code:
for (Integer number: list) System.out.print(number + " ");
or
for (int number: list) System.out.print(number + " ");
Note the elements in list are Integer objects. They are automatically unboxed into int in this foreach loop.
11.11.1 How do you do the following?
a. Create an ArrayList for storing double values?
b. Append an object to a list?
c. Insert an object at the beginning of a list?
d. Find the number of objects in a list?
e. Remove a given object from a list?
f. Remove the last object from a list?
g. Check whether a given object is in a list?
h. Retrieve an object at a specified index from a list?
foreach loop
Point Check
M11_LIAN1878_11_GE_C11.indd 461 12/29/17 4:18 PM
462 Chapter 11 Inheritance and Polymorphism
11.11.2 Identify the errors in the following code. ArrayList<String> list = new ArrayList<>(); list.add("Denver"); list.add("Austin"); list.add(new java.util.Date()); String city = list.get(0); list.set(3, "Dallas"); System.out.println(list.get(3));
11.11.3 Suppose the ArrayList list contains {"Dallas", "Dallas", "Hou- ston", "Dallas"}. What is the list after invoking list.remove("Dallas") one time? Does the following code correctly remove all elements with value "Dallas" from the list? If not, correct the code.
for (int i = 0; i < list.size(); i++) list.remove("Dallas");
11.11.4 Explain why the following code displays [1, 3] rather than [2, 3]. ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(1); System.out.println(list); How do you remove integer value 3 from the list?
11.11.5 Explain why the following code is wrong: ArrayList<Double> list = new ArrayList<>(); list.add(1);
11.12 Useful Methods for Lists Java provides the methods for creating a list from an array, for sorting a list, and for finding maximum and minimum element in a list, and for shuffling a list.
Often you need to create an array list from an array of objects or vice versa. You can write the code using a loop to accomplish this, but an easy way is to use the methods in the Java API. Here is an example to create an array list from an array:
String[] array = {"red", "green", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array));
The static method asList in the Arrays class returns a list that is passed to the ArrayList constructor for creating an ArrayList. Conversely, you can use the following code to create an array of objects from an array list:
String[] array1 = new String[list.size()]; list.toArray(array1);
Invoking list.toArray(array1) copies the contents from list to array1. If the ele- ments in a list are comparable, such as integers, double, or strings, you can use the static sort method in the java.util.Collections class to sort the elements. Here are some examples:
Integer[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array)); java.util.Collections.sort(list); System.out.println(list);
Point Key
array to array list
array list to array
sort a list
M11_LIAN1878_11_GE_C11.indd 462 12/29/17 4:18 PM
11.13 Case Study: A Custom Stack Class 463
You can use the static max and min in the java.util.Collections class to return the maximum and minimal element in a list. Here are some examples:
Integer[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array)); System.out.println(java.util.Collections.max(list)); System.out.println(java.util.Collections.min(list));
You can use the static shuffle method in the java.util.Collections class to perform a random shuffle for the elements in a list. Here are some examples:
Integer[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array)); java.util.Collections.shuffle(list); System.out.println(list);
11.12.1 Correct errors in the following statements:
int[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array));
11.12.2 Correct errors in the following statements:
int[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5}; System.out.println(java.util.Collections.max(array));
11.13 Case Study: A Custom Stack Class This section designs a stack class for holding objects.
Section 10.6 presented a stack class for storing int values. This section introduces a stack class to store objects. You can use an ArrayList to implement Stack, as shown in Listing 11.10. The UML diagram for the class is shown in Figure 11.4.
max and min methods
shuffle method
Point Check
Point Key
VideoNote
The MyStack class
Figure 11.4 The MyStack class encapsulates the stack storage and provides the operations for manipulating the stack.
Returns true if this stack is empty.
Returns the number of elements in this stack.
Returns the top element in this stack without removing it.
Returns and removes the top element in this stack.
Adds a new element to the top of this stack.
A list to store elements.
MyStack
+isEmpty(): boolean
+getSize(): int
+peek(): Object
+pop(): Object
+push(o: Object): void
-list: ArrayList<Object>
Listing 11.10 MyStack.java 1 import java.util.ArrayList; 2 3 public class MyStack {
4 private ArrayList<Object> list = new ArrayList<>(); 5 6 public boolean isEmpty() { 7 return list.isEmpty();
stack empty?
array list
M11_LIAN1878_11_GE_C11.indd 463 12/29/17 4:18 PM
464 Chapter 11 Inheritance and Polymorphism
8 } 9 10 public int getSize() { 11 return list.size(); 12 } 13 14 public Object peek() { 15 return list.get(getSize() − 1); 16 } 17 18 public Object pop() { 19 Object o = list.get(getSize() − 1); 20 list.remove(getSize() − 1); 21 return o; 22 } 23 24 public void push(Object o) { 25 list.add(o); 26 } 27 28 @Override 29 public String toString() { 30 return "stack: " + list.toString(); 31 } 32 }
An array list is created to store the elements in the stack (line 4). The isEmpty() method (lines 6–8) returns list.isEmpty() . The getSize() method (lines 10–12) returns list.size(). The peek() method (lines 14–16) retrieves the element at the top of the stack without removing it. The end of the list is the top of the stack. The pop() method (lines 18–22) removes the top element from the stack and returns it. The push(Object element) method (lines 24–26) adds the specified element to the stack. The toString() method (lines 28–31) defined in the Object class is overridden to display the contents of the stack by invoking list.toString(). The toString() method implemented in ArrayList returns a string representation of all the elements in an array list.
Design Guide In Listing 11.10, MyStack contains ArrayList. The relationship between MyStack and ArrayList is composition. Composition essentially means declaring an instance variable for referencing an object. This object is said to be composed. While inheritance models an is-a relationship, composition models a has-a relationship. You could also implement MyStack as a subclass of ArrayList (see Programming Exercise 11.10). Using composi- tion is better, however, because it enables you to define a completely new stack class without inheriting the unnecessary and inappropriate methods from ArrayList.
11.13.1 Write statements that create a MyStack and add number 11 to the stack.
11.14 The protected Data and Methods A protected member of a class can be accessed from a subclass.
So far you have used the private and public keywords to specify whether data fields and methods can be accessed from outside of the class. Private members can be accessed only from inside of the class, and public members can be accessed from any other classes.
Often it is desirable to allow subclasses to access data fields or methods defined in the superclass, but not to allow nonsubclasses in different packages to access these data fields and methods. To accomplish this, you can use the protected keyword. This way you can access protected data fields or methods in a superclass from its subclasses.
get stack size
peek stack
remove
push
composition
has-a
Point Check
Point Key
why protected?
M11_LIAN1878_11_GE_C11.indd 464 12/29/17 4:18 PM
11.14 The protected Data and Methods 465
The modifiers private, protected, and public are known as visibility or accessibility modifiers because they specify how classes and class members are accessed. The visibility of these modifiers increases in this order:
Visibility increases
private, default (no modifier), protected, public
Table 11.2 summarizes the accessibility of the members in a class. Figure 11.5 illustrates how a public, protected, default, and private datum or method in class C1 can be accessed from a class C2 in the same package, a subclass C3 in the same package, a subclass C4 in a different package, and a class C5 in a different package.
Use the private modifier to hide the members of the class completely so they cannot be accessed directly from outside the class. Use no modifiers (the default) in order to allow the members of the class to be accessed directly from any class within the same package but not from other packages. Use the protected modifier to enable the members of the class to be
Modifier on Members in a Class
Accessed from the
Same Class
Accessed from the
Same Package
Accessed from a Subclass in a Different Package
Accessed from a Different
Package
Public ✓ ✓ ✓ ✓
Protected ✓ ✓ ✓ –
Default (no modifier) ✓ ✓ – –
Private ✓ – – –
tabLe 11.2 Data and Methods Visibility
Figure 11.5 Visibility modifiers are used to control how data and methods are accessed.
public class C1 { public int x; protected int y; int z; private int u;
protected void m() { } }
package p1;
package p2;
public class C2 { C1 o = new C1(); can access o.x; can access o.y; can access o.z; cannot access o.u;
can invoke o.m(); }
public class C3 extends C1 { can access x; can access y; can access z; cannot access u;
can invoke m(); }
public class C4 extends C1 { can access x; can access y; cannot access z; cannot access u;
can invoke m(); }
public class C5 { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; cannot access o.u;
cannot invoke o.m(); }
M11_LIAN1878_11_GE_C11.indd 465 12/29/17 4:18 PM
466 Chapter 11 Inheritance and Polymorphism
accessed by the subclasses in any package or classes in the same package. Use the public modifier to enable the members of the class to be accessed by any class.
Your class can be used in two ways: (1) for creating instances of the class and (2) for defin- ing subclasses by extending the class. Make the members private if they are not intended for use from outside the class. Make the members public if they are intended for the users of the class. Make the fields or methods protected if they are intended for the extenders of the class but not for the users of the class.
The private and protected modifiers can be used only for members of the class. The public modifier and the default modifier (i.e., no modifier) can be used on members of the class as well as on the class. A class with no modifier (i.e., not a public class) is not accessible by classes from other packages.
Note A subclass may override a protected method defined in its superclass and change its visibility to public. However, a subclass cannot weaken the accessibility of a method defined in the superclass. For example, if a method is defined as public in the superclass, it must be defined as public in the subclass.
11.14.1 What modifier should you use on a class so a class in the same package can access it, but a class in a different package cannot access it?
11.14.2 What modifier should you use so a class in a different package cannot access the class, but its subclasses in any package can access it?
11.14.3 In the following code, the classes A and B are in the same package. If the ques- tion marks in (a) are replaced by blanks, can class B be compiled? If the question marks are replaced by private, can class B be compiled? If the question marks are replaced by protected, can class B be compiled?
package p1;
public class A { ? int i;
? void m() { ... } }
package p1;
public class B extends A { public void m1(String[] args) { System.out.println(i); m(); } }
(a) (b)
11.14.4 In the following code, the classes A and B are in different packages. If the ques- tion marks in (a) are replaced by blanks, can class B be compiled? If the question marks are replaced by private, can class B be compiled? If the question marks are replaced by protected, can class B be compiled?
package p1;
public class A { ? int i;
? void m() { ... } }
package p2;
public class B extends A { public void m1(String[] args) { System.out.println(i); m(); } }
(a) (b)
change visibility
Point Check
M11_LIAN1878_11_GE_C11.indd 466 12/29/17 4:18 PM
11.15 Preventing Extending and Overriding 467
11.15 Preventing Extending and Overriding Neither a final class nor a final method can be extended. A final data field is a constant.
You may occasionally want to prevent classes from being extended. In such cases, use the final modifier to indicate a class is final and cannot be a parent class. The Math class is a final class. The String, StringBuilder, and StringBuffer classes, and all wrapper classes for primitive data types are also final classes. For example, the following class A is final and cannot be extended:
public final class A { // Data fields, constructors, and methods omitted }
You also can define a method to be final; a final method cannot be overridden by its subclasses. For example, the following method m is final and cannot be overridden:
public class Test { // Data fields, constructors, and methods omitted
public final void m() { // Do something } }
Note The modifiers public, protected, private, static, abstract, and final are used on classes and class members (data and methods), except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.
11.15.1 How do you prevent a class from being extended? How do you prevent a method from being overridden?
11.15.2 Indicate true or false for the following statements:
a. A protected datum or method can be accessed by any class in the same package.
b. A protected datum or method can be accessed by any class in different packages.
c. A protected datum or method can be accessed by its subclasses in any package.
d. A final class can have instances.
e. A final class can be extended.
f. A final method can be overridden.
Point Key
Point Check
Key Terms actual type 448 casting objects 451 constructor chaining 441 declared type 448 dynamic binding 448 inheritance 434 instanceof 452 is-a relationship 434 method overriding 443 multiple inheritance 440
override 443 polymorphism 447 protected 464 single inheritance 440 subclass 434 subtype 434 superclass 434 supertype 434 type inference 457
M11_LIAN1878_11_GE_C11.indd 467 12/29/17 4:18 PM
468 Chapter 11 Inheritance and Polymorphism
ChapTer summary 1. You can define a new class from an existing class. This is known as class inheritance.
The new class is called a subclass, child class, or extended class. The existing class is called a superclass, parent class, or base class.
2. A constructor is used to construct an instance of a class. Unlike properties and methods, the constructors of a superclass are not inherited in the subclass. They can be invoked only from the constructors of the subclasses, using the keyword super.
3. A constructor may invoke an overloaded constructor or its superclass’s constructor. The call must be the first statement in the constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor, which invokes the superclass’s no-arg constructor.
4. To override a method, the method must be defined in the subclass using the same sig- nature and the same or compatible return type as in its superclass.
5. An instance method can be overridden only if it is accessible. Thus, a private method can- not be overridden because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
6. Like an instance method, a static method can be inherited. However, a static method can- not be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.
7. Every class in Java is descended from the java.lang.Object class. If no superclass is specified when a class is defined, its superclass is Object.
8. If a method’s parameter type is a superclass (e.g., Object), you may pass an object to this method of any of the parameter’s subclasses (e.g., Circle or String). This is known as polymorphism.
9. It is always possible to cast an instance of a subclass to a variable of a superclass because an instance of a subclass is always an instance of its superclass. When casting an instance of a superclass to a variable of its subclass, explicit casting must be used to confirm your intention to the compiler with the (SubclassName) cast notation.
10. A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype.
11. When invoking an instance method from a reference variable, the actual type of the variable decides which implementation of the method is used at runtime. This is known as dynamic binding.
12. You can use obj instanceof AClass to test whether an object is an instance of a class.
13. You can use the ArrayList class to create an object to store a list of objects.
14. You can use the protected modifier to prevent the data and methods from being accessed by nonsubclasses from a different package.
15. You can use the final modifier to indicate a class is final and cannot be extended and to indicate a method is final and cannot be overridden.
M11_LIAN1878_11_GE_C11.indd 468 12/29/17 4:18 PM
Programming Exercises 469
Quiz Answer the quiz for this chapter online at the book Companion Website.
programming exerCises
Sections 11.2–11.4 11.1 (The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■■ Three double data fields named side1, side2, and side3 with default val- ues 1.0 to denote three sides of a triangle.
■■ A no-arg constructor that creates a default triangle. ■■ A constructor that creates a triangle with the specified side1, side2, and side3.
■■ The accessor methods for all three data fields. ■■ A method named getArea() that returns the area of this triangle. ■■ A method named getPerimeter() that returns the perimeter of this triangle. ■■ A method named toString() that returns a string description for the triangle.
For the formula to compute the area of a triangle, see Programming Exercise 2.19. The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.
Sections 11.5–11.14 11.2 (The Person, Student, Employee, Faculty, and Staff classes) Design a
class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (fresh- man, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the MyDate class defined in Program- ming Exercise 10.14 to create an object for date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name.
Draw the UML diagram for the classes and implement them. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.
11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was defined to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and with- draw funds. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.
Draw the UML diagram for the classes and implement them. Write a test program that creates objects of Account, SavingsAccount, and CheckingAccount and invokes their toString() methods.
M11_LIAN1878_11_GE_C11.indd 469 12/29/17 4:18 PM
470 Chapter 11 Inheritance and Polymorphism
11.4 (Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.
public static Integer max(ArrayList<Integer> list)
Write a test program that prompts the user to enter a sequence of numbers ending with 0 and invokes this method to return the largest number in the input.
11.5 (The Course class) Rewrite the Course class in Listing 10.6. Use an Array- List to replace an array to store students. Draw the new UML diagram for the class. You should not change the original contract of the Course class (i.e., the definition of the constructors and methods should not be changed, but the private members may be changed.)
11.6 (Use ArrayList) Write a program that creates an ArrayList and adds a Loan object, a Date object, a string, and a Circle object to the list, and use a loop to display all the elements in the list by invoking the object’s toString() method.
11.7 (Shuffle ArrayList) Write the following method that shuffles the elements in an ArrayList of integers:
public static void shuffle(ArrayList<Integer> list)
**11.8 (New Account class) An Account class was specified in Programming Exercise 9.7. Design a new Account class as follows:
■■ Add a new data field name of the String type to store the name of the customer.
■■ Add a new constructor that constructs an account with the specified name, id, and balance.
■■ Add a new data field named transactions whose type is ArrayList that stores the transaction for the accounts. Each transaction is an instance of the Transaction class, which is defined as shown in Figure 11.6.
VideoNote
New Account class
Figure 11.6 The Transaction class describes a transaction for a bank account.
Transaction
The get and set methods for these data �elds are provided in the class, but omitted in the UML diagram for brevity.
The date of this transaction.
The type of the transaction, such as "W" for withdrawal, "D" for deposit.
The amount of the transaction.
The new balance after this transaction.
Construct a Transaction with the speci�ed date, type, balance, and description.
The description of this transaction.
+Transaction(type: char, amount: double, balance: double, description: String)
-date: java.util.Date
-type: char
-amount: double
-balance: double
-description: String
■■ Modify the withdraw and deposit methods to add a transaction to the transactions array list.
■■ All other properties and methods are the same as in Programming Exercise 9.7.
M11_LIAN1878_11_GE_C11.indd 470 12/29/17 4:18 PM
Programming Exercises 471
Write a test program that creates an Account with annual interest rate 1.5%, balance 1000, id 1122, and name George. Deposit $30, $40, and $50 to the account and withdraw $5, $4, and $2 from the account. Print an account summary that shows the account holder name, interest rate, balance, and all transactions.
*11.9 (Largest rows and columns) Write a program that randomly fills in 0s and 1s into an n-by-n matrix, prints the matrix, and finds the rows and columns with the most 1s. (Hint: Use two ArrayLists to store the row and column indices with the most 1s.) Here is a sample run of the program:
Enter the array size n: 4
The random array is
0011
0011
1101
1010
The largest row index: 2
The largest column index: 2, 3
11.10 (Implement MyStack using inheritance) In Listing 11.10, MyStack is imple- mented using composition. Define a new stack class that extends ArrayList.
Draw the UML diagram for the classes then implement MyStack. Write a test pro- gram that prompts the user to enter five strings and displays them in reverse order.
11.11 (Sort ArrayList) Write the following method that sorts an ArrayList of numbers: public static void sort(ArrayList<Integer> list)
Write a test program that prompts the user to enter five numbers, stores them in an array list, and displays them in increasing order.
11.12 (Sum ArrayList) Write the following method that returns the sum of all num- bers in an ArrayList:
public static double sum(ArrayList<Double> list)
Write a test program that prompts the user to enter five numbers, stores them in an array list, and displays their sum.
*11.13 (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header:
public static void removeDuplicate(ArrayList<Integer> list)
Write a test program that prompts the user to enter 10 integers to a list and dis- plays the distinct integers in their input order and separated by exactly one space. Here is a sample run:
Enter 10 integers: 34 5 3 5 6 4 33 2 2 4
The distinct integers are 34 5 3 6 4 33 2
11.14 (Combine two lists) Write a method that returns the union of two array lists of integers using the following header:
public static ArrayList<Integer> union( ArrayList<Integer> list1, ArrayList<Integer> list2)
M11_LIAN1878_11_GE_C11.indd 471 12/29/17 4:18 PM
472 Chapter 11 Inheritance and Polymorphism
For example, the addition of two array lists {2, 3, 1, 5} and {3, 4, 6} is {2, 3, 1, 5, 3, 4, 6}. Write a test program that prompts the user to enter two lists, each with five integers, and displays their union. The numbers are separated by exactly one space. Here is a sample run:
Enter five integers for list1: 3 5 45 4 3
Enter five integers for list2: 33 51 5 4 13
The combined list is 3 5 45 4 3 33 51 5 4 13
*11.15 (Area of a convex polygon) A polygon is convex if it contains any line segments that connects two points of the polygon. Write a program that prompts the user to enter the number of points in a convex polygon, enter the points clockwise, then displays the area of the polygon. For the formula for computing the area of a polygon, see http://www.mathwords.com/a/area_convex_polygon.htm. Here is a sample run of the program:
Enter the number of points: 7
Enter the coordinates of the points: −12 0 −8.5 10 0 11.4 5.5 7.8 6 -5.5 0 −7 −3.5 −13.5
The total area is 292.575
**11.16 (Addition quiz) Rewrite Listing 5.1, RepeatAdditionQuiz.java, to alert the user if an answer is entered again. (Hint: use an array list to store answers.) Here is a sample run:
What is 5 + 9? 12 Wrong answer. Try again. What is 5 + 9? 34 Wrong answer. Try again. What is 5 + 9? 12 You already entered 12 Wrong answer. Try again. What is 5 + 9? 14 You got it!
**11.17 (Algebra: perfect square) Write a program that prompts the user to enter an inte- ger m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here are some sample runs:
Enter an integer m: 1500
The smallest number n for m * n to be a perfect square is 15
m * n is 22500
Enter an integer m: 63
The smallest number n for m * n to be a perfect square is 7
m * n is 441
M11_LIAN1878_11_GE_C11.indd 472 12/29/17 4:18 PM
Programming Exercises 473
**11.18 (ArrayList of Character) Write a method that returns an array list of Charac- ter from a string using the following header:
public static ArrayList<Character> toCharacterArray(String s)
For example, toCharacterArray("abc") returns an array list that contains characters 'a', 'b', and 'c'.
**11.19 (Bin packing using first fit) The bin packing problem is to pack the objects of vari- ous weights into containers. Assume each container can hold a maximum of 10 pounds. The program uses an algorithm that places an object into the first bin in which it would fit. Your program should prompt the user to enter the total number of objects and the weight of each object. The program displays the total number of containers needed to pack the objects and the contents of each container. Here is a sample run of the program:
Enter the number of objects: 6
Enter the weights of the objects: 7 5 2 3 5 8
Container 1 contains objects with weight 7 2
Container 2 contains objects with weight 5 3
Container 3 contains objects with weight 5
Container 4 contains objects with weight 8
Does this program produce an optimal solution, that is, finding the minimum number of containers to pack the objects?
M11_LIAN1878_11_GE_C11.indd 473 12/29/17 4:18 PM
A01_GIPE3898_09_SE_FM.indd 3 12/20/17 5:56 PM
This page intentionally left blank
Objectives ■■ To get an overview of exceptions and exception handling (§12.2).
■■ To explore the advantages of using exception handling (§12.2).
■■ To distinguish exception types: Error (fatal) vs. Exception ( nonfatal) and checked vs. unchecked (§12.3).
■■ To declare exceptions in a method header (§12.4.1).
■■ To throw exceptions in a method (§12.4.2).
■■ To write a try-catch block to handle exceptions (§12.4.3).
■■ To explain how an exception is propagated (§12.4.3).
■■ To obtain information from an exception object (§12.4.4).
■■ To develop applications with exception handling (§12.4.5).
■■ To use the finally clause in a try-catch block (§12.5).
■■ To use exceptions only for unexpected errors (§12.6).
■■ To rethrow exceptions in a catch block (§12.7).
■■ To create chained exceptions (§12.8).
■■ To define custom exception classes (§12.9).
■■ To discover file/directory properties, to delete and rename files/ directories, and to create directories using the File class (§12.10).
■■ To write data to a file using the PrintWriter class (§12.11.1).
■■ To use try-with-resources to ensure that the resources are closed automatically (§12.11.2).
■■ To read data from a file using the Scanner class (§12.11.3).
■■ To understand how data is read using a Scanner (§12.11.4).
■■ To develop a program that replaces text in a file (§12.11.5).
■■ To read data from the Web (§12.12).
■■ To develop a Web crawler (§12.13).
Exception Handling and Text I/O
Chapter
12
M12_LIAN1878_11_GE_C12.indd 475 12/29/17 4:19 PM
476 Chapter 12 Exception Handling and Text I/O
12.1 Introduction Exceptions are runtime errors. Exception handling enables a program to deal with runtime errors and continue its normal execution.
Runtime errors occur while a program is running if the JVM detects an operation that is impossible to carry out. For example, if you access an array using an index that is out of bounds, you will get a runtime error with an ArrayIndexOutOfBoundsException. If you enter a double value when your program expects an integer, you will get a runtime error with an InputMismatchException.
In Java, runtime errors are thrown as exceptions. An exception is an object that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate abnormally. How can you handle the exception so the program can continue to run or else terminate gracefully? This chapter introduces this subject, and text input and output.
12.2 Exception-Handling Overview Exceptions are thrown from a method. The caller of the method can catch and handle the exception.
To demonstrate exception handling, including how an exception object is created and thrown, let’s begin with the example in Listing 12.1, which reads in two integers and displays their quotient.
lisTing 12.1 Quotient.java 1 import java.util.Scanner; 2 3 public class Quotient { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter two integers 8 System.out.print("Enter two integers: "); 9 int number1 = input.nextInt(); 10 int number2 = input.nextInt(); 11 12 System.out.println(number1 + " / " + number2 + " is " + 13 (number1 / number2)); 14 } 15 }
Point Key
exception
Point Key
VideoNote
Exception-handling advantages
read two integers
integer division
Enter two integers: 5 2 5 / 2 is 2
If you entered 0 for the second number, a runtime error would occur, because you cannot divide an integer by 0. (Note a floating-point number divided by 0 does not raise an exception.) A simple way to fix this error is to add an if statement to test the second number, as shown in Listing 12.2.
Enter two integers: 3 0 Exception in thread "main" java.lang.ArithmeticException: / by zero at Quotient.main(Quotient.java:13)
M12_LIAN1878_11_GE_C12.indd 476 12/29/17 4:19 PM
12.2 Exception-Handling Overview 477
Listing 12.2 QuotientWithIf.java 1 import java.util.Scanner; 2 3 public class QuotientWithIf { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter two integers 8 System.out.print("Enter two integers: "); 9 int number1 = input.nextInt(); 10 int number2 = input.nextInt(); 11 12 if (number2 != 0) 13 System.out.println(number1 + " / " + number2 14 + " is " + (number1 / number2)); 15 else 16 System.out.println("Divisor cannot be zero "); 17 } 18 }
read two integers
test number2
Before introducing exception handling, let us rewrite Listing 12.2 to compute a quotient using a method, as shown in Listing 12.3.
Listing 12.3 QuotientWithMethod.java 1 import java.util.Scanner; 2 3 public class QuotientWithMethod { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) { 6 System.out.println("Divisor cannot be zero"); 7 System.exit(1); 8 } 9 10 return number1 / number2; 11 } 12 13 public static void main(String[] args) { 14 Scanner input = new Scanner(System.in); 15 16 // Prompt the user to enter two integers 17 System.out.print("Enter two integers: "); 18 int number1 = input.nextInt(); 19 int number2 = input.nextInt(); 20 21 int result = quotient(number1, number2); 22 System.out.println(number1 + " / " + number2 + " is " 23 + result); 24 } 25 }
quotient method
terminate the program
read two integers
invoke method
Enter two integers: 5 0 Divisor cannot be zero
M12_LIAN1878_11_GE_C12.indd 477 12/29/17 4:19 PM
478 Chapter 12 Exception Handling and Text I/O
The method quotient (lines 4–11) returns the quotient of two integers. If number2 is 0, it cannot return a value, so the program is terminated in line 7. This is clearly a problem. You should not let the method terminate the program—the caller should decide whether to termi- nate the program.
How can a method notify its caller an exception has occurred? Java enables a method to throw an exception that can be caught and handled by the caller. Listing 12.3 can be rewritten, as shown in Listing 12.4.
Listing 12.4 QuotientWithException.java 1 import java.util.Scanner; 2 3 public class QuotientWithException { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) 6 throw new ArithmeticException("Divisor cannot be zero"); 7 8 return number1 / number2; 9 } 10 11 public static void main(String[] args) { 12 Scanner input = new Scanner(System.in); 13 14 // Prompt the user to enter two integers 15 System.out.print("Enter two integers: "); 16 int number1 = input.nextInt(); 17 int number2 = input.nextInt(); 18 19 try { 20 int result = quotient(number1, number2); 21 System.out.println(number1 + " / " + number2 + " is " 22 + result); 23 } 24 catch (ArithmeticException ex) { 25 System.out.println("Exception: an integer " + 26 "cannot be divided by zero "); 27 } 28 29 System.out.println("Execution continues ..."); 30 } 31 }
quotient method
throw exception
read two integers
try block invoke method
catch block
If an Arithmetic Exception occurs
Enter two integers: 5 3 5 / 3 is 1
Enter two integers: 5 0 Divisor cannot be zero
Enter two integers: 5 3 5 / 3 is 1 Execution continues ...
M12_LIAN1878_11_GE_C12.indd 478 12/29/17 4:19 PM
12.2 Exception-Handling Overview 479
If number2 is 0, the method throws an exception (line 6) by executing
throw new ArithmeticException("Divisor cannot be zero");
The value thrown, in this case new ArithmeticException("Divisor cannot be zero"), is called an exception. The execution of a throw statement is called throwing an exception. The exception is an object created from an exception class. In this case, the excep- tion class is java.lang.ArithmeticException . The constructor ArithmeticException(str) is invoked to construct an exception object, where str is a message that describes the exception.
When an exception is thrown, the normal execution flow is interrupted. As the name sug- gests, to “throw an exception” is to pass the exception from one place to another. The statement for invoking the method is contained in a try block. The try block (lines 19–23) contains the code that is executed in normal circumstances. The exception is caught by the catch block. The code in the catch block is executed to handle the exception. Afterward, the statement (line 29) after the catch block is executed.
The throw statement is analogous to a method call, but instead of calling a method, it calls a catch block. In this sense, a catch block is like a method definition with a parameter that matches the type of the value being thrown. Unlike a method, however, after the catch block is executed, the program control does not return to the throw statement; instead, it executes the next statement after the catch block.
The identifier ex in the catch–block header
catch (ArithmeticException ex)
acts very much like a parameter in a method. Thus, this parameter is referred to as a catch– block parameter. The type (e.g., ArithmeticException) preceding ex specifies what kind of exception the catch block can catch. Once the exception is caught, you can access the thrown value from this parameter in the body of a catch block.
In summary, a template for a try-throw-catch block may look as follows:
try { Code to run; A statement or a method that may throw an exception; More code to run; } catch (type ex) { Code to process the exception; }
An exception may be thrown directly by using a throw statement in a try block, or by invoking a method that may throw an exception.
The main method invokes quotient (line 20). If the quotient method executes normally, it returns a value to the caller. If the quotient method encounters an exception, it throws the exception back to its caller. The caller’s catch block handles the exception.
Now you can see the advantage of using exception handling: It enables a method to throw an exception to its caller, enabling the caller to handle the exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know what to do in case of error. This is typically the case for the library methods. The library method can detect the error, but only the caller knows what needs to be
throw statement
exception throw exception
handle exception
catch–block parameter
advantage
Enter two integers: 5 0 Exception: an integer cannot be divided by zero Execution continues ...
M12_LIAN1878_11_GE_C12.indd 479 12/29/17 4:19 PM
480 Chapter 12 Exception Handling and Text I/O
done when an error occurs. The key benefit of exception handling is separating the detection of an error (done in a called method) from the handling of an error (done in the calling method).
Many library methods throw exceptions. Listing 12.5 gives an example that handles an InputMismatchException when reading an input.
Listing 12.5 InputMismatchExceptionDemo.java 1 import java.util.*; 2 3 public class InputMismatchExceptionDemo { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 boolean continueInput = true; 7 8 do { 9 try { 10 System.out.print("Enter an integer: "); 11 int number = input.nextInt(); 12 13 // Display the result 14 System.out.println( 15 "The number entered is " + number); 16 17 continueInput = false; 18 } 19 catch (InputMismatchException ex) { 20 System.out.println("Try again. (" + 21 "Incorrect input: an integer is required)"); 22 input.nextLine(); // Discard input 23 } 24 } while (continueInput); 25 } 26 }
create a Scanner
try block
catch block
If an InputMismatch Exception occurs
Enter an integer: 3.5 Try again. (Incorrect input: an integer is required) Enter an integer: 4 The number entered is 4
When executing input.nextInt() (line 11), an InputMismatchException occurs if the input entered is not an integer. Suppose 3.5 is entered. An InputMismatchException occurs and the control is transferred to the catch block. The statements in the catch block are now executed. The statement input.nextLine() in line 22 discards the current input line so the user can enter a new line of input. The variable continueInput controls the loop. Its initial value is true (line 6) and it is changed to false (line 17) when a valid input is received. Once a valid input is received, there is no need to continue the input.
12.2.1 What is the advantage of using exception handling? 12.2.2 Which of the following statements will throw an exception?
System.out.println(1 / 0); System.out.println(1.0 / 0);
Point Check
M12_LIAN1878_11_GE_C12.indd 480 12/29/17 4:19 PM
12.3 Exception Types 481
12.2.3 Point out the problem in the following code. Does the code throw any exceptions?
long value = Long.MAX_VALUE + 1; System.out.println(value);
12.2.4 What does the JVM do when an exception occurs? How do you catch an exception?
12.2.5 What is the output of the following code?
public class Test { public static void main(String[] args) { try { int value = 30; if (value < 40) throw new Exception("value is too small"); } catch (Exception ex) { System.out.println(ex.getMessage()); } System.out.println("Continue after the catch block"); } }
What would be the output if the line
int value = 30;
were changed to
int value = 50;
12.2.6 Show the output of the following code:
public class Test { public static void main(String[] args) { for (int i = 0; i < 2; i++) { System.out.print(i + " "); try { System.out.println(1 / 0); } catch (Exception ex) { } } } }
(a)
public class Test { public static void main(String[] args) { try { for (int i = 0; i < 2; i++) { System.out.print(i + " "); System.out.println(1 / 0); } } catch (Exception ex) { } } }
(b)
12.3 Exception Types Exceptions are objects, and objects are defined using classes. The root class for exceptions is java.lang.Throwable.
The preceding section used the classes ArithmeticException and InputMismatch- Exception. Are there any other types of exceptions you can use? Can you define your own exception classes? Yes. There are many predefined exception classes in the Java API. Figure 12.1 shows some of them, and in Section 12.9, you will learn how to define your own exception classes.
Point Key
M12_LIAN1878_11_GE_C12.indd 481 12/29/17 4:19 PM
482 Chapter 12 Exception Handling and Text I/O
Note The class names Error, Exception, and RuntimeException are somewhat confusing. All three of these classes are exceptions and all of the errors occur at runtime.
The Throwable class is the root of exception classes. All Java exception classes inherit directly or indirectly from Throwable. You can create your own exception classes by extending Exception or a subclass of Exception.
The exception classes can be classified into three major types: system errors, exceptions, and runtime exceptions.
■■ System errors are thrown by the JVM and are represented in the Error class. The Error class describes internal system errors, though such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Examples of subclasses of Error are listed in Table 12.1.
system error
exception
Figure 12.1 Exceptions thrown are instances of the classes shown in this diagram, or of subclasses of one of these classes.
Error
ClassNotFoundException
Many more classes
Many more classes
IOException
RuntimeException
LinkageError
VirtualMachineError
Object Throwable
Exception
Many more classes
ArithmeticException
NullPointerException
IndexOutOfBoundsException
IllegalArgumentException
Class Reasons for Exception
LinkageError A class has some dependency on another class, but the latter class has changed incompatibly after the compilation of the former class.
VirtualMachineError The JVM is broken or has run out of the resources it needs in order to continue operating.
tabLe 12.1 Examples of Subclasses of Error
■■ Exceptions are represented in the Exception class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program. Examples of subclasses of Exception are listed in Table 12.2.
Class Reasons for Exception
ClassNotFoundException Attempt to use a class that does not exist. This exception would occur, for example, if you tried to run a nonexistent class using the java command or if your program were composed of, say, three class files, only two of which could be found.
IOException Related to input/output operations, such as invalid input, reading past the end of a file, and opening a nonexistent file. Examples of subclasses of IOException are InterruptedIOException, EOFException (EOF is short for End of File), and FileNotFoundException.
tabLe 12.2 Examples of Subclasses of Exception
M12_LIAN1878_11_GE_C12.indd 482 12/29/17 4:19 PM
12.3 Exception Types 483
■■ Runtime exceptions are represented in the RuntimeException class, which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions normally indicate programming errors. Examples of subclasses are listed in Table 12.3.
runtime exception
Class Reasons for Exception
ArithmeticException Dividing an integer by zero. Note floating-point arithmetic does not throw exceptions (see Appendix E, Special Floating-Point Values).
NullPointerException Attempt to access an object through a null reference variable.
IndexOutOfBoundsException Index to an array is out of range.
IllegalArgumentException A method is passed an argument that is illegal or inappropriate.
tabLe 12.3 Examples of Subclasses of RuntimeException
RuntimeException, Error, and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning the compiler forces the program- mer to check and deal with them in a try-catch block or declare it in the method header. Declaring an exception in the method header will be covered in Section 12.4.
In most cases, unchecked exceptions reflect programming logic errors that are unrecover- able. For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it; an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in a program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate that you write code to catch or declare unchecked exceptions.
12.3.1 Describe the Java Throwable class, its subclasses, and the types of exceptions. 12.3.2 What RuntimeException will the following programs throw, if any?
unchecked exception checked exception
Point Check
public class Test { public static void main(String[] args) { System.out.println(1 / 0); } }
(a)
public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } }
(b)
public class Test { public static void main(String[] args) { String s = "abc"; System.out.println(s.charAt(3)); } }
(c)
public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } }
(d)
public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }
(e)
public class Test { public static void main(String[] args) { System.out.println(1.0 / 0); } }
(f)
M12_LIAN1878_11_GE_C12.indd 483 12/29/17 4:19 PM
484 Chapter 12 Exception Handling and Text I/O
12.4 More on Exception Handling A handler for an exception is found by propagating the exception backward through a chain of method calls, starting from the current method.
The preceding sections gave you an overview of exception handling and introduced several predefined exception types. This section provides an in-depth discussion of exception handling.
Java’s exception-handling model is based on three operations: declaring an exception, throwing an exception, and catching an exception, as shown in Figure 12.2.
Point Key
Figure 12.2 Exception handling in Java consists of declaring exceptions, throwing exceptions, and catching and processing exceptions.
Catch exception
Declare exception
Throw exception
method1() {
try { invoke method2; } catch (Exception ex) { Process exception; } }
method2() throws Exception {
if (an error occurs) {
throw new Exception(); } }
12.4.1 Declaring Exceptions In Java, the statement currently being executed belongs to a method. The Java interpreter invokes the main method to start executing a program. Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. Because system errors and runtime errors can happen to any code, Java does not require that you declare Error and RuntimeException (unchecked exceptions) explicitly in the method. However, all other exceptions thrown by the method must be explicitly declared in the method header so the caller of the method is informed of the exception.
To declare an exception in a method, use the throws keyword in the method header, as in this example:
public void myMethod() throws IOException
The throws keyword indicates myMethod might throw an IOException. If the method might throw multiple exceptions, add a list of the exceptions, separated by commas, after throws:
public void myMethod() throws Exception1, Exception2, ..., ExceptionN
Note If a method does not declare exceptions in the superclass, you cannot override it to declare exceptions in the subclass.
12.4.2 Throwing Exceptions A program that detects an error can create an instance of an appropriate exception type and throw it. This is known as throwing an exception