Design a class to overload a function Joystring() as follows:
1) void Joystring (String s, char ch1, char ch2) with one string argument and two argument ch2 in the given string s and prints the new string.
2) void Joystring(String s) with one string argument that prints the position of the first space and the last space of the given string.
3) void Joystring (String s1, String s2) with two string arguments that contain the two strings with a space between them and prints the resultant string.
Answers
Java program to construct a class that overloads a function named Joystring().
Language used : Java Programming
Program :
public class Something
{
static void Joystring (String s, char ch1, char ch2)
{
System.out.println("New String is " +s+ch1+ch2);
}
static void Joystring(String s)
{
int i1=s.indexOf(" ");
int i2=s.lastIndexOf(" ");
System.out.println("Index position of first space : "+i1);
System.out.println("Index position of last space :"+i2);
}
static void Joystring (String s1, String s2)
{
System.out.println("New String is " +s1+" "+s2);
}
public static void main(String []args)
{
Joystring("Hi",'u','v');
Joystring("Are you there, buddy?");
Joystring("Good","Morning!");
}
}
Output :
New String is Hiuv
Index position of first space : 3
Index position of last space :14
New String is Good Morning!
Explanation :
- void Joystring (String s, char ch1, char ch2) function takes a string and two characters and concatenates them into one and prints that string.
- void Joystring(String s) takes a string and returns the indices of first space and the last space of that string.
- void Joystring (String s1, String s2) takes two strings and concatenates them into one with a space in between.
- Overloading occurs when two or more methods of a class shares the same name and different no.of parameters; atleast their types.
- So, based on the parameters given during the method call, the corresponding method gets called and the computation gets performed.
Learn more :
- Differences between method overloading and overriding.
https://brainly.in/question/1087167
https://brainly.in/question/314609
Hope it helps you... Thank you!