Results 1 to 3 of 3
Thread: using methods between classes
- 04-17-2010, 11:22 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 35
- Rep Power
- 0
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 classJava 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.
-
There error is telling you what is wrong. Please look again at this utility class here:
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?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()); } } }
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!
- 04-18-2010, 03:14 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Classes and main methods?
By CyberFrog in forum New To JavaReplies: 17Last Post: 05-26-2009, 04:47 AM -
Classes and Methods help
By border9 in forum New To JavaReplies: 5Last Post: 01-30-2009, 06:51 PM -
How to call methods of different classes
By adeeb in forum New To JavaReplies: 2Last Post: 06-06-2008, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks