Week 1 - Review Classes
Week 1 - Review Classes
John Lewis
William Loftus
numChars = title.length()
num1 38
num1 38
Before:
num2 96
num2 = num1;
num1 38
After:
num2 38
name2 = name1;
continued
26
the final frontier.
SPACE, THE FINAL FRONTIER.
26
import java.util.Random;
num1 = generator.nextInt();
System.out.println ("A random integer: " + num1);
num1 = generator.nextInt(10);
System.out.println ("From 0 to 9: " + num1);
continued
num1 = generator.nextInt(10) + 1;
System.out.println ("From 1 to 10: " + num1);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
gen.nextInt(25)
gen.nextInt(6) + 1
gen.nextInt(100) + 10
gen.nextInt(50) + 100
gen.nextInt(10) – 5
gen.nextInt(22) + 12
Range
0 to 12
1 to 20
15 to 20
-10 to 0
Range
0 to 12 gen.nextInt(13)
1 to 20 gen.nextInt(20) + 1
15 to 20 gen.nextInt(6) + 15
-10 to 0 gen.nextInt(11) – 10
import java.util.Scanner;
continued
Anatomy of a Class
Encapsulation
Anatomy of a Method
Method declarations
//-----------------------------------------------------------------
// 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.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);
die1.roll();
die2.setFaceValue(4);
System.out.println ("Die One: " + die1 + ", Die Two: " + die2);
Sample Run
Die One: 5, Die Two: 2
Die One: 1, Die Two: 4
Sum: 5
Die One: 4, Die Two: 2
New sum: 6
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
obj.doIt(); helpMe();
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();
main
doIt
obj.doIt(); helpMe();
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 © 2012 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);