• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not sure whether to override or overload methods in interface

 
Ranch Hand
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working on the parser for my interpreter, and with a lot of help I've come up with this idea on how to do it:

1. Split the source string into 2 parts with String.split(); get an array parts[] with two elements at [0] and [1].
2. If parts.length==1 then call directive.execute(); else if parts.length>1, String input=parts.[1]; call directive.execute(input)

The String input gets sent on to the appropriate class for tokenizing and handling. Now all the classes should be using directive.execute(input) except for one class (EndStatement), where input will be null and it only calls directive.execute(). I know I'm supposed to override or overload the method declared in my interface to do that, but I'm not sure how and reading up on the subject hasn't really helped me get my head around it either. Could someone help get me on track? Thanks!

Code for interface method and example classes


Code for parsing loop

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd just pass in an empty string if there are no arguments
 
Noam Ingalls
Ranch Hand
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should I write that sentence for the case where parts>1 then?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I only half read your original post. I thought you were just splitting it into two parts - the command and the rest of the line.

As you are splitting it up into multiple parts, I would just pass the whole array to your execute method and leave it to the individual implementing classes to interpret/validate the values.


Obviously you'll need to change the declaration of the execute method in your interface to accept a String[] instead of a String
 
Noam Ingalls
Ranch Hand
Posts: 60
Firefox Browser Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. Currently my PRINTLN command class only supports arguments in quotes. I'm trying to make PRINTLN support the equivalent of System.out.println() (effectively that command would be parsed as parts[0], with parts[1] being empty), but when I used an if-else statement to try and do that, it gave me exception errors, again it's ArrayIndexOutOfBounds. Is there some way to modify it?

This is what I tried:

>
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will tell you if the second element of an array is null, provided that array has at least two elements.



If you want to test whether an array has fewer than two elements (which is a completely different thing), you need to look at the array's length member.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic