Results 1 to 7 of 7

Thread: .class expected

  1. #1
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default .class expected

    Hello, I'm currently learning about inheritance and such and am having a slight problem compiling my code:

    Java Code:
    public abstract class BankAccount {
    	String name;
    	float balance;
        
        public BankAccount(String aName, float aBalance) {
        	name = aName;
        	balance = aBalance;
    	}
        
        public String toString()
        {
        	return (name + "'s bank account with $" + balance);
        }
        
        public void withdraw(float anAmount)
        {
        	balance = balance - anAmount;
        }
    }
    This BankAccount class is the superclass.

    Java Code:
    public class SavingsAccount extends BankAccount {
    	
    
        public SavingsAccount() {
        super(name, 0.0f);
        }
       
    
            public void withdraw(float anAmount)
        {
        	super.withdraw(anAmount + float(0.75)); //Problem line
        } 
        
        
    }
    I'm basically trying to override my withdraw method I have in my BankAccount class. It's gives me a ".class expected" error and points to the line indicated by //Problem line.

    Does anybody know why this is happening??

    Thanks for any help

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    super.withdraw(anAmount + float(0.75)); //Problem line
    What is this supposed to do?

    Perhaps you mean 0.75f which tells the compiler to regard the literal 0.75 as a float. But it would be better to use double rather than float throughout your program.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    float(0.75) is not valid... try (float) 0.75 or 0.75f. anAmount + 0.75 would work as well.

    Also, I'd suggest using doubles. Judging by class names, you want accuracy, and floats are very inaccurate, and also only support much smaller values than doubles.
    Last edited by Singing Boyo; 06-27-2009 at 06:14 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The problem with anAmount+0.75 is that withdraw() requires a float argument and the compiler will grumble about the withdraw(float) not being applicable for the double argument.
    Last edited by pbrockway2; 06-27-2009 at 06:03 AM.

  5. #5
    dre
    dre is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the help guys.

    I switched the floats to double as suggested by you guys, but now it is complaining about something else.

    cannot find symbol constructor BankAccount() is the error I'm getting now, it is pointing to the

    public SavingsAccount() {

    }

    line in the SavingsAccount class. Can anybody see why this is occurring?

    EDIT: Figured it out, thanks guys.
    Last edited by dre; 06-27-2009 at 06:31 AM.

  6. #6
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    [QUOTE=dre;74409]... now it's complaining: cannot find symbol: constructor BankAccount() pointing to: public SavingsAccount() {}

    Common noob mistake... BankAccount doesn't have a no-arg constructor (aka "default" constructor)... The compiler generates a public no-arg constructor for you if you do not provide a constructor at all, but it does NOT generate the default constructor as soon as a you provide any constructor, no matter it's arguments... which makes sense, otherwise you couldn't stop folks who shouldn't from creating instances of a class... coz you couldn't NOT have a default constructor... Clear?

    ... here's my 2c worth on the code ...

    Java Code:
    public abstract class BankAccount
    {
      private String name;
      private double balance;
    
      public BankAccount(String name, double balance) {
        this.name = aName;
        this.balance = aBalance;
      }
    
      public void withdraw(double amount) {
        this.balance -= amount;
      }
    
      public void deposit(double amount) {
        this.balance += amount;
      }
    
      public void yieldMonthlyInterest() {
        this.balance += this.balance * (getAnnualInterestRate() / 12.0);
      }
    
      @Override
      public String toString() {
        return (name + "'s bank account has a balance of $" + balance);
      }
    
      protected abstract double getAnnualInterestRate();
    
    }
    
    public class SavingsAccount extends BankAccount
    {
      public static final double WITHDRAWAL_FEE = 0.75;
      public static final double ANNUAL_INTEREST_RATE = 0.04;
    
      public SavingsAccount() {
        super(name, 0.0);
      }
    
      @Override
      public void withdraw(double amount) {
        super.withdraw(amount + WITHDRAWAL_FEE);
      }
    
      @Override
      public abstract double getAnnualInterestRate() {
        return ANNUAL_INTEREST_RATE;
      }
    
    }
    Disclaimer: This code hasn't been compiled let alone tested.

    1. It uses doubles everywhere. Java does all floating point arithmetic as doubles, so using floats is pointless an basically annoying (though still possible where necessary) because you need to cast everything back to float all the time. Yeck!
    2. I added an abstract method called getAnnualInterestRate just as an example of this pattern; also note that BankAccount's yieldMonthlyInterest method calls the getAnnualInterestRate method, even though BankAccount doesn't know what the implementation of that method will be.
    3. SavingsAccount's implementation of getAnnualInterestRate just returns the value of a constant... this is a fairly common pattern, when you know that each subtype must have an attribute, but the value is to be determined by each subtype.


    Hope that provides some food for thought.

    Cheers. Keith.
    Last edited by corlettk; 06-27-2009 at 02:37 PM. Reason: x/12 ... I'm a putz!

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Nice, but shouldn't your getAnnualInterest() in SavingsAccount be this...
    Java Code:
    @Override
    public /*abstract??? Nope*/double getAnnualInterestRate() {
        return ANNUAL_INTEREST_RATE;
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 16
    Last Post: 05-28-2009, 03:41 PM
  2. Class Expected error
    By Radman23 in forum New To Java
    Replies: 11
    Last Post: 01-14-2009, 04:12 AM
  3. 'class' or 'interface' expected
    By denisdoherty in forum New To Java
    Replies: 23
    Last Post: 04-22-2008, 06:13 PM
  4. class or interface expected -compile err
    By ravi503 in forum Java Servlet
    Replies: 1
    Last Post: 03-26-2008, 11:45 AM
  5. Replies: 9
    Last Post: 08-08-2007, 07:16 PM

Posting Permissions

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