couple questions i have...
i'm learning java now and got some questions maybe the ppl who are familiar with the java more than i do can answer..
1. the way how java passes parameters to a method.. is this technique consistent between primitive types and objects?
2. how could i write a method called average (lets say) that accepts 2 integer parameters and returns their avg as a float value.
3. and then overloading the #2 to make it 3 integers..
4. write a method called multiconcat that takes a string and an integer as parameters. return a string that consists of the string parameter concatenated with itself count times where count is the integer parameter. for example if the parameter values are hi and 4, the return values is hihihihi. return the original string if the integer parameter is less than 2.
some help would be appreciated.
Message was edited by:
besoulboy
[901 byte] By [
besoulboya] at [2007-11-27 8:57:18]

> i'm learning java now and got some questions maybe
> the ppl who are familiar with the java more than i do
> can answer..
>
> 1. the way how java passes parameters to a method..
> is this technique consistent between primitive types
> and objects?
Consistent in what way?
> 2. how could i write a method called average (lets
> say) that accepts 2 integer parameters and returns
> their avg as a float value.
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
> 3. and then overloading the #2 to make it 3
> integers..
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
> 4. write a method called multiconcat that takes a
> string and an integer as parameters. return a string
> that consists of the string parameter concatenated
> with itself count times where count is the integer
> parameter. for example if the parameter values are hi
> and 4, the return values is hihihihi. return the
> original string if the integer parameter is less than
> 2.
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
> some help would be appreciated.
>
> Message was edited by:
> besoulboy
You're welcome.
Are these homework questions? Because I would say the best solution to 2 and 3 would be have a single method that accepts an int array as a parameter. That way it doesn't matter how many numbers you want to average it will do it.
As for 1. Java use pass-by-value. Look it up, read and understand. Some people mistakenly believe Java uses pass-by-reference for objects but if you do a search you find many explanations why this is wrong.
4. is simple if you understand parameters, loops and String concatenation. If you don't understand then read your book and/or course material.
"single method that accepts an int array as a parameter"how would i write this?
> "single method that accepts an int array as a> parameter"> > how would i write this? http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html
"Consistent in what way?"meaning that does it work the same way between how primitive type and objects gets passed on
> "Consistent in what way?"> > meaning that does it work the same way between how> primitive type and objects gets passed onAh, OK. That has already (implicitly) been answered in reply #2.
public int average (int ... list){int result = 0.0;if (list.length != 0){int sum = 0;for (int num : list)sum += sum;result = (int)sum / list.length;}return result;}does this look ok?
> ...> does this look ok?You shouldn't be asking questions like this. Ask you compiler. When you and your compiler are having an argument which you cannot resolve, post back here.
> "Consistent in what way?"
>
> meaning that does it work the same way between how
> primitive type and objects gets passed on
Ah, OK. That has already (implicitly) been answered in reply #2.
so it's same correct?
i'm not understanding the answer from reply#2 clearly.
> ...> i'm not understanding the answer from reply#2 clearly.This was the answer: "Java use pass-by-value. Look it up, read and understand.". So, what references did you read on the subject, and and what was not clear to you?