I recently joined this community as a Java golfer, and I haven't been able to find a definitive consensus on acceptable solution formats for Java.
From what I gather, the following are allowed (please correct if consensus is otherwise):
- full program (class definition with
main
method) - full method (modifiers, signature, body)
- lambda expression
What I'm wondering about is whether a multi-method solution is acceptable.
Description
Such a solution would consist of program text defining two or more methods as they would appear in a class definition, a choice of one such method as an entry point to the program (which would take input and produce output according to standard rules), and, optionally, a class name. To clarify: a multi-method solution would not include fields, initializers, or class definitions; only methods.
Execution
These solutions would be tested by inserting the program text into an empty definition of a public class (as named by the author, if applicable) and then invoking, from a class within the same package, the entry method on an instance of the class produced by the default constructor or on the class itself if the method is static (author's choice).
Rationale
It's easy to imagine this being a desirable format for complex solutions. It permits subroutines more cheaply than a lambda or method solution (where class definitions or functional interface declarations would be required), and it is preferable to a full program solution because I/O can be done flexibly through method parameters and return values and class boilerplate is eliminated.
If this question gets enough activity, I assume consensus will be decided by answer score. Answers may then be affirmative, negative, or affirmative with conditions/exceptions. If this has already been addressed, someone can refer to that discussion and close this one.
-
\$\begingroup\$ People do this all the time \$\endgroup\$Pavel– Pavel2017年08月10日 02:12:30 +00:00Commented Aug 10, 2017 at 2:12
1 Answer 1
You're allowed to define just methods
We accept the definition of a single method as a code-golf answer, such:
public void f(int a, int b, int c){System.out.println("Blah blah");}
And it should be equally acceptable to define many methods.
public int c(int a, int b){return a+b;}
public void f(int a, int b){System.out.println(c(a,b))}
As long as you're not assuming anything else about the program, EG. A Class Name, Imports, etc.
-
\$\begingroup\$ I see. Perhaps I didn't need to write such an elaborate question then! \$\endgroup\$Jakob– Jakob2017年08月10日 02:01:38 +00:00Commented Aug 10, 2017 at 2:01
-
3\$\begingroup\$ Always good to ask, also, don't fret asking these questions over on the nineteenth byte, our chat room \$\endgroup\$ATaco– ATaco2017年08月10日 02:02:56 +00:00Commented Aug 10, 2017 at 2:02
-
\$\begingroup\$ But what about Java's lambdas? \$\endgroup\$Okx– Okx2017年08月10日 18:45:47 +00:00Commented Aug 10, 2017 at 18:45
-
\$\begingroup\$ @Okx Java lambdas are allowed, but there is some debate about whether or not type signatures need to be included/counted. \$\endgroup\$user45941– user459412017年08月10日 20:47:07 +00:00Commented Aug 10, 2017 at 20:47