• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help on testprogram and subclass please

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
Okay I think I've made some headway with my subclass problem. I'm suppose to create a SavingsAccount subclass from my superclass Account as well as a TestSavingsAccount program. I have started this but I need some help. My first question is regarding the JOptionPane display. On my input displays I have quite a bit of info to display as you can see. I don't know how do display it all. I'm getting errors regarding that so I need some help on how to display it all properly. I"m sure you will see when you read the code below. Also I want my output to display the new balance two separate times, first with the new amount after deposit then after withdrawl. I know I have to work on the code so if the withdrawl is more than the balance "Insufficient Funds" returns but right now I need help on my subclass. I know I"m not writing the code correctly to bring over my withdrawl and deposit methods from my Account Class can someone please help me.
Thanks in advance, below is my Account Class and my TestSavingsAccount program coupled with my SavingsAccount subclass:

My TestSavingsAccount code and SavingsAccount subclass:
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stacey,
In terms of your question about the JOptionPane, the errors you are getting are not related to how you are displaying the information. You have a couple of very basic syntax errors (i.e. typos) in your method call. Let�s take a look at this section:

When you attempt to compile this, you will get an error similar to:
Error: (25,50) ')' expected
D:/Dev/TestSavingsAccount.java:25: ')' expected
Error: (27,9) unclosed string literal
D:/Dev/TestSavingsAccount.java:27: unclosed string literal
The fist error message tells us that the compiler is expecting a closing parenthesis on line 25, character 9. (Your line numbers will probably differ then mine based on spacing in your file.) For my file, "String input1 =" is line 24. Therefore, the compiler is saying that the problem is with the line:

(I added the spacing counts to help make things easier). The character position is character 50 (since I have an 8 space indent in my file) it is not happy with the quote; it�s "expecting" a closing parenthesis. The thing to remember is at this point, as the compiler is reading the code, it is looking purely at syntax, and not method declarations. Therefore, it does not know your method call has more parameters coming. But it thinks there should be a ) � with these types of errors (i.e. where the compiler is expecting something), it is best to look at the code both just before and just after the spot that the compiler says the error exists. If we do that, breaking it down to a simpler construct, we have this:

If you look at that, you should quickly see the problem. We need a + in order to concatenate the variable and the string literal:

Again, the reason the compiler stated you were missing a ) and not a + is that it doesn�t know your intent. All it knows is proper syntax. To it, it seeing this:

It sees the opening ( and is looking for the closing ) � when it can�t find it, it throws an error indicating that it can�t find it, but is expecting it to be in the first place that is syntactically correct, which is after the variable since "string1" + variable is syntactically correct.
The next error indicates that you have an unclosed string literal at position 9 of line 27:

"Unclosed sting literal" is a fancy way of saying you do not have a closing quote. It sees the opening quote at position 9 on line 27 matches up with the closing quote at potion 27 on line 27, but does not see a closing quote for the string literal in line 26. If you close that quote, it will fix this error but create another one, as you will ultimately have:

You�ll end up getting another "')' expected" error. Do you see why?
Here�s a couple of tips to help you spot these errors quickly:
Take a look at this line:

First it will be easier to see if you have a + if you put a space between it and any surrounding variables. While this:

is perfectly acceptable, and will compile, this:

is easier to read and thus easier to spot errors.
Secondly, it is a convention to place the + at the end of a line of code when breaking code onto multiple lines like this:

Something that will also help keep your code neater is to create the message string separately as a variable, then use that variable in your JOptionPane call:

If you look, you will see the JOptionPane method call is a lot neater and it easier to see what you are passing to it. Also, by making the windowTitle string, I can reuse it in all the GUI windows; and if I need to ever change the Program�s title, I only have to change it in one place and all my windows will the new title (obviously not a likely scenario for a class assignment, but very real in business when the marketing department decides �Some-Program� will sell better then �This_Application�). Also note that the Parentheses surrounding the message string are not required. I could have also written:

