Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.ArrayList; /* * * This program rearranges an array list of words (i.e. alphabetic strings) so that * any word beginning with an uppercase

import java.util.ArrayList;
/*
*
* This program rearranges an array list of words (i.e. alphabetic strings) so that
* any word beginning with an uppercase letter is shifted up in the array list  such that
* they are in front of any word beginning with a lowercase letter. The order of the uppercase words
* and lowercase words should not change
*
* Example:  "Every" "dog" "named" "Rover" "in" "the" "city" "of" "Toronto" "always" "seems" "to" "be" "friendly"
*
* would be rearranged to:
*
*           "Every" "Rover" "Toronto" "dog" "named" "in" "the" "city" "always" "seems" "to" "be" "friendly"  
*
*
*/
public class ShiftStrings
{
/**
Shifts all strings starting with an uppercase letter to the
beginning, without otherwise changing the order of the elements
@param list ArrayList of strings to be shifted
*/
public static void shiftUppercase(ArrayList list)
{
 //-----------Start below here. To do: approximate lines of code = 9
 // Hints: You will need a variable to keep track of the index of the last uppercase word shifted to the front,
 // You will need to use the method Character.is uppercase() and the String method charAt(),
 // You will need to use the ArrayList methods size(), get(), remove(), and the add() method that takes two parameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}

public static void main(String[] args)
{
 ArrayList words = new ArrayList<>();
words.add("So");
words.add("much");
words.add("has");
words.add("been");
words.add("written");
words.add("about");
words.add("The");
words.add("Beatles");

System.out.println("Before shifting words:" + words);
shiftUppercase(words);
System.out.println("After shifting words:" + words);
System.out.println("Expected:[So, The, Beatles, much, has, been, written, about]");

words.clear();
words.add("Every");
words.add("dog");
words.add("named");
words.add("Rover");
words.add("in");
words.add("the");
words.add("city");
words.add("of");
words.add("Toronto");
words.add("always");
words.add("seems");
words.add("to");
words.add("be");
words.add("friendly");

System.out.println("Before shifting words:" + words);
shiftUppercase(words);
System.out.println("After shifting words:" + words);
System.out.println("Expected:[Every, Rover, Toronto, dog, named, in, the, city, of, always, seems, to, be, friendly]");
}
}


Include screenshots too for formatting.

Step by Step Solution

3.41 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

mport java utilArrayList This program rearranges an array list of words ie alphabetic ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles of heat transfer

Authors: Frank Kreith, Raj M. Manglik, Mark S. Bohn

7th Edition

495667706, 978-0495667704

More Books

Students also viewed these Programming questions

Question

What is the role the character wants?

Answered: 1 week ago

Question

Stonewalling and how do they deal with it?

Answered: 1 week ago