Write a class with two methods each having the same name sayadd. One method shou
ID: 3613840 • Letter: W
Question
Write a class with two methods each having the same name sayadd. One method should accept two integers as parameters and returntheir sum. The other method should accept two strings as parametersand return the string formed by concatenation of the two strings.In main call the method Add first by passing integer parameters andthen by passing string parameters and obtain their return values inmain and print them. Show that the overloaded method calls theappropriate method according to the parameters passed to it.
Explanation / Answer
please rate - thanks import java.util.*; public class over { public static void main(String []args) {String cat; int sum; sum=add(4,5); cat=add("abc","def"); System.out.println("added= "+sum+" Concatenated= "+cat); } public static int add(int n,int m) {System.out.println("In integer- parameters are: "+n+" "+m); return n+m; } public static String add(String n,String m) {System.out.println("In String- parameters are: "+n+" "+m); return n+m; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.