1 / 20

Creating Simple Classes

Creating Simple Classes. Data Members. Operations. Account # Balance Holder name phone# Overdrawn (true/false). Open Credit Debit Check_balance Print_account_information. Class Account. Outline of Class Account. There are 3 categories of methods. Constructors:

zack
Télécharger la présentation

Creating Simple Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Simple Classes

  2. Data Members Operations Account # Balance Holder name phone# Overdrawn (true/false) Open Credit Debit Check_balance Print_account_information Class Account Outline of Class Account

  3. There are 3 categories of methods • Constructors: • Creates an instance of a class • Name is always the same as the class itself: Account() • Class_name object_name = new Class_name(); • Mutators • Changes the state of an object • Accessors • Retrieves a value from an object and returns it, or displays values from an object. It does not change the state of an object.

  4. Define the data type of each attribute Account #: Long, integer number, each unique Balance: Float Holder name: String phone#: String Overdrawn (true/false): Boolean, to be used later

  5. Define in words the purpose of each operation • OPEN: to open an account we would need to • Get Holder information including the name of both the holder and co-holder, and their address information & phone #. • Create an account # • Set an initial balance • This method is a constructor .. the name will be “Account” instead of open

  6. Credit: • Add a specific amount to the account balance • This method is a mutator, but does not return a value • Debit: • Deducts the amount from the account balance • This method is a mutator, and does not return a value

  7. Print Account Information: • Displays all information about the account, including the holders name, phone number, the account number, and current balance. • This method is an Accessor, but there is no need to return a value • CheckBalance: • Returns the current account balance upon request • This method is an Accessor and returns a value

  8. A class definition consists of • variables called data members or instance fields. These hold the state of an object. • Variables which are class data members, only one instance of these variables are ever created. • Constructors used to create objects of the class. • Methods used to modify the state of the object, and to report on the state of the object.

  9. Basic outline of a class definition Access_Specifier class Class_name{ …. Class members go here ….. // class variables // instance variables //constructors // class - static methods // instance methods }

  10. Access specifiers • Private: If a member of a class is private, you CANNOT access it outside the class. It can only be used inside the body of the class itself. • Public: If a member of a class if public, it can be accessed outside the class. • Static: The static modifier, can be combined with either of the previous modifiers. In terms of a method, it simply means that the method can be called independently of being applied to an individual object… These are static methods that we saw in class Math. When applied to a data member (instance variable), the variable is a CLASS variable, and only one instance of the variable will be created, and will belong to and be shared by all objects of the class. These variables are created and initialized, even if no objects of a class are declared.

  11. To declare a variable in a class definition the syntax is: Access_specifier type variable_name; • To declare a method in a class definition the syntax is: Access_specifier return_type Method_name(parameter_list) { executable code }

  12. Outline of class Account public class Account { // put class variables here // put instance variables here // put constructors here // put static methods here // put instance methods here }

  13. public class Account { // CLASS VARIABLES GO HERE private static int accountNumber = 1; // INSTANCE VARIABLES GO HERE private int account; private String holderName; private String holderPhoneNumber; private double balance; private boolean overdrawn = false; // CONSTRUCTORS GO HERE public Account(String holder, String phone, double newBalance) { account = accountNumber; accountNumber++; holderName = holder; holderPhoneNumber = phone; balance = newBalance; }

  14. // STATIC METHODS GO HERE public static void ShowNextAccountNumber() { System.out.println("the next available account number is: "+ accountNumber); return; } // INSTANCE METHODS GO HERE public void credit (double amount) { balance = balance + amount; return; } public void debit (double amount) { balance = balance - amount; return; } public void printAccountInformation() { System.out.println(" The account number is : " + account); System.out.println(" The account holder is : " + holderName); System.out.println(" The account holder's phone number is : " + holderPhoneNumber); System.out.println(" The account balance is : " + balance); System.out.println(" The account is overdrawn : " + overdrawn); return; } }

  15. Testing Account public class testAccount { public static void main (String args[]) { Account myAccount; Account tomsAccount; myAccount = new Account("Camille Hayhurst", "304-276-3456", 3564.70); tomsAccount = new Account("Tom Jones", "345-332-3214", 256.00); System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); System.out.print("\n If we add another account "); Account.showNextAccountNumber(); myAccount.credit(3456.11); System.out.println("\n After the credit my account balance is: " + myAccount.checkBalance()); tomsAccount.debit(1543.0); System.out.println("\n After the debit tom's account balance is: " + tomsAccount.checkBalance()); System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); } }

  16. myAccount = new Account("Camille Hayhurst", "304-276-3456", 3564.70); accountNumber = 1 account: 1 holderName: Camille Hayhurst holderPhoneNumber: 304-276-3456 balance: 3564.70 overdrawn = false; account: 2 holderName: Tom Jones holderPhoneNumber: 345-332-3214 balance: 256.00 overdrawn = false; tomsAccount = new Account("Tom Jones", "345-332-3214", 256.00);

  17. System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); The account number is : 1 The account holder is : Camille Hayhurst The account holder's phone number is : 304-276-3456 The account balance is : 3564.7 The account is overdrawn : false The account number is : 2 The account holder is : Tom Jones The account holder's phone number is : 345-332-3214 The account balance is : 256.0 The account is overdrawn : false

  18. System.out.print("\n If we add another account "); Account.showNextAccountNumber(); If we add another account the next available account number is: 3

  19. myAccount.credit(3456.11); System.out.println("\n After the credit my account balance is: " + myAccount.checkBalance()); tomsAccount.debit(1543.0); System.out.println("\n After the debit tom's account balance is: " + tomsAccount.checkBalance()); After the credit my account balance is: 7020.81 After the debit tom's account balance is: -1287.0

  20. System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); The account number is : 1 The account holder is : Camille Hayhurst The account holder's phone number is : 304-276-3456 The account balance is : 7020.8099999999995 The account is overdrawn : false The account number is : 2 The account holder is : Tom Jones The account holder's phone number is : 345-332-3214 The account balance is : -1287.0 The account is overdrawn : false

More Related