This discussion is archived
6 Replies Latest reply: Jan 15, 2013 11:31 AM by abillconsl RSS

A simple java command line program i made, yours thoughts and opinions...

982162 Newbie
Currently Being Moderated
Greetings to everone
 import java.util.*;
import java.io.*;

           class birdnest

    {
          public static void main(String []args)
  {
         boolean p;
         boolean q;
         Scanner read1 = new Scanner(System.in);
         System.out.println("The weather is cold, Please evaluate..T/F");
         p = read1.nextBoolean();
         System.out.println("There's a lot of wind outside, Please evaluate..T/F");
         q = read1.nextBoolean();
      
                                         if(p==true && q==false)
                                       {
                                           System.out.println("1st Level passed only!!");
                                       } 
                           
                                            if(q==true && p==false)
                                         {
                                           System.out.println("2nd Level passed only!!!"); 
                                         }

                                if(p==true && q==true)   
                              {
                                  System.out.println("1st and 2nd Levels passed!!!!");                                                         
                              }
                     
                      else if (p==false && q==false)
                 {
                           System.out.println("Game Over baby!");
                 }




          }
   }
  
Please tell me your thoughts and opinions!

Edited by: 979159 on Jan 12, 2013 4:25 PM
  • 1. Re: A simple java command line program i made, yours thoughts and opinions...
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    OK, you bagged for it... ;o)

    Please follow Java naming conventions:
    http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
    Your class shoul rather be named <tt>BirdNest</tt> (insted of <tt>birdnest</tt>

    Space in your editor is cheap, so don't be eager with your variable names. E.g. <tt>p</tt> would better be named <tt>isColdWether</tt> or <tt>isWetherCold</tt> but not <tt>wetherIsCold</tt>.

    Variables should be declared when they are used, not in advance. Java does not have the restriction that all variables must be declared at the beginning of a method.

    Then I'd resolve the if/else cascade to an enum
    class BirdNest{
      enum Input{ 
        TRUE_TRUE("1st and 2nd Levels passed!!!!"),
        TRUE_FALSE("1st and 2nd Levels passed!!!!"),
        FALSE_TRUE("2nd Level passed only!!!"),
        FALSE_FALSE("Game Over baby!"),
        private final String message;
        Input( String message) { this.message=message;}
        String getMessage() { return message;}
      }
      public static void main(String []args)  {
           Scanner read1 = new Scanner(System.in);
           System.out.println("The weather is cold, Please evaluate..T/F");
           Boolean  p = read1.nextBoolean(); // autoboxing to wrapper class, kept ugly naming...
           System.out.println("There's a lot of wind outside, Please evaluate..T/F");
           Boolean   q = read1.nextBoolean();
           // get the enum constant matching input:
           Input input = Input.valueOf(p.toString().toUpperCase()+"_"+q.toString().toUpperCase());
           System.out.println(input.getMessage());      
      }
    }
    Yes, this is not a beginners approach and it does require some experience and "feeling" for OOP in general and Java in special, but you asked for it...

    bye
    TPD
  • 2. Re: A simple java command line program i made, yours thoughts and opinions...
    982162 Newbie
    Currently Being Moderated
    Thanks a lot bro , but do you mind shedding some light on what the keyword enum does?
  • 3. Re: A simple java command line program i made, yours thoughts and opinions...
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

    bye
    TPD
  • 4. Re: A simple java command line program i made, yours thoughts and opinions...
    gimbal2 Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

    bye
    TPD
    I would like to point out the hidden message there if you don't mind TPD.

    Any time you ask a "how does this work" type of question, you'll either get a link to the manual or just be told to RTFM, depending on how lazy you've shown yourself to be. Learn from this moment and promise to yourself to be better: "I shall research".
  • 5. Re: A simple java command line program i made, yours thoughts and opinions...
    982162 Newbie
    Currently Being Moderated
    I'm very sorry, i never meant it to be in that way though.
  • 6. Re: A simple java command line program i made, yours thoughts and opinions...
    abillconsl Explorer
    Currently Being Moderated
    Hello Hamad,



    Your code works. So if this is an early attempt, I'd say you're successful. But when you say "give me your thoughts and opinions"? you could mean several things. Can you be more specific?



    One thing that I notice right away from an end user's point of view is what does "first and second levels passed" supposed to mean to me? It kind of sounds like you want a "yes man" to tell you that it's cold and windy all the time. I'm just kidding, but the point is, when you design, think of how the end user will use your application and what will be and won't be intuitive. So you might consider changing your wording. Along those lines, it looks like you want the end user to enter either a T or an F. However you really want a true or false.



    As to the code. Try to clean it up a bit. As was alluded to, Java classes by convention begin with an uppercase letter and every succeeding syllable starts with an uppercase letter. Methods are similar but begin in lowercase. Please see the links provided for additional information - there's quite a bit there. Conventions are important.



    However as I've said to start, if this is an early attempt, I'd say you're on the right road - keep going!



    ~Bill

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points