cs lesson: creating your first class in java
DESCRIPTION
A brief introduction to creating a first class in the programming language Java.TRANSCRIPT
January 28, 2003 233 Katrin Becker Slide 1
Creating Your first Class
January 28, 2003 233 Katrin Becker Slide 2
What do we Need?
• MAIN
• One or more other classes.
January 28, 2003 233 Katrin Becker Slide 3
What's the Difference (main vs others)?
MAIN CLASS OTHER CLASS(ES)
only one per application 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 - depends -
main called implicitly by run-time system
functions explicitly called as necessary
* for now
January 28, 2003 233 Katrin Becker Slide 4
What's the Difference (primitive types vs arrays & classes)?
Primitive Types Arrays & Classes
automatically allocated dynamically allocated
space for them created when they are declared
int i;
space for a reference made when they are declared
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 28, 2003 233 Katrin Becker Slide 5
Java has ONLY pass by value:
413int i
public void F( int other) { other = 17;}
413
String S
public void F( String other) { other = "Fred";}
String: Cordie
↢ ↢
int other
41317
String: Fred
↢↢
F(i);
F(S);
January 28, 2003 233 Katrin Becker Slide 6
But......
MyInt i;
public void F( MyInt other) { other .set( 247 );}
MyInt: v: 413
↢ ↢F(i);
public class MyInt {int v;
public set(int n) {v = n; }
}
v = n;247
January 28, 2003 233 Katrin Becker Slide 7
Back to classes......
January 28, 2003 233 Katrin Becker Slide 8
What's in a (main) Class? [ MAIN.java ]
//Attributes:static int i;static String S;
// Methods:static int f1() { return 0; }
public static void main( String[] args) {}
class MAIN {
} // end MAIN
// "wrapper"
January 28, 2003 233 Katrin Becker Slide 9
What's in a (other) Class? [ Other.java ]
//Attributes:int i;String S;
// Methods:public Other () { // CONSTRUCTOR}public String junk() { return "Hi! Y'all!";}
class Other {
} // end Other
// "wrapper"
January 28, 2003 233 Katrin Becker Slide 10
Access
• global within the class
• local within the class
• public
• private
int i;
void F() {int i;
}public int i;
int i;
January 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 12
Kinds ofMethods / Messages
• access / get / status – see a private member
• modify / set – change a private member
• forwarding – call a member's function
January 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 14
Account ClassQuestions 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 28, 2003 233 Katrin Becker Slide 15
Account ClassQuestions 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 28, 2003 233 Katrin Becker Slide 16
The "Gets"
• Balance
• Limit
• Name
• isOpen
January 28, 2003 233 Katrin Becker Slide 17
Open & Close
• change the "status"
to open: just change status
to close: must be empty first
January 28, 2003 233 Katrin Becker Slide 18
• do some output
January 28, 2003 233 Katrin Becker Slide 19
accept a deposit
• make sure we're open
• ask user for it
• check value of deposit
• add to balance
January 28, 2003 233 Katrin Becker Slide 20
process a withdrawal
• make sure we're open
• ask user for amount
• check value of withdrawal– (check for overdraft)
• subtract from balance
January 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 22
What about constructors?
public Account (int ident)
{
id = ident;
}
public Account (int ident, int OD)
{
id = ident;
overdraft = OD;
}TWO?
January 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 24
Figuring out which Account:
acctno = chooseAccount(NEW, list);
//or
acctno = chooseAccount(OLD, list);
• this will need it's own little menu
January 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 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 28, 2003 233 Katrin Becker Slide 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.\n\n");
else
/* didn't work... put it back */
list[acctfrom].doTransfer(amount);
January 28, 2003 233 Katrin Becker Slide 28
Putting it together: the Account:
public class Account
{
// the vars
// the constructor(s)
// the methods
}
January 28, 2003 233 Katrin Becker Slide 29
Putting it together: the Bank:
public class Bank{ // the static vars// the static methodspublic 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 28, 2003 233 Katrin Becker Slide 30
The Classes:Bank Class
chooseAccount
main
doTransfer balance
openAccount
the "Gets"
Account Class
closeAccount
showBalance
doDeposit
doWithdrawal
doTransfer
open?
limit
nameAccount(s)