I just personally find they help to make things a little more readable. Some people agree, others don�t see the point.

Ok, that will help you with your first error and question. There are some other things which I�ll post in an additional posting...
[ February 29, 2004: Message edited by: Mark Vedder ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I just realized that the javac compiler will present the information a little differently then what I showd (I used my IDE to compile the code). The javac compiler will show something like this:

Same concept, but instead of giving the character position, it points it out with the caret � that is the ^ symbol.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I know I'm not writing the code correctly to bring over my withdrawal and deposit methods from my Account Class can someone please help me.


Ok, let�s take a look at this issue. You appear to be confusing overriding of constructors and methods.
An important point to understand is that while Subclasses do automatically inherit methods, they do not automatically inherit constructors. Let�s make a couple of simple classes to demonstrate this point.

So we have the Class Baby which is a Subclass of Human. Notice that the Baby only overrides the method sayHello() from Human and it does not declare any constructors. Now let�s make a little class to use these classes:

This will compile and run, giving us the output:
ONE:
     name: John Doe
     ah-hem
     Hello, my name is John Doe.
TWO:
     name: John Doe
     ah-hem
     Wahhhh!
Notice that the instance of Baby (�two�) called Baby�s sayHello method since it was overridden, but still calls Human�s getName() and cough() methods. Therefore, we are able to override methods when we need to, or use the superclass� methods if desired.
Now let�s look at the constructors. We declared a no-parameter constructor in the class Human:
Public Human()
But we did not do so for Baby. So you might be thinking that when we call the constructor Baby() in this line of code:
Human two = new Baby();
That we are actually calling the Human() constructor � well, we are, but not directly � if no constructor is specified, as is the case of our Baby class, the object will have a default no-parameter constructor. Therefore, even though we didn�t type it in our class, Baby has a constructor of:
Public Baby() { }
That is what is called by TestClass in the line:
Human two = new Baby();
Ok, so the question then becomes how did two, the instance of baby, get the name �John Doe� when that is only defined in the class Human? The answer is that when you invoke a constructor, all the constructor�s of its superclasses are called first. So, even when we created �one�, with this line:
Human one = new Human();
The JVM actually first called the constructor Object() (since all classes implicitly extend Object) and then processed through the Human constructor code, in this case assigning the value �John Doe� to the attribute name. So when the JVM runs this line:
Human two = new Baby();
The chain of events is:
  • The default constructor Baby() is called
  • The constructor Baby() immediately calls its super constructor Human()
  • The constructor Human() immediately calls its super constructor Object()
  • The Object() constructor runs, then execution returns to the constructor Human()
  • The constructor Human() runs its code, which assigns the value �John Doe� to the attribute name.
  • Execution returns to the Baby() constructor, which has no code of its own.
  • The created object is assigned to the reference two.


  • It is for this reason that if explicitly stated, super() (or any super call like super(�string�), super(1, 2, 2.0), etc) must be the first statement in a constructor. But even if it is not explicitly stated, the no-parameter constructor of the super class is called. Let�s take a look at this by changing our classes a little:

    Notice these changes:
  • We added output strings to all the constructors to show which constructors are called.
  • We added two constructors to the Baby class, but notice that the Baby(String name) constructor does not assign the received name to the name attribute. (This is done in order to illustrate a point).
  • We�ve changed our TestClass to use the constructors that take names.


  • Now when we run the TestClass, we get the following output:
    Creating 'one'
    Constructor Human(String name) called
    Creating 'two'
    Constructor Human() called
    Constructor Baby(String name) called
    ONE:
         name: Fred
         ah-hem
         Hello, my name is Fred.
    TWO:
         name: John Doe
         ah-hem
         Wahhhh!
    Notice that even though we used the constructor Baby(String name) it called the super constructor Human() and not Human(String name). This is due to the fact that we did not explicitly tell it to do so, and therefore the implicit call to super() was made. � and notice that since out Baby(String name) constructor did not assign the name parameter to the name attribute, out Baby object end up with the Human() constructor default name. If we changed the Baby class to have this constructor:

    Our Baby object would get assigned the name passed by the call:
    Human two = new Baby("Pebbles");
    We could also do this:

    This would have the same end result, but the chain of events would be that the name attribute would be assigned the value of �John Doe� (when the implicit call to super() is executed) and then the attribute would be assigned the name �Pebbles� by the this.name = name; line of code.

    Ok, hopefully all that discussion makes things a little clearer for you. So let�s take a look at your SavingsAccount code:

    The first issue is that you have two constructors (on lines 17 & 22) with the save signature, namely:
    public SavingsAccount(double double)
    You cannot have two constructors with the same signature in a class. Nevertheless, when you look at things, these are not what you wanted. In your super call, you are passing the undeclared variables withdraw and deposit. Give that some logical thought, and compare it to the real world. When you make a deposit or a withdraw, do you want to create a new SavingsAccount object? Does the bank open a new account every time you make a withdraw or a deposit? No. So that�s probably not what you want to do. So just delete those constructors completely.
    Instead, your SavingsAccount must simply make the withdraw or deposit, just as your Account class did, using methods. In the case of the Deposit, nothing is going to be different in your SavingsAccount, so do you even need to override that method in your SavigsAccount class? Look at the Baby class above. Since it coughs the same as a Human, it didn�t override the cough method. But we can still call the method on a baby object by saying:

    So your SavingsAccount already has a working and viable deposit method (which it automatically inherits from its superclass Account) so you do not have to rewrite it or write any more code (welcome to the glorious would of Object Oriented Programming and Polymorphism!!!)
    The withdraw method on the other hand is a different story. Here you must have code to handle the possibility of an overdrawn account; so you will want to override the withdraw class in your SavingsAccount class. Now, this is where you were probably getting confused with the call to super.
    Going back to our Baby example, let�s say I did want to override the cough method, so that the Baby does something completely different. I could simply do this:

    This would completely replace anything the Human class does. However, lets say I wanted the Baby.cough() method to do everything the Human.cough() method does, but also something else; I could do this:

    Now my Baby.cough() method does everything the Human.cough() method does, and then some. This saves me rewriting the cough() code from Human in my Baby class, saving extra coding. Granted in this little example that is all of one line of code, but in a real world example, it may be dozens and dozens of lines of code. But even with only one line of code, it makes sense anyway. If I want to change how a human coughs, from �ah-hem� to �ahhh-hemm!� � I can change it in just one place, and all subclasses change with it, so my Baby class will automatically change from �ah-hem giggle� to �ahhh-hemm! giggle� without needing to recode the Baby class.
    Give these ideas some thought and decide how to implement your SavingsAcount.withdraw(double) method. You could:
  • call the super.withdraw(double), test the balance, and then if it causes an overdrawn account, recall the super method with the negative withdraw amount (to return the balance back to the pre-transaction amount) and then throw an OverDrawnException (a class you could write by extending (subclassing) the Exception class) or otherwise signal an overdrawn account.
  • Test if balance - amount will be less then zero. If so, throw the Exception; if not make the withdraw.
  • other ideas...


  • The point being, you need to override the withdraw method and not do it through an alternative constructor. Give a try at reworking your SavingsAccount methods with that in mind. If you are still having problems, post a reply and I or someone else can help you out further.
    There is one other place where you have an issue.
    Take a look at this line in your TestSavingsAccount class:

    Now take a look at the constructors in your SavingsAccount class (after you delete the two erroneous ones I mentioned above)

    Do you notice an issue? My discussion above on constructors and their lack of �automatically� being implemented should point out the problem. Look at the signature of the constructor your TestSavingsAccount calls, and the signatures of the constructors available in your SavingsAccount class.
    I hope this all helps you out. If not, I just did a lot of tying for nothing - lol
     
    Stacey Johnson
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Mark,
    First of all thanks for your help, I'm slowly getting it. One thing I have to keep in mind with my savings account is that I need to have user input for the deposit and withdrawl amounts so I'm not too sure if I"m doing that right. But back to the first two topics you helped me with, I get the new way to write JOption pane code and I like it. I agree that it's easier to read. So I have done that but there are a few questions I have. With the window title, because I have more than one frame appearing do I write it everytime? I'm not sure if it will pick up on each window. Also I'm getting errors that read the following:
    C:\WINDOWS\Desktop\Stacey\Comp 268\TME 2\TestSavingsAccount.java:107: cannot resolve symbol
    symbol : method showInputDialog (<nulltype>,java.lang.String,int)
    location: class javax.swing.JOptionPane
    String input1 = JOptionPane.showInputDialog(null, message, JOptionPane.QUESTION_MESSAGE);
    ^
    [B]cannot resolve symbol
    symbol : method showInputDialog (<nulltype>,java.lang.String,int)
    location: class javax.swing.JOptionPane
    String input2 = JOptionPane.showInputDialog(null, message, JOptionPane.QUESTION_MESSAGE);

    Below is my new code:

    With regard to the constructors, one thing I want to do is change the account number, balance and interest rate. Because like in real life each account has different variables attached to it. So I'm not to sure how to go about that and I've obviously done it wrong because one of the errors I get is:
    symbol : constructor SavingsAccount (int,int,double)
    location: class SavingsAccount
    SavingsAccount mySavingsAccount = new SavingsAccount(11234, 5000, 2.5);

    I've also added these two lines:

    in an attempt to pass variables as user input. Again I know this isn't correct and I'm not too sure how to go about it. I've tried other resourses and I can't seem to find the answer.
    Again I really do appreciate this. If it wasn't for this site and people who care enough to explain things, I dont' think I'd still be working away.
    Stacey
     
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    With the window title, because I have more than one frame appearing do I write it everytime?
    I haven't looked at your code, so I'm not 100% certain I realize what you're asking about. I believe the answer is something like, "If you want a title on each frame, give each frame a title."
    I'm not sure if it will pick up on each window. Also I'm getting errors
    Take a look at the JOptionPane class documentation and notice that there is no version of the showInputDialog method that takes the parameters you've passed in. There is a similar version that takes an additional String parameter (to specify a title or message depending on the position in the argument list you put it in).
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    With the window title, because I have more than one frame appearing do I write it everytime?


    You would pass the windowTitle variable each time you create a frame, but you only have to define windowTitle once. I would do it as a class attribute, and make it a static final since the name will be the same for all instances of your class (hence the static) and since it represents the name of your program, it should not change (hence the final). So something like this:

    You can go ahead and compile and run that and see the results. Notice that even though I defined the variable WINDOW_TITLE, I still need to pass it as a parameter to the JOptionPane when I call it. Otherwise JOptionPane has no access to it, or know that that is what I want to use as my frame title. That is why you are getting the compile errors. As Dirk pointed out, you are not using an available version of the JOptionPane.showInputDialog() method.
    I�ll answer your other questions in a separate post...
    Edit - the first part of my post was cut off (copy & paste error I guess) so I put that "back" in...
    [ March 01, 2004: Message edited by: Mark Vedder ]
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    One thing I have to keep in mind with my savings account is that I need to have user input for the deposit and withdrawal amounts so I'm not too sure if I'm doing that right.


    You are correct that you will need the user input. The question becomes what gets that input? I'm not sure if you are thinking of putting such a method/code in your SavingsAccount or Account class, but that would not be the proper place for it. Think about real world objects. When you go to the bank and make a deposit or withdraw, does the account itself ask you the deposit amount? No, a teller or ATM does. So it will be an object outside of the Account Class or SavingsAccount Class that will get the input, and then pass it to the withdraw or deposit methods of the appropriate account.
    If you look at your test program, you are already doing this:

    So you need to send this to your SavingsAccount object with the call:

    You have this; you simply have it in the wrong place. How can you submit the deposit amount before you get it from the user? (Or before you define the variable dep)
    Take a look at the deposit and withdraw calls you put in, and move them to an appropriate place.
    More to come...
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Stacey Johnson:

    With regard to the constructors, one thing I want to do is change the account number, balance and interest rate. Because like in real life each account has different variables attached to it. So I'm not to sure how to go about that and I've obviously done it wrong because one of the errors I get is:
    symbol : constructor SavingsAccount (int,int,double)
    location: class SavingsAccount
    SavingsAccount mySavingsAccount = new SavingsAccount(11234, 5000, 2.5);


    You are in the right track. It is good design to be able to change the account number, balance and interest rates at construction; and you have the right idea of doing so, but as the error tells you, something is amiss. Let�s examine the error message and determine what that is.
    You are missing the beginning part of the error message. The full message is something like this:
    TestSavingsAccount.java:13: cannot resolve symbol
    symbol : constructor SavingsAccount (int,int,double)
    location: class SavingsAccount
    SavingsAccount mySavingsAccount = new SavingsAccount(11234, 5000, 2.5);
    This issue/error gets into what I was saying at the end of the February 29, 2004 08:09 PM post.
    What the compiler is telling you here is that it cannot resolve (that is find) a symbol. In this case the symbol it cannot find is a constructor, told to us by this line:
    symbol : constructor SavingsAccount (int,int,double)
    The constructor it cannot find is a constructor for the SavingsAccount class that has a signature of two ints and a double. This info is also shown in that line:
    symbol : constructor SavingsAccount (int,int,double)
    So all this is saying is you are calling a non-existent constructor from your SavingsAccount Class. If you look at your SavingsAccount class, you do not have a constructor that matches that signature. Your Account class does have this one:

    but remember, constructors are not automatically extended like methods are. Therefore, if you want your SavingsAccount class to have a constructor that takes these parameters, you need to define one in that Class. If it does the same thing as its super class� constructor, you can simply make a call to that constructor:

    So put that constructor in your SavingsAccount class and you will solve that error, but retain the flexibility you are looking for.
    Does that all make sense? If not, let me know and I can explain it some more. I want to make sure you understand why you are making the changes you are making so you can learn.
    And one more post coming to look at the next issue...
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One last item...
    In your SavingsAccount class, take a look at this part:

    When you override a method, the method is entered in the same manner or syntax. Take a look at this example:

    This example does something very similar to what you want to do. See if that helps you in writing your withdraw method.
    [ March 01, 2004: Message edited by: Mark Vedder ]
     
    Stacey Johnson
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello,
    Okay so I've made some changes to my TestSavingsAccount program and my SavingsAccount class. And things are slowly getting better. I'm still not sure if I have my method down yet as well as I'm getting some errors that I just don't know what to do. So first of all here is my code:

    Now first of all I moved my calls to what I think are the correct places, but when compilng one of my errors is that it doesn't recognize my "with" variable. So I'm not too sure what's happening there. Another couple errors that concern me are below:

    \TestSavingsAccount.java:162: '.class' expected
    and
    TestSavingsAccount.java:162: unexpected type
    required: value
    found : class
    double newBalance = super.withdrawl(double);

    I don't really know where to go with these two.
    I have to say again I really appreciate the help I'm getting. Working full time and doing this in my evenings can really take it out of a person. The help I'm recieving is keeping me sane.
    Stacey
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Stacey,

    Now first of all I moved my calls to what I think are the correct places

    Not quite... you did move the deposit call to the correct location, but when you �moved� your withdraw call, you copied and pasted it to is new location, but forgot to delete the original (and incorrect) location:

    See how you have it twice. You need to remove the first occurance.

    As for the errors you are getting... as the compiler points out, the issue is with this line (line # 162 in your file) of code:

    The two errors are similar, but the second one will help you determine the issue:
    TestSavingsAccount.java:162: unexpected type
    required: value
    found : class
    double newBalance = super.withdrawl(double);

    It is saying that is seeing an unexpected type. It is expecting a value, but is seeing a class (as we�ll see, this is slightly inaccurate, it is actually seeing a primitive type declaration).
    When you call other methods, you have always passed a variable into the method. For example, you have this code elsewhere in your program:

    When you called the deposit method of the mySavingsAccount object, you correctly passed it a variable (namely the variable dep) that was of type double. You did so because the signature of the withdraw method is:
    deposit(double amount)
    So when you call a method using super, it is no different; for the withdraw method, you need to pass it a value that is a double (like '500') or a variable that represents a double (like 'with'). So if we were calling the withdraw method on the calss Account, we would use code like this:

    For a call from a subclass to its super class' mthode. we do not need to do anything different; we need to pass it a variable of type double. In the code you have, you are passing the word double (or the primitive declaration of double). So you need to change that method to:

    Do you see the difference?

    There is some other trouble lurking as well with this method... I�ll put that in the next post...
    [ March 01, 2004: Message edited by: Mark Vedder ]
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I just noticed something with your methods. I mistakenly assumed that your withdraw method returned the balance after doing a withdraw. That is not the case as I now see. So in SavingsAccount, this statement will not compile:

    This is because your witdraw method in Account returns a void, not a double. So you need to change this to:

    The next issue is then with what you are doing next in your logic:

    After getting the new balance, you are testing to see if it is less then zero. If it is, you pop up an error message; that�s fine. But your next line sets the newBalance to the amount of the withdraw. I�m not sure where you might have been heading with that. But if you think it through logically, it doesn�t make sense to set the balance (or newBalance to the amount of the withdraw. This of it this way. Say I had account with $100 in it. I then try to make a withdraw for $500. Your code executes and sets my balance to -$400. Your if statement see that this would cause an overdrawn account and gives me an alert. It then sets my newBalance to my withdraw amount, or to $500. So I started with $100, tried to withdraw $500. and ended up with $500. A nice profit of $400. I think what you are meaning to do here is return the balance to its original amount. So what you want to do is �withdraw� the negative amount of the original withfraw:

    This would return the balance back to its original amount. There are other ways of doing this logic, but we won�t get in to them at this point so we do not confuse the issue any.
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One other thing; in regards to the use of the WINDOW_TITLE variable, I see you have used it in your deposit and withdraw dialog boxes. However I also see you defined a String windowTitle2 just before the withdraw JOptionPane - I�m not sure why you did such since it is not needed. You are (correctly) using the final WINDOW_TITLE as the parameter for the dialog box�s title in both calls. You never use windowTitle2. Take a look at your code when I remove the ancillary code:

    windowTitle2 has the same value as WINDOW_TITLE, so it really doesn�t make sense to create a new variable that has the same value (or the same name in this case). You can just keep using WINDOW_TITLE as you have done. That was the whole idea behind using the final WINDOW_TITLE - you only have to create it once, and you can keep using it over and over and over again for all your JOptionPanes. Therefore you can delete that line.
    Lastly, there is a bug on the horizon (this bug would not cause your program to not compile, but would cause user confusion when the program is run). Take a look at this part of your code:

    With the highlights I�ve added, you should see the issue. I do not think that was your intention.
     
    Stacey Johnson
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, I'm down to 4 errors 3 having to do with my withdraw method and one I'm not to sure why. They are as follows

    java:157: illegal start of type
    super.withdrawl(amount);
    ^
    TestSavingsAccount.java:157: <identifier> expected
    super.withdrawl(amount);
    ^
    TestSavingsAccount.java:160: illegal start of type
    if (newBalance < 0) {
    ^
    TestSavingsAccount.java:169: <identifier> expected
    }
    ^

    Not too sure what's going on now. Do I need to put in an else statement? is that why it's saying if is an illegal start of type? I've had the last error message on other programs but as I worked out the bugs, it went away but this one still hasn't so I have no idea what it wants. For awhile it was saying a ";" was expected now it says identifier. Below is my updated code so you can see the whole picture:

    Thanks for all your help.
    Stacey
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Stacey,
    These errors are all related to the same problem; your withdraw method within the SavingsAccount class is not contained within a method declaration. You simply have the code sitting "out in the open" � so when the compiler gets there, it is not seeing a legal statement. You need to place all the withdraw code (starting with the comment "//withdraw from balance" and ending with the if statement�s closing brace) inside a method (i.e. have a method declaration and then place the code inside the method�s braces). In order for it to properly override the withdraw method from the Account class, the method declaration�s signature (i.e. its name, return type, and received parameters list) much match that of the withdraw method in the Account class. Take a look SuperClass & SubClass example I posted above and see how I did that.
    So your SavingsAccount withdraw method will end up being this:

    Also notice I fixed another small error � you had a typo in this line:

    Notice the extra 'l'?
    [ March 03, 2004: Message edited by: Mark Vedder ]
     
    Stacey Johnson
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Finally nearing the end of this one. Mark, I have changed the last but and added the method, everything compiles but now when I try to run the program I get an exception error:
    Exception in thread "main" java.lang.NoSuchMethodError:main
    I'm not sure, is it telling me that my main method is wrong? Or maybe it has a problem with my withdraw method? I'm not to sure where to look. It didn't give me any stack trace lines to go from so I'm kinda lost.
    Stacey
     
    Stacey Johnson
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello again;
    Weirdly enough I have gotten around the execption error. I am using JCreatorV3 LE and after I am finished compiling the program I need to put my cursor in the Testprogram area not where my subclass is written. Kinda bizzar but I guess it's because I'm put both my testprogram and subclass into the same java file. Would it be easy for me just to split the two or does it matter?
    Thanks for all your help, it's nice to see light again and have actually learned something on the way up!
    Stacey
     
    Mark Vedder
    Ranch Hand
    Posts: 624
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Stacey,
    I�m glad to hear you�re nearing the end; but I�m more glad that you feel you�ve learnt something. That, after all, is the point of all this
    To answer some of your questions, a Exception in thread "main" java.lang.NoSuchMethodError:main is exactly what it says and what you deduced it to be, the java runtime environment cannot find the main method of the program you are attempting to run. The main cause (no pun intended) is the obvious case of the class not actually having a main method. The next typical cause is typing wrong name or class when trying to run your application. Indirectly, that is what happened to you. I concluded that based on this statement:
    I am using JCreatorV3 LE and after I am finished compiling the program I need to put my cursor in the Testprogram area not where my subclass is written. Kinda bizzar but I guess it's because I'm put both my testprogram and subclass into the same java file.
    That is actually common in various IDEs. When your cursor is in the area of the SavingsAccount program, it �sees� you as working on that class, so when issue a compile or run command, it does so on that class (some IDEs will allow you to configure themselves such that this is not the case). So your IDE was in effect issuing the command java SavingsAccount and thus the JRE attempted to run the main method in SavingsAccount, and not TestSavingsAccount. When in doubt, you can (and should) always drop to a command line (i.e. a DOS window) and run a standard java (or javac in the case of compiling) command. This is one of the reasons many people advocate that beginners not use IDEs but rather stick to basic text editors and command line commands; it takes the issue of learning an IDE and its behaviors/usage out of the picture.
    Would it be easy for me just to split the two or does it matter? At this stage of the game, I would recommend you generally put all your classes in their own separate files. It will help prevent idiosyncrasies like this with your IDE. Also, at this point in your learning process, there will likely never be a case where a design decision would warrant putting multiple classes (shy of inner classes if you have learned about them yet) into the same file. So you can just cut the SavingsAccount class from the TestSavingsAccount.java file and paste it into a SavingsAccount.java file.
     
    A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic