Results 1 to 12 of 12

Thread: Method Help

  1. #1
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Method Help

    Write a method called printNumbers that accepts a maximum number as a parameter and prints each number from 1 up to that maximum, inclusive, boxed by square brackets. For example, consider the following calls:

    printNumbers(15);
    printNumbers(5);
    These calls should produce the following output:

    [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
    [1] [2] [3] [4] [5]
    You may assume that the value passed to printNumbers is 1 or greater.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~

    I already wrote the whole program but what is a method? I can't seem to figure that out.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Method Help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: Method Help

    I figured it out, just realized I wasn't suppose to have my class within.

  4. #4
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: Method Help

    Could someone help me with finding the method for this problem? I'm stuck on it.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
    Write a method called printPowersOf2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. For example, consider the following calls:

    printPowersOf2(3);
    printPowersOf2(10);
    These calls should produce the following output:

    1 2 4 8
    1 2 4 8 16 32 64 128 256 512 1024

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Method Help

    What have you tried?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: Method Help

    I have no idea if I'm on the right track or not but this is what I have.

    private*static*void*printPowersOf2(int*i)*{
    for*(int*x*=*0;*x*>=*i;*x++)*{

    I don't know how you put something to the power of and am I doing it right?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method Help

    Do you know what a power is? Let's say you wanted to take x to the third power. How would you do that
    on paper if you didn't use exponents? You can also check out the Math class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: Method Help

    Quote Originally Posted by jim829 View Post
    Do you know what a power is? Let's say you wanted to take x to the third power. How would you do that
    on paper if you didn't use exponents? You can also check out the Math class.

    Regards,
    Jim
    x to the third power would be x * x * x but what if you wanted to find 2 to the x power?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method Help

    At long as x is an integral value, you do it the same way. how would you find out what 2 to the third power is?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Samplify is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: Method Help

    Quote Originally Posted by jim829 View Post
    At long as x is an integral value, you do it the same way. how would you find out what 2 to the third power is?

    Regards,
    Jim
    2*2*2 right?
    I was pondering about it and is there something wrong with this? I tried running it but it wouldn't work

    private static void printPowersOf2(int n) {
    for (int i = 1; i >= n; i++) {
    System.out.print(Math.pow(2, i));
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method Help

    Look at your for loop carefully.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Method Help

    it wouldn't work
    Please explain what "wouldn't work" means. If there are errors, copy the full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2013, 08:08 PM
  2. Replies: 6
    Last Post: 06-21-2013, 04:42 PM
  3. Replies: 7
    Last Post: 04-11-2013, 06:31 AM
  4. Replies: 2
    Last Post: 03-23-2012, 05:53 AM
  5. Replies: 1
    Last Post: 10-17-2011, 02:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •