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

  • SEARCH
  • Home
  • 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 7754283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:16:04+00:00 2026-06-01T12:16:04+00:00

I am working on a homework assignment looking at inheritance in java. I am

  • 0

I am working on a homework assignment looking at inheritance in java. I am having a little trouble understand how to access an array in the superclass from the subclass. I looked at several other questions, and since I am so new to java, I’m still just not quite getting it.

Here is the super class

import java.text.NumberFormat;

/**
 * Bank represents a single Bank containing a number of BankAccounts.
 */
public class Bank {

    // Member variables:

    /** The array of BankAccount objects contained in this bank. */
    protected BankAccount[] myAccounts = new BankAccount[2000];

    /** The number of BankAccount objects stored in the array in this bank. */
    protected int numberOfAccounts = 0;


    // Constructors:

    /**
     * Creates a Bank.
     */
    public Bank() {}


    // Methods:

    /** 
     * Creates an account with the name and balance, and adds it to 
     * the bank's list of accounts.
     * If the name already exists, no account will be created.
     * @param aName The name for the new account.
     * @param aBalance The initial balance for the new account.
     */
    public void createAccount( String aName, double aBalance) {
        BankAccount existing = this.findAccount( aName);
        if( existing != null) return;
        BankAccount anAccount = new BankAccount( aBalance, aName);
        this.myAccounts[ numberOfAccounts] = anAccount;
        this.numberOfAccounts++;
    }

    /** 
     * Finds an account in the bank's list of accounts by name.
     * If no account is found, this method returns null.
     * @param aName The name of the BankAccount to search for.
     * @return The BankAccount bearing the given name, if found.
     */
    public BankAccount findAccount( String aName) {
        BankAccount answer = null;
        for( int index = 0; index < numberOfAccounts; index++) {
            BankAccount anAccount = this.myAccounts[ index];
            if( aName.equals( anAccount.getName())) {
                return( anAccount);
            }
        }
        return( answer);
    }

    /** 
     * Returns a String which represents a short summary of 
     * all the accounts in the bank.
     * @return A String representation of all accounts and their balances in the bank.
     */
    public String toString() {
        String answer = "";
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        for( int index = 0; index < numberOfAccounts; index++) {
            BankAccount anAccount = this.myAccounts[ index];
            String money = currencyFormatter.format( anAccount.getBalance());
            answer += anAccount.getName() + " \t" + money + "\n";
        }
        return( answer);
    }

}

and here is the start of the subclass

public class BankSubClass extends Bank{
    private double interestPaid;

    // Constructor
    public BankSubClass(String aName, double aBalance, double aInterest) {
        super();
        this.interestPaid = aInterest;
    }

    // Getters
    public double getInterestPaid() {return(this.interestPaid);}

    // Setters
    public void setInterestPaid(double setInterestPaid) {this.interestPaid = setInterestPaid;}

    // Other methods
    public double endOfYear() {
        for (i=0;i<BankAccount.length();i++) {

        }
    }

}

That for loop at the end are where things are getting a little tripped up. Netbeans is throwing up errors saying “cannot find symbol: variable i”. Maybe this has nothing to do with the bank account array I’m trying to use, I don’t know. Any help is much appreciated

Thanks for your time!

edit

So here is a continuation of the same homework

Thanks for the replies everyone, your suggestions took care of that problem, I am currently hitting another speed bump at the moment however. The idea behind this method that the for loop is in is to run through the array of BankAccount objects, check to see if any of them are of the InterestAccount type (a class I previously built) and if they are, call the yearlyUpdate() method from that class

Here is the InterestAccount class

public class InterestAccount extends BankAccount {
    private double interestRate;

    // Constructor
    /**
     * Create and interest bearing bank account with a balance, name,
     * and interest rate
     * @param aBalance The balance of the account
     * @param aName The name tied to the account
     * @param myInterestRate The interest rate of the account
     */
    public InterestAccount(double aBalance, String aName, double myInterestRate) {
        super(aBalance, aName);
        this.interestRate = myInterestRate;
    }

    // Getters
    /**
     * Gets the interest rate of the account
     * @return the interest rate of the account
     */
    public double getInterestRate() {return(this.interestRate);}

    // Setters
    /**
     * Sets the interest rate of the account
     * @param interestSet The new interest rate of the account
     */
    public void setInterestRate(int interestSet) {this.interestRate = interestSet;}

