CS Lesson: Creating Your First Class in Java

612 views

Published on

A brief introduction to creating a first class in the programming language Java.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
612
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CS Lesson: Creating Your First Class in Java

  1. 1. Creating Your first Class January 233 Katrin Slide 1
  2. 2. What do we Need? • MAIN • One or more other classes. January 233 Katrin Slide 2
  3. 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. 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. 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. 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
  7. 7. Back to classes...... January 233 Katrin Slide 7
  8. 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. 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. 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. 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. 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. 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. 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. 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
  16. 16. The "Gets" • • • • Balance Limit Name isOpen January 233 Katrin Slide 16
  17. 17. Open & Close • change the "status" to open: just change status to close: must be empty first January 233 Katrin Slide 17
  18. 18. print • do some output January 233 Katrin Slide 18
  19. 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. 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. 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. 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. 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. 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. 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. 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. 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. 28. Putting it together: the Account: public class Account { // the vars // the constructor(s) // the methods } January 233 Katrin Slide 28
  29. 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
  30. 30. Bank Class The Classes: chooseAccount doTransfer main Account Class balance limit open? name Account(s) the "Gets" doTransfer openAccount closeAccount showBalance January 233 Katrin doDeposit doWithdrawal Slide 30

×