dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Dummies AI
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Dummies AI
    • Log In
    Explore Book
    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.

    importjava.util.Random;

    importjava.text.NumberFormat;

    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>

    <strong>}</strong>

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

    }

    }

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

    classProcessBetterAccounts {

    public static void main(String args[]) {

    <strong>BetterAccount</strong>anAccount;

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

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

    <strong>anAccount.fillWithData();</strong>

    anAccount.display();

    }

    }

    }

    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: 

    Beginning Programming with Java For Dummies

    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: 

    Java
    Trending
    Starting an Online Business All-in-One For Dummies Cheat Sheet
    10 Common Misconceptions About Somatic Exercises
    Format Numbers in Thousands and Millions in Excel Reports
    How a New Pope Is Elected in the Conclave
    From Category
    Java
    Important Features of the Java Language
    How to Download and Install TextPad
    How to Install JavaFX and Scene Builder
    Getting a Value from a Method in Java
    A Few Things about Java GUIs

    Quick Links

    About For Dummies Terms of Use Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant