• No results found

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

N/A
N/A
Protected

Academic year: 2021

Share "It has a parameter list Account(String n, double b) in the creation of an instance of this class."

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

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) which will be used in the creation of an instance of this class.

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:

(2)

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) {

(3)

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() {

(4)

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

(5)
(6)

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

(7)

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:

(8)

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()); } }

References

Related documents

In the process of service composition, service requestor and service provider have different needs, the service requester wants to carry out their tasks with high quality at a

***Gathering Prayer Mary Maren Bjork Timeless God, on this day we remember all the saints of our lives.. Some have gone before; some are sitting right next to us in

In an earlier era, when the human population and the quantities of waste generated were much smaller and when highly toxic forms of waste were uncommon, it was possible to believe

The main objective of this work was to develop, implement, and evaluate a web-based scheduling application for a collaborative preparation of class schedules at

altera showed a distinct effect on the emergence and survival of annual and perennial species 28.. and negatively affected the growth of individuals belonging to both groups

■ Consider adding a full-time Cabinet member--one who can bring the faculty together to craft processes for developing course-level learning outcomes, tie these into the

Just as many companies may be unready to hire and retain veterans, many veterans don’t know how to find work in the civilian sector.. They may not know how to locate or connect

Understand the words and rest assured in hindi with example sentences and bundle you name that you getting word wise available procure the server.. We insure for we hope and please