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: Define method output?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Define method output?

    I'm learning java and some parts are really confusing while other parts I can catch on immediately. Below is some of the methods i have to find the results for. but i don't quite understand. I really would appreciate the help to dissecting the method so i can learn whats going on. Thanks

    1)Add user input via Scanner to main that calls each method.
    2)Output the results of calling each method, this is not as straightforward for the third method.
    3)For each method, make a new version that takes in String for the first parameter instead of an integer. You will probably have to change the return type as well. Test each of these methods and both show and explain how they are different from the original methods.




    public class Hw3
    {
    public static void main(String[] args)
    {
    // Stuff goes here.
    }

    public static int hw3_method1(int a, int b)
    {
    for(int i=0; i<b; i++)
    {
    System.out.printf("%s%3d%s %4d%s\n", "|", i, "|", a, "|");
    }
    return a;
    }

    public static int hw3_method2(int a, int b)
    {
    int retval = 0;
    for(int i=0; i<b; i++)
    {
    retval += a;
    }
    return retval;
    }

    public static int[] hw3_method3(int a, int b)
    {
    int[] retval = new int[b];
    for(int i=0; i<b; i++)
    {
    retval[i] = (i+a);
    }
    return retval;
    }

    // You will need to change what is here.
    // You will also need to add stuff.
    public static int hw3_method1_modified()
    {
    return 0;
    }

    public static int hw3_method2_modified()
    {
    return 0;
    }

    public static int[] hw3_method3_modified(int a, int b)
    {
    return new int[1];
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Define method output?

    Post your code in code tags. Learn how at the Announcement at the top of this sub-Forum.

    The 3 items you've listed appear to be taken out of context, and your intro to them is no help. We can't read minds and aren't mysteriously able to see the rest of the assignment to aid in our understanding of what you need help with, so you need to provide us that insight.

    "Finding results for" methods doesn't really make sense.

    Give your variables and methods meaningful names.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Define method output?

    Sorry. I agree the assignment seems weird to me, in which I'm totally lost. I'm attaching the assignment files to see if it helps.


    public class Hw3
    {
    public static void main(String[] args)
    {
    // Stuff goes here.
    }
     
    public static int hw3_method1(int a, int b)
    {
    for(int i=0; i<b; i++)
    {
    System.out.printf("%s%3d%s %4d%s\n", "|", i, "|", a, "|");
    }
    return a;
    }
     
    public static int hw3_method2(int a, int b)
    {
    int retval = 0;
    for(int i=0; i<b; i++)
    {
    retval += a;
    }
    return retval;
    }
     
    public static int[] hw3_method3(int a, int b)
    {
    int[] retval = new int[b];
    for(int i=0; i<b; i++)
    {
    retval[i] = (i+a);
    }
    return retval;
    }
     
    // You will need to change what is here.
    // You will also need to add stuff.
    public static int hw3_method1_modified()
    {
    return 0;
    }
     
    public static int hw3_method2_modified()
    {
    return 0;
    }
     
    public static int[] hw3_method3_modified(int a, int b)
    {
    return new int[1];
    }
     
    }
    Attached Files Attached Files

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,408
    My Mood
    Mellow
    Thanks
    83
    Thanked 473 Times in 464 Posts

    Default Re: Define method output?

    Having the complete context helps, but I agree that it's an assignment with minimal instructions, maybe just barely enough.

    For number 1, I assume the user is to be given an option of which method to call, like a menu:

    Select the desired action:
    a. Call method 1
    b. Call method 2
    c. Call method 3
    etc. . . .
    h. Exit

    Choice:
    Some of the menu choices may include a sub choice (or choices) to designate the variables passed to the methods, but I'm not clear on that.

    For number 2, each method returns a result, and that result should be output to the screen. For example, method 2 returns an int which looks like a * b. Your program should print that result:

    The result of method2 is: (the result)

    Number 3 is pretty self explanatory, but if it is not yet to you, it should make more sense to you after finishing numbers 1 and 2.

    Good luck!

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Mraudemar (September 23rd, 2013)

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Define method output?

    Thank you for the clarification! I'm still unsure about method 1 but your suggestion is greatly helpful. Method 2 makes more sense to me now since you pointed it out, in fact, it's actually similar to a problem on a previous assignment. Thanks Again!

Similar Threads

  1. How to call printDate method to output the dates?
    By my21 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2013, 01:28 AM
  2. [SOLVED] Method calls and output looks a little funny
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 09:47 PM
  3. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  4. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 04:21 AM
  5. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM