Need help with this Array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2019 Views - Last Post: 12 November 2012 - 05:06 PM Rate Topic: -----

#1 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Need help with this Array

Posted 10 November 2012 - 01:43 AM

I had a Program Working with ArrayList's But now am Changing it into a program with Just Arrays.. but am finding while working with ArrayList the past couple of Weeks and changing over to Arrays is quiet the pain..


Here is my Class Bank{}
/** 
	   This bank contains a collection of bank accounts. 
	 * @param <BankAccount>
	*/
	public class Bank
	{ 
		static int count=0;
	
	
	   /**
      Constructs a bank with no bank accounts. 
	   */
		
	   public Bank( int size) 
   { 
	
	accounts = new BankAccount[size];
	for (int i = 0; i < accounts.length; i++)
		accounts[i] = new BankAccount();

		   
     
   } 
	   /** 
	      Adds an account to this bank. 
	      @param a the account to add 
	   */
	   public void addAccount(BankAccount a) 
	   { 
		   	BankAccount accounts = new BankAccount();
	   } 
	     
	   /** 
	      Gets the sum of the balances of all accounts in this bank. 
      @return the total balance 
	   */
	   public double getTotalBalance() 
	   { 
	      double total = 0; 
	      for (BankAccount a : accounts) 
	      { 
	         total = total + a.getBalance(); 
	      } 
	      return total; 
	   } 
	     
	   /** 
	      Counts the number of bank accounts whose balance is at 
	      least a given value. 
	      @param atLeast the balance required to count an account 
	      @return the number of accounts having at least the given balance 
	   */
	   public int count(double atLeast) 
	   { 
	      int matches = 0; 
	      for (BankAccount a : accounts) 
	      { 
	         if (a.getBalance() >= atLeast) matches++; // found a match 
	      } 
	      return matches; 
	   } 
	  
	   /** 
	      Finds a bank account with a given number. 
	      @param accountNumber the number to find 
	      @return the account with the given number, or null if there 
	      is no such account 
	   */
	   public BankAccount find(int accountNumber) 
	   { 
	      for (BankAccount a : accounts) 
	      { 
	         if ( a.getAccountNumber() == accountNumber) // found a match 
	            return a; 
	      } 
	      return null; // no match in the entire array list 
	   } 
	     
	   /** 
	      Gets the bank account with the largest balance. 
	      @return the account with the largest balance, or null if the 
	      bank has no accounts 
	   */
	   
	   
	   public BankAccount getMaximum() 
	   { 
	      if (accounts.length == 0) return null; 
	      
	      
	      BankAccount largestYet = accounts.length; 
	        
	      for (int i = 1; i < accounts.length; i++) 
	      { 
	         BankAccount a = accounts.length; 
	         if (a.getBalance() > largestYet.getBalance()) 
	            largestYet = a; 
	      } 
	      return largestYet; 
	   } 
	     
	  
	  
	   /** 
	      Adds an account to the bank. 
	      @param accountNumber the account number of this account 
	      @param initialBalance the initial balance of this account 
	   */
	   public void addAccount(int accountNumber, double initialBalance) 
	   { 
		 
		   for(int i=0;i<count;i++)
		   accounts[i]=new BankAccount();
		   //accounts[i].getAccountNumber();
		   
	   }
	   /** 
	      Deposits money into an account 
	      @param accountNumber the account number 
	      @param amount the amount to be deposited 
	   */
	   public void deposit(int accountNumber, double amount) 
	   { 
		   BankAccount account = accounts[accountNumber];
		   account.deposit(amount);
			  
	   } 
	     
	   /** 
	      Withdraws money from an account. 
	      @param accountNumber the account number 
	      @param amount the amount to be withdrawn 
	   */
	   public void withdraw(int accountNumber, double amount) 
	   { 
		   
		   BankAccount account = accounts[accountNumber];
		   account.withdraw(amount);
	   
	   }
	   /** 
	      Gets an account balance 
	      @param accountNumber the account number 
	      @return the account balance 
	   */
	   public double getBalance(int accountNumber) 
	   {
		   BankAccount account = accounts[accountNumber];
		   return account.getBalance();
	   
	   } 
	     
	   private BankAccount[] accounts;
	}





