ICSE Java Programming Equal and Uppercase strings 2022 Q4 SPECIMEN PAPER Solved
Question 5
Define a class to accept two strings, convert them into uppercase, check and display whether two strings are equal or not, if the two strings are not equal, print the string with the highest length or print the message both the strings are of equal length.
import java.util.Scanner;
public class ICSEJava {
public static void main(String[] args) {
StringCheck obj = new StringCheck();
obj.inputString();
obj.display();
}
}
class StringCheck {
public void display() {
if (this.isequal)
System.out.println("Both the strings are equal");
else
System.out
.println(" String with highest length :"
+ (this.message_first.length() < this.message_second.length()
? this.message_second
: this.message_first));
}
public void inputString() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the first string:");
this.message_first = in.nextLine();
System.out.println("Enter the second string:");
this.message_second = in.nextLine();
System.out.println("OUTPUT:");
System.out.println(this.toUpperCase(this.message_first));
System.out.println(this.toUpperCase(this.message_second));
if (!this.message_first.equals(this.message_second))
this.isequal = false;
in.close();
}
private String toUpperCase(String msg) {
String temp = "";
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z') {
temp += (char) (msg.charAt(i) - 32);
} else
temp += (msg.charAt(i));
}
return temp;
}
StringCheck() {
this.message_first = "";
this.message_second = "";
this.isequal = true;
}
private boolean isequal;
private String message_first;
private String message_second;
}
No comments:
Post a Comment