Aim:
To write a java program to find the account balance using package.
Algorithm:
- Declare class balance and pass parameterized constructor.
- Initialize name and balance as n and b.
- Use member function to check balance less than 0 and print if yes.
- Declare another class account import package.
- Access show function and print result.
Program:
package MyPack; class Balance { String name; double bal; Balance(String n, double b) { name = n; bal = b; } void show() { if(bal<0) System.out.print("--> "); System.out.println(name + ": Rs. " + bal); } } class AccountBalance { public static void main(String args[ ]) { Balance current[ ] = new Balance[3]; current[0] = new Balance("Nils", 123.45); current[1] = new Balance("Poojan", 345.12); current[2] = new Balance("Darshan", -12.34); for(int i=0; i<3; i++) { current[ i ].show(); } } }
Execution:
Nils: Rs. 123.45 Poojan: Rs. 345.12 --> Darshan: Rs. -12.34