A tricky program 2 to Java Beginners,

Java Programmer's Blog: A tricky program 2 to Java Beginners,

Question:- You should bring the output by not modifying the main method,The output is "Great Java Programmer".

class NMM //No main method
{
public static void main(String []args)
{
System.out.println(รข€œJavaรข€);
}
}
Seems tricky? Anybody?

Replies

  • Manish Goyal
    Manish Goyal
    May be this code is answer

    class NMM //No main method
    {
    static
    {
    System.out.println("Great Java Programmer");
    System.exit(0);
    }
    public static void main(String []args)
    {
    System.out.println("Java");
    }
    }[IMG]file:///C:/Users/Inder/AppData/Local/Temp/moz-screenshot-5.png[/IMG][IMG]file:///C:/Users/Inder/AppData/Local/Temp/moz-screenshot-6.png[/IMG]
  • ms_cs
    ms_cs
    goyal420
    May be this code is answer

    class NMM //No main method
    {
    static
    {
    System.out.println("Great Java Programmer");
    System.exit(0);
    }
    public static void main(String []args)
    {
    System.out.println("Java");
    }
    }[IMG]file:///C:/Users/Inder/AppData/Local/Temp/moz-screenshot-5.png[/IMG][IMG]file:///C:/Users/Inder/AppData/Local/Temp/moz-screenshot-6.png[/IMG]
    Nice, the main method must run buddy
  • mohan_185
    mohan_185
    I am new to java programming . may be this solution may not be optimal
    class NMM
    {
    public static void main(String []args)
    {
    System.out.println("java");
    }


    static
    {
    System.out.println("great");
    NMM.main(null);
    System.out.println("programmer");
    System.exit(0);

    }

    }
  • sookie
    sookie
    @Mohan Good job done

    @ms_cs Are you sure output should be "Great Java Programmer" [in one single line] and not in multiple lines like
    "Great
    Java Programmer"
    or
    "Great
    Java
    Programmer"
  • mohan_185
    mohan_185
    @sookie thanks buddy
  • ms_cs
    ms_cs
    @mohan_185 : Nice buddy..

    @sookie : yes output should be in one line..
  • mohan_185
    mohan_185
    @ms_cs
    output cant be in online because
    System.out.println(โ€œJavaโ€);

    After printing java it will go to the next line then only it will print programmer
  • ms_cs
    ms_cs
    cool Mohan, there is a trick,or may be a hack ๐Ÿ˜‰,
  • sookie
    sookie
    [Edit: Removed..Sorry for spamming]

    @ms_cs Well I am giving up. Today realised "I am not even a Java beginner..so sad" ๐Ÿ˜” Send me the answer through PM if can't post it here. ๐Ÿ˜€
  • abhinaykumar
    abhinaykumar
    public class NMM
    {
    static
    {
    System.out.print("Great ");
    NMM.main(null);
    System.out.print("Programmer ");
    System.exit(0);

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

    }

    }

    the output is "Great Java Programmer" in single line
    the logic is nothing but using println prints the message in new line if u use only print in sop statement u can get it in single line buddy....
    we cannot print the output in single line coz in the main method u used the println statement for java so its impossible to get the desired output without changing main method.....
  • mathew720
    mathew720
    dude i would love to learn Java how , in much time could i learn it , which site would you opt , funny part is i am not even an engineer

    , i have few basics of c++ long back in high school ,its interesting i guess answer i am waiting for an answer too
  • abhinaykumar
    abhinaykumar
    is there really a trick to print the way with out changin the main method if yes please send me the answer...
  • sookie
    sookie
    abhinaykumar
    is there really a trick to print the way with out changin the main method if yes please send me the answer...
    As per me, following program works without changing the main method but I am still not sure it is the answer. But still feeling good after trying atleast something of my own ๐Ÿ˜
    
    import java.io.OutputStream;
    import java.io.PrintStream;
    
    /**
     *
     * @author sookie
     */
    public class Print_GreatJavaProgrammer {
    public static void main(String []args)
    {
    System.out.println("Java");
    }
                
    static 
    {
     PrintStream printStreamOriginal=System.out;
    
     /* Setting the out member of the System class in such a way that it prints nothing*/ 
     System.setOut(new PrintStream(new OutputStream(){
    			public void write(int b) {
    			}
    		}));
    
        /* Calling the main method */
       Print_GreatJavaProgrammer.main(null);
        
       /* Re-setting it so that it prints in actual manner */ 
       System.setOut(printStreamOriginal);
       System.out.println("Geat Java Programmer");
       System.exit(0);
    }
    
    }
    
    Feel free to ask if have any questions regarding it. ๐Ÿ˜€

    * Still waiting for the actual answer :-| *
  • abhinaykumar
    abhinaykumar
    hai Sookie can u xplain the statements u used in the static method........
  • ms_cs
    ms_cs
    Sookie:- nicely you have done.
    Let me post mine's solution

    import java.io.*;
    public class NMM
    {
    static
    {

    final PrintStream out = System.out;

    PrintStream newOut = new PrintStream(out)
    {

    public void println(String str1)
    {
    print("Great ");
    print(str1);
    super.println("Programmer");
    }
    };

    System.setOut(newOut);
    }


    public static void main(String [ ]argums)
    {
    System.out.println("Java ");
    }
    }
  • Manish Goyal
    Manish Goyal
    @ms_cs Can you please explain this program?
  • ms_cs
    ms_cs
    goyal420
    @ms_cs Can you please explain this program?
    @goyal: I just have written code for overriding the method println(String) which will be used in the main mehtod,,that;s it
  • sookie
    sookie
    abhinaykumar
    hai Sookie can u xplain the statements u used in the static method........
    Sure !
    System.setOut(new PrintStream(new OutputStream(){
    public void write(int b) {
    }
    }));
    In the above statement, I am making System.out to behave in such a manner that it prints/writes nothing as output in console. It is simply overriding the write() method of OutputStream abstract class. The concept that is used above is called as "Anonymous inner classes". Alternatively you could do is , create a class that extends "OutputStream" class and then overrides its "write" method -just like I did in above statement and then instantiate that implementing class and pass as an argument to "PrintStream" class .

    Well, this was syntax related. Now what exactly that statement does is -
    If you look at System.out.println then System.out is a PrintStream type of class which calls println() method of PrintStream to write anything on console and println() calls "write" method internally . If you look at the API carefully then you will find that write() method is being overriden from FilterOutputStream class which ultimately extends OutputStream class. *sighs* Hope you understood the concept [As per me it was not at all a problem for JAVA BEGINNER until the beginner knows the ready made answer. ๐Ÿ˜€ ]

    Now coming to the next line
    Print_GreatJavaProgrammer.main(null);
    Now this triggers the main() method and tries to print "Java" from the main() method but because we have already set the write() method as below
    public void write(int b) {
    }
    So, it will not print "Java" in the console.

    Now coming to next line
    System.setOut(printStreamOriginal); //where PrintStream printStreamOriginal=System.out;
    Now using above statement , I am again setting the System.out value to the one default provided by Java folks. System.setOut(System.out). It will revert back the System.out to original PrintStream write() method. No overriding, nothing. Only default values.

    Now coming to last line
    System.out.println("Geat Java Programmer");
    The above statement of static block works as normal Java program because we have already re-set the System.out to default print() method.

    Finally System.exit(0) to exit from this program and thread hopefully *just kidding* ๐Ÿ˜€

    Hope you got the concept and if still any questions, feel free to ask. ๐Ÿ˜€ I think solution by ms_cs is quite easy to understand but will work only for few cases.
  • abhinaykumar
    abhinaykumar
    thanks sookie
  • abhinaykumar
    abhinaykumar
    import java.util.Scanner;
    public class Manager
    {
        public static void main(String[] args)
        {
            String[] units={ " ","one","two","three","four","five","six","seven","eight","nine" };
            String[] teens={ " ","ten","eleven","twele","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
            String[] tens={ " ", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred" };
            Scanner nte=new Scanner(System.in);
            System.out.println("Enter any Value from One to Hundred");
            try
            {
            int num=Integer.parseInt(nte.next());
            if(num<10)
            {
                int k=num % 10;
                System.out.println(units[k]);
            }
            else
            {
                if(num<19)
                {
                    int m,n;
                    m=num/10;
                    n=num%10;
                    int l=n+m;
                    System.out.println(teens[l]);
                }
                else
                {
                    if(num>19)
                    {
                        int a,b;
                        a= num/10;
                        b= num % 10;
                        System.out.println(tens[a-1]+units);
                    }
                    else
                    {
                      
                    }              
                }
            }
            }
            catch(NumberFormatException ex)
            {
                        ex.printStackTrace();
            }
            }
    }


    hai guys can anyone tellme how to reduce lines in this program
  • sookie
    sookie
    Try the below one - if your all cases still working or not
    
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public
    [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Manager {[/SIZE]
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] main(String[] args) {[/SIZE]
    [SIZE=2]String[] units = { [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"one"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"two"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"three"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"four"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"five"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"six"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE]
    [SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"seven"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"eight"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"nine"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] };[/SIZE]
    [SIZE=2]String[] teens = { [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ten"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"eleven"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"twele"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"thirteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE]
    [SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fourteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fi fteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"sixteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"seventeen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"eighteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE]
    [SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"nineteen"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] };[/SIZE]
    [SIZE=2]String[] tens = { [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"twenty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"thirty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fourty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"fifty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"sixty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE]
    [SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"seventy"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"eighty"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ninety"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"hundred"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] };[/SIZE]
    [SIZE=2]Scanner nte = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Scanner(System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]in[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Enter any Value from One to Hundred"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] num = Integer.[I]parseInt[/I](nte.next());[/SIZE]
    [SIZE=2][/SIZE] 
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (num>1 && num < 10) {[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println(units[num%10]);[/SIZE]
    [SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (num>1 && num < 19) {[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println(teens[(num/10)+(num%10)]);[/SIZE]
    [SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (num>1 && num > 19) {[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println(tens[num/10 - 1] + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+units[num%10]);[/SIZE]
    [SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Invalid Number"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    [LEFT][SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (NumberFormatException ex) {[/SIZE]
    [SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Number Format Exception"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
    
    I guess you should have kept it in separate thread so that we can try to extend this program problem further. ๐Ÿ˜€โ€‹

    [/LEFT]
  • abhinaykumar
    abhinaykumar
    okay sookie
  • Malhar
    Malhar
    class Test1
    {
    static{
    System.out.print("Great");
    }
    public static void main(String []args)
    {
    System.out.println("Java");
    }
    }
  • TheV
    TheV
    import java.util.Scanner;
    class Manager{
    public static void main(String[] args){
    String[] units={ "","one","two","three","four","five","six","seven", "eight","nine","ten","eleven","twele","thirteen","fourteen","fi fteen","sixteen","seventeen","eighteen","nineteen","twenty" };
    String[] tens={ " "," ","twenty ", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred" };
    Scanner nte=new Scanner(System.in);
    System.out.println("Enter any Value from one to Hundred");
    try{
    int num=nte.nextInt();
    if(num<=20){
    System.out.println(units[num]);
    }
    else{
    int a= num/10;
    int b= num % 10;
    System.out.println(tens[a]+" "+units[b]);
    }
    }
    catch(Exception ex){
    System.out.println("Invalid Input");
    }
    }
    }
    I work out this way....
  • Hemant Patel
    Hemant Patel
    // another logic ๐Ÿ˜‰
    import java.util.*;
    class nmm
    {
    public static void main(String ...k)
    {
    System.out.println("Java");
    }
    nmm() {
    System.out.println("Great Java Programer");
    System.exit(0);
    }
    static nmm jcore=new nmm();
    }
  • spiky
    spiky
    class NMM //No main method
    {
    init
    {
    System.out.print("Great");
    }
    public static void main(String []args)
    {
    System.out.print(โ€œ Javaโ€);
    show();
    }
    static void show()
    {
    System.out.print(" Programmer");
    }
    }
  • Agam Agarwal
    Agam Agarwal
    the solution is easy
    just stop the main method from executing and print what u want to print
    can be done as follows

    class NMM //No main method
    {
    static
    {System.out.println("Great Java Programmer");
    System.exit(0);
    }
    public static void main(String []args)
    {
    System.out.println("Java");
    }
    }
  • TheV
    TheV
    You can do this way tooo...

    class NMM{
    static{
       System.out.print("Great");
       NMM.main(null);
      System.out.print("Programer");
      System.exit(1);
    }
    public static void main(String []args){
        System.out.println("Java");
    }
    }
  • Java freak
    Java freak
    public class helloworld{
                   
    static
    {
    System.out.print("Great ");
    }                   
    public static void main(String args[])
    {
               
    System.out.println("java ");
                           
    }
        static
    {
    System.out.print("Devleoper");
    }
    
    }

You are reading an archived discussion.

Related Posts

hi friends, can any one tell me what is cloud computing? how this concept of cloud come into existence? What is the benefit of using Windows own Azure cloud over...
Hi Friends, What is NP-Complete problem? Is it true to say that a problem which is solvable in polynomial time that are verifiable in polynomial time?
hi friends, anybody have idea about what is ipv6 and ipv4? ๐Ÿ˜’
hi friends, some day's ago i watched MI(mission impossible) a hollywood film by tom cruise in that film somebody send email to tom cruise.And after reading that email,email is vanished...
CEans, Here's a new project idea that stuck my mind today, after looking at a surveillance camera in my GYM ๐Ÿ˜ฒ . However, I think it can be really useful...