In: Computer Science
Test Your Understanding
Create a JAVA package by name ClassEx2 and in the main() method
of
the class, assign your name to a String object and your father name
to a
different String object and do the following;
Concatenate your name and your father name.
Get the length of your full name.
Find the index of any character in your full name.
Replace the occurrence of any lower case character in your
full
name to capital character.
Compare your first name with your father name , display
message
indicating which name is first lexicographically.
Complete code in java:-
class ClassEx2 {
public static void main(String ... args) {
// Creating string object of my
name.
String name = new
String("Alice");
System.out.println("My name:
"+name);
// Creating string object of my
father's name.
String fatherName = new
String("John");
System.out.println("My father's
name: "+fatherName);
// Concatenating both
names.
String fullName =
name+fatherName;
System.out.println("Full name after
concatenation: "+fullName);
// Measuring length of
concatenated name.
int len = fullName.length();
System.out.println("Length of full
name: "+len);
// Getting index of 'e' in full
name.
int indexOfE =
fullName.indexOf("e");
System.out.println("Index of
character 'e' in full name: "+indexOfE);
// Replacing 'i' with 'I' in
full name.
fullName = fullName.replace('i',
'I');
System.out.println("Full name after
replacement of 'i' with 'I': "+fullName);
// Comparing both names.
int comp =
name.compareTo(fatherName);
if(comp == 0) {
System.out.println("Both names are equal lexicographically");
}
else if(comp > 0) {
System.out.println("My name is greater than my father's name
laxicographically");
}
else {
System.out.println("My father's name is greater than my name
laxicographically");
}
}
}
Screenshot of output:-