pl. anyone can provide ans for the following code

Discussion in 'Java' started by gandhi.pathik@gmail.com, Jan 25, 2006.

  1. Guest

    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
     
    , Jan 25, 2006
    #1
    1. Advertising

  2. wrote:
    > 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");
    }
    }
     
    Francesco Devittori, Jan 25, 2006
    #2
    1. Advertising

  3. shruds Guest

    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
     
    shruds, Jan 25, 2006
    #3
  4. 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
     
    =?UTF-8?B?TWFyY2luIEtyenlzenRvZiBExIVicm93c2tp?=, Jan 25, 2006
    #4
  5. Chris Smith Guest

    <> wrote:
    > 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
     
    Chris Smith, Jan 25, 2006
    #5
  6. Guest

    thanks a lot this worked
     
    , Jan 25, 2006
    #6
  7. Chris Uppal Guest

    shruds wrote:

    > 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
     
    Chris Uppal, Jan 25, 2006
    #7
  8. Joe Attardi Guest

    > Where did you get this?
    Perhaps from his homework assignment? :)
     
    Joe Attardi, Jan 25, 2006
    #8
  9. Chris Uppal Guest

    Joe Attardi wrote:

    > 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
     
    Chris Uppal, Jan 25, 2006
    #9
  10. 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 25, 2006
    #10
  11. Chris Uppal Guest

    Thomas Hawtin wrote:

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


    Tut tut! Platform dependent line delimiters...

    -- chris
     
    Chris Uppal, Jan 25, 2006
    #11
  12. Chris Uppal wrote:
    > Thomas Hawtin wrote:
    >
    >> 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 25, 2006
    #12
  13. Vova Reznik Guest

    wrote:
    > 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");
    }
    }
    }
     
    Vova Reznik, Jan 26, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    3
    Views:
    3,282
    Steven Cheng[MSFT]
    Feb 3, 2004
  2. Rajeev

    Can anyone provide methe best books for C?

    Rajeev, Mar 14, 2006, in forum: C Programming
    Replies:
    8
    Views:
    284
    Rajeev
    Mar 14, 2006
  3. jaffarkazi
    Replies:
    10
    Views:
    428
    Richard Bos
    Jul 4, 2008
  4. hpsoar
    Replies:
    5
    Views:
    341
  5. Aaron Brockhurst

    Can anyone help with the following code

    Aaron Brockhurst, Jul 28, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    107
    Uri Guttman
    Jul 28, 2003
Loading...

Share This Page