• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does one write a method that takes in an integer and adds up the int between 1 and that number?

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to write a method called collectiveSum which takes in an integer (assume it will be positive) and adds up the integers between 1 and that number. I need to do this with both a while loop and with a for loop.


please explain
Thank you
 
Saloon Keeper
Posts: 15529
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Justin. This is called a Triangle Number. If this is an assignment, show us what you've tried so far, and what you're stuck on.

If this isn't an assignment, note that you can calculate triangle numbers in constant time without using a loop.
 
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Robbins wrote:I need to write a method called collectiveSum which takes in an integer (assume it will be positive) and adds up the integers between 1 and that number. I need to do this with both a while loop and with a for loop.


please explain
Thank you


This sounds like you need us to do your homework. The solution to your problem is very easy but you should let us know what you have done so far and what the problems are. After all you should understand the solution and not just copy & paste it. Just my opinion.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Break the problem down into tiny pieces. Code each tiny piece at a time. If I were doing this, the first thing I'd do is write a method named collectiveSum that doesn't take ANY argument, but simply prints "i'm in collectiveSum ". Then i'd make sure i could compile it, call it, and run it before doing ANYTHING else.

Once that works, change it so you can pass it an integer, and print it out. So it would then print "collectiveSum got the number <number>". I want to PROVE to myself the number I pass in is what it can print.

then I might try writing a loop that simply PRINTS the numbers from 1 to <number>. After all, there's no point in trying to add them up if I cant figure out what they are first...

etc.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hi Justin. This is called a Triangle Number. If this is an assignment, show us what you've tried so far, and what you're stuck on.

If this isn't an assignment, note that you can calculate triangle numbers in constant time without using a loop.




fred rosenberger wrote: Break the problem down into tiny pieces. Code each tiny piece at a time. If I were doing this, the first thing I'd do is write a method named collectiveSum that doesn't take ANY argument, but simply prints "i'm in collectiveSum ". Then i'd make sure i could compile it, call it, and run it before doing ANYTHING else.

Once that works, change it so you can pass it an integer, and print it out. So it would then print "collectiveSum got the number <number>". I want to PROVE to myself the number I pass in is what it can print.

then I might try writing a loop that simply PRINTS the numbers from 1 to <number>. After all, there's no point in trying to add them up if I cant figure out what they are first...

etc.



Thank you two very much!!!

I have a lot of gaps in my knowledge here please help me understand.
My problem is I don't know what it means to "write a method" I barely understand what a method even is or what it does. I keep hearing, "think verb/action/doing something" that it's the behaviors of an object. But I don't understand what I should write or the syntax et certa.


This is my attempt here. Here is my thinking process within the comments....



Here I import scanner and I try to set up the code according to the directions, if what I said above was even true though. Also not entirely sure I understand for loops. Nonetheless here is the code:



Not sure I entirely understand what the for loop does. I think of it (in order) like: starting value, ending value w/condition, then update/iteration.

So for this i = 1 (starting pt) and let's say for exmple we choose '5' as our x. Not entirely sure, but in my head it goes like this:

1<=5 (true)
then
z = 0 + 1; so z = 1
then ++i increments 'i' from '1' to '2'

2<=5 (true)
then
z = 1 + 2 = 3; so z = 3

3<=5 (true)
then
z = 3 + 3 = 6; so z = 6

4<=5 (true)
then
z = 6 + 4 = 10; so z = 10

5<=5 (true)
then
z = 10 + 5; so z = 15

And then the boolean is false, the program exits out of the for loop and does the last statement which prints the value of 'z'

Not entirely sure about doing a while loop version of this.
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Justin, have you gone through the Java tutorials yet? It's very important to understand exactly what a method is, because without them you wouldn't be able to do anything.

In your collectiveSum(int a) method, you don't have to use a Scanner, because you already have the input, namely the parameter a. You can use a Scanner in the main() method to get the input from the user, and then you pass the value when you call collectiveSum().

I will give you something small to start with, so you can focus on the implementation, and get a feeling for how calling methods works. However, you should really first go through this tutorial trail: https://docs.oracle.com/javase/tutorial/java/index.html
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Justin, have you gone through the Java tutorials yet? It's very important to understand exactly what a method is, because without them you wouldn't be able to do anything.

