Forums Register Login

Not sure whether to override or overload methods in interface

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
I'd just pass in an empty string if there are no arguments
+Pie Number of slices to send: Send
How should I write that sentence for the case where parts>1 then?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:

>
1
+Pie Number of slices to send: Send
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.
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1153 times.
Similar Threads
Why declaring a variable as string?
is it possible to change jvm locale by using command line parameters ?
HashMap implementation for an interpreter: cannot find symbol error
HashMap values-- I think I know why but how do I fix this null pointer exception?
Restart login if a wrong password is writed. (Java Swing)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:38:44.