Java puzzle accessing main's arguments without them being passed in
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have a Java puzzle I'm having trouble solving. Given the following three classes:
public class A1
protected boolean foo()
return true;
public class B1 extends A1
public class C1
private static boolean secret = false;
public boolean foo()
secret = !secret;
return secret;
public static void main(String args)
C1 c = new C1();
for (int i = 0; i < args.length; i++)
c.foo();
A1 a = new B1();
if (a.foo() == c.foo())
System.out.println("success!");
I need to complete the class B1 , however I want, without change the classes A1
and C1
or adding new files, such that for at least one argument, C1
will always print the string "success!".
I think that I need to override the method foo()
of the class A1
,
and rewrite it by getting the number of arguments from the main function on C1
, to get the right return value.
My problem is that I don't know how to do that. (Remember that I'm not allowed to solve this problem by writing A1 a = new B1(args.length)
instead of A1 a = new B1()
. That'd be too easy.)
Any suggestions on how to do it, or even totally different solutions, would be appreciated!
java
add a comment |Â
up vote
6
down vote
favorite
I have a Java puzzle I'm having trouble solving. Given the following three classes:
public class A1
protected boolean foo()
return true;
public class B1 extends A1
public class C1
private static boolean secret = false;
public boolean foo()
secret = !secret;
return secret;
public static void main(String args)
C1 c = new C1();
for (int i = 0; i < args.length; i++)
c.foo();
A1 a = new B1();
if (a.foo() == c.foo())
System.out.println("success!");
I need to complete the class B1 , however I want, without change the classes A1
and C1
or adding new files, such that for at least one argument, C1
will always print the string "success!".
I think that I need to override the method foo()
of the class A1
,
and rewrite it by getting the number of arguments from the main function on C1
, to get the right return value.
My problem is that I don't know how to do that. (Remember that I'm not allowed to solve this problem by writing A1 a = new B1(args.length)
instead of A1 a = new B1()
. That'd be too easy.)
Any suggestions on how to do it, or even totally different solutions, would be appreciated!
java
Any rules about using reflection?
â Jorn Vernee
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a Java puzzle I'm having trouble solving. Given the following three classes:
public class A1
protected boolean foo()
return true;
public class B1 extends A1
public class C1
private static boolean secret = false;
public boolean foo()
secret = !secret;
return secret;
public static void main(String args)
C1 c = new C1();
for (int i = 0; i < args.length; i++)
c.foo();
A1 a = new B1();
if (a.foo() == c.foo())
System.out.println("success!");
I need to complete the class B1 , however I want, without change the classes A1
and C1
or adding new files, such that for at least one argument, C1
will always print the string "success!".
I think that I need to override the method foo()
of the class A1
,
and rewrite it by getting the number of arguments from the main function on C1
, to get the right return value.
My problem is that I don't know how to do that. (Remember that I'm not allowed to solve this problem by writing A1 a = new B1(args.length)
instead of A1 a = new B1()
. That'd be too easy.)
Any suggestions on how to do it, or even totally different solutions, would be appreciated!
java
I have a Java puzzle I'm having trouble solving. Given the following three classes:
public class A1
protected boolean foo()
return true;
public class B1 extends A1
public class C1
private static boolean secret = false;
public boolean foo()
secret = !secret;
return secret;
public static void main(String args)
C1 c = new C1();
for (int i = 0; i < args.length; i++)
c.foo();
A1 a = new B1();
if (a.foo() == c.foo())
System.out.println("success!");
I need to complete the class B1 , however I want, without change the classes A1
and C1
or adding new files, such that for at least one argument, C1
will always print the string "success!".
I think that I need to override the method foo()
of the class A1
,
and rewrite it by getting the number of arguments from the main function on C1
, to get the right return value.
My problem is that I don't know how to do that. (Remember that I'm not allowed to solve this problem by writing A1 a = new B1(args.length)
instead of A1 a = new B1()
. That'd be too easy.)
Any suggestions on how to do it, or even totally different solutions, would be appreciated!
java
java
edited 4 hours ago
John Kugelman
233k51390443
233k51390443
asked 4 hours ago
MrSonic
344
344
Any rules about using reflection?
â Jorn Vernee
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago
add a comment |Â
Any rules about using reflection?
â Jorn Vernee
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago
Any rules about using reflection?
â Jorn Vernee
4 hours ago
Any rules about using reflection?
â Jorn Vernee
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
Since secret
is static, you could just create a new C1
and call foo
on that, just be sure to invert the result as well:
class B1 extends A1
public boolean foo()
return !(new C1().foo());
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing theargs
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)
â Jorn Vernee
4 hours ago
add a comment |Â
up vote
-1
down vote
Maybe I didn't understand question but I think
protected boolean foo()
return false;
In class B1 will be working
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Since secret
is static, you could just create a new C1
and call foo
on that, just be sure to invert the result as well:
class B1 extends A1
public boolean foo()
return !(new C1().foo());
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing theargs
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)
â Jorn Vernee
4 hours ago
add a comment |Â
up vote
6
down vote
accepted
Since secret
is static, you could just create a new C1
and call foo
on that, just be sure to invert the result as well:
class B1 extends A1
public boolean foo()
return !(new C1().foo());
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing theargs
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)
â Jorn Vernee
4 hours ago
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Since secret
is static, you could just create a new C1
and call foo
on that, just be sure to invert the result as well:
class B1 extends A1
public boolean foo()
return !(new C1().foo());
Since secret
is static, you could just create a new C1
and call foo
on that, just be sure to invert the result as well:
class B1 extends A1
public boolean foo()
return !(new C1().foo());
answered 4 hours ago
Jorn Vernee
18.6k33051
18.6k33051
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing theargs
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)
â Jorn Vernee
4 hours ago
add a comment |Â
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing theargs
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)
â Jorn Vernee
4 hours ago
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
This is an excellent solution!, Just for knowledge, do you think there is a solution for my problem as I have shown above?
â MrSonic
4 hours ago
@MrSonic For the problem of accessing the
args
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)â Jorn Vernee
4 hours ago
@MrSonic For the problem of accessing the
args
array from B1, not that I know of. Although I suppose technically a VM implementation could choose to capture the arguments in some globally accessible place, e.g. the system properties. Either way, I suspect this is the expected solution to the puzzle :)â Jorn Vernee
4 hours ago
add a comment |Â
up vote
-1
down vote
Maybe I didn't understand question but I think
protected boolean foo()
return false;
In class B1 will be working
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
add a comment |Â
up vote
-1
down vote
Maybe I didn't understand question but I think
protected boolean foo()
return false;
In class B1 will be working
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Maybe I didn't understand question but I think
protected boolean foo()
return false;
In class B1 will be working
Maybe I didn't understand question but I think
protected boolean foo()
return false;
In class B1 will be working
answered 4 hours ago
ekiryuhin
336
336
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
add a comment |Â
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
guess nope... number of arguments could be different
â Andrew Preizner
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
It isn't working for even number of argument on C1..
â MrSonic
4 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52580234%2fjava-puzzle-accessing-mains-arguments-without-them-being-passed-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Any rules about using reflection?
â Jorn Vernee
4 hours ago
you can do whatever you like, as long you complete only class B1 without changing the classes A1,C1 and not adding additional files.
â MrSonic
4 hours ago