Title: Chapter 4: Writing Classes
1Chapter 4 Writing Classes
2Writing Classes
- We've been using predefined classes.
- Now we will learn to write our own classes to
define new objects - Chapter 4 focuses on
- class declarations
- method declarations
- instance variables
- encapsulation
- method overloading
- graphics-based objects
3Objects
- An object has
- state - descriptive characteristics
- behaviors or actions - what it can do (or be
done to it) - For example, a particular bank account
- has an account number
- has a current balance
- can be deposited into
- can be withdrawn from
4Classes
- A class is a blueprint of an object
- A model or pattern from which objects are created
- A class defines the methods and types of data
associated with an object - Instantiation
- Creating an object from a class is called
instantiation - an object is an instance of a particular class
- For example
- Account class could describe many bank accounts,
- toms_savings is a particular bank account with a
particular balance
5Classes
- The String class was provided for us by the Java
standard class library - But we can also write our own classes that define
specific objects that we need - For example, suppose we wanted to write a program
that simulates the flipping of a coin - We could write a Coin class to represent a coin
object
6Classes
- A class contains data declarations and method
declarations
Data declarations
Method declarations
7Defining Classes
- The syntax for defining a class is
- class class-name
- declarations
- constructors
- methods
-
- The variables, constructors, and methods of a
class are generically called members of the class
8Defining Classes
- class Account
- int account_number
- double balance
- Account (int account, double initial)
- account_number account
- balance initial
- // constructor Account
- void deposit (double amount)
- balance balance amount
- // method deposit
- // class Account
9Data Scope
- The scope of data
- the area in a program in which that data can be
used (referenced) - Data declared at the class level can be used by
all methods in that class - Local data or variable
- Data declared within a method can only be used in
that method
10Constructors
- A constructor
- is a special method that is used to set up a
newly created object - often sets the initial values of variables
- has the same name as the class
- does not return a value
- has no return type, not even void
- The programmer does not have to define a
constructor for a class
11Constructors
- Constructs can take parameters
- often used to initialize some variables in the
object - For example, the Account constructor could be set
up to take a parameter specifying its initial
balance - Account toms_savings new Account (1,125.89)
12Classes and Objects
- A class defines the data types for an object, but
a class does not store data values - Each object has its own unique data space(memory)
- instance variables
- The variables defined in a class because each
instance of the class has its own - Methods are shared among all objects of a class
- All methods in a class have access to all
instance variables of the class
13Classes and Objects
Objects
Class
int account_number double balance
14Object References
- The declaration of the object reference variable
and the creation of the object can be separate
activities - Account toms_savings
- toms_savings new Account (1, 125.89)
- Once an object exists, its methods can be invoked
using the dot operator - toms_savings.deposit (35.00)
15References
- An object reference holds the memory address of
an object - Chess_Piece bishop1 new Chess_Piece()
- All interaction with an object occurs through a
reference variable - References have an effect on actions such as
assignment
bishop1
16Assignment
- The act of assignment takes a copy of a value and
stores it in a variable - For primitive types
- num2 num1
17Reference Assignment
- For object references, the value of the memory
location is copied - bishop2 bishop1
18Writing Methods
- A method declaration specifies the code that will
be executed when the method is invoked (or
called) - When a method is invoked, the flow of control
jumps to the method and executes its code - When complete, the flow returns to the place
where the method was called and continues - The invocation may or may not return a value,
depending on how the method was defined
19Method Control Flow
- The called method could be within the same class,
in which case only the method name is needed
20Method Control Flow
- The called method could be part of another class
or object
21Method Declarations
- A method declaration begins with a method header
char calc (int num1, int num2, String message)
method name
parameter list
The parameter list specifies the type and name of
each parameter The name of a parameter in the
method declaration is called a formal argument
return type
22Method Declarations
- The method header is followed by the method body
char calc (int num1, int num2, String message)
int sum num1 num2 char result
message.charAt (sum) return result
sum and result are local data They are created
each time the method is called, and are destroyed
when it finishes executing
The return expression must be consistent with the
return type
23The return Statement
- The return type of a method indicates the type of
value that the method sends back to the calling
location - A method that does not return a value has a void
return type - The return statement specifies the value that
will be returned - Its expression must conform to the return type
24Parameters
- Each time a method is called, the actual
arguments in the invocation are copied into the
formal arguments
ch obj.calc (25, count, "Hello")
25Encapsulation
- You can take one of two views of an object
- internal - the structure of its data, the
algorithms used by its methods - external - the interaction of the object with
other objects in the program - From the external view, an object is an
encapsulated entity, providing a set of specific
services - These services define the interface to the object
26Encapsulation
- An encapsulated object can be thought of as a
black box - Its inner workings are hidden to the client,
which only invokes the interface methods
Methods
Client
Data
27Encapsulation
- An object should be self-governing
- any changes to the object's state (its
variables) should be accomplished by that
object's methods - We should make it difficult, if not impossible,
for another object to "reach in" and alter an
object's state - The user, or client, of an object
- can request its services,
- but it should not have to be aware of how those
services are accomplished
28Visibility Modifiers
- We accomplish encapsulation through the
appropriate use of visibility modifiers - A modifier
- a Java reserved word that specifies particular
characteristics of a programming construct - We've used the modifier final to define a
constant - Java has three visibility modifiers
- public, private, and protected
- We will discuss the protected modifier later
29Visibility Modifiers
- public visibility
- Members of a class that are declared with public
can be accessed from anywhere - private visibility
- Members of a class that are declared with private
can only be accessed from inside the class - default visibility
- Members declared without a visibility modifier
can be accessed by any class in the same package - Java modifiers are discussed in detail in
Appendix F
30Visibility Modifiers
- As a general rule, no object's data should be
declared with public visibility - Methods that provide the object's services
- usually declared with public visibility so that
they can be invoked by clients - Public methods are also called service methods
- Other methods, called support methods, can be
defined that assist the service methods - they should not be declared with public
visibility
31Writing Classes
- See BankAccounts.java (page 188)
- See Account.java (page 189)
- An aggregate object is an object that contains
references to other objects - An aggregate object represents a has-a
relationship - An Account object is an aggregate object
- it contains a reference to a String object (that
holds the owner's name) - A bank account has a name
32 BankAccouts.java
- import Account
- public class BankAccounts
- public static void main (String args)
- Account acct1 new Account ("Ted Murphy",
72354, 102.56) - Account acct2 new Account ("Jane Smith",
69713, 40.00) - Account acct3 new Account ("Edward
Demsey", 93757, 759.32) - acct1.deposit (25.85)
- double smithBalance acct2.deposit
(500.00) - System.out.println ("Smith balance after
deposit " smithBalance) -
- System.out.println ("Smith balance after
withdrawal " - acct2.withdraw (430.75, 1.50))
- acct3.withdraw (800.00, 0.0) // exceeds
balance
33Account.java
- import java.text.NumberFormat
- public class Account
- private NumberFormat fmt NumberFormat.getCurre
ncyInstance() - private final double RATE 0.045 //
interest rate of 4.5 - private long acctNumber
- private double balance
- private String name
- public Account (String owner, long account,
double initial) - name owner
- acctNumber account
- balance initial
-
- public double deposit (double amount)
- if (amount lt 0) // deposit value is
negative - System.out.println ()
34Account.java
- public double withdraw (double amount, double
fee) - amount fee
- if (amount lt 0) // withdraw value is
negative - System.out.println ()
- System.out.println ("Error Withdraw
amount is invalid.") - System.out.println ("Account "
acctNumber) - System.out.println ("Requested "
fmt.format(amount)) -
- else if (amount gt balance) // withdraw
value exceeds balance - System.out.println ()
- System.out.println ("Error
Insufficient funds.") - System.out.println ("Account "
acctNumber) - System.out.println ("Requested "
fmt.format(amount)) - System.out.println ("Available "
fmt.format(balance)) -
- else balance balance - amount
- return balance
35Account.java
- public double addInterest ()
- balance (balance RATE)
- return balance
-
- public double getBalance ()
-
- return balance
-
-
- public long getAccountNumber ()
-
- return acctNumber
-
- public String toString ()
-
- return (acctNumber "\t" name "\t"
fmt.format(balance))
36Writing Classes
- Sometimes an object has to interact with other
objects of the same type - For example, we might add two Rational number
objects together as follows - r3 r1.add(r2)
- One object (r1) is executing the method and
another (r2) is passed as a parameter - See RationalNumbers.java (page 196)
- See Rational.java (page 197)
37Overloaded Methods
- Method overloading
- the process of using the same method name for
multiple methods - Signature
- The signature of each overloaded method must be
unique - The signature is based on the number, type, and
order of the parameters - The compiler must be able to determine which
version of the method is being invoked by
analyzing the parameters - The return type of the method is not part of the
signature
38Overloading Methods
39Overloaded Methods
- The println method is overloaded
- println (String s)
- println (int i)
- println (double d)
- etc.
- The lines
- System.out.println ("The total is")
- System.out.println (total)
- invoke different versions of the println method
40Overloaded Methods
- Constructors are often overloaded to provide
multiple ways to set up a new object - Account (int account)
- account_number account
- balance 0.0
- // constructor Account
- Account (int account, double initial)
- account_number account
- balance initial
- // constructor Account
41SnakeEyes.java
- import Die
- public class SnakeEyes
- public static void main (String args)
- final int ROLLS 500
- int snakeEyes 0, num1, num2
- Die die1 new Die() // creates a
six-sided die - Die die2 new Die(20) // creates a
twenty-sided die - for (int roll 1 roll lt ROLLS roll)
-
- num1 die1.roll()
- num2 die2.roll()
- if (num1 1 num2 1) // check
for snake eyes - snakeEyes
-
42Die.java
- public class Die
- private final int MIN_FACES 4
- private int numFaces // number of sides on
the die - private int faceValue // current value
showing on the die - public Die ()
- numFaces 6
- faceValue 1
-
- public Die (int faces)
- if (faces lt MIN_FACES)
- numFaces 6
- else
- numFaces faces
- faceValue 1
-
43Die.java
- public int roll ()
-
- faceValue (int) (Math.random()
numFaces) 1 - return faceValue
-
- public int getFaceValue ()
-
- return faceValue
-
-
44The StringTokenizer Class
- The next example makes use of the StringTokenizer
class, which is defined in the java.util package - A StringTokenizer object separates a string into
smaller substrings (tokens) - By default, the tokenizer separates the string at
white space - The StringTokenizer constructor takes the
original string to be separated as a parameter - Each call to the nextToken method returns the
next token in the string
45Method Decomposition
- A method should be relatively small, so that it
can be readily understood as a single entity - A potentially large method should be decomposed
into several smaller methods as needed for
clarity - Therefore, a service method of an object may call
one or more support methods to accomplish its
goal - See PigLatin.java (page 207)
- See PigLatinTranslator.java (page 208)
46PigLatin.java
- import PigLatinTranslator
- import cs1.Keyboard
- public class PigLatin
-
- public static void main (String args)
- String sentence, result, another
- PigLatinTranslator translator new
PigLatinTranslator() - do
- System.out.println ()
- System.out.println ("Enter a sentence
(no punctuation)") - sentence Keyboard.readString()
- System.out.println ()
- result translator.translate
(sentence) - System.out.println ("That sentence in
Pig Latin is") - System.out.println (result)
47PigLatinTranslator.java
- import java.util.StringTokenizer
- public class PigLatinTranslator
- public String translate (String sentence)
- String result ""
- sentence sentence.toLowerCase()
- StringTokenizer tokenizer new
StringTokenizer (sentence) - while (tokenizer.hasMoreTokens())
- result translateWord
(tokenizer.nextToken()) - result " "
-
- return result
-
- private String translateWord (String word)
- String result ""
48PigLatinTranslator.java
- private boolean beginsWithVowel (String word)
- String vowels "aeiouAEIOU"
- char letter word.charAt(0)
- return (vowels.indexOf(letter) ! -1)
-
- private boolean beginsWithPrefix (String str)
- return ( str.startsWith ("bl")
str.startsWith ("pl") - str.startsWith ("br")
str.startsWith ("pr") - str.startsWith ("ch")
str.startsWith ("sh") - str.startsWith ("cl")
str.startsWith ("sl") - str.startsWith ("cr")
str.startsWith ("sp") - str.startsWith ("dr")
str.startsWith ("sr") - str.startsWith ("fl")
str.startsWith ("st") - str.startsWith ("fr")
str.startsWith ("th") - str.startsWith ("gl")
str.startsWith ("tr")
49Applet Methods
- In previous examples we've used the paint method
of the Applet class to draw on an applet - The Applet class has several methods that are
invoked automatically at certain points in an
applet's life - The init method, for instance, is executed only
once when the applet is initially loaded - The Applet class also contains other methods that
generally assist in applet processing
50Graphical Objects
- Any object we define by writing a class can have
graphical elements - The object must simply obtain a graphics context
(a Graphics object) in which to draw - An applet can pass its graphics context to
another object just as it can any other parameter - See LineUp.java (page 212)
- See StickFigure.java (page 215)
51LineUp.java
- import StickFigure
- import java.applet.Applet
- import java.awt.
- public class LineUp extends Applet
- private final int APPLET_WIDTH 400
- private final int APPLET_HEIGHT 150
- private final int HEIGHT_MIN 100
- private final int VARIANCE 30
- private StickFigure figure1, figure2, figure3,
figure4 - public void init ()
- int h1, h2, h3, h4 // heights of stick
figures - h1 (int) (Math.random() VARIANCE)
HEIGHT_MIN - h2 (int) (Math.random() VARIANCE)
HEIGHT_MIN - h3 (int) (Math.random() VARIANCE)
HEIGHT_MIN - h4 (int) (Math.random() VARIANCE)
HEIGHT_MIN
52LineUp.java
- public void paint (Graphics page)
-
- figure1.draw (page)
- figure2.draw (page)
- figure3.draw (page)
- figure4.draw (page)
-
-
53StickFigure.java
- import java.awt.
- public class StickFigure
-
- private int baseX // center of figure
- private int baseY // floor (bottom of
feet) - private Color color // color of stick
figure - private int height // height of stick
figure - public StickFigure (int center, int bottom,
Color shade, int size) -
- baseX center
- baseY bottom
- color shade
- height size
-
-
54StickFigure.java
- public void draw (Graphics page)
-
- int top baseY - height // top of head
- page.setColor (color)
- page.drawOval (baseX-10, top, 20, 20) //
head - page.drawLine (baseX, top20, baseX,
baseY-30) // trunk - page.drawLine (baseX, baseY-30, baseX-15,
baseY) // legs - page.drawLine (baseX, baseY-30, baseX15,
baseY) - page.drawLine (baseX, baseY-70, baseX-25,
baseY-70) // arms - page.drawLine (baseX, baseY-70, baseX20,
baseY-85) -
-