This week's book giveaway is in the Android forum.We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Knute Snortum
• Junilu Lacar
• Devaka Cooray
Saloon Keepers:
• Ganesh Patekar
• Tim Moores
• Carey Brown
• Stephan van Hulst
• salvin francis
Bartenders:
• Ron McLeod
• Frits Walraven
• Pete Letkeman

# Dviding Numbers in an array

Greenhorn
Posts: 12
Hello

I wanted to create a Calc class so that I could use simple static methods like Calc.sum( an array with numbers inside) to,well,carry out simple calculations. Simple enough until i got to the the division method.
for the sum method i wrote this:

Sure enough this method doesn't work with division. Can you please give me hints on how to solve this? I kind of want to solve this by myself and I am not asking for code or anything since i mainly want to do this for the sake of learning. I tried using nested for loops, but i just got confused and made whatever i already had more complex.

Thanks

author
Sheriff
Posts: 23601
138

In terms of doing an operation on a series, only adding (arithmetic) and multiplying (geometric) really makes sense. Subtracting and dividing are generally done with two numbers.

Anyway, what are you having a problem with? Meaning why did you think it would need nested loops?

Henry

Ranch Hand
Posts: 141
Let's see what you coded for the division method. It will be easier to give you hints.

Johny Jackson
Greenhorn
Posts: 12
I am not sure pasting the code would help since it's really idiotic..hehe...

Well that code works for the first 2 numbers as you can see...
I thought i needed to use a nested loop since I didn't know how to use something similar to the Sum method. This is what i had before

Well as you can see for yourself, that fails also

Henry Wong
author
Sheriff
Posts: 23601
138

Well that code works for the first 2 numbers as you can see...

It may work for two numbers, but it won't work for anything else.

I thought i needed to use a nested loop since I didn't know how to use something similar to the Sum method. This is what i had before

How did you solve the subtraction issue? The solution should be somewhat the same.

With addition and multiplication, it's easy -- start with zero and one respectively. With Subtraction and division, you need to start from something else -- what do you think it is?

Henry

Johny Jackson
Greenhorn
Posts: 12
Actually I haven't done the subtraction method yet.

Well addition I used double sum = 0 and multiplication I used double total = 1, and for division i used 1 too, but that doesn't work obviously. I don't know what I should be using...

Johny Jackson
Greenhorn
Posts: 12
Aha! i think i figured it out thanks to your "what the initial value should be" tip after giving it some more thought.

Is that the best way to do it though???

Henry Wong
author
Sheriff
Posts: 23601
138

Aha! i think i figured it out thanks to your "what the initial value should be" tip after giving it some more thought.

Aren't aha moments great ?!? ...

Henry

Johny Jackson
Greenhorn
Posts: 12
Yes, those moments are the greatest but sometimes you go back to them, even on the same day, and try to figure out how you came up wiht the solution, hehe...

BTW, is there another way to pass input to the method without declaring its type? Sort of like the System.out.print method of java. It doesn't care about the type and takes anything the user passes!

Henry Wong
author
Sheriff
Posts: 23601
138

BTW, is there another way to pass input to the method without declaring its type?

No. Java is strongly typed. You need to specify the type of parameters.

Sort of like the System.out.print method of java. It doesn't care about the type and takes anything the user passes!

System.out.print does it via method overloading. They have a method for each primitive type, so that is how it can take any type.

For objects, it gets a bit easier, as you can specify the super class (even the top super class -- Object) as the parameters.

As for printf, that uses a bunch of techniques. It uses a var-arg of objects, that allows the method to have variable parameters. And depends on autoboxing for the primitives.

Henry

Johny Jackson
Greenhorn
Posts: 12