    // Other Methods
    /**
     * Calculates the interest earned on the account over a year
     * @return the interest earned over a year
     */
    public double yearlyUpdate() {
        double answer = (super.getBalance()*this.interestRate);
        return answer;
    }
}

Here is the super class I am currently working with

import java.text.NumberFormat;

/**
 * Bank represents a single Bank containing a number of BankAccounts.
 */
public class Bank {

    // Member variables:

    /** The array of BankAccount objects contained in this bank. */
    protected BankAccount[] myAccounts = new BankAccount[2000];

    /** The number of BankAccount objects stored in the array in this bank. */
    protected int numberOfAccounts = 0;


    // Constructors:

    /**
     * Creates a Bank.
     */
    public Bank() {}


    // Methods:

    /** 
     * Creates an account with the name and balance, and adds it to 
     * the bank's list of accounts.
     * If the name already exists, no account will be created.
     * @param aName The name for the new account.
     * @param aBalance The initial balance for the new account.
     */
    public void createAccount( String aName, double aBalance) {
        BankAccount existing = this.findAccount( aName);
        if( existing != null) return;
        BankAccount anAccount = new BankAccount( aBalance, aName);
        this.myAccounts[ numberOfAccounts] = anAccount;
        this.numberOfAccounts++;
    }

    /** 
     * Finds an account in the bank's list of accounts by name.
     * If no account is found, this method returns null.
     * @param aName The name of the BankAccount to search for.
     * @return The BankAccount bearing the given name, if found.
     */
    public BankAccount findAccount( String aName) {
        BankAccount answer = null;
        for( int index = 0; index < numberOfAccounts; index++) {
            BankAccount anAccount = this.myAccounts[ index];
            if( aName.equals( anAccount.getName())) {
                return( anAccount);
            }
        }
        return( answer);
    }

    /** 
     * Returns a String which represents a short summary of 
     * all the accounts in the bank.
     * @return A String representation of all accounts and their balances in the bank.
     */
    public String toString() {
        String answer = "";
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        for( int index = 0; index < numberOfAccounts; index++) {
            BankAccount anAccount = this.myAccounts[ index];
            String money = currencyFormatter.format( anAccount.getBalance());
            answer += anAccount.getName() + " \t" + money + "\n";
        }
        return( answer);
    }

}

And finally, here is the subclass in which I am trying to run this for loop

public class BankSubClass extends Bank{
    private double interestPaid;

    // Constructor
    public BankSubClass(String aName, double aBalance, double aInterest) {
        super();
        this.interestPaid = aInterest;
    }

    // Getters
    public double getInterestPaid() {return(this.interestPaid);}

    // Setters
    public void setInterestPaid(double setInterestPaid) {this.interestPaid = setInterestPaid;}

    // Other methods
    public double endOfYear() {
        double trackInterest=0;
        for (int i=0;i<numberOfAccounts;i++) {
            BankAccount working = myAccounts[i];
            boolean hasInterest = working instanceof InterestAccount;
            if (hasInterest) {
                trackInterest = trackInterest + working.yearlyUpdate();
            }
            return trackInterest;
        }
    }

}

currently netbeans can’t find the “yearlyUpdate()” method when attempting to call it on “working” what I’m not understanding is since the previous code verified that the working object was of the InterestAccount type, it should have that method available

Thanks for the help!

  • 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-01T12:16:05+00:00Added an answer on June 1, 2026 at 12:16 pm

    In your loop, variable i is undeclared. Also, since I think you wish to loop through the accounts in the array, I think you should be using the following:

    for(int i = 0; < numberOfAccounts; i++)
    {
        BankAccount bankAccount = myAccounts[i];
        // other stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a homework assignment and I ran into a little snag. I'm
I am working on a homework assignment, and I am going a little "above
Having some issues with one small function I'm working on for a homework assignment.
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
Some classmates and I are working on a homework assignment for Java that requires
I'm working on my homework and am having trouble figuring out the correct syntax
Ok so I'm working on my homework and am having trouble figuring out how
I'm working on a homework assignment for Java, where a program is supposed to
I am working on a homework assignment, beginning C#. Have to accept input from
I am working on a homework assignment on implementing interfaces, and am a little

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.