Lab: 3 - Working with strings in Java

Assigned
Wednesday, 30 August 2023
Summary
You will submit this lab for grading (4 points). In this lab, you will use strings and a utility object called the StringBuilder to manipulate strings.

OBJECTIVE

The objective of this lab is to get experience working with the String and StringBuilder object.

Start this lab session by sharing something (most interesting or helpful and 3 Java methods) you learned from the readings or on your own (about Java).

Java Library - Class String

Java Library - Class StringBuilder

See the documentation for both classes, familiarize with their constructors and methods. Take a look at Table 3.3 in BJP for a short list of useful methods.

Working with strings in Java

Note: Copy and paste the terminal output of your programs in a .txt file as evidence of testing your methods with different inputs.

Create a Java program StringsExercisesApp.java that contains the methods in task 1 to 4. To run your program you should specify as first argument the method name to be called followed by its arguments depending on the method.

  1. (1 point) Write and test a method reverseString that takes a String as its argument and constructs and returns a String containing the same char values, but in the opposite order.
    • For efficiency, use a StringBuilder object to collect the char values as you extract them from the given string, then convert the StringBuilder to a String at the end. Test the method with “Hello, world!”. The method should return “!dlrow ,olleH”. The StringBuilder class has the built-in reverse method. Do NOT use this method for answering the exercise!
  2. (1 point) Write and test a method called rot13 that takes a String as argument, applies rot13 encryption to each of the letters of the English alphabet that occurs in that String (leaving any other characters unchanged), and returns the result. Capital letters should be encrypted as capital letters and lower-case letters as lower-case letters. For example, the method call rot13(“Hello, world!”) should return “Uryyb, jbeyq!”
    • About rot13: one traditional way of lightly disguising the content of a text written using the twenty-six-letter English alphabet is rot13 encryption, in which every letter is replaced by the letter thirteen positions before or after it in the alphabet. In this cryptosystem, ‘A’ is replaced by ‘N’, ‘B’ is replaced by ‘O’, and so on; after the midpoint of the alphabet, you back up instead of counting forwards, so that ‘N’ is replaced by ‘A’, ‘O’ by ‘B’, and so on through ‘Z’, which is replaced by ‘M’ (see rot13.com. In social media, rot13 encryption is occasionally used to conceal (briefly) the answers to riddles and trivia questions.
  3. (1 point) Write and test a method called countSubstring that counts and displays the number of substrings that match a target string using the substring method. Consider a string "this and this and that and this" and a target string “this”. In this example, there are 3 substrings.
    • The substring method in the String class returns a new String that is a substring of a given string. One version of this method takes two arguments. These arguments are the start index and end index of characters within the original string. Note that the substring does not include the character at the end index. Do NOT use the startsWith method for this excercise.
  4. (1 point) Write and test a method printSquaresTable that prints out a table of numbers from 1 to N (see figure below), along with their squares and squares roots. The program needs to display three numbers per line, where the header table and 1st column are left-justified and each computation is right-justified and the columns’ with is approx. 15 spaces.
    • Use the System.out print or println methods and the format method in the String class, which generates a String result from a format string (given as the first argument to the method) by embedding string representations of the subsequent arguments, which can be of various types. As in a call to the printf library function in C, the format string contains “format specifiers” that identify the data types of the values to be printed, control field width, precision, justification, and so on. (Java being what it is, the format specifiers can have even more bells and whistles than in C (see details in documentation), so study them carefully and try some experiments if your reading leaves any uncertainties in your mind.)

      today's reading

Lab Submission

You will submit your files via Gradescope by the end of the week. Submit the StringExersicesApp.java and a text file showing the outcome of your program.

I strongly recommend that each student keep a copy of the lab. Therefore, don’t forget to share the files/folder with your lab partner!

Remember: Write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments).

For those with more time

  • Rewrite the function printSquaresTable using System.out.printf instead of the String.format method.
  • Self-check Problems 19-22 Chapter 4.