Add

Creating New Accounts in Java Methods

 


Making a method.

Imagine a table containing the information about three accounts. (If you have trouble imagining such a thing, just look at  In the figure, each account has a last name, an identification number, and a balance. In addition (and here’s the important part), each account knows how to display itself on the screen. Each row of the table has its own copy of a display method. Though it may seem strange, generating account values at random is common practice. When you write new code, you want to test the code to find out if it runs correctly. You can make up your own data (with values like “Smith”, 0000, and 1000.00). But to give your code a challenging workout, you should use some unexpected values. If you have values from some real-life case studies, you should use them. But if you have don’t have real data, randomly generated values are easy to create.

I need some code to implement the ideas in Fortunately,

import java.text.NumberFormat;

import static java.lang.System.out;

class Account {

String lastName;

int id;

double balance;

void display() {

NumberFormat currency =

NumberFormat.getCurrencyInstance();

out.print(“The account with last name “);

out.print(lastName);

out.print(“ and ID number “);

out.print(id);

out.print(“ has balance “);

out.println(currency.format(balance));

}

}

The Account class in defines four things — a lastName, an id, a balance, and a display. So each instance of Account class has its own lastName variable, its own id variable, its own balance variable, and its own display method. These things match up with the four columns.

 

How the display Method Behaves When No One’s Looking 

/*

* This is not real code:

*/

void display() {

NumberFormat currency =

NumberFormat.getCurrencyInstance();

out.print(“The account with last name “);

out.print(“Aju”);

out.print(“ and ID number “);

out.print(9936);

out.print(“ has balance “);

out.println(currency.format(8734.00));

}


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.