In your collectiveSum(int a) method, you don't have to use a Scanner, because you already have the input, namely the parameter a. You can use a Scanner in the main() method to get the input from the user, and then you pass the value when you call collectiveSum().

I will give you something small to start with, so you can focus on the implementation, and get a feeling for how calling methods works. However, you should really first go through this tutorial trail: https://docs.oracle.com/javase/tutorial/java/index.html




Thank you!!! I am slowly getting it, I think. I placed what I think is the correct answer in your code. Made comments for the parts that I didn't quite understand.
I am kind of confused by the fact that you can use a method and call it up there in the code even though the method is at the bottom. So java doesn't really care where the method is located? so long as it exists somewhere in the code?





Thanks again

EDIT: Thinking it over. Maybe I do understand some of int sum = collectiveSum(n); means.
There we are basically saying that the int var sum is equivalent to the method collectiveSum(n), at that number n. But I don't really understand the n part. Why is it needed? Wouldn't the method carry out and there would already be the letter n identified by what the user typed in for n with the int n = input.nextInt(). It almost seems redundant to have collectiveSum(n), whereas just having collectiveSum() makes sense to me. Its like saying sum is the collectiveSum() method. Idk why but the n in the parens is really throwing me off.

 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Robbins wrote:I am kind of confused by the fact that you can use a method and call it up there in the code even though the method is at the bottom. So java doesn't really care where the method is located? so long as it exists somewhere in the code?


Yes. Everything inside methods, constructors and initializers is procedural. That means it's performed step by step, from top to bottom. Most of the things outside methods, constructors and initializers is declarative, meaning it just exists.

//why is it final? what does final do to the code? I am use to seeing it as public there


You're used to it, because people have a nasty tendency to write public everywhere. Do you even know why you're writing public everywhere? It should be used as sparsely as possible, until you understand why you're using it. The opposite is true for final. Classes and fields should be declared final wherever possible, unless you have a good reason not to. It's best to get into this habit early. You'll learn more about these concepts when you get into inheritance and visibility.

//understood but shouldn't it be "int" instead of "void"? because we have to return a int value?


No. We're not returning the value, we're just outputting it to the screen.

//not sure what's happening here. We have an int var named "sum" I don't understand what collectiveSum(n) means and why the n in the parens?


We're calling collectiveSum. Think of a method as a factory. You put something in, and you get something out. Here, we put the value of n inside, and we assign the output of the factory to the variable sum.

//understood, except for what n and sum become


Here, we're performing string concatenation. When you have a string like "The sum of 1 through ", and you use the + operator, the other operand (in this case n) gets evaluated and then converted to a string, and added to the other string. So if the user entered 4, the first + operation would evaluate to "The sum of 1 through 4". It's then catenated again with " is: ", and the result of that is catenated with the value of sum: "The sum of 1 through 4 is: 10".

//so this is the real method? and that's why it's int and named collectiveSum with int n as it's only argument availability to be passed in, so it only accepts integers


Yes, we've declared the collectiveSum method, and told the compiler that as input it expects an int, and as output it returns another int. The block between the curly braces contains its definition: What actually happens when you call it.
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Robbins wrote:Thinking it over. Maybe I do understand some of int sum = collectiveSum(n); means.
There we are basically saying that the int var sum is equivalent to the method collectiveSum(n), at that number n. But I don't really understand the n part. Why is it needed? Wouldn't the method carry out and there would already be the letter n identified by what the user typed in for n with the int n = input.nextInt(). It almost seems redundant to have collectiveSum(n), whereas just having collectiveSum() makes sense to me.



Several things are happening here. First off, we're declaring sum by writing int before it. This tells the compiler that we want to start using a new variable within the method. collectiveSum(n) then calculates the nth triangle number. n is not redundant, because if you had another integer m in your main method, then should sum be the nth or the mth triangle number? Arguments that you pass to a method do not have to be the same name as the parameters declared by the method itself. The name of the parameter declared by collectiveSum can be anything, and is only used inside its own definition. You don't even need to use a variable when you call it. You can just as easily use a literal. Try this program:

In our previous code, we told collectiveSum to calculate the sum using whatever the value of n was at the time of calling the method.

Its like saying sum is the collectiveSum() method. Idk why but the n in the parens is really throwing me off.


No. sum is a variable containing the int value of whatever collectiveSum(n) returned. The = operator assigns a value to a variable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic