Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

pl. anyone can provide ans for the following code

Thread view: 
gandhi.pathik@gmail.com - 25 Jan 2006 08:58 GMT
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
Francesco Devittori - 25 Jan 2006 09:04 GMT
> pl. look at the code first
>
[quoted text clipped - 20 lines]
> 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");
    }
 }
shruds - 25 Jan 2006 09:06 GMT
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
gandhi.pathik@gmail.com - 25 Jan 2006 09:15 GMT
thanks a lot this worked
Chris Uppal - 25 Jan 2006 13:01 GMT
> 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
Marcin Krzysztof Dąbrowski - 25 Jan 2006 09:08 GMT
gandhi.pathik@gmail.com napisał(a):

> public class Experiment
> {
[quoted text clipped - 13 lines]
> 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
Chris Smith - 25 Jan 2006 09:10 GMT
> pl. look at the code first
>
[quoted text clipped - 12 lines]
>     //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?

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Joe Attardi - 25 Jan 2006 16:36 GMT
> Where did you get this?
Perhaps from his homework assignment? :-)
Chris Uppal - 25 Jan 2006 16:48 GMT
> 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 sh.t ;-)

   -- chris
Thomas Hawtin - 25 Jan 2006 18:05 GMT
My preferred solution:

> public class Experiment
> {
[quoted text clipped - 9 lines]
>     }
>     //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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Uppal - 25 Jan 2006 18:28 GMT
>      static void println(String text) {
>          java.lang.System.out.println("1\n2\n3");
>      }

Tut tut!  Platform dependent line delimiters...

   -- chris
Thomas Hawtin - 25 Jan 2006 20:49 GMT
>>      static void println(String text) {
>>          java.lang.System.out.println("1\n2\n3");
>>      }
>
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Francesco Devittori - 26 Jan 2006 07:37 GMT
Thomas Hawtin wrote:
> gandhi.pathik@gmail.com wrote:
>
[quoted text clipped - 28 lines]
>
> Tom Hawtin

Now that's a nice solution!

Francesco
Vova Reznik - 26 Jan 2006 21:55 GMT
> pl. look at the code first
>
> public class Experiment
> {
>     static
>     {

        System.setOut(new MyPrinter(System.out));

>         System.out.println("1");
>     }
[quoted text clipped - 6 lines]
>     //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");
            }
        }       
    }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.