Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:05:46+00:00 2026-06-08T03:05:46+00:00

I just have what I’m sure is a very easy and quick question here…

  • 0

I just have what I’m sure is a very easy and quick question here… So let’s say I have an Account class as follows:

import java.text.NumberFormat;

public class Account
{
    private final double RATE = 0.03; // interest rate of 3.5%
    private long acctNumber;
    private double balance;
    private String name;

    //-----------------------------------------------------------------
    // Sets up the account by defining its owner, account number,
    // and initial balance.
    //-----------------------------------------------------------------
    public Account (String owner, long account, double initial)
    {
        name = owner;
        acctNumber = account;
        balance = initial;
    }
    //----------------------------------------------------------------- 
    // Deposits the specified amount into the account. Returns the
    // new balance.
    //-----------------------------------------------------------------
    public double deposit (double amount)
    {
        balance = balance + amount;
        return balance;
    }
    //-----------------------------------------------------------------
    // Withdraws the specified amount from the account and applies
    // the fee. Returns the new balance.
    //-----------------------------------------------------------------
    public double withdraw (double amount, double fee)
    {
        balance = balance - amount - fee;
        return balance;
    }
    //-----------------------------------------------------------------
    // Adds interest to the account and returns the new balance.
    //-----------------------------------------------------------------
    public double addInterest ()
    {
        balance += (balance * RATE);
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns the current balance of the account.
    //-----------------------------------------------------------------
    public double getBalance ()
    {
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns a one-line description of the account as a string.
    //-----------------------------------------------------------------
    public String toString ()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        return acctNumber + "\t" + name + "\t" + fmt.format(balance);
    }
}

And I create the Bank class shown here…

public class Bank 
{   
    Account[] accounts;// = new Account[30];
    int count=0;
    String name;

    public Bank(String name)
    {
        this.name = name; 
        accounts = new Account[30];
    }
    public void addAccount(Account acct)
    {
        accounts[count] = acct;
        count++;
    }
    public void addInterest()
    {
        //for (Account acct : accounts)
            //acct.addInterest();
        for(int i = 0; i < count; i++)
            accounts[i].addInterest();
    }
}

I receive an error if I try to use the addInterest() method with the
for (Account acct: accounts) loop you see commented out. Can someone please provide me with insight on why this is? I thought these loops were equivalent. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:05:47+00:00Added an answer on June 8, 2026 at 3:05 am

    The for loop over an iterable array iterates all 30 elements, not only the elements you really added.

    You may use an ArrayList<Account> and add elements as needed. This allows you to omit the count field:

    public class Bank 
    {   
        ArrayList<Account> accounts = new ArrayList<Account>();
        String name;
    
        public Bank(String name)
        {
            this.name = name; 
        }
        public void addAccount(Account acct)
        {
            accounts.add(acct);
        }
        public void addInterest()
        {
            for (Account acct : accounts)
                acct.addInterest();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just have a quick question about how to get the length of a
I just have a quick question about the conditional operator. Still a budding programmer
I just have a quick question about how to generate id's on-the-fly for HTML
I just have a quick best practice question regarding custom cells in a UITableView.
i just have a quick question about concurrent programming in Java. for example, i
I just have a brief question regarding MatLab. Say that we have the equation:
Just have a quick question about the GAC I created an assembly Awesome.DLL. Had
I just have a quick question. I've noticed that I don't have stdafx.h in
I just have a quick question. I am running through an array of images
I just have one simple question here is the desc: I'm working on news

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.