Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Array objects...

Posted on 2011-10-24
9
Medium Priority
?
206 Views
Last Modified: 2012-05-12
In this code, I am trying to create 10 accounts with initial $100 in each account.

After creating and array of 10 accounts, I have tried to deposit 100 in each account and then print their balance. I am getting an error.  After correction on this, how do I deposit the initial $100 using its constructor>

Thank you
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class ATM {
    public static void main(String[] args){
        Account[] myAccount = new Account[10];
        for(int i=0;i<myAccount.length;i++)
            myAccount[i].deposit(100.0);
        
        for(int i=0;i<myAccount.length;i++)
            System.out.println(myAccount[i].getBalance());        
    }
}

class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private String dateCreated;
    
    public Account(){
        CurrentDate();
    }
    
    public Account(int id, double balance){
        this.id=id;
        this.balance=balance;
        CurrentDate();
    }
    
    private void CurrentDate() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        dateCreated=dateNow;
    }
            
    public void setId(int id)
    {
        if(id>100)this.id=id;
    }
    
    public int getId()
    {
        return id;
    }
    
    public void setBalance(double balance)
    {
        this.balance=balance;
    }
    
    public double getBalance()
    {
        return balance;
    }
 
    public void setAnualInterestRate(double annualInterestRate)
    {
        this.annualInterestRate=annualInterestRate;
    }
    
    public double getAnualInterestRate()
    {
        return annualInterestRate;
    }
    
    public String getDateCreated()
    {
        return dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return annualInterestRate/12;
    }
    
    public void withdraw(double amount)
    {
        if(amount<=balance) balance=balance-amount;
    }
    
    public void deposit(double amount)
    {
        if(amount>0) balance=balance+amount;
//        System.out.println(balance);
    }
}

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 4
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 37017839
After line 6 you need to create instances of Account. You don't do that...
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 800 total points
ID: 37017852
   Account[] myAccount = new Account[10];
        for(int i=0;i<myAccount.length;i++){
     myAccount[i] = new Account();  
          myAccount[i].deposit(100.0);
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37017855
You could do
for (int i = 0; i < myAccount.length; i++) {
            myAccount[i] = new Account();
            myAccount[i].deposit(100.0);
            System.out.println(myAccount[i].getBalance());
        }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 37017863
WHne you exceute this
Account[] myAccount = new Account[10];
you just create the array as a whole
then you need to cretae each individual element as an instance of Account class before you can invoke any method
on that object.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37017871
Before you construct ech of your elements this element contains null and when you invoke method
on the object which is equal to null - that's when you get null pointer exception, no atter if this object is element of the array
or single vraibale
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37017891
thanks
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37017903
Don't forget to create elements of arrays - it is easy to confuse it with creation of arrays
and forget about it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37017907
No problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37038859
:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question