Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Thread: Java ... I Need Your Help so I Can Help My Son

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java ... I Need Your Help so I Can Help My Son

    import java.util.Scanner;
    import java.util.Random;

    public class TheBrainGame2
    {
    static String Rating;
    static int count;
    static int Random;
    static int Guess;


    public static void main(String[] args)
    {
    Guess = count;
    if (count <= 10)
    {
    Rating = Rating(count);
    }
    }


    public static int NumberGenerator(int Random)
    {
    Random RandomNumber = new Random(12);
    count = 0;
    for(int x = 0; x < 100; x++)

    {
    int Number = 1 + RandomNumber.nextInt(100);
    System.out.print(Number + " ");
    count++;
    if(count > 15)

    {
    System.out.println(" ");
    count = 0;
    }
    }

    return Random;
    }


    public static String Rating(int count)
    {
    {
    if(count > 10)
    Rating = "Fail! You are a Cyber Dud!";
    if(count == 10)
    Rating = "Circuit Breaker";
    if(count <= 9)
    Rating = "Techno Geek";
    if(count <= 7)
    Rating = "Super Chip";
    if(count <= 5)
    Rating = "Cyber Brain";
    if(count <= 3)
    Rating = "Micro Genius";
    if(count == 1)
    Rating = "Genius";
    }
    return Rating;
    }



    public static void Answer(int Answer)
    {
    Scanner input = new Scanner(System.in);
    int x;
    System.out.println("Enter your answer please");
    x = in.nextInt();
    }


    public static String Guess(int Guess)
    {
    while(Guess != Random && count >= 10)
    System.out.println("Enter Guess #");
    if(Guess < Random)
    System.out.println("Wrong! you are too low!");
    if(Guess > Random)
    System.out.println("Wrong! you are too high!");
    if(Guess == Random)
    System.out.println("Correct! you are a ...");
    count = count + 1;
    return Rating;
    }
    }
    Last edited by GIban; September 15th, 2012 at 02:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    fails to compile.
    Please copy and paste here the full text of the error messages.
    We don't write code for you, you will have to write the code. When you have problems or questions, post them.

    Also posted at: http://www.java-forums.org/new-java/...lp-my-son.html
    Last edited by Norm; September 14th, 2012 at 09:23 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    curmudgeon (September 14th, 2012)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    System.out.print(Number + " ";

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    I always have to look askance at posts like this when someone states they are helping their "friend" or their "son" as if many of them feel they'll get more help this way rather than coming out and saying that it really is their own homework problem. I could be wrong, but again it always makes me wonder...

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Curmudgeon,

    I am his Dad and he is not doing to well in this subject. I only found this out at Parent Teacher Conference last night. So I took the liberty to be proactive. I will not give him the answer just to pass the course. However, I will make suggestions just to get him back on track by reviewing the suggestion provided by members of this forum.

    On the other hand, we were able to make part of the code work if we manually enter a 1 or any number we will get a reply back. The rest of the code he was stumped. It took 3 hours to accomplish that part of the coding and he went to bed late, 10 pm. That was good progress as I was very persistance. Unfortunately, I would not be of any help or assitance since he will be at his mothers house for the next week. Then its school break and he will be with me for another week. I will continue where we left off even if he does not turn in a working code.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Don't be discouraged about your son's progress. It is possible that his brain doesn't work the way a programmer's brain works. Not everyone can easily learn programming. I see a couple of students a month that never get it.
    I read a good blog about this several months ago, but don't have a link to it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    We tried to break the code into sections by using the comment syntax to isolate each section and to see each portion of each code compile, but it didn't work.

    So when faced with multiple tasking in a program, and in the above case, can it be done? My explanation to my son is work and build each task individually then combine them. Not sure if this mental thinking is correct or not.

    Seems liking I am getting the thirst to learn coding in java.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Every java file you submit to the compiler must be syntactically correct to get a clean compile.
    I don't know about the technique of commenting out sections of a program that does not compile. I'd rather start with a skeleton of the class and its methods and incrementally add code in small chunks and compile often. Fix the errors and compile again until no errors, then add a few more lines and do it again.
    Having a lot of unused code in the source could be confusing.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    curmudgeon (September 14th, 2012)

  11. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Yep, as Norm says, above, if you aren't using an IDE (and you shouldn't if just learning Java), then be sure not to try to add good code to bad. In other words don't try to add any new functionality until the current code compiles well.

  12. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by curmudgeon View Post
    ... if you aren't using an IDE (and you shouldn't if just learning Java),...
    I don't agree with just this part of the comment.
    An IDE is a great learning tool in my experience. There are many features in some IDEs that help you learn syntax as you go. I agree that an IDE can hide some of the important stuff from the user, but that is not a good reason to not use an IDE. In fact hiding some things from the user is the OOP design in black and white. It is also how most students are taught from day 1 (in every class I have ever seen). They throw hello world at you, and say to ignore 3/4 of the keywords in the code for now, just use it the way you see it. I think the hand-holding features like live syntax checking and code highlighting provided by an IDE is a wonderful tool. Too many people expect the new guy to 'just get it' when they have no idea what it is they need to get yet.

  13. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by jps View Post
    I don't agree with just this part of the comment.
    An IDE is a great learning tool in my experience. ....
    We're probably digressing too far from the original poster's question with this discussion, but while I agree with the utility of an IDE and its use early in the programming process, my concern is that it shouldn't be used *too* early. It's hard enough trying to scale the steep learning of programming in Java, that it can be much harder and confusing to try to scale two steep learning curves at the same time. Not only that, but all Java programmers should be familiar with the mechanics of command-line compilation and running, if only to gain a full respect and understanding of classpath and package structure. My own personal bias is that the first few weeks of Java programming should be done using a text editor and the command line tools, but then once the new programmer is familiar with this process, then sure, tackle doing this with Eclipse, NetBeans, IntelliJ IDEA or whatever IDE they'd like to try. ymmv,

  14. The Following 2 Users Say Thank You to curmudgeon For This Useful Post:

    jps (September 14th, 2012), Norm (September 14th, 2012)

  15. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Back on topic,

    I agree with norm and curmudgeon about the top-down approach rather than the left-to-right approach.

    Frame out the basic structure (what classes you need, a rough idea of what methods and fields, etc.) and make sure this structure compiles and runs, even if it doesn't do anything. Bonus points are to go back after you have the basic structure done and add comments to describe what it is you want to do in a method and roughly the order you want to do it in.

    ex. (a little more verbose than you would usually want to do, but it illustrates the purpose)

    public class SomeClass
    {
        /**
        * Prints the name and age of a person
        */
        public static void printInfo(String name, int age)
        {
            // print out the name
            // print out the age
        }
     
        /**
        * Converts a person's age in years to months, days, hours, minutes, and seconds
        */
        public static int[] ageConverter(int age)
        {
            // initialize the results array
            int[] results = new int[5];
            // calculate age in months
            // calculate age in days
            // calculate age in hours
            // calculate age in minutes
            // calculate age in seconds
     
            return results;
        }
     
        public static void main(String[] args)
        {
            // ask the user for their name
            // ask the user for their age
            // print out the basic info
            // convert their age to other time units and print it out
        }
    }

    At this point you can switch to the left-to-right approach for implementing the methods as these tend to be somewhat linear. Build and test your code often! This helps you to make sure what you're coding didn't break something else and does what you want it to.

    Last remarks (slightly off-topic again):

    Regardless of how you write/compile your code I would suggest you format your code using spaces/tabs. The general rule of thumb is after every open curly brace you indent by 1 tab and after every closing curly brace you de-dent by 1 tab. If you do this properly you can identify errors where you have a missing brace and/or extra braces much easier. it also makes reading your code much simpler as you can tell what blocks of code are related to each other.

    You can get near-bare-bones editors which have nothing more than a text editor with syntax highlighting and auto-indentation. This gives you the two most useful benefits for beginners that IDE's offer without any potential pitfall or short-coming. If you use Windows, I would recommend using Notepad++. For Linux GEdit has these features, and I think Emacs/XEmacs do too (not positive about this, though). I'm not sure what to use with Macs since I've never really used one.

  16. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    I will be playing around with his code once I get to my home computer. The IDE that I installed is called Bluejay.

    I was reading a tutorial for java but cannot grasp this code, such as the meaning of [] as in the below:

    public static int[] ageConverter(int age)


    Anyway, THANKS for pointing me in the right direction.

  17. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java ... I Need Your Help so I Can Help My Son

    The [] denotes an array. More information about arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  18. #15
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    I'm not sure if you pasted the code correctly, but there are some brackets missing in several lines. Fix those first.

    Remember that if you have a main method in your program, Java will start at main first, so check what it's doing from there. Is it successfully calling the other methods?

  19. #16
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error messages.
    We don't write code for you, you will have to write the code. When you have problems or questions, post them.

    Also posted at: Java ... I Need Your Help so I Can Help My Son

    I edit the code and removed the comments.

    The error that the compiler is highlighted is:

    public static void Answer(int Answer)
    {
    Scanner input = new Scanner(System.in);
    int x;
    System.out.println("Enter your answer please");
    x = in.nextInt();

    }
    Last edited by GIban; September 15th, 2012 at 02:42 PM.

  20. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    The error that the compiler is highlighted
    Please copy and paste here the full text of the error messages. Sorry, I can't see your computer or its highlighting.
    Here is a sample of an error message from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  21. #18
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    My son and classmate figured out the error. But can some explain why?


    public static void Answer(int Answer)
    {
    Scanner input = new Scanner(System.in);
    int x;
    System.out.println("Enter your answer please");
    x = input.nextInt();
     
     
     
    }
    Last edited by GIban; September 15th, 2012 at 04:00 PM.

  22. #19
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error messages. Sorry, I can't see your computer or its highlighting.
    Here is a sample of an error message from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    I highlighted the error in red.


    x = in.nextInt();


    "cannot find symbol - variable in"

    x =in.nextInt();
    Last edited by GIban; September 15th, 2012 at 03:59 PM.

  23. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    cannot find symbol - variable in
    The compiler could not find a definition for the variable: in
    Where was in defined? You must define all variables before using them.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #21
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by Norm View Post
    The compiler could not find a definition for the variable: in
    Where was in defined? You must define all variables before using them.
    Correct, the compiler could not find the variable: in because that was the error. The variable should have been: input.

    I'm currently self tutoring myself in Java. Having fun but I wish it was like doing a webpage and coding in HTML or CSS. With HTML/CSS, you can see the results in picture format instead of an input output format.

  25. #22
    Junior Member
    Join Date
    Sep 2012
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    After numerous hours and attempts at trying to understand Java on this particular coding of "Guessing The Number", I was able to find a code similar to my initial question.

    Unfortunately, and as a first time user of Java, it is not easy. Not even with ALL the tutorials on the web. The "Hello World" example doesn't do any justice, at least to me. The "Guess The Number" program on the web gives a somewhat decent explanation to an already sought out question.

    But there are a lot of Java information on the web that lacks explanation by example, in my opinion to get me on track. So here's what I learned during the initial learning curve process based on the question "Design a program based on the following:"


    1) User input guesses a number.
    2) Computer generates a random number and stores it in memory.
    3) User has ? tries.
    4) Program responds by printing on the screen if the user guess is low, high, correct, or failed.

    Now this may seem simple to the already seasoned Java Programer, but to me, I haven't a clue. The first approach is to break the question in a logical manner .....

    1) Write a program to generate a random number.

    Well you have to first start by understanding what this part of the code does. This is simple and I understand the function of the below code.

    public class NumberGuessing {
     
          public static void main(String[] args) {
     
           }
     
    }

    The part I don't understand are the library of names that are not listed. I can easily find the Java "Key Words", "Operators", "Element", etc on a Java Syntax cheat sheet. The library names like "random", "println", "import java.util.*", "math", etc. How do I learn these without doing several simple practice java coding by examples?

    Can anyone lead me in the right direction on taking a java coding approach based on the question? I provided a WORKING java program just so "YOU" won't have to give me the written java code answer. There is an explanation on the below java code but it doesn't explain things like using Math.random or where to find Math and random.

     
     
    import java.util.Scanner;
     
     
     
    public class NumberGuessingGame {
     
          public static void main(String[] args) {
     
                int secretNumber;
     
                secretNumber = (int) (Math.random() * 999 + 1);
     
                System.out.println("Secret number is " + secretNumber); // to be removed
     
                // later
     
                Scanner keyboard = new Scanner(System.in);
     
                int guess;
     
                do {
     
                      System.out.print("Enter a guess: ");
     
                      guess = keyboard.nextInt();
     
                      System.out.println("Your guess is " + guess);
     
                      if (guess == secretNumber)
     
                            System.out.println("Your guess is correct. Congratulations!");
     
                      else if (guess < secretNumber)
     
                            System.out
     
                                       .println("Your guess is smaller than the secret number.");
     
                      else if (guess > secretNumber)
     
                            System.out
     
                                       .println("Your guess is greater than the secret number.");
     
                } while (guess != secretNumber);
     
          }
     
    }
    Last edited by GIban; September 16th, 2012 at 09:33 AM.

  26. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java ... I Need Your Help so I Can Help My Son

    How do I learn these without doing several simple practice java coding by examples?
    I think writing several simple java programs is a good way to learn. I don't know a shorter way.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #24
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Java ... I Need Your Help so I Can Help My Son

    Quote Originally Posted by Norm View Post
    Don't be discouraged about your son's progress. It is possible that his brain doesn't work the way a programmer's brain works. Not everyone can easily learn programming. I see a couple of students a month that never get it.
    I read a good blog about this several months ago, but don't have a link to it.
    Is it because they don't put the effort into it? I think programming can be challenging, but because his son struggles in a subject shouldn't automatically mean is brain isn't wired correctly.
    There are so many examples that people thought there child to be incompetent, and then to find out they learn just differently? I would love to see that article by the way.

  28. #25
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Java ... I Need Your Help so I Can Help My Son

    I also completed the program for the child. Am I allowed to post it or is that against the rules? I don't want get a strike against me.

Page 1 of 2 12 LastLast