8 Replies - 1231 Views - Last Post: 08 October 2009 - 10:11 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

adding class objects to an arrayList of objects

Posted 08 October 2009 - 08:38 PM

Ok, this is what I am trying to do:

"Write a Java class, Bank.java, that implements a bank as an ArrayList of Account objects. The Bank object should have a field, nextAccount, initialized to 101. There is a method, addAccount(String name, double balan), that accepts a name and an initial balance as parameters. As each account is added, it is given the value in nextAccount, which is then incremented. Every account is given the interest rate, 3.5%."

The error I’m having is listed in the code. I am not sure what to do here.

account class
public class Account
{
	private int accountNumber;
	private String ownersName;
	private double interestRate;
	private double balance;
	
	public Account(int aNum, String oName, double iRate, double bal)
	{
		accountNumber = aNum;
		ownersName = oName;
		interestRate = iRate;
		if(bal < 0.0)
		{
			balance = 0.0;
		}
		else
		{
			balance = bal;
		}
	}
	
	public int getAccountNumber()
	{
		return accountNumber;
	}
	
	public String getOwnersName()
	{
		return ownersName;
	}
	
	public double getInterestRate()
	{
		return interestRate;
	}
	
	public double getBalance()
	{
		return balance;
	}
	
	public void setOwnersName(String newOwnersName)
	{
		ownersName = newOwnersName;
	}
	
	public void setInterestRate(double newInterestRate)
	{
		interestRate = newInterestRate;
	}
	
	public void deposit(double amt)
	{
		if(amt > 0.0)
		{
			balance = balance + amt;
		}
		else
		{
			balance = balance;
		}
	}
	
	public void withdraw(double amt)
	{
		if(amt > 0.0)
		{
			balance = balance - amt;
		}
		else
		{
			balance = balance;
		}
	}
	
	public void addInterest()
	{
		balance = balance + (balance * (interestRate / 100));
	}
	
	public void print()
	{
		System.out.println("Account Number: " + accountNumber);
		System.out.println("Owners Name: " + ownersName);
		System.out.println("Interest Rate: " + interestRate);
		System.out.println("Balance: " + balance);
	}
}



bank class
import java.util.*;

public class Bank
{
	private int nextAccount;
	private ArrayList<Account> accounts;
	
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
	}
	
	public void addAccount(String name, double balan)
	{		
		accounts.add(new Account(int aNum, String oName, double iRate, double bal)); // error: “.class expected”
		bal = balan;
		oName = name;
		aNum = nextAccount;
		iRate = .035;
		nextAccount++;
	}
} 


This post has been edited by mattlyons: 08 October 2009 - 08:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: adding class objects to an arrayList of objects

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 08:41 PM

Where is your Account class?
Was This Post Helpful? 0
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 08:49 PM

whoops i knew i forgot something. i edited it into my original post.
Was This Post Helpful? 0
  • +
  • -

#4 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 08:52 PM

Its because you have the type in the method call. You will probably have a bunch of other errors once you fix that though.

This post has been edited by syfran: 08 October 2009 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,069
  • Joined: 27-December 08

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 08:54 PM

To the compiler, it looks like you are trying to add a method definition to an ArrayList of Accounts. There should be real values in each place, matching the type, position and quantity as defined by the parameter list. In other words, there should be an int, String, double, and double in that order when you instantiate a new account. And they need to be real values or initialized variables.

new Account(int aNum, String oName, double iRate, double bal)


Was This Post Helpful? 1
  • +
  • -

#6 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 09:00 PM

thanks maco. i got it compiling with your helpful input.
Was This Post Helpful? 0
  • +
  • -

#7 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 09:24 PM

Ok, here is what I have to do now:

"There is a method, find(int number), that finds an account with a given account number within the ArrayList. See p. 101. When made into a method, this returns the position, not the content, of the account, or -1 if the account is not present. "

I wrote something below that is sorta what he was referring to on p.101 except I switched it up a little bit. The book was trying to use "searchString" but I didn't. Is the logic I have here correct, just my syntax is off? The error is on the first line of the while loop and it is as follows: "incompatible types - found Account but expected java.lang.String"

bank class
import java.util.*;
import java.lang.*;

public class Bank
{
	private int nextAccount;
	private ArrayList<Account> accounts;
	
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
	}
	
	public void addAccount(String name, double balan)
	{		
		accounts.add(new Account(nextAccount, name, .035, balan));
		nextAccount++;
	}
	
	public int find(int number)
	{
		int index = 0;
		boolean found = false;
		while(index < accounts.size() && !found)
		{
			String acc = accounts.get(index);
			if(acc.contains(number))
			{
				found = true;
			}
			else
			{
				index++;
			}
		}
		if(found = true)
		{
			return index + 1;
		}
		else
		{
			return -1;
		}
	}	   
}



account class is in first post
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10560
  • View blog
  • Posts: 39,069
  • Joined: 27-December 08

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 09:39 PM

In looking at this method, a few errors pop out at me.

The first being here. Your ArrayList contains Account objects, not String objects, so these are incompatible types.
 
String acc = accounts.get(index);



Next, remember that the = is for assignment and == is for comparison. Since you are assigning found true, this if statement will always evaluate to true.
if(found = true)



Some things to consider when revising your code in terms of efficiency.
-Try using a for loop
-Consider ending the method (with a return) if you find the given account without using boolean variables
-Look at the ArrayList contains(E element) method, where E is the datatype enclosed in the angle brackets.
Was This Post Helpful? 0
  • +
  • -

#9 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: adding class objects to an arrayList of objects

Posted 08 October 2009 - 10:11 PM

Ok I used the 3 tips when redoing my code. Just to let yall know, I haved missed the past two classes on this material due to religious observances and have been trying to catch up on my own and I don't think I did a complete thorough job. So if my logic on solving this problem just keeps being impossible to do, then my bad. I am still learning ArrayLists without the help of my instructor. So bear with me!

revised code
import java.util.*;
import java.lang.*;

public class Bank
{
	private int nextAccount;
	private ArrayList<Account> accounts;
	
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
	}
	
	public void addAccount(String name, double balan)
	{		
		accounts.add(new Account(nextAccount, name, .035, balan));
		nextAccount++;
	}
	
	public int find(int number)
	{
		int position = -1;
		for(int i = 0; i < accounts.size(); i++)
		{
			if(accounts[i].contains(accounts[i] number))
			{
				position = i + 1;
			}
			else
			{
				position = position;
			}
		}
		return position
	}
}



the if-statement line has an error too: ')' expected
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1