<\/span><\/h2>\n\n\n\nYou are given a class Solution and its main method in the editor.
Your task is to create the class Add and the required methods so that the code prints the sum of the numbers passed to the function add.<\/p>\n\n\n\n
Note:<\/strong> Your add method in the Add class must print the sum as given in the Sample Output<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThere are six lines of input, each containing an integer.<\/p>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nThere will be only four lines of output. Each line contains the sum of the integers passed as the parameters to add in the main method.<\/p>\n\n\n\n
<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 1\n 2\n 3\n 4\n 5\n 6<\/code><\/pre>\n\n\n\n<\/span>Sample Output<\/strong><\/span><\/h4>\n\n\n\n 1+2=3\n 1+2+3=6\n 1+2+3+4+5=15\n 1+2+3+4+5+6=21<\/code><\/pre>\n\n\n\n<\/span>Solution – Java Varargs – Simple Addition<\/strong><\/span><\/h2>\n\n\n\nimport java.io.*;\nimport java.lang.reflect.*;\nimport java.util.*;\nimport java.text.*;\nimport java.math.*;\nimport java.util.regex.*;\n\n\nclass Add {\n public void add(int... intArgs) {\n int sum = 0;\n String separator = \"\";\n for (int i : intArgs) {\n sum += i;\n System.out.print(separator + i);\n separator = \"+\"; \n }\n System.out.println(\"=\" + sum);\n }\n}\n\n\npublic class Solution {\n\n public static void main(String[] args) {\n try{\n\t\t\tBufferedReader br=new BufferedReader(new InputStreamReader(System.in));\n\t\t\tint n1=Integer.parseInt(br.readLine());\n\t\t\tint n2=Integer.parseInt(br.readLine());\n\t\t\tint n3=Integer.parseInt(br.readLine());\n\t\t\tint n4=Integer.parseInt(br.readLine());\n\t\t\tint n5=Integer.parseInt(br.readLine());\n\t\t\tint n6=Integer.parseInt(br.readLine());\n\t\t\tAdd ob=new Add();\n\t\t\tob.add(n1,n2);\n\t\t\tob.add(n1,n2,n3);\n\t\t\tob.add(n1,n2,n3,n4,n5);\t\n\t\t\tob.add(n1,n2,n3,n4,n5,n6);\n\t\t\tMethod[] methods=Add.class.getDeclaredMethods();\n\t\t\tSet<String> set=new HashSet<>();\n\t\t\tboolean overload=false;\n\t\t\tfor(int i=0;i<methods.length;i++)\n\t\t\t{\n\t\t\t\tif(set.contains(methods[i].getName()))\n\t\t\t\t{\n\t\t\t\t\toverload=true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tset.add(methods[i].getName());\n\t\t\t\t\n\t\t\t}\n\t\t\tif(overload)\n\t\t\t{\n\t\t\t\tthrow new Exception(\"Overloading not allowed\");\n\t\t\t}\n\t\t\t}\n\t\t\tcatch(Exception e)\n\t\t\t{\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t\t\n\t\n\t\n\n}<\/pre>\n\n\n\n