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 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 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 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 +...
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...
Write a program to print 2 strings. One string must be written by parent process and...
Write a program to print 2 strings. One string must be written by parent process and another string must be written by a child process In language C
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements,...
Write the program in Java. Demonstrate that you know how to define variables, create conditional statements, write a loop, use arrays, obtain input from the user, display output, test and create a JavaDoc. Create project PaystubTest2 that is a Paystub calculator with program Paystub##.java (where ## is your initials) based on user input. (10 points) You must use the ReadStringFromUser and ReadFloatFromUser methods in the program to obtain user input. (20 points) This program will calculate the Gross Earnings, FICA...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT