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

Thread: I have no idea how this works.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have no idea how this works.

    1st Post, I'm taking a high school AP computer science class based around java, and my teacher told me to do the commented out statement.

    I've never seen "for" before or "+=" and i don't know what addSum(int n) means.
    Really i have no idea how this works and my class is too big for my teacher help me, since i suck less at java than other students.
    Can anyone explain this to me? I just guessed it was an algorithm, really i have no idea.


    //calcualate and return the sum of all odd integers from 1 to n
    public int addSum(int n)
    {
    int sum = 0;
    for(for i = 1;i<=n;i+=2)
    sum += i;
    return sum;


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have no idea how this works.

    Thanks a lot i'll read those and look at tutorials before posting

  4. #4
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: I have no idea how this works.

    Let me try to describe to you what the method means:
    methods are designed to do things - you probably know that. Some methods take arguments or commonly known as parameters. Your method above takes in a primitive of type int (n). You might ask yourself where you will find the int variable to be passed into the method when called: It is easy, after you instantiate your class(the same class your method was created for), you will be able to call the addSum(int n) method by for instance saying myObject.addSum(4);
    After you pass the argument to the method, the method will loop through (the number of times depend on the size of your int value passed). After that, each time a value of i is produced by the loop (for loop), it gets added to the sum - which was initialized to zero at first(during declaration). After int i is no longer less than n, the loop ends and the sum is shown.
    There are other ways of calculating variables in java. Eg: "X *= Y" is same as X * Y then assign value to X
    X/=Y is like division (x/y) then assign to X. X%=Y means getting the modulus (remainder) then assign to x. All of the work the same way. Just check out some tutorials and you will be fine. javabeginner.com is cool too.
    I hope this helps.

Similar Threads

  1. It works!!! eureka!!! Java...Day 12.
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 18th, 2011, 09:41 PM
  2. GUI Only Works Sometimes
    By bgd223 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 12th, 2011, 08:08 AM
  3. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 09:19 AM
  4. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  5. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM