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 2 of 2 FirstFirst 12
Results 26 to 49 of 49

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

  1. #26
    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 idea is to help the OP learn how to program, not to do his work for him.
    If you do post code, it should have comments describing how the solution was found and what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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

    TS, if you want me to I can post it or if you prefer the community to answer direct questions let me know. I understand Norm thinking, and I don't want to rob you of an education. There's a personal satisfaction of working through the problem.

  3. #28
    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 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?
    Java is a fairly well documented language. The Java standard library is documented in the Java API specifications (a.k.a. "Javadoc"). You can navigate through the library by clicking the links, or you can do the way I like to do it: use your favorite search engine.

    For example:

    Say I wanted to learn about the Random class in Java. I would search for the phrase "Java 7 Random" (you can leave out the 7, but I like to include it so I get the documentation for the latest version of Java). The first link I got was the Javadoc for the Random class. Usually the Javadoc is one of the first few results.

    Some IDE's provide built-in ways to access the Javadoc. For example, I use Eclipse (a free and opensource IDE) and if I highlight over a particular method or class it will bring up a small context block with the Javadoc for that item. If I want to learn more about the item, I can click in the box and open up the Javadoc in the built-in Eclipse web browser.
    Last edited by helloworld922; September 16th, 2012 at 03:28 PM.

  4. #29
    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

    Please correct me if I am using the wrong Java terminology. I am giving it a shot to learn this Java language along side my son. I am writing the code in a logical approach. Each approach is a method.

    1) The program selects a random number.
    2) The program then asks for a user input.

    Why is it that doing it this way does not work but if I remove this piece of code, " public static void Guess(){" and place the {} accordingly the program functions?

    Is this a wrong approach to Java or code program?


    import java.util.*;
     
    public final class NumberTwoX{    
        public static void main(String[] args){   
            Random Rand = new Random();
                for(int x = 0; x < 100; x++);//Makes a random number less than 100{    
                int Number;
                Number = (int)(Math.random() * 100);//Chooses the amount of numbers that can be randomly generated
                System.out.println(Number + " = Random Number");//Prints the number that is made. Block this line using comment statement when done coding            
        }
     
     
        public static void Guess(){
            Scanner input = new Scanner (System.in);
            System.out.println("Enter your guess:  ");
            int Guess;
            Guess = input.nextInt();
        }
     
     
    }
    Last edited by GIban; September 17th, 2012 at 08:42 AM.

  5. #30
    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

    What is the guess() method supposed to do? It defines an int variable, assigns it a value and then drops it. Should the method return the value it gets from the user?

    NOTE: coding conventions are that variable and method names begin with lowercase letters.
    Class names with uppercase.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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 followed the coding convention, variables and method start with a lower case and every first word a Upper Case.

    The guess() method should return a value to the user but at this point I just want to see "Enter your guess: " on the terminal screen. Perhaps I am not following what you are trying to teach me.


    import java.util.*;
     
    public final class NumberTwoXe{    
        public static void main(String[] args){   
            Random Rand = new Random();
                for(int x = 0; x < 100; x++);//Makes a random number less than 100{    
                int number;
                number = (int)(Math.random() * 100);//Chooses the amount of numbers that can be randomly generated
                System.out.println(number + " = Random Number");//Prints the number that is made. Block this line using comment statement when done coding            
        }
     
     
        public static void guess(){
            Scanner input = new Scanner (System.in);
            System.out.println("Enter your guess:  ");
            int guess;
            guess = input.nextInt();
        }
     
     
    }

  7. #32
    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 guess() method should return a value
    See how to define a method: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Having the names of methods be the same as variable names can be confusing. Try giving them different names. Method names are usually verbs, variables are nouns.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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

    Can you explain this part of the code:

    for(int x = 0; x < 100; x++)

  9. #34
    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

    That is a for loop. Have you read the tutorial that describes for loops?
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Java for loops are like those is most other modern languages. They are used when you know how many times you want to go around the loop. In the posted code it's 100 times.

    When coding for and while loops it is important that the code inside the loop's control be inclosed in {}s. The code posted in the main() method doesn't do that. That makes it harder to read the code and easier for the programmer to make a mistake when coding by thinking some code is inside a loop when it isn't.
    Last edited by Norm; September 17th, 2012 at 10:19 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    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

    Norm,

    I appreciate you guiding me in the right direction. I spent hours trying to figure this out all morning. I know you gave me some hints but I am not getting it. Again, my approach is one bit of code for every action or in this java case method().

    I changed the coding up to meet proper java convection, but still can't make these two methods function together. As individual method(), they work. The program should respond like:

    18 = Random Number
    Enter your answer:

     
    import java.util.Random;
    import java.util.Scanner;
     
    public class NumberTwoX{    
        public static void main(String[] args){   
            Random Rand = new Random();
            int number;
            number = (int)(Math.random() * 99 + 1);//Chooses the amount of numbers that can be randomly generated
            System.out.println(number + " = Random Number");//Prints the number that is made. Block this line using comment statement when done coding            
        }
     
     
        public static void guess(){
            Scanner input = new Scanner (System.in);
            System.out.print("Enter your answer:  ");
            int answer;
            answer = input.nextInt();
     
        }
     
     
    }

  11. #36
    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

    What is the purpose of the guess() method? It reads a number from the user and does nothing with it. Should it return the number to the caller?
    Where does the code call the guess() method? If a method is not called, it will not be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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 give up ... hours spent and can't make it work per method. Works if I just remove this string of code:

     
            }
     
        public static void guess(){

  13. #38
    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

    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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

    Ok ... more reading and still can't figure out how to make both of these two method() work together. Anymore reading and I am positive that it can't be done without removing one line of code.


    Thanks for you

  15. #40
    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

    Post the code you have so far and a question about it. I have a hard time knowing what your question is at this point.

  16. #41
    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/where does your code "CALL" the guess() method?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    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

    Post the code you have so far and a question about it. I have a hard time knowing what your question is at this point.

    JPS,

    Here's what I am trying to understand more for me than my son.

    I have two method(), the first method() generates a random number, the second method() asks for a user input. The first method() outputs a number but the second method does not. If I remove this part of the code

    }
    public static void guess(){

    I get the result I want. I am trying separate the code in blocks so that each block of code can be used later in other programs. This is the goal and what I think my son is trying to achieve so that each block of code can be used in later projects. Also, this will help me understand small block of codes and how to interface each block.


    Norm,
    How/where does your code "CALL" the guess() method?

    I still don't know what you are asking? Sorry ... getting to old




     
     
    import java.util.Random;
    import java.util.Scanner;
     
    public class NumberTwoX{    
        public static void main(String[] args){   
            Random Rand = new Random();
            int number;
            number = (int)(Math.random() * 99 + 1);//Chooses the amount of numbers that can be randomly generated
            System.out.println(number + " = Random Number");//Prints the number that is made. Block this line using comment statement when done coding            
        }
     
     
        public static void guess(){
            Scanner input = new Scanner (System.in);
            System.out.print("Enter your answer:  ");
            int answer;
            answer = input.nextInt();
     
        }
    Last edited by GIban; September 18th, 2012 at 09:53 PM.

  18. #43
    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

    Your code calls several methods:
    Math.random()
    System.out.println(num...
    input.nextInt()

    It needs to call the guess() method if you want the method to execute.

    Have you tried Google about: how to call a method

    This page of the tutorial has examples of calling methods:
    http://docs.oracle.com/javase/tutori...arguments.html
    Last edited by Norm; September 18th, 2012 at 03:08 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    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

     Have you tried Google about: how to call a method

    I tried everything you suggested and Google. At my current level of "Self Tutoring", Java or any other programing is just not for me. Been there back in 1977, taking a class in Fortran, didn't do good at all. Tried to self teach myself Basic to program a PIC chip as I am all into electronics. Only thing I got out of the Basic Self Tutoring Guide is the hardware, books, and dust.

    And today, inspired to learn Java since I now have a teenager taking a class. Programing is just not for me! Only so much I can read and research and every book and tutoring starts out with "Hello World". Nothing mention about the Java API Library, or the break down of method statement like "static", "main". As they say, it will all be discuss later on in the chapter so for now just take it as it is.

    I was fortunate to find a "Cheat Sheet" on Java. I'll give this a day or two and then I'll close this Topic....I guess you can say I am exhausted reading, reading, guessing and not getting an understanding.

  20. #45
    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

    I now have a teenager taking a class.
    Sounds like it's time to get the teenager more involved.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #46
    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 GIban View Post
    ...I have two method(), the first method() generates a random number, the second method() asks for a user input. The first method() outputs a number but the second method does not. If I remove this part of the code

    }
    public static void guess(){
    Have you given any thought as to WHY removing those two lines has the effect that it does?

    Quote Originally Posted by GIban View Post
    I get the result I want. I am trying separate the code in blocks so that each block of code can be used later in other programs.
    That is one of the basic ideas involved with object oriented programming. Good thinking.

    Quote Originally Posted by GIban View Post
    ... how to interface each block.
    That is the problem you are fixing by removing the two lines of code.

    In your code the way it is, (with the code NOT removed), the JVM 'calls' your main method. That is how it all starts, (as far as your code and this discussion goes). Execution starts at public static void main(String[] args) {
    That open curly brace on that line is the marker that says beginning. When the JVM called your method, it called for a complete block of code to be executed. We just discussed the start marker. The end marker is the marker on the first of the two lines you remove to get it to do what you want. So when your main method is called those defining markers say execute all code between { and the corresponding }. In this case 4 lines of code.

    In your code with the two lines removed, you are extending the scope of your main method to include 4 more lines of code. Now when main is called, 8 lines of code are executed instead of 4. Do you see how this changes what you have told the computer to do?

    Now look at what happens without removing the lines of code. You said run 4 lines of code when main is called. main is called by the JVM and is considered the starting point for your program. This is why the 4 lines in the body of main are executed. Somewhere behind the scenes, hidden away from you, is a line of code that basically says: main(args); that causes your code to execute. What Norm has been trying to tell you, you have not said for your other lines of code to ever be executed. So they don't ever execute. If you want a method to run, you have to call the method. When you want the method to run, is where you call it. You have to call your guess method to be executed from code that is being executed somewhere. (In this program there is a short list of options). If this seems confusing try searching "calling a method" and "using method return" and feel free to ask more questions.

    But don't get discouraged. It is really hard to see the bigger picture when you are being force-fed random pixels from the pattern. Being a survivor from your rowboat myself, proudly wearing the scars of war, how ever you like to see it, it just takes practice.

  22. #47
    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

    Sounds like it's time to get the teenager more involved.

    Yes, I am hoping he enjoys programing and will stay with it as I got some PIC circuits that I would like for him to program. Or, even better, teach is Ol man how to. An old project hidden in the closet.

  23. #48
    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

    jps,

    Thanks for taking the time in explaining this java code in "laymans term".

    So I did the below an added this Guess method = new Guess(); in the main method.

    And changed this line of code from public static void guess(){ to public class Guess(){. Not sure why. Also capitalize first letter of Guess so as to meet java convection.



    Now I have an identifier expected errror on this line of code System.out.println("Enter your answer: ");. YES I am Googling for the answer to this error.



    import java.util.Random;
    import java.util.Scanner;
     
    public class NumberTwo1{    
        public static void main(String[] args){ 
            int number;
            Random Rand = new Random();//This is a code to generate Random numbers
            number = (int)(Math.random() * 99 + 1);//A number from 1 to 100
            System.out.println(number + " = Random Number");  
     
            Guess method = new Guess();
     
     
        }
     
        public class Guess {
     
            int answer;
     
            System.out.println("Enter your answer:  ");
            Scanner input = new Scanner (System.in);
            answer = input.nextInt();
     
     
        }    
     
     
    }
    Last edited by GIban; September 18th, 2012 at 10:53 PM.

  24. #49
    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

    You have identified the problem and a solution:
    You were not calling your method, and now you are, but not doing so correctly. You are calling in a good place. So most of your problem is fixed.
    What you have in your code for calling the method looks more like creating an object than calling a method. Check out some of the links Norm posted about calling methods, and consider the examples he quoted from your code showing method calls you are already making.

Page 2 of 2 FirstFirst 12