Chapter 2: Using Objects

Download Report

Transcript Chapter 2: Using Objects

Chapter 4: Writing Classes
Writing 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
2
Objects

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
3
Classes

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
4
Classes

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
Classes

A class contains data declarations and method
declarations
int x, y;
char ch;
Data declarations
Method declarations
Defining 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
7
Defining 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
8
Data 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
Constructors

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
10
Constructors

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);
11
Classes 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
12
Classes and Objects
Objects
account_number
Class
2908371
balance
573.21
int account_number
double balance
account_number
4113787
balance
9211.84
13
Object 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);
14
References

An object reference holds the memory address of an
object
Chess_Piece bishop1 = new Chess_Piece();
bishop1


All interaction with an object occurs through a reference
variable
References have an effect on actions such as
assignment
15
Assignment


The act of assignment takes a copy of a value and
stores it in a variable
For primitive types:
num2 = num1;
Before
After
num1
num2
num1
num2
5
12
5
5
16
Reference Assignment

For object references, the value of the memory location is
copied:
bishop2 = bishop1;
Before
bishop1
bishop2
After
bishop1
bishop2
17
Writing 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
Method Control Flow

The called method could be within the same class, in
which case only the method name is needed
compute
myMethod();
myMethod
Method Control Flow

The called method could be part of another class or
object
main
obj.doIt();
doIt
helpMe();
helpMe
Method Declarations

A method declaration begins with a method header
char calc (int num1, int num2, String message)
method
name
return
type
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
Method 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;
}
The return expression must be
consistent with the return type
sum and result
are local data
They are created each time
the method is called, and
are destroyed when it
finishes executing
The 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
23
Parameters

Each time a method is called, the actual arguments in the invocation
are copied into the formal arguments
ch = obj.calc (25, count, "Hello");
char calc (int num1, int num2, String message)
{
int sum = num1 + num2;
char result = message.charAt (sum);
return result;
}
Encapsulation

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
25
Encapsulation


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
Client
Methods
Data
26
Encapsulation

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
27
Visibility 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
28
Visibility 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
29
Visibility 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
30
Writing 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
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
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println ();
System.out.println (acct1);
System.out.println (acct2);
System.out.println (acct3);
}
Account.java
import java.text.NumberFormat;
public class Account {
private NumberFormat fmt= NumberFormat.getCurrencyInstance();
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 < 0) {
// deposit value is negative
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else balance = balance + amount;
}
return balance;
Account.java
public double withdraw (double amount, double fee) {
amount += fee;
if (amount < 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 > 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;
Account.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));
}
Writing 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)

Overloaded 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
37
Overloading Methods
Version 1
Version 2
float tryMe (int x)
{
return x + .375;
}
float tryMe (int x, float y)
{
return x*y;
}
Invocation
result = tryMe (25, 4.32)
Overloaded 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
39
Overloaded 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
40
SnakeEyes.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();
Die die2 = new Die(20);
// creates a six-sided die
// creates a twenty-sided die
for (int roll = 1; roll <= ROLLS; roll++)
{
num1 = die1.roll();
num2 = die2.roll();
}
}
}
if (num1 == 1 && num2 == 1) // check for snake eyes
snakeEyes++;
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of snake eyes: " + snakeEyes);
System.out.println ("Ratio: " + (float)snakeEyes/ROLLS);
Die.java
public class Die {
private final int MIN_FACES = 4;
private int numFaces;
private int faceValue;
public Die () {
numFaces = 6;
faceValue = 1;
}
public Die (int faces) {
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;
faceValue = 1;
}
// number of sides on the die
// current value showing on the die
Die.java
public int roll ()
{
faceValue = (int) (Math.random() * numFaces) + 1;
return faceValue;
}
public int getFaceValue ()
{
return faceValue;
}
}
The 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
Method 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)

PigLatin.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);
}
}
System.out.println ();
System.out.print ("Translate another sentence (y/n)? ");
another = Keyboard.readString();
} while (another.equalsIgnoreCase("y"));
PigLatinTranslator.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 = "";
if (beginsWithVowel(word))
result = word + "yay";
else if (beginsWithPrefix(word))
result = word.substring(2) + word.substring(0,2) + "ay";
else result = word.substring(1) + word.charAt(0) + "ay";
return result;
}
PigLatinTranslator.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") ||
str.startsWith ("gr") || str.startsWith ("wh") ||
str.startsWith ("kl") || str.startsWith ("wr") ||
str.startsWith ("ph") );
}
Applet 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
Graphical 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)

LineUp.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;
figure1 = new StickFigure (100, 150, Color.red, h1);
figure2 = new StickFigure (150, 150, Color.cyan, h2);
figure3 = new StickFigure (200, 150, Color.green, h3);
figure4 = new StickFigure (250, 150, Color.yellow, h4);
setBackground (Color.black);
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
LineUp.java
}
public void paint (Graphics page)
{
figure1.draw (page);
figure2.draw (page);
figure3.draw (page);
figure4.draw (page);
}
StickFigure.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;
}
StickFigure.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, top+20, baseX, baseY-30); // trunk
page.drawLine (baseX, baseY-30, baseX-15, baseY); // legs
page.drawLine (baseX, baseY-30, baseX+15, baseY);
}
page.drawLine (baseX, baseY-70, baseX-25, baseY-70); //
arms
page.drawLine (baseX, baseY-70, baseX+20, baseY-85);
}