Class BankAccounts{}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BankAccount {
	
		private int accountNumber;
		private double balance;
	 private Lock balanceChangeLock;

		 public BankAccount()
		   {   
		      balance = 0;
		      balanceChangeLock = new ReentrantLock();
		   }

		public BankAccount(int anAccountNumber, double initialBalance)
		{

			this.accountNumber = anAccountNumber; 
			this.balance = initialBalance;

		}

		public void deposit(double amount)
		   {  
		      balanceChangeLock.lock();
		      try
		      {
		         double newBalance = balance + amount;
		         balance = newBalance;
		      }
		      finally
		      {
		         balanceChangeLock.unlock();
		      }
		   }

		public void withdraw(double amount)
		   {   
		      balanceChangeLock.lock();
		      try
		      {
		         double newBalance = balance - amount;
		         balance = newBalance;
		      }
		      finally
		      {
		         balanceChangeLock.unlock();
		      }
		   }
		 

		public double getBalance()
		   {   
		      return balance;
		   }
		
		public int getAccountNumber() {
			return accountNumber;
			}

		

		  
		}



Here is my Class Bank Tester{}
public class BankTester {
	public static void main(String[] args) 
	   { 
		
		BankAccount[] accounts = new BankAccount[4];
		
	      int dannysAccount = 0; 
	      int sallysAccount = 1; 
	      int harrysAccount = 2; 
	      int jerrysAccount = 3; 
	        
	      accounts[0].addAccount(dannysAccount, 1000); 
	      accounts[1].addAccount(sallysAccount, 2000); 
	      accounts[2].addAccount(harrysAccount, 3000); 
	      accounts[3].addAccount(jerrysAccount, 10000); 
	  
	      accounts[1].deposit(200); 
	      accounts[2].withdraw(500); 
	      accounts[3].deposit(1000); 
	      accounts[4].withdraw(7000); 
	  
	      System.out.println( 
	         "Danny's Account Balance: " +  accounts[0].getBalance()); 
	      System.out.println("Expected: 1200"); 
	      System.out.println( 
	         "Sally's Account Balance: " +  accounts[1].getBalance()); 
	      System.out.println("Expected: 1500"); 
	      System.out.println( 
	         "Harry's Account Balance: " +  accounts[2].getBalance()); 
	      System.out.println("Expected: 4000"); 
	      System.out.println( 
	         "Jerry's Account Balance: " +  accounts[3].getBalance()); 
	      System.out.println("Expected: 3000"); 
	   } 
	}







Is This A Good Question/Topic? 0
  • +

Replies To: Need help with this Array

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Need help with this Array

Posted 10 November 2012 - 02:53 AM

What is it you need help with? If you're getting errors, post them, copied and pasted from exactly what you're seeing at your end. If you have questions, ask.

Remember Arrays are 0-indexed, so if you have an array of 4 elements, the indices of those elements will be 0 through 3. I see in your test program you have one place where the indices run from 1 through 4.
Was This Post Helpful? 0
  • +
  • -

#3 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 10 November 2012 - 05:27 AM

Hi,

the parts of the code im having problems with are


Class Bank{}
  public void addAccount(int accountNumber, double initialBalance) 
	   { 
		 
		   for(int i=0;i<count;i++)
		   accounts[i]=new BankAccount();
		   //accounts[count].getAccountNumber();
		   
	   }


  public BankAccount getMaximum() 
	   { 
	      if (accounts.length == 0) return null; 
	      
	      
	      BankAccount largestYet = accounts.length; 
	        
	      for (int i = 1; i < accounts.length; i++) 
	      { 
	         BankAccount a = accounts.length; 
	         if (a.getBalance() > largestYet.getBalance()) 
	            largestYet = a; 
	      } 
	      return largestYet; 
	   } 



public void addAccount(BankAccount a) 
	   { 
		   	BankAccount accounts = new BankAccount();  //Variable not being used  
	   } 
	     






Class BankTester{}
//THE Method AddAccount undefined for the type BankAccount 
accounts[0].addAccount(dannysAccount, 1000); 
	      accounts[1].addAccount(sallysAccount, 2000); 
	      accounts[2].addAccount(harrysAccount, 3000); 
	      accounts[3].addAccount(jerrysAccount, 10000);


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Need help with this Array

Posted 10 November 2012 - 05:38 AM

