CS 171 & 171L Introduction to Computer Science I & Lab Fall 2019 

Lab 5: Strings

In this lab, you will explore using Strings.

1. Introductions

Introduce yourself to your lab partner. Tell your partner about your favorite fall activity.

2. using Strings

Complete the methods in Lab5.java, as shown below. After you complete each method, demonstrate it to your instructor. Change the driver/navigator roles after each method.

// Lab 5
// Your names here!

import java.util.Random;


public class Lab5 {

Random randomGenerator = new Random();

// Don't change main
public static void main(String args[]) {
  System.out.println("isLower:");
  System.out.println(isLower("computer")); // true
  System.out.println(isLower("cOmPuTeR")); // false

  System.out.println("stars:");
  System.out.println(stars("shot")); // sh*t
  System.out.println(stars("FORT")); // F*RT
  System.out.println(stars("pass")); // p*ss
  System.out.println(stars("sequoia")); // s*q****
  System.out.println(stars("Why!!!")); // Why!!! - don't change y

  System.out.println("rand5:");
  System.out.println(rand5("abcdefghijklmnopqurstuvwxyz")); // super - maybe
  System.out.println(rand5("abcdefghijklmnopqurstuvwxyz")); // lunch - maybe
  System.out.println(rand5("abcdefghijklmnopqurstuvwxyz")); // bkfst - maybe
  System.out.println(rand5("abcdefghijklmnopqurstuvwxyz")); // bkfst - maybe
  System.out.println(rand5("a")); // aaaaa

  System.out.println("removePunctuation:");
  System.out.println(removePunctuation("speed of art!")); // not at the pool!

  System.out.println("reverse:");
  System.out.println(reverse("live")); // evil
  System.out.println(reverse("reknits")); // stinker
  System.out.println(reverse("desserts")); // stressed
  System.out.println(reverse("12345")); // 54321

  System.out.println("palindrome:");
  System.out.println(palindrome("bob")); // true
  System.out.println(palindrome("dave")); // false
  System.out.println(palindrome("A man, a plan, a canal: Panama.")); // true
  System.out.println(palindrome("Oozy rat in a sanitary zoo.")); // true
  System.out.println(palindrome("computer science")); // false

  System.out.println("scrabbleScore:");
  System.out.println(scrabbleScore("compute")); // 13
  System.out.println(scrabbleScore("quiz"));    // 22

  System.out.println("bits(5) = " + bits(5)); // 101
  System.out.println("bits(6) = " + bits(6)); // 110
  System.out.println("bits(127) = " + bits(127)); // 1111111
  System.out.println("bits(0) = " + bits(0)); // 0
  }

   // Create a method that takes a single string parameter and returns true if all the letters are lowercase. 
   // Hint: use the tolowerCase and equals method of String.
  public static boolean isLower(String s) {
     return false;
  }

   // Create a method that takes a single string parameter and returns a new String that is 
   // the same as the original string except all vowels are replaced with a '*' character. 
   // Hint: use the repeated calls to the replace method of String.
  public static String stars(String s) {
     return "";
  }

   // Create a method that takes a single String parameter and returns a new string that is 5 characters long with each 
   // character in the new string coming from a randomly generated position in the original string.
  public static String rand5(String s) {
     int p = randomGenerator.nextInt(100);  // 0 <= p < 100, change 100 to suit your needs
     return "";
  }

   // Create a method removePunctuation that takes a single String parameter 
   // and returns a String containing just letters (no spaces, digits, or other special characters). 
  public static String removePunctuation(String s) {
     return "";
  }

   // Create a method reverse that takes a single String parameter 
   // and returns a String that contains the orginal string's characters 
   // in reverse order.
  public static String reverse(String s) {
     return "";
  }

  // phrase same forward of backward, excluding case, whitespace, and punctuation.
  // use removePunctuation
  public static boolean palindrome(String s) {
     return false;
  }

  // returns the scrabble score of the string, nonletters have value 0
  // use case
  // {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10}
  public static int scrabbleScore(String s) {
    return -1;
  }

  // returns binary representation of n
  // Use the + operator for strings
  // use /2 and %2
  // 6 (ten) = 110 (two)
  public static String bits(int n) {
     return "";
  }

}

References

You are done!

Finally, demonstrate your program to your instructor or lab assistant and remember to email a copy of the code to your lab partner.

Congratulations!


Copyright © 2019, David A. Reimann. All rights reserved.