Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 12 - If y cn rd ths, y cn b a gd prgrmmr.


In this lab, you will explore String manipulation by removing characters from Strings. Even with many characters removed, it is still possible too get the meaning. While this isn’t exactly lossy compression, it does show you that even with information removed, you can still understand what you are looking at even if the quality is not the same.

What You’ll Learn

  • Character Functions
  • String methods (contains and charAt)
  • Loops
  • Method Overloading

Step 0: Constant

Create a public static final String called VOWELS at the topic of your class. Initialize it to “aeiouAEIOU”. You will use this later.

Step 1: lettersAndWhitespaceOnly(String)

For this method, you will take in a message, and keep only letters and whitespace. Everything else (numbers, punctuation, etc) will be removed. So for example

String value = lettersAndWhitespaceOnly("The number 42 is a number that is popular due to Hitchhikers Guide to the Galaxy.")

would set value equal to

"The number  is a number that is popular due to Hitchhikers Guide to the Galaxy"


String value = lettersAndWhitespaceOnly("!@!(*&(*&)!(!x*@)9191828101")

would set the value equal to


Writing lettersAndWhitespaceOnly

For this method, we already gave you the method stub, so you will only need to write a loop.

To help the loop conditions are from 0 to the length of the String, so as follows:

for(int i = 0; i < message.length(); i++) {
   // do something here

You will then want to check the character at that location, to see if it is a letter or whitespace and if it is, add it too the return string.

Some helpful methods are:

Testing lettersAndWhitespaceOnly

You should test lettersAndWhitespaceOnly as you write it, and definitely after you write it. Here are some examples, you can put in your main - but you should not limit yourself to these examples.

 //Testing letters only
  System.out.println("Testing letters only");
  System.out.println(lettersAndWhitespaceOnly("The number 42 is a number that is popular due to Hitchhikers Guide to the Galaxy."));

Step 2: removeWhiteSpace(String)

For this method, you will take in a message, and remove all whitespace from the message. Imeanwecanallreadwithoutwhitespace - ok, let’s try that again. We can all read without whitespace.

In either case, this is a nice warm up, that will help for the other methods.

Writing removeWhiteSpace(String)

You will start off with a very similar loop as the last method, and instead of looking for whitespace and letters - you will only need to look for whitespace. The difference is:

  • If you find whitespce - DON’T Add it to the return String
  • everything else, add it!

Testing removeWhiteSpace(String)

As always, you should test as you write it, but after you write it, here are some tests you can add to your main method.

System.out.println("Testing remove whitespace");
System.out.println(removeWhiteSpace("Look at all     this white      space."));
System.out.println(removeWhiteSpace("                           c                      "));

You may want to add other tests, and don’t trust whitespace to only be spaces. Tab characters are valid whitespace along with other invisible style characters.

Step 3: hasCharacter(char, String)

This is your first helper method for the lossy compression, and will make it easier to work getting this one right. The goal is to take a character, and check to see if that character is in a group of characters. For example, is x in the AeiOu? You know that it isn’t, so the method should return false.

Start thinking of ways that you can solve this problem, and then you can start writing after you get an idea.

Writing hasCharacter(char, String)

First, you will need to write the method signature. If you look at the comments there is a good spot to add it. The hasCharacter method is a public static method that returns a boolean value. It needs to take in a character and String as parameters in that order. You can call them what you want, but I called my character x and my string group (ok, maybe not the best names for each…)

Second, you will want to go through the String to see if the character from the parameter exists in the String. You can do this either with a loop and charAt, or you can do it with .contains (group.contains()). I found using the built in String method to be easier.

Testing hasCharacter(char, String)

You will want to work through this method, and here are some sample tests.

 System.out.println("Testing hasCharacter");
 System.out.println(hasCharacter('x', "bingx"));
 System.out.println(hasCharacter('4', "hello"));
 System.out.println(hasCharacter("Lumos".charAt(1), VOWELS));

Notice, the last case takes the character in Lumos, and then tests it against the VOWELS constant you made! This is helpful, as you may need to remember it for the next method you are writing.

Step 4: stringShortener(String, String)

Next, you will want to find the comments just above the main method - the ones for the 2 parameter stringShortner method. You will work on this one first, and then go back up to the one parameter version.

This method is your lossy compression! (finally!). The goal is to write a method that takes in a String, and removes all characters that show up in the second string passed in. It will keep a character if it is the start of a word (i.e. following a space)

For example

You can be a good programmer, if you can read this.

would return if VOWELS was used as the list of characters to remove

Y cn b a gd prgrmmr, if y cn rd ths.

Writing stringShortener(String, String)

  • First write the method signature. It is a public static method that returns a String, and takes in two String parameters - a message and a group of characters to remove as a String - in that order.

  • You will then need to create an empty String like was created in the first two methods you wrote. This is the same String you will return. Hint: I would compile here, just to make sure it is compiling.

  • Next, loop through the first String (your message), and check to see if the character at position i is in your group of characters to remove. Hint use hasCharacter! If it is NOT in that group, OR if the character before your current location is a space (the tricky part), then you add it to the the rtn string.

It may be easier to ignore the ‘space’ rule, and get it working. You will then want to try it again, figuring out the space condition. Remember, prototype and repeat tirelessly.

Testing stringShortener(String, String)

This is definitely a method you should test via tossing in printlns as you write it! It will be extremely hard if you don’t do that. After you have what you think is a working method, you can add the following to your main method

System.out.println("Testing stringShortener");
System.out.println(stringShortener("To be or not to be: that is the question", ":ah"));
System.out.println(stringShortener("Two roads diverged in a wood, and I – I took the road less traveled", "T-I "));
System.out.println(stringShortener("The course of true love never did run smooth", VOWELS));

Step 5: stringShortener(String)

Now go up into your code that talks about a stringShortener that only takes in 1 parameter. This is your chance to practice method overloading.

The goal is to assume the default group to remove is VOWELS, as really, who needs vowels when you are writing?

Writing stringShortener(String)

  • First write the method signature. It is the same as the other stringShortener, but only has a String messafe as a parameter

  • call stringShortener(String, String) passing in message and VOWELS as your parameter. Return the value returned from that method!

Testing stringShortener(String)

Here are some tests to help you out.

System.out.println("Testing stringShortener Overloaded");
System.out.println(stringShortener("The course of true love never did run smooth."));
System.out.println(stringShortener("If you can read this, you can be a good programmer."));

Step 7 - Turning in

Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get ten submission attempts, so make sure it is mostly working before you submit. We will bypass the testMethod in our tests, running our own tests on each method you wrote.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.