Question

In: Computer Science

Meant to be written in Java JDK 14.0 4. Define 2 String variables str1 and str2,...

Meant to be written in Java JDK 14.0

4. Define 2 String variables str1 and str2, get input from console and assign input to str1 and str2. (a) Display the length of str1 and str2 (b) Display the first and last characters in str1 and str2 (c) Compare str1 and str2 to check if they are equal or not: (1) The comparison is case sensitive (2) The comparison is case insensitive (d) Compare str1 with str2 to check which string is smaller; display the smaller string first: (1) The comparison is case sensitive (2) The comparison is case insensitive (e) Convert characters in str1 to upper case and display new string (f) Convert characters in str2 to lower case and display new string 2020 (g) Extract the first and last characters in str1, extract the 2nd character from the front and the 2nd character from the end in str2; put these 4 characters together to create a new string and display (h) Extract the first words and the last words from both str1 and str2. Combine these words to create a new string. The words should be separated by white space. Display this new string

Solutions

Expert Solution

import java.util.Scanner;

public class StringOperations {
   public static void main(String[] args) {
//       Define 2 String variables str1 and str2, get input from console and assign input to str1 and str2.
       String str1,str2;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter string1 :");
       str1 = sc.nextLine();
       System.out.print("Enter string2 :");
       str2 = sc.nextLine();
//       (a) Display the length of str1 and str2
       System.out.println("string1 length : "+str1.length());
       System.out.println("string2 length : "+str2.length());
      
//       (b) Display the first and last characters in str1 and str2
       System.out.println("first character of string1 :"+str1.charAt(0));
       System.out.println("last character of string1 :"+str1.charAt(str1.length()-1));
       System.out.println("first character of string2 :"+str2.charAt(0));
       System.out.println("last character of string2 :"+str2.charAt(str2.length()-1));
      
//       (c) Compare str1 and str2 to check if they are equal or not:
       System.out.println("(1) The comparison is case sensitive ");
       System.out.println(str1.equals(str2)?"Both string1 and string2 are equal":"Both String1 and string2 are not equal");
      
//       (2) The comparison is case insensitive
       System.out.println("(1) The comparison is case insensitive ");
       System.out.println(str1.equalsIgnoreCase(str2)?"Both string1 and string2 are equal":"Both String1 and string2 are not equal");
      
//       (e) Convert characters in str1 to upper case and display new string
       System.out.println("string1 in upper case :"+str1.toUpperCase());
      
//       (f) Convert characters in str2 to lower case and display new string 2020
       System.out.println("string2 in lower case :"+str2.toLowerCase());
      
//       (g) Extract the first and last characters in str1, extract the 2nd character from the front and the 2nd character from the end in str2;
//       put these 4 characters together to create a new string and display
       String newString = str1.charAt(0)+""+str1.charAt(str1.length()-1)+""+str2.charAt(1)+""+str2.charAt(str2.length()-2);
       System.out.println("new string after extract requrired characters : "+newString);
      
//       (h) Extract the first words and the last words from both str1 and str2.
//       Combine these words to create a new string. The words should be separated by white space. Display this new string
       String str1words[] = str1.split(" ");
       String str2words[] = str2.split(" ");
       String newString2 = str1words[0]+" "+str1words[str1words.length-1]+" "+str2words[0]+" "+str2words[str2words.length-1];
       System.out.println("new string after extract requrired string : "+newString2);
      
////       (d) Compare str1 with str2 to check which string is smaller; display the smaller string first:
       System.out.println("Compare str1 with str2 to check which string is smaller;");
       int value = str1.compareTo(str2);
       if(value<0){
           System.out.println("smaller string is :"+str1);
       }else if(value>0){
           System.out.println("smaller string is :"+str2);
       }else{
           System.out.println("both are equal");
       }
   }
}


Related Solutions

Meant to be written in Java JDK 14.0 1. Define 2 double variables. (a) Use method(s)...
Meant to be written in Java JDK 14.0 1. Define 2 double variables. (a) Use method(s) define in Math class to check if these 2 numbers are equal or not. Display result (b) Write code (not to use Math method) check if these 2 numbers are equal or not. Display result
Meant to be written in Java JDK 14.0 A String object is called palindrome if it...
Meant to be written in Java JDK 14.0 A String object is called palindrome if it is same when read from the front and the end. For example, String “abcba” is palindrome, while “12322” is not. Write Java program to check a String object of 5 characters is palindrome or not: (a) The comparison is case sensitive (b) The comparison is case insensitive
Meant to be written in Java JDK 14.0 Write a program to display the conversion table...
Meant to be written in Java JDK 14.0 Write a program to display the conversion table from meter to feet using formatted output (printf()): 1 meter = 3.28084 feet; 1 foot = 12 inch When display the number, round the number to 2 decimal places. Set the number in the 1st the 2nd columns to the left align, set the 3rd column to the right align: meter(s) feet inch(es) 1 3.28 37.37 2 x.xx xx.xx 3 x.xx xxx.xx
Meant to be written in Java JDK 14.0 In New York state, the vehicle plate number...
Meant to be written in Java JDK 14.0 In New York state, the vehicle plate number has the following format: xxx-yyyy, where x is a letter and y is a digit. Write a program to enter a string, verify if it is a valid plate number or not
Write a Java program to move data among variables: (1) define 4 int variable pos1, pos2,...
Write a Java program to move data among variables: (1) define 4 int variable pos1, pos2, pos3, pos4, input 4 integer numbers from console and assign them to these variables; display message in the following format: “pos1=a-number; pos2=a-number; pos3=a-number; pos4=a-number”. “anumber” is replaced with real number. (2) Left shift data in these variables: for each variable to get the value of the successive variable, with pos4 getting pos1's value. Display pos1, pos2, pos3, and pos4 (3) Right shift data in...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
You have written a JAVA program that creates Shoe object with three instance variables. You will...
You have written a JAVA program that creates Shoe object with three instance variables. You will need to use the exception you created to go along with it. So far you have been using my driver classes. Now it is time for you to create your own driver class that will implement an ArrayList of Shoe objects. This assignment uses the Shoe.java and ShoeException.java to create the driver ShoeStore.Java You will display a menu as follows: Add shoes Print all...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
1)define variables. 2)define functions. 3)define data structure.
1)define variables. 2)define functions. 3)define data structure.
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT