java programming chapter 8 object oriented programming

78
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321300! Chapter 8 Objects and Classes 1

Upload: saaw

Post on 07-Oct-2015

25 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Java Programming -Chapter 8 : Object Oriented Programming

TRANSCRIPT

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Motivations
After learning the preceding chapters, you are capable of solving many programming problems using selections, loops, methods, and arrays. However, these ava features are not su!cient for developing graphical user interfaces and large scale software systems. "uppose you want to develop a graphical user interface as shown below. How do you program it#
2
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Objectives
 $o describe objects and classes, and use classes to model objects %&8.'(.
 $o use )M* graphical notations to describe classes and objects %&8.'(.
 $o demonstrate de+ning classes and creating objects %&8.(.  $o create objects using constructors %&8.-(.  $o access objects via object reference variables %&8.(.  $o de+ne a reference variable using a reference type %&8../(.  $o access an object0s data and methods using the object member
access operator %.( %&8..'(.  $o de+ne data +elds of reference types and assign default values
for an object0s data +elds %&8..(.  $o distinguish between object reference variables and primitive
data type variables %&8..-(.  $o use classes 1ate, 2andom, and 3rame in the ava library
%&8.4(.  $o distinguish between instance and static variables and methods
%&8.5(.  $o de+ne private data +elds with appropriate get and set
methods %&8.8(.  $o encapsulate data +elds to ma6e classes easy to maintain
%&8.7(. 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 4
OO ;rogramming Concepts
using Classes and Objects.
" A class is used to describe someting in te !orld" suc as
occurrences" things" external entities" roles"
organization units" places or structures.
A class describes te structure and beavior o# a set o#
similar objects.
to te actual object" itsel#.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! %
Once a class o# items is de#ined" a speci#ic instance o#
te class can be de#ined. An instance is also called
&object'.
An object as a uniue identit" state" and beaviors.
*e  state  o# an object consists o# a set o# data  fields 
(also +no!n as properties) !it teir current values.
*e behavior  o# an object is de#ined b a set o# metods"
in oter !ords !at te object does.
$n oter !ords an  object is a so#t!are bundle o#
variables and related metods.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! ,
 Type Example of class
entities  1oor 3ire door
2oles $eacher Mohammad, Hassan
1epartment C", ?$ , ?"
#he ta$le gives some e%am&les o' classes and o$ects
lass and *$ect E%am&le
2eal @orld Objects
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
UML Class and Object Diagrams:
A class diagram is simpl a rectangle divided into tree
compartments. *e topmost compartment contains te name o# te
class. *e middle compartment contains a list o# attributes (member
variables)" and te bottom compartment contains a list o# operations
(member #unctions). *e purpose o# a class diagram is to so! te
classes !itin a model.
  .tudent
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 6
Kinds of classes:
Standard Class: on7t reinvent te !eel. 8en tere are e9isting objects tat satis# our needs" use tem. 5earning o! to use standard :ava classes is te #irst step to!ard mastering OOP. ome o# te standard classes are :OptionPane " tring " canner etc
rogrammer defined Class: sing just te tring"  :OptionPane"
canner" :;rame and oter standard classes !ill not meet all o# our
needs. 8e need to be able to de#ine our o!n classes customi<ed #or
our applications. Classes !e de#ine ourselves are called programmer-
de#ined classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! =
Im&ort
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
<=ample of a Class
String name ;
name!n;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
are creating here.
is created.
car' ! ne) &ehicle( ;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
allocated in memor.
allocated in memor.
and the identi'ier hussain is
set to re'er to it.
 
and the identi'ier hussain is
set to re'er to it.
1
2
hussain
1
: Student2
Student hussain= new Student();
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 14
ccessing members of a class . (dot) operator is used to access public members o# a class
A general snta9 to access public members o# a class is as #ollo!s
// s is object of Student class
Student s= new Student();
  object-ef&ar.data
  object-ef&ar.methodName(arguments
  s.get/ame()@ accessing metod
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1%
store name o# a student
tring name @
 public void set/ame(tring n) B
  namen@ D
class *esttudent B
B
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1,
bike2bike1
Bicycle
ownerName
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Objective 1emonstrate creating objects, accessing
data, and using methods.
// Data Member  ri!ate Strin" owner#ame;
  //$eturns name of this bicycle%s owner   ublic Strin" "et&wner#ame( ) {  
return owner#ame;   }
 //'ssi"ns name of this bicycle%s owner   ublic !oid  set&wner#ame(Strin" name) {  
owner#ame = name;   } }
Strin" owner+, owner-;
  bi*e+ = new Bicycle( ); //.reate and assi"n !alues to bi*e+
  bi*e+set&wner#ame(0'dam Smith0);
 
 bi*e- = new Bicycle( ); //.reate and assi"n !alues to bi*e-
  bi*e-set&wner#ame(0Ben 1ones0);
  owner+ =  bi*e+"et&wner#ame( ); //&utut the information
owner- = bi*e-"et&wner#ame( );
 Systemoutrintln(owner+ 2 0 owns a  bicycle0);
}
}
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Constructors
16
*e constructor metod is li+e a normal public metod
e9cept tat it sares te same name as te class and it
as no return value not even void since constructors
never return a value. $t can ave none" one or man
 parameters. Constructors are a special +ind o# metods
tat are invo+ed to construct objects.
 /ormall #or a constructor metod to be use#ul !e
!ould design it so tat it e9pects parameters. *e values
 passed troug tese parameters can be used to set te
values o# te private #ields.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1=
A constructor !it no parameters is re#erred to as a
no-arg constructor . Point to be noted about
constructor.
1. Constructors must ave te same name as te class
itsel#.
2. Constructors do not ave a return tpeHnot even
void.
3. Constructors are invo+ed (called) using te ne!
operator !en an object is created.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
tis case" a no-arg constructor !it an empt bod
is implicitl declared in te class. *is constructor"
called a default constructor " is provided
automaticall only if no constructors are explicitly
declared in the class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 21
class $ccount {
  o)nerName ! nassigned;   balance ! 0.0;
  " public void add(double amt {   balance ! balance 1 amt;   "   public void deduct(double amt {   balance ! balance 2 amt;   "
public void set3alance (double bal {  balance ! bal;
  "   public void set4)nerName (String name {
o)nerName ! name;   " 
return o)nerName;   " "
class 3est'ccount{
  /4 3his samle ro"ram uses both the Bicycle and 'ccount classes 4/
 ublic static !oid main(Strin" ar"s) {  
 'ccount acct;  
acct = new 'ccount( );
  acctset&wner#ame(my#ame);   acctsetBalance(-5666);  
} }
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 22
  class 5ircle {
double radius ! '.0;
return radius * radius * 6.'7'89;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Circle ourCircle ne! Circle()@
ourCircle
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
2ecall that you use Math.methodame%arguments( %e.g., Math.pow%, '.((
to invo6e a method in the Math class. Can you invo6e getArea%( using Circle/.getArea%(# $he answer is no. All the methods used before this chapter are static methods, which are de+ned using the static 6eyword. However, getArea%( is non9static. ?t must be invo6ed from an object using
object2efDar.methodame%arguments( %e.g., myCircle.getArea%((.
More e=planations will be given in the section on E"tatic Dariables, Constants, and Methods.F
3>
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
2eference 1ata 3ields
 $he data +elds can be of reference types. 3or e=ample, the following "tudent class contains a data +eld name of the "tring type.
31
"
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he null Dalue
?f a data +eld of a reference type does not reference any object, the data +eld holds a special literal value, null.
32
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1efault Dalue for a 1ata 3ield
 $he default value of a data +eld is null for a reference type, : for a numeric type, false for a boolean type, and Gu::::G for a char type. However, ava assigns no default value to a local variable inside a method.
33
  Student student ! ne) Student(;
"
"
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
  S>stem.out.println(@ is 1 @;
S>stem.out.println(> is 1 >;
initiali<ed
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1ierences between Dariables of ;rimitive 1ata $ypes and Object $ypes
3%
Object tpe Circle c c re#erence
Created using ne! Circle()
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
3,
Je#ore0
Je#ore0
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Iarbage Collection As shown in the previous +gure, after the assignment statement c/ J c', c/ points to the same object referenced by c'. $he object previously referenced by c/ is no longer referenced. $his object is 6nown as garbage. Iarbage is automatically collected by DM.
3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Iarbage Collection, cont   $?; ?f you 6now that an object is no longer needed, you can e=plicitly assign null to a reference variable for the object.  $he DM will automatically collect the space if the object is not referenced by any variable.
36
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he 1ate Class
 ava provides a system9independent encapsulation of date and time in the java.util.1ate class. Kou can use the 1ate class to create an instance for the current date and time and use its to"tring method to return the date and time as a string.
3=
Constructs a ate object #or te current time.
Constructs a ate object #or a given time in
milliseconds elapsed since :anuar 1" 1=>" K4*.
?eturns a string representing te date and time.
?eturns te number o# milliseconds since :anuar 1"
1=>" K4*.
*e G sign indicates
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he 1ate Class <=ample
S>stem.out.println(date.toString(;
displays a string li6e Sun Mar 09 '6A80A'9 BST 006.
4>
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he 2andom Class
 Kou have used Math.random%( to obtain a random double value between :.: and /.: %e=cluding /.:(. A more useful random number generator is provided in the java.util.2andom class.
41
Gne9t;loat()0 #loat
Gne9tJoolean()0 boolean
Constructs a ?andom object !it te current time as its seed.
Constructs a ?andom object !it a speci#ied seed.
?eturns a random int value.
?eturns a random int value bet!een > and n (e9clusive).
?eturns a random long value.
?eturns a random double value bet!een >.> and 1.> (e9clusive).
?eturns a random #loat value bet!een >.>; and 1.>; (e9clusive).
?eturns a random boolean value.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he 2andom Class <=ample
?f two 2andom objects have the same seed, they will generate identical seLuences of numbers. 3or e=ample, the following code creates two 2andom objects with the same seed .
42
  S>stem.out.print(random'.ne@tEnt('000 1 ;
for (int i ! 0; i D '0; i11
  S>stem.out.print(random.ne@tEnt('000 1 ;
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
1isplaying I)? Components
@hen you develop programs to create graphical user interfaces, you will use ava classes such as  3rame, Button, 2adioButton, ComboBo=, and *ist to create frames, buttons, radio buttons, combo bo=es, lists, and so on. Here is an e=ample that creates two windows using the 3rame class.
43
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 44
## $enerate frame using standard class %&rame
import java9.s!ing.:;rame@
:;rame #rame1 ne( :;rame()@
#rame1.set*itle(L8indo! 1L)@
#rame1.setisible(true)@
#rame2.setisible(true)@ D
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
?nstance Dariables, and Methods
$nstance metods are invo+ed b an instance o#
te class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
%4
tatic variables are sared b all te instances o# te
class.
tatic metods are not tied to a speci#ic object.
tatic constants are #inal variables sared b all te
instances o# te class.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
%%
use te static modi#ier.
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
%,
 
get/umberO#Objects()0 int
circle2
radius %
is 2.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
%
 
Construct a circle !it radius 1
Circle2() B
 radius 1.>@
Circle2(double ne!?adius) B
  ?eturn numberO#Objects
return numberO#Objects@ D
double getArea() B
 D
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! %6
public class *estCircle2 B ain metod
 public static 'oid main(tringEF args) B
stem.out.println(LJe#ore creating objectsL)@
stem.out.println(L*e number o# Circle objects is L G Circle2.numberO#Objects)@
  Create c1
stem.out.println(LnA#ter creating c1L)@
 stem.out.println(Lc10 radius (L G c1.radius G L) and number o# Circle objects (L G
c1.numberO#Objects G L)L)@
c1.radius =@ ispla c1 and c2 A;*I? c2 !as created
 stem.out.println(LnA#ter creating c2 and modi#ing c1L)@
 stem.out.println(Lc10 radius (L G c1.radius G L) and number o# Circle objects (L G
c1.numberO#Objects G L)L)@
stem.out.println(Lc20 radius (L G c2.radius G L) and number o# Circle objects (L G
c2.numberO#Objects G L)L)@ D
D
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
%=
public
*e class" data" or metod is visible to an class in an
 pac+age.
private 
*e data or metods can be accessed onl b te declaring
class.
*e get and set metods are used to read and modi# private
 properties.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! ,>
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $o ma6e class easy to maintain.
?nternal details of a class are declared private and hidden from the clients. $his is information hiding.
,1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
,2
  Circle
*e number o# circle objects created.
Constructs a de#ault circle object.
Constructs a circle object !it te speci#ied radius.
?eturns te radius o# tis circle.
ets a ne! radius #or tis circle.
?eturns te number o# circle objects created.
?eturns te area o# tis circle.
*e - sign indicates
 private modi#ier
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
;assing Objects to Methods
,3
As we can pass int and double values, we can also pass an object to a method.
@hen we pass an object, we are actually passing the reference %name( of an object  – it means a duplicate of an object is O$
created in the called method.
;assing by value for primitive type value %the value is passed to the parameter(
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
,4
Student
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
;assing a "tudent Object
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
;assing a "tudent Object
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
;assing a "tudent Object
class Fibrar>5ard {   private Student o)ner;   public void set4)ner(Student st {
  o)ner ! st;   "
data mem$er 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! ,6
class Student {
public void setName(String studentName {
public void setBmail(String address {
  private Student o)ner;  
  public String get4)nerName( {
  public int getNumber4f3ooGs( {
  public void set4)ner(Student stud {
public String toString( {
*BmailA 1 o)ner.getBmail( 1 n 1
  3ooGs 3orro)edA 1 borro)5nt;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! ,=
  Student stud;
  stud.setEmail("[email protected]");
second card, too
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
@e pass the same "tudent object to card/ and card'
• +ince e are actuall &assing
a re'erence to the same
o$ect, it results in oner o'
to Li$rarard o$ects
&ointing to the same +tudent
o$ect
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Array of Objects ?n ava, in addition to arrays of primitive data
types, we can declare arrays of objects
An array of primitive data is a powerful tool, but an array of objects is even more powerful.
 $he use of an array of objects allows us to model the application more cleanly and logically.
1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300! 2
class 8erson {
public void setName(String personName
public void set$ge(int person$ge {
public void setKender(char
{ Herson latte;
latte.getKender(;""
methods.
methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
 $he ;erson Class @e will use ;erson objects to illustrate the use of an array
of objects.
Herson latte;
S>stem.out.println( Se@ A  1 latte.getKender( ;
 
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Code
allocated et.
allocated et.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Code
created, $ut the Person
o$ects themselves are
created, $ut the Person
o$ects themselves are
&erson
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Code
 
created and the re'erence
&osition 0.
created and the re'erence
&osition 0.
&erson
&erson
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Herson < =prsn!ne) Herson <0=;
Hrsn<0= ! ne) Herson( ;
Hrsn<0=.set$ge(0;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 01321300!
Q*e cKra!-Rill Companies"
reproduction or displa. Capter 1> - 6
 
  int age;
  char gender;
name ! +4ptionHane.sho)Enput#ialog(null%Bnter nameA;
  age ! Enteger.parseEnt(+4ptionHane.sho)Enput#ialog(null%Bnter ageA;
  inpStr ! +4ptionHane.sho)Enput#ialog(null%Bnter genderA;
  gender ! inpStr.char$t(0;