Skip to content

Commit f8db004

Browse files
Create 25.2 Method Overloding.java
1 parent edee100 commit f8db004

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

25.2 Method Overloding.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Create a java class containing two methods. The first add method receives two integer arguments and second add method receives two String arguments. Both methods are named as add() and perform the addition of their parameters or concatenate strings when called. The user should press 1 or 2 to call respective methods and input of 2 integers or 2 strinngs is taken according to the methods called.
3+
4+
Input Format
5+
6+
1 or 2 to be taken in first line of input
7+
second line accepts 2 int type inputs, when 1 is pressed or take 2 String type inputs when 1 is pressed.
8+
9+
Constraints
10+
11+
user never presses a number apart from 1 or 2 in first line.
12+
13+
Output Format
14+
15+
if a number entered in first line is not 1 or 2, display ERROR and exit. otherwise add 2 int numbers if 1 is pressed, or concatenate 2 strings if 2 is chosen
16+
17+
Sample Input 0
18+
19+
1
20+
12 23
21+
Sample Output 0
22+
23+
35
24+
*/
25+
26+
import java.io.*;
27+
import java.util.*;
28+
29+
class Java{
30+
int add(int a,int b)
31+
{
32+
return a+b;
33+
}
34+
String add(String s1,String s2)
35+
{
36+
return s1+s2;
37+
}
38+
}
39+
40+
public class Solution {
41+
42+
public static void main(String[] args) {
43+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
44+
Scanner sc = new Scanner(System.in);
45+
int c = sc.nextInt();
46+
Java prokrishpatel = new Java();
47+
if(c==1)
48+
{
49+
int a1 = sc.nextInt();
50+
int a2 = sc.nextInt();
51+
System.out.print(prokrishpatel.add(a1,a2));
52+
}
53+
else if(c==2)
54+
{
55+
String s1 = sc.next();
56+
String s2 = sc.next();
57+
System.out.print(prokrishpatel.add(s1,s2));
58+
}
59+
else
60+
System.out.print("ERROR");
61+
}
62+
}

0 commit comments

Comments
 (0)