Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: WHY WONT THIS COMPILE!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default WHY WONT THIS COMPILE!

    hey guys and girls, im pretty new at programming, only a junior in highschool and im working with objects. If someone could help me out, i would be very thankful. Here is the class that contains the constructor and the methods.

    public class BankAccount
    {
         double balance;
         String name = "";
         double deposit;
         double withdraw;
         public BankAccount(double setBalance,String setName)
         {
             balance = setBalance;
             name = setName;
         }
         public void deposit(double setDeposit, double newBalance)
         {
             deposit = setDeposit;
             balance = newBalance;
             newBalance = deposit + balance;
         }
         public void withdraw(double setWithdraw, double newBalance)
         {
            withdraw = setWithdraw;
            balance = newBalance;
            newBalance = balance - withdraw;
         }
    }

    My second class contains this -

    import java.io.*;
    import java.util.*;
    public class Tester extends BankAccount 
    {
        public static void main(String args[])
        {
            Tester o1 = new Tester();
     
            Scanner kb = new Scanner(System.in);
            System.out.print("Enter amount initially put in the account: ");
            double x = kb.nextDouble();
            o1.balance = x;
            System.out.println(x);
        }  
    }

    of course there will be a lot more into the second class, but i have to find out why this second class wont compile. sorry for my poor choice of words, once again im a pretty new programmer.

    The compile error is this -

    Constructor BankAccount in class BankAccount cannot be applied to the given types.

    (Public class Tester extends BankAccount) is what is highlighted.
    Last edited by JavaPF; October 11th, 2011 at 03:21 AM. Reason: Use highlight tags!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: WHY WONT THIS COMPILE!

    For future reference, please use the code tags, and there is no need to shout (exclamation point, all caps). The compiler error is referring to the constructor in the parent class BankAccount, the defined constructore depends upon several parameters,
    public BankAccount(double setBalance,String setName)
    however the child class Tester does not contain this type of constructor. Any reason the class Tester extends BankAccount?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHY WONT THIS COMPILE!

    if i dont extend it, i wont be able to use the variable in the first class, in my second class. As far as i know at least.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: WHY WONT THIS COMPILE!

    Quote Originally Posted by usmc0311 View Post
    if i dont extend it, i wont be able to use the variable in the first class, in my second class. As far as i know at least.
    What happened when you tried? And can't you just add a getter method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: WHY WONT THIS COMPILE!

    1) Are BankAccount and Tester in the same package/location? That might be why...if not:
    2) Change your constructor to have different arguments.

    BankAccount()
    instead of
     BankAcount(double setBalance, String setName)

    Also, in your second class, your last line of code is displaying the variable "x", not "o1.balance"

    And as Kevin said, you should be able to access the variables using name.variable as long as you set them first (since you don't have a default value assigned)...

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: WHY WONT THIS COMPILE!

    Quote Originally Posted by wrightm96 View Post
    1) Are BankAccount and Tester in the same package/location? That might be why...
    No, copeg already explained the cause. It has nothing to do with package or location.

    Quote Originally Posted by wrightm96 View Post
    if not:
    2) Change your constructor to have different arguments.

    BankAccount()
    instead of
     BankAcount(double setBalance, String setName)
    No, that's not the correct solution. It might get rid of the error, but it causes other, more fundamental problems in the program's design.

    Quote Originally Posted by wrightm96 View Post
    Also, in your second class, your last line of code is displaying the variable "x", not "o1.balance"
    Right, but that's after he set balance equal to x, so it shouldn't matter that much.

    Quote Originally Posted by wrightm96 View Post
    And as Kevin said, you should be able to access the variables using name.variable as long as you set them first (since you don't have a default value assigned)...
    That is not what I suggested, and it's not even really true. Variable access has nothing to do with when you set them. It has more to do with access (private, public, packages, etc).

    I appreciate that you're trying to help, but providing false information is not helpful.
    Last edited by KevinWorkman; October 11th, 2011 at 12:04 PM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WHY WONT THIS COMPILE!

    Just curious, being a junior in High School myself, what is the name of the course (is it AP, standard, honors, etc.)

    Ok, now on to your problem:

    1. Copeg makes a good point, there is no particular reason for Tester to extend BankAccount. All of BankAccount's variables and methods are public. You may be better off creating a BankAccount object rather than a Tester. However, since this is likely for school, you may be required to make Tester a child of BankAccount so...

    2. If you absolutely need Tester to extend BankAccount try making a call to BankAccount's constructor using super Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    That should be enough to get you started
    Last edited by DougFane; October 12th, 2011 at 02:43 PM. Reason: left out a word

Similar Threads

  1. [SOLVED] Help with if statement that wont act right...
    By deathpk in forum Loops & Control Statements
    Replies: 2
    Last Post: October 10th, 2011, 02:35 PM
  2. anagram program wont compile please help
    By Rusak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2011, 06:23 PM
  3. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 02:38 PM
  4. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 03:40 PM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM