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
}
}
|
|
Complete Java program:
public class Class07
{
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);
// **** Indirect access to stu1.balance:
stu1.deposit( 450 );
x = stu1.convToString( );
System.out.println("After depsoiting $450: " + x);
// **** Directly accessing variable stu1.balance:
stu1.balance = stu1.balance + 450;
x = stu1.convToString( );
System.out.println("After depositing another $450: " + x);
}
}
|
(No demo program available)
|
|
|
|
public class BankAccount
{
private int accNum; // Private access
private String name; // -- limit to ONLY inside
private double balance; // the class "BankAccount"
/* ==============================================
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
}
}
|
|
public class Class06
{
public static void main(String[] args)
{
BankAccount stu1;
stu1 = new BankAccount(); // Create one BankAccount object with
// 3 PRIVATE data fields:
stu1.accNum = 12345; // Now these statements will be in error.
stu1.name = "Mary"; // error...
stu1.balance = 1000.0; // error...
String x;
x = stu1.convToString( );
System.out.println("stu1: " + x);
stu1.deposit( 450 ); // ****** NO error ******
// because deposit() is public !
x = stu1.convToString( );
System.out.println("After depsoiting $450: " + x);
stu1.balance = stu1.balance + 450; // error....
x = stu1.convToString( );
System.out.println("After withdraw $100: " + x);
}
}
|
Compiler messages:
> javac Class06.java Class06.java:10: accNum has private access in BankAccount stu1.accNum = 12345; // Now these statements will be in error. ^ Class06.java:11: name has private access in BankAccount stu1.name = "Mary"; // error... ^ Class06.java:12: balance has private access in BankAccount stu1.balance = 1000.0; // error... ^ Class06.java:25: balance has private access in BankAccount stu1.balance = stu1.balance + 450; // error.... ^ Class06.java:25: balance has private access in BankAccount stu1.balance = stu1.balance + 450; // error.... ^ 5 errors |
How to compile the program:
|