Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone#...
-
Upload
oliver-norton -
Category
Documents
-
view
216 -
download
2
Transcript of Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone#...
![Page 1: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/1.jpg)
Creating Simple Classes
![Page 2: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/2.jpg)
Outline of Class Account
Class Account
Account #BalanceHolder namephone#Overdrawn (true/false)
Data Members
OpenCreditDebitCheck_balancePrint_account_information
Operations
![Page 3: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/3.jpg)
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.
![Page 4: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/4.jpg)
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
![Page 5: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/5.jpg)
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
![Page 6: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/6.jpg)
• 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
![Page 7: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/7.jpg)
• 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
![Page 8: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/8.jpg)
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.
![Page 9: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/9.jpg)
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}
![Page 10: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/10.jpg)
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.
![Page 11: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/11.jpg)
• 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
}
![Page 12: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/12.jpg)
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
}
![Page 13: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/13.jpg)
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;
}
![Page 14: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/14.jpg)
// 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;
}
}
![Page 15: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/15.jpg)
Testing Accountpublic 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();
}}
![Page 16: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/16.jpg)
myAccount = new Account("Camille Hayhurst", "304-276-3456", 3564.70);
tomsAccount = new Account("Tom Jones", "345-332-3214", 256.00);
account: 1holderName: Camille HayhurstholderPhoneNumber: 304-276-3456balance: 3564.70overdrawn = false;
account: 2holderName: Tom JonesholderPhoneNumber: 345-332-3214balance: 256.00overdrawn = false;
accountNumber = 1
![Page 17: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/17.jpg)
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
![Page 18: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/18.jpg)
System.out.print("\n If we add another account "); Account.showNextAccountNumber();
If we add another account the next available account number is: 3
![Page 19: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/19.jpg)
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
![Page 20: Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.](https://reader036.fdocuments.net/reader036/viewer/2022082820/56649e995503460f94b9bb68/html5/thumbnails/20.jpg)
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