|
public class BankAccount { public int accNum; // These variables store information public String name; // about "BankAccounts" public double balance; /* ============================================== convToString(x): return a String containing information of BankAccount x ============================================== */ public static String convToString( BankAccount x ) { return( "Account number: " + x.accNum + ", Name: " + x.name + ", Balance: " + x.balance); } /* ============================================== deposit(x, amount): Add "amount" to x.balance ============================================== */ public static void deposit( BankAccount x, double amount ) { x.balance += amount; } /* ====================================================== withdraw(x, amount): Subtract "amount" from x.balance ====================================================== */ public static void withdraw( BankAccount x, double amount ) { if ( x.balance >= amount ) x.balance -= amount; // Subtract "amount" from x.balance } } |
Notice that:
|
|
public class Class05 { public static void main(String[] args) { BankAccount stu1; stu1 = new BankAccount(); // Create one BankAccount object with // 3 data fields: // stu1.studID, stu1.name, stu1.major stu1.accNum = 12345; stu1.name = "Mary"; stu1.balance = 1000.0; String x; x = BankAccount.convToString( stu1 ); System.out.println("BankAccount 1: " + x); BankAccount.deposit( stu1, 450 ); x = BankAccount.convToString( stu1 ); System.out.println("After depsoiting $450: " + x); BankAccount.withdraw( stu1, 100 ); x = BankAccount.convToString( stu1 ); System.out.println("After withdraw $100: " + x); } } |
|
Notice this is a new form of syntax
We must also introduce a new way (syntax) to define methods that use the new invocation syntax !!!
|
The syntax to tell Java that a method is invoked using the new method invocation format is as follows:
***** Caveat: this is not complete correct (yet) **** public class BankAccount { public int accNum; // These variables are used to public String name; // store information on public double balance; // "BankAccounts" /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public |
In other words:
|
|
public class BankAccount { public int accNum; // These variables are used to public String name; // store information on public double balance; // "BankAccounts" /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public String convToString( ) { return( "Account number: " + this.accNum + ", Name: " + this.name + ", Balance: " + this.balance); } /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { this.balance += amount; } /* ====================================================== withdraw(amount): Subtract "amount" from balance ====================================================== */ public void withdraw( double amount ) { if ( this.balance >= amount ) this.balance -= amount; // Subtract "amount" from balance } } |
Important note:
|
public class Class06 { public static void main(String[] args) { BankAccount stu1; stu1 = new BankAccount(); // Create one BankAccount object with // 3 data fields: // stu1.studID, stu1.name, stu1.major stu1.accNum = 12345; stu1.name = "Mary"; stu1.balance = 1000.0; String x; x = stu1.convToString( ); System.out.println("stu1: " + x); stu1.deposit( 450 ); x = stu1.convToString( ); System.out.println("After depsoiting $450: " + x); stu1.withdraw( 100 ); x = stu1.convToString( ); System.out.println("After withdraw $100: " + x); } } |
Output: (the same as the previous program - ofcourse, only changes made are how to write it...)
stu1: Account number: 12345, Name: Mary, Balance: 1000.0 After depsoiting $450: Account number: 12345, Name: Mary, Balance: 1450.0 After withdraw $100: Account number: 12345, Name: Mary, Balance: 1350.0 |
How to run the program:
|
|
|
public class BankAccount { public int accNum; // These variables are used to public String name // store information on public double balance; // "BankAccounts" /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public String convToString( ) { return( "Account number: " + accNum + ", Name: " + name + ", Balance: " + balance); } /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { balance += amount; } /* ====================================================== withdraw(amount): Subtract "amount" from balance ====================================================== */ public void withdraw( double amount ) { if ( balance >= amount ) balance -= amount; // Subtract "amount" from balance } } |
Explanation:
|
How to run the program:
|
|
|