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.

Results 1 to 5 of 5

Thread: help with creating a method and returning a value

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with creating a method and returning a value

    Hi. I am having trouble understanding some simple concepts. I am frustrated.

    I typed this simple code and it counts up by three's.



    import acm.program.*;

    public class Freespace extends ConsoleProgram {

    private static final int MAX_COUNT = 300;


    public void run() {

    println("Count upwards by 3's");
    for (int i = 0; i <= MAX_COUNT; i = i + 3) {
    println(i);
    }
    }
    }

    I want to create a private method that does the counting for me and then returns it to the public method, but I am stuck.

    I basically want the method to be called countThree and have the calculations and for loop done there. Please help. My textbook is vague on its example and Google results are always in a different library I dont know the syntax of yet.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with creating a method and returning a value

    The code you posted is a method with code inside it. What do you want to change about the method you have posted? What do you want the new method you are working on to do?

    See the tutorial about methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)


    One problem I see is that you are using 3rd part classes and are NOT using java SE classes and methods. That can make many of the responses you get here not make sense.

    Why are you using the acm packages instead of the java SE packages and clases?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with creating a method and returning a value

    The ACM packages are part of The Art and Science of Java textbook... the source of my learning.

    Basically, I am learning information hiding and would like the main public class to receive the code that counts by three from the new Private method I create.

    It is hard for me to elaborate.

    --- Update ---

    import acm.program.*;

    public class Freespace extends ConsoleProgram {

    private static final int MAX_COUNT = 300;


    public void run() {

    println("Count upwards by 3's");

    // now I want to get a return result from the new method countThree below




    private int countThree() {

    for (int i = 0; i <= MAX_COUNT; i = i + 3) {
    println(i);
    }

    //how can I get this result returned to the public void


    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with creating a method and returning a value

    get this result returned
    Use the return statement.
    See the tutorial: http://docs.oracle.com/javase/tutori...turnvalue.html


    When posting code Please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: help with creating a method and returning a value

    The countThree() method doesn't have anything to return that I can see. It is designed to count (print) and that's all.

Similar Threads

  1. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  2. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 04:59 PM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM