Results 1 to 9 of 9
  1. #1
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default help with basic sum calculator in java

    Ok so I am trying to make a sum calculator that calculates the following:

    ∑ 2n+1

    In this problem the user inputs the upper limit of the program and the lower limit is always 0.

    This is what I got:

    import java.io.*;
    public class sum
    {
    public static void main(String args[]) throws IOException
    {

    BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));

    int n;

    System.out.print("Input the upper limit: ");
    n = Integer.parseInt(keybd.readLine());

    int calc=2*n+1;
    for (int i=0; i<=n; i++)
    {
    calc=calc+i;
    }
    System.out.println(calc);


    }

    }


    The program works (sort of). When I do test runs it gives me wrong answers, when n >= 2. When I input 0 or 1 for upper limit it gives me correct answer, but when the upper limit is greater than or equal to 2, the answer is wrong.

    For instance when I run the program and input 2 for the upper limit, the answer it gives me is 8 ...

    but when i do the sum by hand the correct answer is 9:

    [2(0)+1] + [2(1)+1] + [2(2)+1]
    = 1 + 3 + 5
    = 9

    This problem has been bugging me for several hours. Any help appreciated thanks! :)
    Last edited by java157; 03-17-2011 at 02:03 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try calc += ((2*i)+1).

    Also, calculate by hand what got code is actually doing.

  3. #3
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Try calc += ((2*i)+1).
    Didn't work

    Quote Originally Posted by sunde887 View Post
    Also, calculate by hand what got code is actually doing.
    I tried the code by hand and it works when upper limit is 0 or 1 but not for any number greater than or equal to 2.

    When I fix it to work for n=2, it does not work for any other values of n

    for instance, instead of

    int calc=2*n+1;
    for (int i=0; i<=n; i++)
    {
    calc +=calc+i;
    }
    System.out.println(calc);

    I tried

    int calc=2*n+1;
    for (int i=0; i<=n; i++)
    {
    calc +=calc+i;
    }
    System.out.println(calc+1); // the bold is where the difference is

    AGHHHHH! This problem is giving me a migraine :confused:
    Last edited by java157; 03-17-2011 at 03:50 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Set calc to 0 then try putting
    Java Code:
    calc+=((2*I) + 1);

  5. #5
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Set calc to 0 then try putting
    Java Code:
    calc+=((2*I) + 1);
    It worked :D
    Last edited by java157; 03-17-2011 at 03:49 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    += is just programmer short hand. In the example it means
    Java Code:
    calc = calc + ((2 * i) + 1);
    There is also one for each operator
    Java Code:
    += 
    *=
    -=
    /=
    %=
    Now technically what I did borders on the side of spoon feeding, however it seemed like you were putting in the effort. I would, however; like to explain what you were actually doing.

    You would prompt the user for some number and then do
    Java Code:
    2 * n _+ 1
    So if the user enters 3 it would set calc to 7.

    In the loop you were doing
    Java Code:
    calc = calc + 1;
    
    or
    
    calc += 1;
    
    or
    
    calc++;
    Where you thought it was doing the full operation each time it was simply incrementing an already determined variable(calc).

    if the user entered 2 it would be
    Java Code:
    2 * 2 + 1  =  5
    and then it would increment it 3 times(0, 1, 2), producing 8.

    You want to set calc to 0, and then each time through the loop you want to perform the operation 2n + 1. This new way evaluates to
    Java Code:
    0 + 2 * 0 + 1 == 1
    
    1 + 2 * 1 + 1 == 4
    
    4 + 2 * 2 + 1 == 9
    Also, if your done please mark your thread solved with the thread tools.

    You can also increase people's rep if they help you by clicking the rep button. I hope I did a good job explaining this all to you.

  7. #7
    java157 is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you for being so helpful :)

  8. #8
    xp_vista007 is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Look to this :

    import javax.swing.*;

    public class mathoperation
    {
    public static void main(String[] args)
    {
    double a = new Double(JOptionPane.showInputDialog("First number :")).doubleValue();
    double b = new Double(JOptionPane.showInputDialog("Second number : ")).doubleValue();
    String s = JOptionPane.showInputDialog("Identificator (just + - * /)");
    System.out.println(s);
    if (s.equals("+")){
    double r = a + b;
    JOptionPane.showMessageDialog (null, r);
    }
    else
    if (s.equals("-")){
    double r = a - b;
    JOptionPane.showMessageDialog (null, r);
    }
    else
    if (s.equals("*")){
    double r = a * b;
    JOptionPane.showMessageDialog (null, r);
    }
    else
    if (s.equals("/")){
    double r = a / b;
    JOptionPane.showMessageDialog (null, r);
    }
    }
    }

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Basic Calculator
    By trtoy in forum New To Java
    Replies: 1
    Last Post: 12-25-2009, 01:06 AM
  2. Basic Calculator
    By Rose88 in forum Java Applets
    Replies: 3
    Last Post: 06-25-2009, 12:34 AM
  3. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  4. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 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
  •