If you haven’t already created a folder named cs112 for your
work in this course, follow these
instructions to do so.
Then create a subfolder called lab3 within your cs112 folder,
and put all of the files for this lab in that folder.
Review this example to trace through a program that works with arrays.
Download GuessingGame.pdf which contains a specification for the program we would like you to write.
Wrire your program on the paper copy that the TAs will give you. At the end of lab, please create a PDF of your code and submit it to the portal in Gradescope.
In Problem Set 2, there are several
problems in which you will need to work with String objects in Java.
It turns out that Java lacks many of Python’s built-in operators and functions for strings. Instead, most operations involving strings – including indexing and slicing – are done using methods that are inside the string object.
If you would like to review, this is an example of a code trace using Java Strings.
The table below compares the ways in which some common string operations would be accomplished in Python and Java:
|
operation |
in Python |
in Java |
|---|---|---|
|
assigning to a variable (and, in Java, declaring the variable) |
|
|
|
concatenation |
|
|
|
finding the number of characters |
|
|
|
indexing |
|
|
|
slicing |
|
|
|
converting all letters to uppercase |
|
|
|
converting all letters to lowercase |
|
|
|
determining if a substring is in a string |
|
|
|
finding the index of the first occurence of a character or substring |
|
|
|
testing if two strings are equivalent |
|
|
Additional notes:
Java has a separate type called char for values that consist of
a single character. Literals of this type are surrounded by
single quotes (e.g., the 'o' in the call s1.indexOf('o')),
whereas string literals must be surrounded by double quotes.
(In Python, there is no separate char type, and string literals
can be surrounded by either single or double quotes.)
The charAt() method that we use for indexing in Java returns a
value of type char. For example, the call s1.charAt(0) in the
table above would return the char value 'B', since 'B' is
the first character in the string "Boston".
To ensure that you understand how the above string operations work in Java, let’s write a simple Java test program that you can use to play around with the different string operations.
Getting started
If you haven’t already done so, create a folder named lab3
for your work on this lab. You can find instructions for
creating folders here.
As needed, open your lab3 folder using the File->Open Folder
or File->Open menu option in VS Code.
Select File->New File, which will open up an empty editor window.
Select File->Save, and give the new file the name StringTest.java.
Here are the tasks you should perform as part of your StringTest class:
Create a class header and a main method.
In the main method, begin by initializing two string variables
that we will use in the later steps:
String s1 = "Boston"; String s2 = "University";
If you have done everything correctly, the statements:
System.out.println(s1); System.out.println(s2);
should output:
Boston University
If for some reason you don’t see this output, make whatever changes are needed before proceeding.
We’re now going to solve a series of puzzles in which we construct
an expression involving operations on s1 and/or s2 to produce a
specified target value.
For example, let’s say that we want to construct an expression
involving s1 or s2 that produces the following value:
"ton"
One possible expression that works for this puzzle is
s1.substring(3, 6). To see this, we can add the line:
System.out.println( s1.substring(3, 6) );
which should output:
ton
Now solve the puzzles below. As you do so, add a println statement
to your main method to test each expression that you construct.
Construct an expression involving s1 or s2 that produces
the following value:
"Uni"
Construct an expression involving s1 or s2 that produces
the following value:
"UNI"
Hint: Chain two method calls together. For example:
System.out.println( s1.toLowerCase().substring(0, 3) );
should output:
bos
Construct an expression involving s1 or s2 that produces
the following value:
'v'
Note that the single quotes mean that you must produce a
char, not a String. See the notes under the table above
that discuss the char type.
Construct an expression involving s1 or s2 that produces
the following value:
"STONE"
Write a static method called replaceStart that takes two strings s1
and s2, and does the following:
If the length of s1 (call it s1Len) is less than the
length of s2, the method returns a string in which
the first s1Len characters of s2 are replaced by s1.
For example:
System.out.println( replaceStart("abc", "xxxxxxx") );
should output:
abcxxxx
and,
System.out.println( replaceStart("hello", "xxxxxxx") );
should output:
helloxx
If the length of s1 is greater than or equal to the length
of s2, the method just returns s1. For example:
System.out.println( replaceStart("hello", "bye") );
should output:
hello
Why does the method replaceStart need to be called from within
the println method? In other words, what would you expect to
happen if our program just made the call to replaceStart on its
own line, rather than calling it as part of a println? Make sure
you understand.
To get the most out of this task, draw out a memory diagram of the arrays on a piece of paper (follow examples we did in lecture, and the instructions of your TA). This is the best way to understand the correct answer to each of the questions, and why.
Let’s draw what things look like in memory for the each of the following code snippets:
// snippet A int[] a1 = {1, 2, 3, 4}; int[] a2 = a1; // snippet B int[] b1 = {1, 2, 3, 4}; int[] b2 = {1, 2, 3, 4};
Given the two snippets above, what is the value of the following expression?
a1 == a2
Given the two snippets above, what is the value of the following expression?
b1 == b2
Consider the following static method:
public static void mystery(int[] vals) { for (int i = 0; i < vals.length; i++) { vals[i] *= -1; } int[] vals2 = new int[vals.length]; for (int i = 0; i < vals.length; i++) { vals2[i] = vals[i]; } vals = vals2; for (int i = 0; i < vals.length; i++) { vals[i] += 2; } }
What does the array a1 look like after the execution of the
following code snippet, which you many assume is part of the
main method of a valid Java class?
int[] a1 = {1, 2, 3, 4}; mystery(a1);
Draw pictures to show what things look like in memory. You may find
it helpful to use the following template, which shows separate
method frames for main() and mystery():
+--------------+ | mystery | | ------- | | +----+ | | vals2 | | | | +----+ | | +----+ | | vals | | | | +----+ | +--------------+ | main | | ---- | | +----+ | | a1 | | | | +----+ | +--------------+
Getting started
As needed, open your lab3 folder using the File->Open Folder
or File->Open menu option in VS Code.
Select File->New File, which will open up an empty editor window.
Select File->Save, and give the new file the name
ArrayPractice.java.
Here are the tasks you should perform as part of your ArrayPractice class:
Before the class header, you should include the following
statement, which will give us access to the methods in the
Arrays class:
import java.util.*;
Create a class header and a main method, and add the following
initial statements to your main method:
int[] a = {2, 4, 6, 8}; System.out.println( a ); // What do you expect to see? System.out.println( Arrays.toString(a) ); // Now, what do you expect to see?
Write a static method named equals that takes two arrays of integers
and determines if the two arrays are identical (i.e. same elements
in the same positions). The method should return true or
false accordingly.
For this (and each of the following methods), you will need to add
calls to your main method to test them.
Example:
int[] a1 = {1, 2, 3, 4}; int[] a2 = {1, 2, 3, 4}; System.out.println( a1 == a2 ); System.out.println( equals(a1, a2) );
You should expect to see:
false true
Congratulations! You just wrote your own equals method for
arrays! However the Arrays class contains many methods, one of
which is the equals method. To use the built-in Arrays method,
try this:
Example:
System.out.println( Arrays.equals(a1, a2) );
You should expect to see:
true
Write a static method named square that takes an array of
integers and squares each element of the array.
This method does not need to return anything. (If you’re not sure why, please ask!)
Example:
int[] a1 = {1, 2, 3, 4}; square(a1); System.out.println( Arrays.toString(a1) );
You should expect to see:
[1, 4, 9, 16]
Write a static method named shiftLeft that takes an array of
integers and shifts each element one position to the left.
The last element of the array should end up being 0, and the
original first element should be returned by the method.
For example:
int[] a3 = {1, 2, 3, 4, 5, 6}; System.out.println( shiftLeft(a3) ); System.out.println( Arrays.toString(a3) );
You should expect to see:
1 [2, 3, 4, 5, 6, 0]
Hint: To determine the necessary logic, you may find it helpful to begin by writing down examples of specific assignments statements that the method will need to perform as part of the shifting. Then look for the general pattern in these assignments, and use it to write the necessary loop.
Other hints:
You will need to store the original first element in a variable before you do the shifting, so that you can return the first element at the end of the method.
Don’t forget to put a 0 in the last position of the array.
Write a static method named replace that takes an array of
integers and two integer variables, val1 and val2 and replaces
all occurences of val1 with val2. The method should return
how many values were replaced. If no value was replaced (i.e.
there were no occurrences of val1 in the array), the method
should return a zero.
For example:
int[] a4 = {1, 2, 3, 2, 2, 6}; int numReplaced; // first test numReplaced = replace(a4, 2, 9); System.out.println( Arrays.toString(a4) ); System.out.println( "numReplaced = " + numReplaced );
You should expect to see:
[1, 9, 3, 9, 9, 6] numReplaced = 3
Adding these statements:
// second test numReplaced = replace(a4, 2, 9); System.out.println( Arrays.toString(a4) ); System.out.println( "numReplaced = " + numReplaced);
You should expect to see:
[1, 9, 3, 9, 9, 6] numReplaced = 0
How could I produce the exact same output as above without needing to
declare the variable numReplaced?
What is the output of the following code segment?
int[] a4 = {1, 2, 3, 2, 2, 6}; replace(a4, 2, 9); System.out.println( replace(a4, 2, 9) );
Write a static method named interleave that takes two arrays of
integers, creates and returns a third array of integers which contains
all the elements of the two arrays, but in interleaved order.
For example:
int[] a5 = {1, 2, 3, 4, 5}; int[] a6 = {6, 7, 8, 9, 10}; int[] a7 = interleave(a5, a6); System.out.println( Arrays.toString(a7) );
This should produce:
[1, 6, 2, 7, 3, 8, 4, 9, 5, 10]
Note:
Write a static method named isMirror that takes two arrays of
integers and determines if the second array is an exact mirror
image of the first array. The method should return true or
false accordingly.
Example 1:
int[] a8 = {1, 2, 3, 4, 5}; int[] a9 = {5, 4, 3, 2, 1}; System.out.println( isMirror(a8, a9) );
This should produce:
true
Example 2:
int[] a10 = {1, 4, 5, 2, 1}; System.out.println( isMirror(a8, a10) );
This should produce:
false
Write a new static method named isMirror that takes two arrays of
strings and determines if the second array is an exact mirror
image of the first array. The method should return true or
false accordingly. (Note: It’s okay for two methods to have
the same name, as long as the number and/or types of the parameters
are different.)
Example:
String[] s1 = { "abc", "def", "ghi" }; String[] s2 = new String[3]; s2[0] = new String("ghi"); s2[1] = new String("def"); s2[2] = new String("abc"); System.out.println( isMirror(s1, s2) ); // what do you expect to see?
Hint:
Last updated on February 11, 2026.