Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Importing a Class

    Hello, and thank you in advance for your help.

    I am new to Java. I am currently learning through a tutorial using Command-prompt. The tutorial is a little out of date, but not too much. I am trying to import one class file into another to extend it but it's not working.
    It tells me I need a "." after "import Account" and I need a semicolon after that, but that doesn't make any sense and when I try it, it doesn't work.

    If anyone can tell me the problem I will be very grateful.

    Account:
    Code:
    public class Account 
    	{
    			protected double balance;
    			
    			public Account(double amount)
    				{balance = amount;}
    
    			public Account()
    				{balance = 0.0;}
    
    			public void Deposit(double amount)
    				{balance += amount;}
    
    			public double Withdraw(double amount)
    				{
    					if (balance >= amount)
    						{
    							balance -= amount;
    							return amount;
    						}
    					else
    							return 0.0;
    				}
    
    			public double GetBalance()
    				{return balance;}
    	}
    InterestBearingAccount:
    Code:
    import Account;
    
    class InterestBearingAccount extends Account
    	{
    		private static double DInterest = 7.95;
    		
    		private double InterestRate;
    
    		public InterestBearingAccount(double Amount, double Interest)
    			{
    				balance = Amount;
    				InterestRate = Interest;
    			}
    
    		public InterestBearingAccount(double Amount)
    			{
    				balance = Amount;
    				InterestRate = DInterest;
    			}
    
    		public InterestBearingAccount()
    			{
    				balance = 0.0;
    				InterestRate = DInterest;
    			}
    
    		public void AddInterest()
    			{
    				balance += (balance*InterestRate/100)/12;
    			}
    	}

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hi!

    You actually shouldnt need any import statement if the classes exist within the same package.

    I presume this is the case - so just delete the import line and things should work out no problems.

    Are you using the Eclipse IDE for your development?


  •  

    Posting Permissions

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