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.*;importjava.lang.reflect.*;importjava.util.*;// Write your code hereclassAdd{publicStringadd(int...n){intsum_=0;Stringdisp="";for(inti:n){sum_+=i;disp+=(i+"+");}returndisp.substring(0,disp.length()-1)+"="+sum_;}}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());// Create new Add objectAddob=newAdd();// Display resultsSystem.out.println(ob.add(n1,n2));System.out.println(ob.add(n1,n2,n3));System.out.println(ob.add(n1,n2,n3,n4,n5));System.out.println(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());}// Check for Method Overloadingif(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 →
Java 15
: