Question

You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end the loop.

Requirements a) Add the necessary code to StringPlay.java: Method countWords 1. Receives a String value containing the user-input phrase passed from Lab09.java 2. Separates the phrase into individual words 3. Returns an int value containing a count of the individual words in the phrase Method changeToUppercase 1. Receives a String value containing the user-input phrase passed from Lab09.java 2. Declares a StringBuilder object 3. Separated the phrase into individual words, storing those words in an array of String type 4. Beginning with the first entry in the array, appends each word with all letters changed to uppercase (and a following space) to the StringBuilder object 5. Returns the StringBuilder object Method findMiddleWord 1. Receives a String value containing the user-input phrase passed from Lab09.java 2. Determines the number of words in the String 3. Finds the middle word 4. Returns the String representing the middle or center word in the String

// This is a test harness for Lab09.

import java.util.Scanner;

public class Lab09
{
public static void main( String[] args)
{
    Scanner input = new Scanner( System.in );   
    String finish = "Finish";                    
  
    System.out.printf("%nEnter a phrase containing at least three words%nOr enter finish to stop" );
  
    String newPhrase = input.nextLine();
  
    // You need to change the control condition here
    while( newPhrase != finish )
    {
      // Call static methods of class StringPlay
      System.out.printf("Your phrase contains %d words.", StringPlay.countWords( newPhrase ) );
      System.out.printf("%nYour phrase with all letters in uppercase: %s",
                        StringPlay.changeToUppercase( newPhrase ) );
      System.out.printf("%nThe middle word of your phrase is: %S%n%n",
                        StringPlay.findMiddleWord( newPhrase ) );
    
      System.out.printf("%nEnter a phrase containing at least three words%nOr enter finish to stop" );

      newPhrase = input.nextLine();
    }
  
}

}

// This is a 'helper' class for Lab09.

public class StringPlay
{

// Static method to count the words in the input String
public static int countWords( String phrase )
{

}

// Static method to change all letters in the input String to uppercase
public static StringBuilder changeToUppercase( String phrase )
{

}

//Static method to find middle word of the input String
public static String findMiddleWord( String phrase )
{

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

StringPlay.java

class StringPlay   
{
int countWords(String str) // checks how many number of words present in a string
{
int count=1;
for(int i=0;i<str.length()-1;i++)
if((str.charAt(i)==' ') && (str.charAt(i+1)!=' '))
count++;
return count; // return number of words
}

String ToUppercase(String str) // convert string to uppercase
{
StringBuilder sb = new StringBuilder(); // creating object of StringBuilder class
String strArray[] = str.split(" "); // store words in the string into string array
for (String s : strArray)
sb.append(s).append(" "); // append array data into Stringbuilder object
sb.deleteCharAt(sb.length() - 1);
for(int i = 0 ; i < sb.length() ; i++)
if(sb.charAt(i) >= 97 && sb.charAt(i) <=122) // check for small case letter ... ascii code of "a" is 97 ... "z" is 122.
sb.setCharAt(i, (char)(sb.charAt(i)-32)); // The character at the specified index is set to to uppercase
return sb.toString(); // return uppercase string
}

String findMiddleWord(String str) // finds the middle word from the string
{
String array[]=str.split(" "); // stores words in the string into array
int temp=array.length; // count array length
if(temp%2==0)
return array[temp/2-1]; // returns middle word
else
return array[temp/2]; // returns middle word
}

} // close class

Lab09.java

import java.util.Scanner; // import Scanner class from java.util package

public class Lab09
{
public static void main( String[] args) // main method
{
StringPlay sp=new StringPlay(); // creating the object of StringPlay class
Scanner input = new Scanner( System.in ); // creaing Scanner object

System.out.printf("%nEnter a phrase containing at least three words%nOr enter finish to stop" );
  
String newPhrase = input.nextLine(); // get input from user
String a="finish";
// You need to change the control condition here
while(newPhrase != a)
{
// Call static methods of class StringPlay
System.out.printf("Your phrase contains %d words.", sp.countWords( newPhrase.trim() ) ); // calls countWords() method
System.out.printf("%nYour phrase with all letters in uppercase: %s",
sp.ToUppercase( newPhrase.trim() ) ); // calls ToUppercase() method
System.out.printf("%nThe middle word of your phrase is: %S%n%n",
sp.findMiddleWord( newPhrase.trim() ) ); // calls findMiddleWord() method

System.out.printf("%nEnter a phrase containing at least three words%nOr enter finish to stop" );

newPhrase = input.nextLine();
newPhrase=newPhrase.trim();

}
  
} // end of main

} // end of class

Output

C:\Users\AKSHAY\Desktop>javac StringPlay.java

C:\Users\AKSHAY\Desktop>javac Lab09.java

C:\Users\AKSHAY\Desktop>java Lab09

Enter a phrase containing at least three words
Or enter finish to stop Each day provides its own gifts
Your phrase contains 6 words.
Your phrase with all letters in uppercase: EACH DAY PROVIDES ITS OWN GIFTS
The middle word of your phrase is: PROVIDES


Enter a phrase containing at least three words
Or enter finish to stop Information systems department
Your phrase contains 3 words.
Your phrase with all letters in uppercase: INFORMATION SYSTEMS DEPARTMENT
The middle word of your phrase is: SYSTEMS

C: \Users IAKSHAY\Desktop javac StringPlay-java C: \Users IAKSHAY\Desktop javac Labe9.java c: \Users\AKSHAY\Desktop>java Labe

Add a comment
Know the answer?
Add Answer to:
You will write three static methods to manipulate an input String in different ways using various...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • Using C++ programming. Write a program that takes a string of input from the user and...

    Using C++ programming. Write a program that takes a string of input from the user and separates the string of words based on the premise that the string contains words whose first letter is uppercase. Then, display the phrase (or sentence) to a string in which the words are separated by spaces and only the first word of the phrase starts with an uppercase letter. For example, if the user enters "IAmTheTeacher", then the program would display: "I am the...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the...

    Write a JAVA program that has 2 methods with the same name: printCaps(String input) prints the input in capital letters, printCaps(String input, int num) prints in capital letters the specified number of times on a new line: public static void printCaps(String input) public static void printCaps(String input, int num)

  • USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...

    USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and to LowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt()...

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • JAVA public static String generatePassword(String gp)        {            String password="";       ...

    JAVA public static String generatePassword(String gp)        {            String password="";            boolean b= false;            for (int i =0;i            {                char ch = gp.charAt(i);                if (i == 0)                {                    password += Character.toUpperCase(ch);                }                if (ch == 'S' || ch == 's')           ...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT