r/javahelp • u/symoreo • Jul 07 '20
Define a function which takes any number of arguments
Hi all,
Define a function which can take any number of arguments and returns the sum of all the elements (only int and float type).
The arguments can include integers, string, char, floats, boolean (all the primitive data types) and arrays.
if you are passing an array, again it might have all the primitive data types.
its like:
float result = functionName(1, 0.5, "Hello", 'S', true, someArray, 100.5, 46, 24.332, someArray2, "Yes")
// the number of arguments can be any. The function should be able to take it.
This was asked to me in an interview. The interviewer told that your function should be able to return the sum of only integer and floats (not even byte or double. Only int and float)
I wrote the program although it was quite huge (not so much though). I wrote it on a piece of paper. When I came home, I tried to replicate it and ran on my PC. It didnt :/. I had used a try catch concept for this.
Can you guys help me out with an efficient program for this problem? Thanks!
9
Jul 07 '20
Haven't run it, but this is a good use of streams:
private static float foo(Object ... vargs) {
return (float)Stream.of(vargs)
.filter( a -> a instanceof Float || a instanceof Integer )
.mapToDouble( a -> (((Number) a).doubleValue())).sum();
}2
u/symoreo Jul 07 '20
This works! I wasn't aware of Stream library. Guess I will need to look into it. Thanks!
1
u/xyphanite Quantum Brewer Jul 07 '20
Very nice use of streams. That said, streams are really scary for beginners.
2
Jul 07 '20
Maybe. But in some cases they can be easier to understand their meaning. I think you could teach streams quite early.
1
u/xyphanite Quantum Brewer Jul 07 '20
I honestly think they could as well. Coming from a pure OOP background, it's harder for me to become effective with streams than if I had initially been taught functional programming. Knowing how universities generally teach older material, I unfortunately doubt this will occur, unless students are taught Python in addition to Java / C++.
2
u/xyphanite Quantum Brewer Jul 07 '20
Can you post the code of the program you wrote to see where you're at? If it's a huge amount of code, please follow the guidelines in the sidebar under Code Posting.
4
u/symoreo Jul 07 '20
Hi, this was my code. But it is wrong.
import java.util.ArrayList;
public class MyC {
public static int add(ArrayList ar) { float total = 0; for (int i=0; i < ar.size(); i++) { try { total += float(ar.get(i)) } catch (Exception e) { e.printStackTrace(); } } return total; } public static void main(String[] args) { ArrayList A1 = new ArrayList(); A1.add(100.5); A1.add("Hello"); A1.add('C'); A1.add(90); A1.add(true); A1.add(106.79); System.out.println(add(A1)); }}
1
u/tsvk Jul 07 '20
You are confusing the concept of
arrays with thejava.util.ArrayListclass, that is anarray-based implementation of theListinterface.
1
1
u/desirecampbell Jul 07 '20
The best part of this question will be when you realize you've always been doing this.
0
u/tcbenkhard Jul 07 '20
There is no way you can write this in 1 function? I assume you misunderstood the question?
2
u/tsvk Jul 07 '20
It's quite doable, I tried it and came up with a 15-line method.
1
u/tcbenkhard Jul 07 '20
With primitives? I would like to see that signature?
2
u/mjg123 Jul 07 '20
float functionName(Object... args) does the trick, the ints and floats will be boxed.
1
1
u/MRH2 Intermediate Brewer Jul 07 '20
which version of Java did this appear in?
1
0
Jul 07 '20
[deleted]
2
u/tsvk Jul 07 '20
Assumably you won't have access to 3rd-party libraries in a programming assignment.
1
13
u/bean_factory Jul 07 '20 edited Jul 07 '20
If I understand the problem correctly then something like this should work:
Note that this implementation adds top-level boxed Integers and Floats to the sum, but does not do the same for elements of top-level Integers[] and Floats[]. You could add additional checks to the if statement if this is needed.
edit: The question is a little confusing because Strings are not a primitive type and Java has autoboxing and unboxing for other primitive types