Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Design a class to overload a function Joystring() as follows: [15] (i) void Joystring(String s, char ch1, char ch2) with one string and two character arguments that replaces the character argument ch1 with
the character argument ch2 in the given string s and prints the new string
Example:
Input value of s = “TECHNALAGY”
ch1 = ‘A’
ch2 = ‘O’
Output : “TECHNOLOGY” (ii) void Joystring(String s) with one string argument that prints the position of the first space and the last space of the given String s.
Example:
Input value of = “Cloud computing means Internet based computing”
First Index : 5
Last Index : 36 (iii) void Joystring(String s1, String s2) with two string arguments that combines the two strings with a space between them and prints the
resultant string
Example :
Input value of s1 = “COMMON WEALTH”
s2 = “GAMES”
Output : “COMMON WEALTH GAMES”
(use library functions)
Answer:
import java.util.Scanner;
public class StringOperations {
public void Joystring(String s, char ch1, char ch2) {
String output = s.replace(ch1, ch2);
System.out.println("Output = " + output);
}
public void Joystring(String s) {
int firstIndexOfSpace = s.indexOf(' ');
int lastIndexOfSpace = s.lastIndexOf(' ');
System.out.println("First index of space = " + firstIndexOfSpace);
System.out.println("Last index of space = " + lastIndexOfSpace);
}
public void Joystring(String s1, String s2) {
String output = s1.concat(" ").concat(s2);
System.out.println("Output = " + output);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringOperations stringOperations = new StringOperations();
// Joystring method 1
System.out.print("Enter string: ");
String s1 = scanner.nextLine();
System.out.print("Enter ch1: ");
char ch1 = scanner.nextLine().charAt(0);
System.out.print("Enter ch2: ");
char ch2 = (char) scanner.nextLine().charAt(0);
stringOperations.Joystring(s1, ch1, ch2);
// Joystring method 2
System.out.print("Enter string: ");
String s2 = scanner.nextLine();
stringOperations.Joystring(s2);
// Joystring method 3
System.out.print("Enter s1: ");
String s3 = scanner.nextLine();
System.out.print("Enter s2: ");
String s4 = scanner.nextLine();
stringOperations.Joystring(s3, s4);
}
}
C. Sir| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2015