Week6-Writing Classes
Week6-Writing Classes
Writing Classes
John Lewis
William Loftus
Anatomy of a Class
Encapsulation
Anatomy of a Method
Method declarations
die1.roll();
die2.roll();
System.out.println("Die One: " + die1 + ", Die Two: " + die2);
continue
die1.roll();
die2.setFaceValue(4);
System.out.println("Die One: " + die1 + ", Die Two: " + die2);
//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
continue
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue(int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}
continue
//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
die1 faceValue 5
die2 faceValue 2
RollingDice Die
faceValue : int
main (args : String[]) :
void roll() : int
setFaceValue (int value) :
void
getFaceValue() : int
toString() : String
Quick Check
What is the relationship between a class and an
object?
Anatomy of a Class
Encapsulation
Anatomy of a Method
Client Methods
Data
public private
Variables
Violate Enforce
encapsulation encapsulation
Support other
Methods
Provide services
methods in the
to clients
class
Anatomy of a Class
Encapsulation
Anatomy of a Method
compute myMethod
myMethod();
obj.doIt(); helpMe();
method
parameter list
name
return result;
}
acct1.deposit(25.85);
continue
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}
import java.text.NumberFormat;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account(String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
continue
Copyright © 2017 Pearson Education, Inc.
continue
//-----------------------------------------------------------------
// Deposits the specified amount into the account. Returns the
// new balance.
//-----------------------------------------------------------------
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount from the account and applies
// the fee. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw(double amount, double fee)
{
balance = balance - amount - fee;
return balance;
}
continue
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
balance 102.56
balance 40.00
myAcct.deposit(50);