Java puzzle accessing main's arguments without them being passed in

The name of the pictureThe name of the pictureThe name of the pictureClash 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!










share|improve this question























  • 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














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!










share|improve this question























  • 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












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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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());







share|improve this answer




















  • 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


















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






share|improve this answer




















  • 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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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());







share|improve this answer




















  • 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















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());







share|improve this answer




















  • 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













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());







share|improve this answer












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());








share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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
















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













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






share|improve this answer




















  • 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














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






share|improve this answer




















  • 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












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






share|improve this answer












Maybe I didn't understand question but I think



protected boolean foo() 
return false;



In class B1 will be working







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke