THINK FIRST·CODE LATER

← All labs

StringTools: design and test a utility class

Problem

Write a utility class StringTools (final, private constructor, a constant VOWELS, static methods only):

  • countVowels(s);
  • countWords(s) — words are separated by one or more spaces;
  • titleCase(s) — hello BIG world → Hello Big World;
  • slug(s) — trimmed, lower case, spaces replaced by _;
  • isPalindrome(s) — ignoring case and everything that is not a letter or digit.

StringToolsApp.main calls all five on every non-empty input line.

For the input

hello BIG world
A man, a plan, a canal: Panama
  two   spaces

the program prints

"hello BIG world"
  vowels: 4, words: 3
  title: Hello Big World
  slug: hello_big_world
  palindrome: false
"A man, a plan, a canal: Panama"
  vowels: 10, words: 7
  title: A Man, A Plan, A Canal: Panama
  slug: a_man,_a_plan,_a_canal:_panama
  palindrome: true
"  two   spaces  "
  vowels: 3, words: 2
  title:   Two   Spaces  
  slug: two___spaces
  palindrome: false

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l12-5-data-1.txt

hello BIG world
A man, a plan, a canal: Panama
  two   spaces
What your program must print for l12-5-data-1.txt
"hello BIG world"
  vowels: 4, words: 3
  title: Hello Big World
  slug: hello_big_world
  palindrome: false
"A man, a plan, a canal: Panama"
  vowels: 10, words: 7
  title: A Man, A Plan, A Canal: Panama
  slug: a_man,_a_plan,_a_canal:_panama
  palindrome: true
"  two   spaces  "
  vowels: 3, words: 2
  title:   Two   Spaces  
  slug: two___spaces
  palindrome: false

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l12-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l12-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.