Results 1 to 3 of 3
  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default using methods between classes [SOLVED]

    I have 3 classes, the RunAccount, HelpAccount(place for methods) and Account. I am trying to print all the accounts but I am getting this compiler error "printAll(HelpAccount[]) in HelpAccount cannot be applied to (Account[])
    HelpAccount.printAll(accountUser);" anybody know how to fix it? Below is my code.
    Java Code:
    import java.util.Scanner;
    import javax.swing.*;
    
    public class Account {
    
    	private int id;
    	private String firstName;
    	private String lastName;
    	private double balance;
    	private double interestRate;
    
    	private static int numberOfAccounts=0;
    
    	// constructor
    	Account(String fn, String ln) {
    		numberOfAccounts++;
    		id = numberOfAccounts;
    		firstName = fn;
    		lastName = ln;
    		balance = 0;
    		interestRate = 0.03;
    	}
    
    	public int getId() {return id;}
    
    	public String getFirstName() {return firstName;}
    	public void setfirstName(String n) { firstName = n;}
    
    	public String getLastName() {return lastName;}
    	public void setLastName(String n) { lastName = n;}
    
    	public double getBalance() {return balance;}
    	public void withdraw (double withdrawAmount)
    	{
    		balance = balance - withdrawAmount;
    	}
    	public void deposit (double depositAmount)
    	{
    		balance = balance + depositAmount;
    	}
    
    	public double getInterestRate() {	return interestRate;}
    	public void setInterestRate (double value) {interestRate = value;}
    
    	public static int getNumberOfAccounts() {return numberOfAccounts;};
    
    	 public String toString ()
    	{
    		StringBuffer sb = new StringBuffer();
    		sb.append("\n======= Begin MyClass object =======\n");
    		sb.append("\nID: " + id);
    		sb.append("\nFirst Name: " + firstName);
    		sb.append("\nLast Name: " + lastName);
    		sb.append("\nBalance: " + balance);
    		sb.append("\nInterest Rate: " + interestRate);
    		sb.append("\n\n======= End MyClass object ========\n");
    		return (new String(sb));
    
    	} // end toString method
    
    } // end class
    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class RunAccount
    {
    	public static void main(String[] args)
    	{
    	Account[] accountUser = new Account[10];
    	accountUser[0] = new Account ("George", "Washington");
    	accountUser[1] = new Account ("Abraham", "Lincoln");
    	accountUser[2] = new Account ("Barack", "Obama");
    	accountUser[3] = new Account ("Bill", "Clinton");
    
    	HelpAccount.printAll(accountUser);
    	}
    
    }
    Java Code:
    import java.util.Scanner;
    import javax.swing.*;
    
    public class HelpAccount
    {
    	public static void printAll (HelpAccount[] print)
    	{
    		for (int i=0; i < Account.getNumberOfAccounts(); i++)
    		{
    			System.out.println(print[i].toString());
    		}
    	}
    }
    Last edited by soccer_kid_6; 04-18-2010 at 03:17 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There error is telling you what is wrong. Please look again at this utility class here:

    Java Code:
    import java.util.Scanner;
    import javax.swing.*;
    
    public class HelpAccount
    {
    	public static void printAll (HelpAccount[] print)
    	{
    		for (int i=0; i < Account.getNumberOfAccounts(); i++)
    		{
    			System.out.println(print[i].toString());
    		}
    	}
    }
    Are you sure that the parameter should be an array of HelpAccount objects? Is that the type of array that you are in fact passing to this method? Should you change the parameter type of this method and if so, what should you use?

    Please try to answer these questions and your solution will come to you. If not, please post back (actually, please do this regardless).

    Best of luck!

  3. #3
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default

    yeah i need to pass an array of object because i need to pass it both string and doubles which I am trying to print. Know what I mean?. Check toString method in the Account class

    nevermind i see what you meant and i fixed it. thanks furable

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Classes and main methods?
    By CyberFrog in forum New To Java
    Replies: 17
    Last Post: 05-26-2009, 04:47 AM
  3. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 06:51 PM
  4. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •