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

Exercise 1 Description You will be writing a simple Java program that counts the

ID: 3587003 • Letter: E

Question

Exercise 1 Description

You will be writing a simple Java program that counts the words in a String. A "skelton" of this code is provided in the file given in the instructions above. You will see that there are three methods declared in this file with no code provided. You must fill in the appropriate code. Pay close attention to what each method should be doing based on the information given in the comments before the method.

Exercise 1 Sample Output

This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program.

Enter a string: the quick brown fox jumped
Your string has 5 words in it.

If you provide an empty string, your program should report an error until the user enters a non-empty string:

Enter a string:
ERROR - string must not be empty.

Enter a string: A man, a plan, a canal; Panama
Your string has 7 words in it.

NOTE: As described in the comments in the skeleton file, you may assume that words will have exactly one space between them.

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab07b.java in the same ClosedLab07 folder.

For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header:

    private static String getFirstWord(String input)

It should take a String as input and return the first word of that String, using the definition of a word given in Exercise 1. If the String is empty, it should return the empty string.

Then modify your main program so that after it reports how many words are in your String it also reports what the first word of your input String is.

Exercise 2 Sample Output

This is a sample transcript of what your program should do.

Enter a string: The quick brown fox jumped
Your string has 5 words in it.
The first word is: The

If there is only one word in your String, make sure that your program identifies it correctly:

Enter a string: One
Your string has 1 words in it.
The first word is: One

If the user enters an empty String, the code should report an error as it did in Exercise 1:

Enter a string:
ERROR - string must not be empty.

Enter a string: Four score and seven years ago
Your string has 6 words in it.
The first word is: Four

public class Lab07a f public static void main (Stringl] args) I / Fill in the body with your code // Given a Scanner, prompt the user for a String. If the user enters an empty // String, report an error message and ask for a nonr-empty String. Return the // String to the calling program private static String get InputString(Scanner inScanner) i // Fill in the body // OTE: Do not declare a Scarner in the body of this method. // Given a String return the mnber of words in the String. A word is a sequence of // characters with no spaces. Write this method so that the function call: int count = getWordCount ("The quick brown fox jumped") // results in count having a value of 5. You will call this method from the main method. / For this assigrment you may assume that // words will be separated by exactly one space private static int getWordCount (String input) I // Fill in the body

Explanation / Answer

Exercise 1 Java Code :

import java.util.*;               //to import Scanner class
public class CountWords
{
   public static void main(String args[])
   {
       String str;
       int count=1;
       Scanner obj=new Scanner(System.in);               //making Scanner class object
       System.out.println("Enter a String :");
       str=obj.nextLine();               //nextLine() function of Scanner class
       if(str.length()>0)           //Every String and Array in java has its Length() function
       {
           for(int i=0;i<str.length();i++)
           {
               if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' '))               //charAt() function of String class
               {
                   count++;
               }          
           }
       }
       else
       {
           System.out.println("ERROR - string must not be empty.");           //Error Message
       }
       if(count>1)                                               //To display number of Words in the input String
       {
           System.out.println("Your string has "+count+" words in it.");
       }
   }
}