pl. anyone can provide ans for the following code

G

gandhi.pathik

pl. look at the code first

public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}


In the above code i want to print output as (in order)1,2,3 without
modifying the block which is marked as don't touch it...

can anyone pl. give me suggestion how i can do it?


Regards,
pathik s gandhi
 
F

Francesco Devittori

pl. look at the code first

public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}


In the above code i want to print output as (in order)1,2,3 without
modifying the block which is marked as don't touch it...

can anyone pl. give me suggestion how i can do it?


Regards,
pathik s gandhi

You can add a shutdown hook, i.e. a thread that will be called before
your program ends.

Francesco


public class Experiment
{
static
{
System.out.println("1");
Thread shutDown = new Thread(new ShutDownThread());
Runtime.getRuntime().addShutdownHook(shutDown);
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}

public class ShutDownThread implements Runnable {
public void run() {
System.out.println("3");
}
}
 
S

shruds

try this :
public class Experiment {

static{
System.out.println("1");
}
public static void main(String[] args) {
System.out.println("2");
}
static{
main(null);
System.out.println("3");
System.exit(0);
}
}

hope this helps
 
G

Guest

(e-mail address removed) napisał(a):
public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}


In the above code i want to print output as (in order)1,2,3 without
modifying the block which is marked as don't touch it...

To see 1, 2 execute following code:

new Experiment();
Experiment.main(null);

I don't see where is System.out.println("3");

Regards
Dabek
 
C

Chris Smith

pl. look at the code first

public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}

Looks like some kind of puzzle. I've got a couple good answers, but I
won't give it away to give others a chance to try it.

Where did you get this?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Uppal

shruds said:
static{
main(null);
System.out.println("3");
System.exit(0);
}

Clever.

How about:

===========
import java.io.*;

public class Test
{
static
{
System.setOut(new Hack(System.out));
}

public static void
main(String[] args)
{
System.out.println("2");
}
}

class Hack
extends PrintStream
{
Hack(PrintStream out)
{
super(out);
}

public void
println(String string)
{
boolean hackit = "2".equals(string);
if (hackit) super.println("1");
super.println(string);
if (hackit) super.println("3");
}
}
===========

I think one could find an even more obscure solution by hacking the system
default charset, but I can't be bothered to turn the idea into code.

-- chris
 
C

Chris Uppal

Joe said:
Perhaps from his homework assignment? :)

If it is, and he's handed in any of the actual code that's been shown so far,
then he's in deep shit ;-)

-- chris
 
T

Thomas Hawtin

(e-mail address removed) wrote:

My preferred solution:
public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it

static Experiment System;
static Experiment out;

static void println(String text) {
java.lang.System.out.println("1\n2\n3");
}
}


In the above code i want to print output as (in order)1,2,3 without
modifying the block which is marked as don't touch it...

http://jroller.com/page/tackline?entry=let_s_start_with_a

Tom Hawtin
 
T

Thomas Hawtin

Chris said:
Tut tut! Platform dependent line delimiters...

Yup. Me forgetting the mind bogglingness of PrintStream. It'll
auto-flush (it does not appear documented whether System.out has
auto-flushing enabled) on the new line character (and undocumented
places besides), but that doesn't mean it'll be treated as a line separator.

Tom Hawtin
 
V

Vova Reznik

pl. look at the code first

public class Experiment
{
static
{

System.setOut(new MyPrinter(System.out));
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");

}
//don't touch it
}


static class MyPrinter extends PrintStream{
private static boolean printed = false;

public MyPrinter(OutputStream out) {
super(out);
}

public void println(String str){
if(!printed){
printed = true;
super.println("1");
super.println("2");
super.println("3");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top