Upload
oliver-norton
View
215
Download
2
Embed Size (px)
Citation preview
Creating Simple Classes
Outline of Class Account
Class Account
Account #BalanceHolder namephone#Overdrawn (true/false)
Data Members
OpenCreditDebitCheck_balancePrint_account_information
Operations
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.
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
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
• 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
• 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
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.
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}
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.
• 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
}
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
}
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;
}
// 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;
}
}
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();
}}
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
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
System.out.print("\n If we add another account "); Account.showNextAccountNumber();
If we add another account the next available account number is: 3
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
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