Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following program skeleton for the program Exam1B given below. This

ID: 3795510 • Letter: C

Question

Complete the following program skeleton for the program Exam1B given below. This program should ask the user to enter two strings. It should then report which string is the shortest, whether the first characters of each string are the same, if the shortest string has the character 'a' in it (only lowercase 'a' counts here - do not worry about 'A'). If the string is the same length, then the first string should be considered the 'shortest' one for this purpose. Make sure your code produces the same output as that given in the transcript below for the input given in the transcript below. You do not need to handle empty strings for this problem, but you may revive up to 2 points of Exam credit if your solution is correct and also handle empty string without crashing the program. you should get the input using the Scanner method next line(). See the last page of the exam for a description of the next line method and some string methods that may be useful. Here is a sample transcript of how the program should work. Input typed by the user is indicated by bold text

Explanation / Answer


// Exam2B.java
import java.util.Random;
import java.util.Scanner;

class Exam2B
{

      public static void main(String args[])
      {
            Scanner in = new Scanner(System.in);

            System.out.print("Enter a string: ");
            String str1 = in.nextLine();

            System.out.print("Enter another string: ");
            String str2 = in.nextLine();

            // check if second string is shorter
            if(str2.length() < str1.length())
            {
                  System.out.println("The shortest string is: " + str2);

                  if(str2.indexOf("a")!=-1)
                        System.out.println("character 'a' is present in string");
                  else
                        System.out.println("character 'a' is not present in string");
            }
            // else first string is shorter
            else
            {    
                  System.out.println("The shortest string is: " + str1);
                  if(str1.indexOf("a")!=-1)
                        System.out.println("character 'a' is present in string");
                  else
                        System.out.println("character 'a' is not present in string");
            }

            if(str1.charAt(0) == str2.charAt(0))
                  System.out.println("The first characters are the same ");
            else
                  System.out.println("The first characters are not the same ");

      }
}


/*
output:

Enter a string: Beer
Enter another string: Monkey
The shortest string is: Beer
character 'a' is not present in string
The first characters are not the same


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote