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 11 of 11

Thread: Adding convenience methods to an application class

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding convenience methods to an application class

    I'm just starting out to learn Java programming, starting with this basic piece of code:

      public class MyApplication
       {public static void main(String[] args)
         {MainFrame frame = new MainFrame();
     
          if (args.length > 0)
    /*      Need to parse the argument list intelligently, not */
    /*      just assume that only fileNames are present.       */
            for (int A = 0; A < args.length; A++)
              frame.addDocument(new File(args[A]));
     
          else
            frame.insertNewDocument();
     
          frame.setVisible(true);
         }
       }

    As the comment says, I need to parse the command line text. Since there are only a handful of options to detect, using something like the Apache parser is rather like killing flies with a sledgehammer. :-) Looking at the various String methods, i see some deficiencies from what I have found useful, so I'd like to create some convenience methods to avoid making my mainline code too complicated.
    The String class apparently cannot be extended or subclassed, but it occurred to me that methods owned by class MyApplication can perfectly well perform string manipulations; the only question is what attributes such methods need to have. These methods should be accessible globally, so I think that something along the lines of
    public static <returnType> convenienceMethod(String string1, String string2, int limit, bool respectCase) {...}
    where <returnType> might be String, int or bool and the number and type of arguments would vary depending on the method's purpose. Might these be the right sort of attributes for such methods?
    Last edited by jlturriff; September 17th, 2021 at 10:22 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding convenience methods to an application class

    Need to parse the argument list intelligently,
    If the method needs to handle the args being passed to any class, how can it know that what the class expects?
    For example one class expects a filename and another class expects a list of user names.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding convenience methods to an application class

    "any class"? I'm not sure that I understand your question. The methods will be used for processing Strings. E.g.
    public static int words(String phrase) {...}
    or
    public static String word(String phrase, int wordNum) {...}
    Last edited by jlturriff; September 17th, 2021 at 10:10 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding convenience methods to an application class

    By any class I meant the the class that was called by the java command on the command line:
    java AnyClass <the args here>
    The code in the main method of AnyClass would handle the Strings passed as <the args here>.
    What would the called method do with the String(s) passed to it?
    What would the called method return?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding convenience methods to an application class

    Quote Originally Posted by Norm View Post
    By any class I meant the the class that was called by the java command on the command line:
    java AnyClass <the args here>
    Okay. I interpreted "any class" as any non-specific class. :-)

    The code in the main method of AnyClass would handle the Strings passed as <the args here>.
    What would the called method do with the String(s) passed to it?
    What would the called method return?
    As usual, if the strings passed start with '-' they will be evaluated as options for the program; if not, as filepaths. How the options are interpreted would depend on what follows each '-'.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding convenience methods to an application class

    Ok, what problems are you having with the design of this method?
    What exactly is the method supposed to do? What does it return?
    Will the class that is using the method pass anything to the method that it can use to parse and evaluate the passed args?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding convenience methods to an application class

    Quote Originally Posted by Norm View Post
    Ok, what problems are you having with the design of this method?
    My primary concern is how to select the appropriate scope for the method; it should be usable anywhere in the application.

    What exactly is the method supposed to do? What does it return?
    Will the class that is using the method pass anything to the method that it can use to parse and evaluate the passed args?
    Its signatures are
    bool abbrev(String fullWord, int minLength, bool ignoreCase)
    bool abbrev(String fullWord, int minLength) /* respectCase is the default */
    so e.g.
    String     keyWord = "-Vers";
    String fullVersion = "-version";
    String fullVerbose = "-verbose";
     
    if (keyWord.abbrev(fullVersion,4,true))
      System.out.println(keyWord+" is an abbreviation of "+fullVersion+".");
     
    if (keyWord.abbrev(fullVersion,4,false))
      System.out.println(keyWord+" is not an abbreviation of "+fullVersion+".");
     
    if (keyWord.abbrev(fullVerbose,4,true))
      System.out.println(keyWord+" is not an abbreviation of "+fullVerbose+".");
    etc.
    Last edited by jlturriff; September 17th, 2021 at 04:09 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding convenience methods to an application class

    usable anywhere in the application.
    One way would be to make the method static. That would make it usable from anywhere.
    Being static would mean that the method does not need to save any values.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding convenience methods to an application class

    Quote Originally Posted by Norm View Post
    One way would be to make the method static. That would make it usable from anywhere.
    Being static would mean that the method does not need to save any values.
    Good. That's what I wanted to know.
    (How do I mark this thread "solved"?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding convenience methods to an application class

    Strange. Most of your posted code uses static.

    I don't know how to mark a thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding convenience methods to an application class

    Quote Originally Posted by Norm View Post
    Strange. Most of your posted code uses static.
    Yes, I guessed that static Might be the way to do it, but couldn't tell from any of the descriptions that I've seen anywhere.

    I don't know how to mark a thread as solved.
    I found it. Edit the top/first post in the thread. Below the text window is an item "Prefix:" with two possible values: [no prefix] and [SOLVED]; it only appears on the first post. Change the selection and save.

  12. The Following User Says Thank You to jlturriff For This Useful Post:

    Norm (September 17th, 2021)

Similar Threads

  1. Adding a menu to an application (NetBeans)
    By Alex555 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 12th, 2012, 06:26 AM
  2. Adding sound to a game application
    By Shaybay92 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 07:56 AM
  3. Please help!!! Adding java app to a web application
    By snapper in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2011, 10:26 AM
  4. Adding a frame/textarea to an application
    By zincc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 04:43 PM
  5. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM