• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dviding Numbers in an array  RSS feed

 
Johny Jackson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see what you coded for the division method. It will be easier to give you hints.

 
Johny Jackson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that insightful reply Henry I have just began reading Inheritance and Polymorphism in Head First. Method overloading sounds a bit to complicated for now :P
I created a Print Class so i wouldn't have to type out System.out.print each time. I used Object for that, so the user can input String, double, int, etc.... But I am not sure how flexible and secure that is...Although i did convert the Object to String in my method, not sure if that would help with anything though.
Thanks for the great info.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you print an Object with System.out.println(), that Object is converted to a String anyway. More details here. Notice that if you follow enough links, you end up with Object#toString().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!