Welcome to Dream.In.Code
Become a Java Expert!

Join 150,428 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,083 people online right now. Registration is fast and FREE... Join Now!




Class still will not compile

 
Reply to this topicStart new topic

Class still will not compile

scorpionviolinist
5 Apr, 2008 - 12:53 PM
Post #1

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

I am having the hardest time trying to get this class to compile. Why?

CODE
//*******************************************************
// Accounts.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

public class Accounts
{
  private static int numAccounts;
  private double balance;
  private String name;
  private long acctNum;
  private double newAccount;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Accounts(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }

  //----------------------------------------------
  // Returns account number.
  //----------------------------------------------
  public long getAcctNumber()
  {
    return acctNum;
  }

  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return "Name: " + name +
    "\nAccount Number: " + acctNum +
    "\nBalance: " + balance;
  }

  //----------------------------------------------
  // Holds number of accounts
  //----------------------------------------------
  private static int numAccounts()
  {
    numAccounts++;
    return numAccounts;
  }

  //----------------------------------------------
  // Returns number of accounts
  //----------------------------------------------
  public static int getnumAccounts()
  {
    return numAccounts;
  }
  
  //----------------------------------------------
  // Closes current account
  //----------------------------------------------
  public void close()
  {
    numAccounts--;
    balance=0;
    name=("CLOSED");
  }


  //-----------------------------------------------
  // Consolidates two accounts into one
  //-----------------------------------------------
  public String getName()
  {
    return name;
  }
  public long getAcctNum()
  {
    return acctNum;
  }

  public static Accounts consolidate(Accounts acct1, Accounts acct2)
  {
       String name1 = acct1.getName();
       String name2 = acct2.getName();
       long number1 = acct1.getAcctNum();
       long number2 = acct2.getAcctNum();

    if (name1.equals(name2) && !number1.equals(number2))
    {
       Accounts acct3 = new Accounts(balance, name, acctNum);
       acct1 = CLOSED;
       acct2 = CLOSED;
       return acct3;
    }
     else{
        System.out.println("Accounts could not be consolidated.");
        return null;
         }
  }
}

User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Class Still Will Not Compile
5 Apr, 2008 - 01:15 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
We aren't magic. What are the compiler errors that you are getting?

Per
User is offlineProfile CardPM
+Quote Post

scorpionviolinist
RE: Class Still Will Not Compile
5 Apr, 2008 - 01:18 PM
Post #3

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

These are the errors
C:\Documents and Settings\me>javac Accounts.java
Accounts.java:120: long cannot be dereferenced
if (name1.equals(name2) && !number1.equals(number2))
^
Accounts.java:122: non-static variable balance cannot be referenced from a stati
c context
Accounts acct3 = new Accounts(balance, name, acctNum);
^
Accounts.java:122: non-static variable name cannot be referenced from a static c
ontext
Accounts acct3 = new Accounts(balance, name, acctNum);
^
Accounts.java:122: non-static variable acctNum cannot be referenced from a stati
c context
Accounts acct3 = new Accounts(balance, name, acctNum);
^
Accounts.java:123: cannot find symbol
symbol : variable CLOSED
location: class Accounts
acct1 = CLOSED;
^
Accounts.java:124: cannot find symbol
symbol : variable CLOSED
location: class Accounts
acct2 = CLOSED;
^
6 errors

User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Class Still Will Not Compile
5 Apr, 2008 - 01:41 PM
Post #4

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
Excellent.

Okay, Line 120.
"number1" is a long, so it is not an object and therefore has no methods. You need to use "==".
java

if (name1.equals(name2) && number1 != number2)


Line 122
You are using balance, name, and acctNum from global variables. That doesn't make sense because you should be combining values from acct1 and acct2.

Line 123, 124
setting acct1 and acct2 to CLOSED doesn't actually mean anything. I imagine you should be doing
java

acct1.close();
acct2.close();


You seem to have these objects confused a bit. With part of your code seems to assume that each "Accounts" object will have several accounts, but part of it does not. It seems to me like this class should only contain data for one account, and then you should have several "Account" objects created.

So, you should probably remove the "newAccount" and "numAccounts" globals, along with methods to access them.

Good luck!

Per
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month