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 2 of 2

Thread: Overloading Method

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Overloading Method

    Hi I am trying to call a method to compute 1 - 3 + 5 - 7 + .. .. - (n - 2) + n
    For example: n = 9 the Series would look like 1 - 3 + 5 - 7 + 9 = 5


    I am not looking to get spoon fed. Just hoping someone could help me understand the logic and let me know if I am even close.

    Here is what I have so far.


    import javax.swing.JOptionPane;


    public class OddSum {

    public static void main(String[] args) {


    int oddSeries, m, n;
    String num1Str, num2Str;

    // Get a pair of positive Integers from users
    num1Str = JOptionPane.showInputDialog("Enter a positive integer:");
    m = Integer.parseInt(num1Str);
    num2Str = JOptionPane.showInputDialog("Enter a positive integer:");
    n = Integer.parseInt(num2Str);

    // Call a method to compute 1 - 3 + 5 - 7 + .. .. - (n - 2) + n

    oddSeries = myOddNum(n);

    // output the result
    System.out.println("(OddNum) The series of 1 to " + n + " = " + oddSeries);
    }


    // Method myOddNum with on Parameter
    public static int myOddNum(int myN) {

    int localSum, oddnum;

    localSum = 0;
    oddnum = 0;
    for (int i = 1; i <= myN; i++) {
    if (i%2 != 0);{
    oddnum = myN - 2;}
    localSum = localSum + oddnum;}
    return localSum;
    }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Overloading Method

    Rephrase your question and surround your code in highlight tags.
    What problems are you getting? and what does Overloading have to do with your question?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Advantages of method Overloading and Overriding
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 04:55 AM
  2. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  3. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  4. Method Overloading - Doubt
    By vidya lakshman in forum Java Theory & Questions
    Replies: 2
    Last Post: January 31st, 2011, 09:32 AM
  5. Overloading vs Overriding
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:53 AM