Sunteți pe pagina 1din 8

Lecture 10

Private Variables
Let us start with some code for a class:
public class Account
{
String name;
double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
} // end class Account
The class we are building here will be a template for an account
at a bank. There are two "class" variables, name and balance. The
"constructor" for the class, which has the same name Account as the
class is
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
It has a parameter list Account(String n, double b)
in the creation of an instance of this class.

which will be used

The code for the class is kept in a separate file Account.java.


The next program, Bank.java, will eventually be a collection of
accounts. Right now it has only one. The file Bank.java is kept in the
same directory as "Account":

public class Bank


{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println("\n\nbalance = "+ a.balance);
System.out.println("\n\n");
} //end main()
}// end class Bank
In this application we are constructing an account for "Ronald",
with an initial deposit of $3000. When the application is run its'
output is:

In reviewing this program one sees that the variable balance can
be accessed by the program Bank. As a matter of fact it can be accesed
by any number of programs. The access can be restricted by declaring
the two class variables in Account to be private:
public class Account
{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
} // end class Account
When Bank.java is compiled (after the above change has been made
and saved to the Account.java file) one gets a "variable balance .. not
accessible" error message:

It is not accessible because it has been declared to be private.


The variable balance can only be manipulated within the class Account".
So, how does the bank find out what is in each account? This is done by
adding a toPrint() method to the class Account:

public class Account


{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;

balance = b;
}
public String toPrint()
{
String output;
output = name +", balance = " + balance;
return output;
}
}
Then, Bank is modified by a call to the a.toPrint() method, which
prints out the name and the balance :
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println("\n\n");
System.out.println(a.toPrint());
System.out.println("\n\n");
} //end main()
}// end class Bank
The output is:

Deposits, Withdrawals
Next, we add two methods to to permit the bank to make deposits
and withdrawls;
public class Account
{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}
public String toPrint()
{
String output;

output = name +", balance = " + balance;


return output;
}
public double deposit(double d)
{
if ( d >= 0) balance = balance + d;
if (d < 0)
System.out.println("Your deposit has to be >= 0");
return balance;
}
public double withdrawl(double w)
{
if (w >= 0 && w <= balance) balance = balance -w;
if (w > balance)
System.out.println("You don't have sufficient funds!");
if (w < 0)
System.out.println("Withdrawl must be >= 0");
return balance;
}
}// end Account
We modify Bank to test deposit() and withdrawl() :
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println("Started with $3000");
System.out.println(a.toPrint());
System.out.println("\nThen deposited $450");
a.deposit(450);
System.out.println(a.toPrint());
System.out.println("\nThen tried to deposit -200");
a.deposit(-200);
System.out.println("\nWithdrew $650");
a.withdrawl(650);
System.out.println(a.toPrint());
System.out.println("\nTried to get $4000");
a.withdrawl(4000);
System.out.println(a.toPrint());
System.out.println("\n\n");
} //end main()
}// end class Bank

The output is:

Overloading a Constructor
Suppose the bank wishes to include the social security
number of all new accounts. This can be done by "overloading" the
constructor for the class, as folows.
public class Account
{
private String name;
private String socialsecurity = "";
private double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}
public Account(String n, String s, double b)
{
name = n;
socialsecurity = s;
balance = b;
}
public String toPrint()
{
}
public double deposit(double d)
{
}
public double withdrawl(double w)
{
}
public String printSS()
{
String ssnumber ;
if (socialsecurity.equals(""))
ssnumber = "no social security number for this account";
else
ssnumber = socialsecurity;
return ssnumber;
}
}// end account
This class has a new class variable
private String socialsecurity = "";,

initialized as the null string, and two different constructors for


Account, the previously defined one Account(String n, double b), and a
newly defined one Account(String n, String s, double b). A class can
have any number of constructors. Each constructor has to have the name
of the class, Account in this case. Constructors with different
parameter lists are considered to be distinct constructors.
The text of the methods that were defined earlier toPrint(),
deposit(), and withdrawl() was deleted above for space and readability.
It has to be replaced when the program is run.
The next version of the class Bank shows the two different ways
an account can now be constructed. The first account a is constructed
without a social security number; the second, b, with one. The method
printSS() is used to print out this number, provided it has been
entered.
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
Account b = new Account("David","123-45-678", 4000);
System.out.println("Ronald Started with $3000");
System.out.println(a.toPrint());
System.out.println(a.printSS());
System.out.println("\nDavid Started with $4000");
System.out.println(b.toPrint());
System.out.print("His social security number is ");
System.out.println(b.printSS());
System.out.println("\n\n");
} //end main()
}// end class Bank
The output is:

An Array of Accounts
We shall show the syntax for building a bank with 3 accounts. We
will be assuming that all accounts do have a social security number so,
we will add another print method, toPrint2() , to the class Account to
simplify the output. It is:
public String toPrint2()
{
String output = "name: "+name + "\n";
output = output + "Social Security Number: "+socialsecurity;
output = output +"\nbalance: " + balance;
return output;
}
Once this method has been added to Account we then define Bank as an
array of Accounts:
public class Bank
{
public static void main(String args[])
{
Account[] b ;
b = new Account[3];
b[0] = new Account("Edward", "234-56-789", 300);
System.out.println(b[0].toPrint2()+ " \n");
b[1] = new Account("Amy", "34-567-891", 50000);
System.out.println(b[1].toPrint2()+ "\n");
b[2] = new Account("Charlie", "45-678-912",897.45);
System.out.println(b[2].toPrint2());
}
}
The output is:

S-ar putea să vă placă și