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.
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.
reverseString
that takes a String
as its argument and constructs and returns a String
containing the same char
values, but in the opposite order.
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!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!”
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.
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.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.)
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).
printSquaresTable
using System.out.printf
instead of the String.format
method.