Help With parameter passing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Help With parameter passing

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Question Help With parameter passing

    I have this program, I get the right answer but it does not do the right stuff, I'm supposed to take the SumOfN takes an int n as a parameter and returns the sum ofN, Method does not do this,

    Your program should loop, ask for n, if N > 0, Calculate and Print sumofN
    if N <= 0, stop
    I've been working on this for 3 meesly hours and can't figure it out, pleaseeee help, So frustrated.

    import java.util.*;
    public class sumOfN {
    //Method
    public static int sumOfN(int n) {
    return n;
    }
    // main(): application entry point
    public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    System.out.println("Please enter an integer: ");
    int n = stdin.nextInt();
    //Return for zero and negative numbers
    if (n <= 0) {
    return;
    }
    //Calculate integer Value
    for (count = n; count > 0; count--) {
    sum += count;
    }
    //Print user input in an output form
    System.out.println("SumOfN(" + n + ") has an integer value of " + sum);
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    I think you're on the right track since your code in the main method will do what you want it to do.

    However your instructions are contradictory:

    "SumOfN takes an int n as a parameter and returns the sum ofN"

    "Your program should loop, ask for n, if N > 0, Calculate and Print sumofN"

    Usually a method will either print something or return it. These instructions are unclear which they really want (or if they want both).

    You could do both I suppose. Just stuff your main code in the method you have and that would work. Just don't forget to return the value after you print it.

    As it stands you print out the answer, but the sumOfN method just returns the same number it is given. So it kind of halfway works.

  3. #3
    Join Date
    Sep 2004
    Posts
    103
    The thing is I don't know how to write the code for the method to take the user input, calculate and see if it is 0 and then stop the program or calculate if the answer is 5, get the sum 5 + 4 + 3 + 2 + 1 + 0, could you maybe show me some code that would do this? thanks a bunch.

  4. #4
    Join Date
    Oct 2004
    Posts
    311
    First of, there is a very quick and very clean way to calculate the SumOfN.

    in your example you used 5, which results iun the SumOfN being 1+2+3+4+5=15. This is the hard way of doing things. the fast way (which only works on even numbers, but I'll give a fix for that later) would be:

    n=4; // sumOfN: 1+2+3+4=10
    sumOfN=(n/2)*(n+1);

    This works because:
    1+4 = 5 (n+1)
    2+3 = 5 (n+1)
    This combination can be made n/2 times.

    now, to make this work with odd numbers as well is very straightforward:

    n=5; // SumOfN: 1+2+3+4+5=15
    sumOfN=(((n-1)/2)*n)+n;

    this works because:
    1+4 = 5 (n)
    2+3 = 5 (n)
    this combination can be made (n-1)/2 times (twice in this case).

    You can check if an int is odd by doing:
    if ((i%2) == 1)
    {
    }

    the % gives you the remainder after dividing by 2, which in case of an odd number would be 1.

    Now in you case, this calculation would take place in your sumOfN method, which would take theint as parameter and return the calculated value.

  5. #5
    Join Date
    Sep 2004
    Posts
    103

    But How?

    But still how do I put it into method SumOfN and then Calculate it in SumOfN and then Pull it out of SUmOfN to be printed? that's what I can't figure out, is how to call the method sumOfN, put all the calculations in sumofN to print it out?

  6. #6
    Join Date
    Oct 2004
    Posts
    311
    private static int sumofn(int n)
    {
    int sum = 0;
    // do the calculating here;
    return sum;
    }

    System.out.println(sumofn(5));

  7. #7
    Join Date
    Sep 2004
    Posts
    103

    errors

    I still keep getting errors that the main cannot read sumofn, and it's not reading the user input, can anyone find my flaws

    import java.util.*;

    public class sumOfN {
    //Method

    private static int sumofn(int n)
    {
    int count = 0;
    int sum = 0;
    for (count = n; count > 0; count--) {
    sum += count;
    if (n <= 0) {
    System.out.println("Cannot equal 0");
    return sum;
    }
    }
    return sum;

    }

    public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    System.out.println("Please enter an integer: ");
    sumofn = stdin.nextInt();
    System.out.println("SumOfN" + sumofn(n) + " has an integer value of ");

    }

  8. #8
    Join Date
    Oct 2004
    Posts
    311
    I've never done an input this way before myself, so I can't help you there.

    The problem why the main can't read the sumOfN method is because it is private and you have put it in a different class then your main method. if you make it public, that should solve the problem.
    (This is my fault I see, cause I made it private in my example).
    making a static method private makes no sence though, since static is always called from outside the class itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center