Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
When I was a young object, I wasn’t as smart as the objects you have nowadays. Consider, for example, the object in the code below This object not only displays itself, but it can also fill itself with values.



import static java.lang.System.out;

classBetterAccount {

String lastName;

int id;

double balance;

<strong> </strong>

<strong>void fillWithData() {</strong>

<strong>Random myRandom = new Random();</strong>

<strong>lastName = "" +</strong>

<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'A') +</strong>

<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'a') +</strong>

<strong> </strong> <strong>(char) (myRandom.nextInt(26) + 'a');</strong>

<strong> </strong>

<strong>id = myRandom.nextInt(10000);</strong>

<strong>balance = myRandom.nextInt(10000);</strong>


void display() {

NumberFormat currency = NumberFormat.getCurrencyInstance();

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


out.print(" and ID number ");


out.print(" has balance ");




Here’s a way to use the class in the code above. Check out the new code.

classProcessBetterAccounts {

public static void main(String args[]) {


for (int i = 0; i < 3; i++) {

anAccount = new <strong>BetterAccount</strong>();






The second set of code is pretty slick. Because the code in the first listing is so darn smart, the new code has very little work to do. This new code just creates a BetterAccount object and then calls the methods in the first listing of code.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: