2. What do we Need?
• MAIN
• One or more other classes.
January
233 Katrin
Slide 2
3. What's the Difference (main vs others)?
MAIN CLASS
only one per application
OTHER CLASS(ES)
as many as we need
must contain function
called main
must NOT contain
function called main*
all functions & "globals"
must be static
use static only when we
need class (as opposed
to object) members
NEVER gets instantiated
main called implicitly by
run-time system
- depends functions explicitly called
as necessary
* for now
January
233 Katrin
Slide 3
4. What's the Difference
(primitive types vs arrays & classes)?
Primitive Types
Arrays & Classes
automatically allocated
dynamically allocated
space for them created space for a reference made
when they are declared
when they are declared
int i;
String S;
that space set when
initialized
int i = 5;
new object created when
initialized
String S = "Cordie";
- NA -
new instance created with
new
January
233 Katrin
Slide 4
5. Java has ONLY pass by value:
int i
413
F(i);
String S
413
public void F( int other) {
other = 17;
}
↢
F(S);
String: Cordie
int other
413
17
↢
public void F( String other) {
other = "Fred";
}
↢
String: Fred
January
233 Katrin
Slide 5
6. public class MyInt {
int v;
public set(int n) {
v = n; }
}
F(i);
MyInt i;
But......
↢
MyInt: v: 247
413
January
↢
public void F( MyInt other) {
other .set( 247 );
}
v = n;
233 Katrin
Slide 6
8. What's in a (main) Class? [ MAIN.java ]
class MAIN {
// "wrapper"
//Attributes:
static int i;
static String S;
// Methods:
static int f1() { return 0; }
public static void main( String[] args) {
}
} // end MAIN
January
233 Katrin
Slide 8
9. What's in a (other) Class? [ Other.java ]
class Other {
// "wrapper"
//Attributes:
int i;
String S;
// Methods:
public Other () { // CONSTRUCTOR
}
public String junk() { return "Hi! Y'all!";
}
} // end Other
January
233 Katrin
Slide 9
10. Access
• global within the class
• local within the class
• public
public int i;
int i;
void F() {
int i;
}
• private
int i;
January
233 Katrin
Slide 10
11. Classes can "own" (have)
• primitive variables (like int, boolean)
• references (like String S;)
Can an object OWN another object?
(is that slavery?)
not really, not in Java, although....
If I'm the only one who "knows" this object,
I can be said to own it. (there's a difference between
knowing, owning, and being)
January
233 Katrin
Slide 11
12. Kinds of
Methods / Messages
• access / get / status
– see a private member
• modify / set
– change a private member
• forwarding
– call a member's function
January
233 Katrin
Slide 12
13. Creating a Class
• decide what the class should KNOW
– these become the attributes (data
members)
• decide what the class should Do
– these become the behaviours (methods /
functions / operations)
January
233 Katrin
Slide 13
14. Account Class
Questions to ask:
• What does an object of this sort need
to remember (what are its attributes)?
•
•
•
•
current balance [type?]
overdraft limit [type?]
account name [type?]
is account open? [type?] "status"
January
233 Katrin
Slide 14
15. Account Class
Questions to ask:
• What does an object of this sort need
to do (what are its behaviours)?
– access (the "Gets")
– open / close this account
– print (display) the balance
– accept a deposit
– process a withdrawal
January
233 Katrin
Slide 15
19. accept a deposit
•
•
•
•
make sure we're open
ask user for it
check value of deposit
add to balance
January
233 Katrin
Slide 19
20. process a withdrawal
• make sure we're open
• ask user for amount
• check value of withdrawal
– (check for overdraft)
• subtract from balance
January
233 Katrin
Slide 20
21. Do a transfer?
• How is this different from a deposit /
withdrawal?
• We only ask user for one amount...
• treat one side as a withdrawal ('cause
we already have the code for that)
• treat the other side as a deposit but we
shouldn't ask the user for the amount,
so we need another function
January
233 Katrin
Slide 21
22. What about constructors?
public Account (int ident)
{
id = ident;
}
public Account (int ident, int OD)
{
id = ident;
overdraft = OD;
}
TWO?
January
233 Katrin
Slide 22
23. What does that leave for the main class?
• in main function:
// declare an array for 5 accounts
Account[] list = new Account[MAX];
// fill the array with the actual accounts
for (int i = 0; i < MAX; i++)
list[i] = new Account(i);
January
233 Katrin
Slide 23
24. Figuring out which Account:
acctno = chooseAccount(NEW, list);
//or
acctno = chooseAccount(OLD, list);
• this will need it's own little menu
January
233 Katrin
Slide 24
25. once we have an account:
case 'w' :
System.out.print
("Doing Withdrawal....n");
amount = list[acctno].doWithdrawal();
break;
case 'o' :
System.out.print("New Account.n");
list[acctno].openAcct();
break;
January
233 Katrin
Slide 25
26. Doing a transfer - 1
System.out.print
("Account to transfer FROM:n");
acctfrom = chooseAccount(OLD, list);
System.out.print
("Account to transfer INTO:n");
acctto = chooseAccount(OLD, list);
January
233 Katrin
Slide 26
27. Doing a transfer - 2
amount = list[acctfrom].doWithdrawal();
if (amount!= 0.0)
result =
list[acctto].doTransfer(amount);
if (result != 0.0)
System.out.print
("Transfer Successful.nn");
else
/* didn't work... put it back */
list[acctfrom].doTransfer(amount);
January
233 Katrin
Slide 27
28. Putting it together: the Account:
public class Account
{
// the vars
// the constructor(s)
// the methods
}
January
233 Katrin
Slide 28
29. Putting it together: the Bank:
public class Bank
{
// the static vars
// the static methods
public static void main(String[] args)
{ Account list[] = new Account[MAX];
for (int i = 0; i < MAX; i++)
list[i] = new Account(i);
// do stuff with them
}// end main
}// end Bank
January
233 Katrin
Slide 29