We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.lang.reflect.Method;importjava.util.HashSet;importjava.util.Set;classAdd{publicvoidadd(int...nums){StringBuilderstringBuilder=newStringBuilder();intsum=0;for(intnum:nums){stringBuilder.append(num);if(num!=nums[nums.length-1]){stringBuilder.append("+");}sum+=num;}stringBuilder.append("=").append(sum);System.out.println(stringBuilder);}}publicclassSolution{publicstaticvoidmain(String[]args){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));intn1=Integer.parseInt(br.readLine());intn2=Integer.parseInt(br.readLine());intn3=Integer.parseInt(br.readLine());intn4=Integer.parseInt(br.readLine());intn5=Integer.parseInt(br.readLine());intn6=Integer.parseInt(br.readLine());Addob=newAdd();ob.add(n1,n2);ob.add(n1,n2,n3);ob.add(n1,n2,n3,n4,n5);ob.add(n1,n2,n3,n4,n5,n6);Method[]methods=Add.class.getDeclaredMethods();Set<String>set=newHashSet<>();booleanoverload=false;for(inti=0;i<methods.length;i++){if(set.contains(methods[i].getName())){overload=true;break;}set.add(methods[i].getName());}if(overload){thrownewException("Overloading not allowed");}}catch(Exceptione){e.printStackTrace();}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Varargs - Simple Addition
You are viewing a single comment's thread. Return to all comments →