Neither of your addAccount methods actually makes any sense. Your error is caused by attempting to call a third (non-existent) addAccount method.

You'll find it a lot easier to go back to using ArrayList. A vestigial Bank class would look like

public class Bank {
  private List<BankAccount> accounts;
  // Various overloaded addAccount methods
}


This post has been edited by g00se: 10 November 2012 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 10 November 2012 - 06:22 AM

View Postg00se, on 10 November 2012 - 06:38 AM, said:

You'll find it a lot easier to go back to using ArrayList. A vestigial Bank class would look like



I can do the program without fail with ArrayList, but i need to do it with Just Arrays as well,

The biggest Problem i have is

How can i Create an Array inside Bank, And Add to the Array from the Bank Tester ?


since i Have Account Number and Starting Balance, Would i need to Create a multidimensional array
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Need help with this Array

Posted 10 November 2012 - 06:33 AM

What does the array in the Bank class contain? From your existing code, it seems you want the array to hold BankAccount objects. If you want the array to hold BankAccount objects, then the construct would be:

BankAccount[] accounts = new BankAccount[numberOfAccounts];

Then Bank would have a method,

public void addBankAccount( BankAccount account ){}

that would add accounts to the array of BankAccounts.

ArrayList allows you to add any number of accounts, growing to accommodate. An Array of BankAccounts will have to either be set at a large enough maximum or grown to accommodate more accounts than the array was originally set to hold, as needed.

Come back if you need help making the changes.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Need help with this Array

Posted 10 November 2012 - 06:41 AM

You would need to keep an index as a pointer to the next account slot. btw, save yourself a lot of typing - the word 'Bank' is redundant apart from in the Bank class itself
Was This Post Helpful? 0
  • +
  • -

#8 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 10 November 2012 - 08:37 AM

It seems my Code does not want to do anything really..

does not seem to like anything im doing to it.
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Need help with this Array

Posted 10 November 2012 - 08:50 AM

Take it a step at a time. Post your updated Bank and Account classes.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Need help with this Array

Posted 10 November 2012 - 10:38 AM

in your addAccount() method as you create a new BankAccount object to insert in your array, so there is no reason at all to initialize your array full of BankAcount

Actually, if your account array is full of null you could do, using the index as account number

