Method not found, or abstract/ or add semicolon errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pantherxin
    New Member
    • Apr 2010
    • 12

    Method not found, or abstract/ or add semicolon errors

    If I put commas after public Invoice ( String number ) and void setPartNumber( String number ) I get error method not found, or abstract with this code. Currently getting error message ";" on these 2/3 lines; didnt include 3rd line with same message.


    Code:
    public class Invoice 
    {
        private String partNumber; // part number for this Invoice
       private int partQty; // part qty for this Invoice
    
    // constructor initializes partNumber with String supplied as argument
    public Invoice( String number )
     // constructor initializes partQty with int supplied as argument
    public Invoice( int qty )
    {
    partNumber = number; // initializes partNumber
    partQty = qty; // intializes partQty
    } // end constructor
    
     // method to set part number
    public void setPartNumber( String number )
    // method to set part qty
    public void setPartQty( int qty )
    {
    partNumber = number; // store the part number
    partQty = qty; // store part qty
    } // end method setPartNumber
      // end method setPartQty
    
    // method to retrieve the part number
    public String getPartNumber()
    // method to retrieve the part qty
    public int getPartQty()
    {
    return partNumber;
    return partQty;
     // end method getPartNumber
    
    Please, no direct answer...hint or point to book or website
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You aren't declaring your methods as abstract, so they *must* have a function-body (curly braces). Simple.

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      First of all you can not have an abstract constructor.

      If you use keyword abstract to an constructor you need to override it. But constructors can not be overridden.

      you have
      Code:
      public Invoice( String number )
      You can set the data members in this constructor. Or have a default constructor and use setter and getter methods to set and get the data.

      BTW why you need to override an constructor. You can override methods and you can define methods as abstract. But please note if you have a method as abstract your class must also be abstract.

      Regards
      Dheeraj Joshi

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        @Mark -- It is an constructor not a method. :)

        Regards
        Dheeraj Joshi

        Sorry mark, you are right, even methods do not have body along with a constructor. You were right on target. :)
        Last edited by Dheeraj Joshi; Apr 15 '10, 01:24 PM. Reason: Added sorry sentence at the end

        Comment

        • pantherxin
          New Member
          • Apr 2010
          • 12

          #5
          Well, I am not trying for an abstract constructor...f or some reason that is the message I am getting along with "method not found" if I apply the semicolons at the noted lines of code (methods); which according to my text, there should not be semicolons, but that is the message I get when I try to compile. Thanks for help anyway.

          Comment

          • Fr33dan
            New Member
            • Oct 2008
            • 57

            #6
            Code Block Reminder Stolen from tlhintoq:
            TIP: When you first created your question you were asked to wrap your code with [code] tags.
            [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
            It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

            Comment

            • Fr33dan
              New Member
              • Oct 2008
              • 57

              #7
              Now for your question the reason you are getting an error is because you are trying to implement multiple methods with a single code block:
              Code:
              // method to set part number
              public void setPartNumber( String number )
              // method to set part qty
              public void setPartQty( int qty )
              {
                  partNumber = number; // store the part number
                  partQty = qty; // store part qty
              } // end method setPartNumber
              // end method setPartQty
              The code block for a method must immediately follow the method definition. Because of this you've actually only written a code block for the setPartQty method and the compiler cannot find the code for the setPartNumber method.

              The compiler is trying to interpret the method as a single line of code to which it cannot find the end of (The missing semicolon). When you add the semicolon it realizes that there is no code for the method and tells you that it thinks you meant for the method to be abstract.

              You must either write one method that will combine the two using multiple parameters:
              Code:
              // method to set part information
              public void setPartInfo( int qty, String number)
              {
                  partNumber = number; // store the part number
                  partQty = qty; // store part qty
              } // end method setInfo
              or write separate code blocks for each method:
              Code:
              // method to set part number
              public void setPartNumber( String number ){
                  partNumber = number; // store the part number
              } // end method setPartNumber
              
              // method to set part qty
              public void setPartQty( int qty )
              {
                  partQty = qty; // store part qty
              } // end method setPartQty
              You must make the same choice for your constructor. (In my opinion the better code would be to use the combined for the constructor and separate for the setters but it really depends on what you need it to do)
              Last edited by Fr33dan; Apr 15 '10, 07:01 PM. Reason: Typo / Rewording for clarification

              Comment

              • pantherxin
                New Member
                • Apr 2010
                • 12

                #8
                Oh man, I tried something similar i.e. combining two for one method, but as you see from my error I still tried to create invdividual method statements; which sounds crazy just saying it now. Thank you much!

                Comment

                Working...