BanckAccount accAccount(double balance) {
   for(int i = 0; i < accounts.length; ++i) {
      if(accounts[i] == null) {
          accounts[i] = new BankAccount(i, balance);
          return accounts[i];
   }
   return null;   // no more room



Your tester is completly wrng. You have an array in it... the array is held by the Bank class should be

Bank bank = new Bank(100);
bank.addAccount(....
Was This Post Helpful? 0
  • +
  • -

#11 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 10 November 2012 - 12:41 PM

Thanks for all your Help,, Will Try the Code and post back...

you would think that learning how to code with ArrayList & LinkLists that Arrays would be simple
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Need help with this Array

Posted 10 November 2012 - 12:45 PM

Quote

you would think that (after) learning how to code with ArrayList & LinkLists that Arrays would be simple

Most would take each topic separately and apply it to new material, starting each project from scratch. I believe that approach would have been less confusing to you.
Was This Post Helpful? 0
  • +
  • -

#13 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 12 November 2012 - 12:10 PM

When i Run this code i am getting

Exception in thread "main" java.lang.NullPointerException
at Bank.getBalance(Bank.java:141)
at BankTester.main(BankTester.java:22)


here is my Bank Class{}
public class Bank 
{
	/** 
	   This bank contains a collection of bank accounts. 
	 * @param <BankAccount>
	*/
	
	 private BankAccount [] accounts;
	  int numberofAccounts;

	
	
	   /**
   Constructs a bank with no bank accounts. 
	 * @param i 
	   */
		
	   public Bank(int numberofAccounts) 
{ 
		   accounts = new BankAccount[numberofAccounts];
		   numberofAccounts = 0;
		
} 
	   /** 
	      Adds an account to this bank. 
	      @param a the account to add 
	 * @return 
	   */
	   public boolean addAccount(BankAccount account) 
	   { 
		   if (numberofAccounts == accounts.length) return false; // no more room
		    // otherwise, add new one to end of the list
		   accounts[numberofAccounts] = account;
		   numberofAccounts++;
		    return true;
	   } 
	     
	   /** 
	      Gets the sum of the balances of all accounts in this bank. 
   @return the total balance 
	   */
	   
	   public double getTotalBalance() 
	   { 
	      double total = 0; 
	      for (BankAccount a : accounts) 
	      { 
	         total = total + a.getBalance(); 
	      } 
	      return total; 
	   } 
	     
	   /** 
	      Counts the number of bank accounts whose balance is at 
	      least a given value. 
	      @param atLeast the balance required to count an account 
	      @return the number of accounts having at least the given balance 
	   */
	   public int count(double atLeast) 
	   { 
	      int matches = 0; 
	      for (BankAccount a : accounts) 
	      { 
	         if (a.getBalance() >= atLeast) matches++; // found a match 
	      } 
	      return matches; 
	   } 
	  
	   /** 
	      Finds a bank account with a given number. 
	      @param accountNumber the number to find 
	 * @return 
	      @return the account with the given number, or null if there 
	      is no such account 
	   */
	   public BankAccount find(int accountNumber) 
	   { 
	      for (BankAccount a : accounts) 
	      { 
	         if (a.getAccountNumber() == accountNumber) // found a match 
	            return a; 
	      } 
	      return null; // no match in the entire array list 
	   } 
	     
	   /** 
	      Gets the bank account with the largest balance. 
	      @return the account with the largest balance, or null if the 
	      bank has no accounts 
	   */
  public BankAccount getMaximum() 
	   {
		return null; 
	     
	   } 
	     
	   /** 
	      Adds an account to the bank. 
	      @param accountNumber the account number of this account 
	      @param initialBalance the initial balance of this account 
	   */
	   public void addAccount(int accountNumber, double initialBalance) {
		   
		     
	   } 
	     
	   /** 
	      Deposits money into an account 
	      @param accountNumber the account number 
	      @param amount the amount to be deposited 
	   */
	   public double deposit(int accountNumber, double amount) 
	   {
		return amount; 
		 
	   } 
	     
	   /** 
	      Withdraws money from an account. 
	      @param accountNumber the account number 
	      @param amount the amount to be withdrawn 
	   */
	   public void withdraw(int accountNumber, double amount) 
	   { 
		   
		
	   
	   }
	   /** 
	      Gets an account balance 
	      @param accountNumber the account number 
	      @return the account balance 
	   */
	   public double getBalance(int accountNumber) 
	   {
		   for (BankAccount b : accounts) 
		      { 
		         if (b.getAccountNumber() == accountNumber) // found a match 
		        	 
		        	 return(b.getBalance());
		      } 
		   return (Double) null; 
	   
	   } 
	     
	 
	}




BANK Account Class{}
public class BankAccount {
	private double balance;
	private int accountNumber;


	public BankAccount(int anAccountNumber, double initialBalance)
	{

		accountNumber = anAccountNumber; 
		balance = initialBalance;

	}

	public void deposit(double amount) 
	{
		 balance = balance + amount;
		 
	}

	 public void withdraw(double amount) 
	{
		  balance = balance - amount; 
		 
	}
	 
	public double getBalance() {
	return balance;
	}

	public int getAccountNumber() {
		return accountNumber;
		}




	}





Error at Bank.getBalance(Bank.java:141) is point @
  if (b.getAccountNumber() == accountNumber) // found a match 


And Error at BankTester.main(BankTester.java:22) is point @
 "Danny's Account Balance: " + bank.getBalance(dannysAccount)); 

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Need help with this Array

Posted 12 November 2012 - 12:42 PM

This will go through your whole array

for (BankAccount a : accounts)

but your array may not be full so you cannot use that form of for() in your methods. You will have to:


for(int i = 0; i < numberofAccounts; i++)
if(accounst[i].getNumber() ==
Was This Post Helpful? 0
  • +
  • -

#15 jack_ie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 24-November 09

Re: Need help with this Array

Posted 12 November 2012 - 04:20 PM

View Postpbl, on 12 November 2012 - 01:42 PM, said:

This will go through your whole array

for (BankAccount a : accounts)

but your array may not be full so you cannot use that form of for() in your methods. You will have to:


for(int i = 0; i < numberofAccounts; i++)
if(accounst[i].getNumber() ==



Thanks for that, I have now got that part Working it is giving me back the Index of the Location of the Array..

i will fix up the rest.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2