part I due by 10 p.m. on Monday, July 7, 2025
part II due by 10 p.m. on Wednesday, July 9, 2025
In your work on this assignment, make sure to abide by the policies on academic conduct for this course.
If you have questions while working on this assignment, please come to
office hours, post them on Ed Discussion, or email
cscis111-staff@lists.fas.harvard.edu
50 points total
If you haven’t already created a folder named s111
for your
work in this course, follow these
instructions to do so.
Then create a subfolder called ps3
within your s111
folder,
and put all of the files for this assignment in that folder.
The problems from Part I will all be completed in a single PDF file. To create it, you should do the following:
Access the template that we have created by clicking on this link and signing into your Google account as needed.
When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.
Select File->Rename, and change the name of the file to
ps3_partI
.
Add your work for the problems from Part I to this file.
Once you have completed all of these problems, choose
File->Download->PDF document, and save the PDF file on your
machine. The resulting PDF file (ps3_partI.pdf
) is the one that
you will submit. See the submission guidelines at the end of Part I.
14 points total; individual-only
(5 points) Consider the following program:
public class MethodAdventure { public static int callMe(int a, int b) { b -= 2; a = a - b; System.out.println(a + " " + b); return a; } public static void main(String[] args) { int a = 5; int b = 4; System.out.println(a + " " + b); a = callMe(a, b); System.out.println(a + " " + b); b = 2 * callMe(b, a) + 1; System.out.println(a + " " + b); System.out.println(callMe(a, a)); System.out.println(a + " " + b); } }
In section 1-1 of ps3_partI
(see above), we have given you
tables that you should complete to illustrate the execution of the
program.
We have started the first and third tables for you. You should:
You may not need all of the rows provided in the tables.
(4 points) Fill in the blanks below to create a method that
takes as parameters two integers a
and b
,
and returns the average of the two values.
Make the result as precise as possible, and add the completed
method to ps3_partI
.
public static ______ average(______________________) { double avg = ___________________; __________________; }
For example:
average(3, 6)
should return 4.5
average(10, 20)
should return 15.0
(5 points) Write a method named printPattern
that takes a
single character ch
and two integers representing a number of
rows (numRows
) and number of columns (numCols
) and prints a
pattern consisting of numRows
, where each row is numCols
copies of the character ch
.
For example, the method call printPattern('#', 2, 4);
should
print
#### ####
and the method call printPattern('@', 4, 3);
should print
@@@ @@@ @@@ @@@
This method should not return a value.
Testing Your Methods in VS Code We encourage you to use VS Code to test your methods for parts 2 and 3 above. Here are the steps:
If you haven’t already done so,
create a folder named ps3
for your work on this assignment.
Download the following file: Problem1Test.java
Make sure to put the file in your ps3
folder. If your
browser doesn’t allow you to specify where the file should be
saved, try right-clicking on the link above and choosing
Save as... or Save link as..., which should produce a
dialog box that allows you to choose the correct folder for
the file.
In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment. (Note: You must open the folder; it is not sufficient to simply open the file.)
The name of the folder should appear in the Explorer pane on
the left-hand side of the VS Code window, along with the name
of the Problem1Test.java
file that you downloaded above.
Click on the name Problem1Test.java
, which will open an editor
window for that file.
Put your methods inside the provided class. We have given you some
code in the main
method for testing your methods, but we encourage
you to add additional tests as well.
Run your program and make sure that the tests produce the expected results.
Once you are happy with your methods, put them in your
ps3_partI
file. Do not submit the Problem1Test.java
file.
String
objects11 points total; individual-only
String str1 = "Write a string"; String str2 = "about spring";
(3 points) Given the above statements, state the result of evaluating each of the following expressions (including the quotes, if any).
str1.substring(1, 3)
str1.indexOf('i')
str2.indexOf(' ')
str2.toUpperCase().substring(2, 10)
str1.toLowerCase().charAt(0) + str2.substring(0, 3)
str2.substring(8) + str1.substring(10).toUpperCase()
(6 points) Now let’s go in the reverse direction. For each of
the following values, construct an expression involving
operations on str1
and/or str2
that evaluates to that value.
For full credit, you should not use literal values (e.g., "T"
or
'e'
) unless it is absolutely necessary to do so.
"Write a string about spring"
(you will need to explicitly add a space)"a str"
"write SPRING"
'e'
(note the single quotes)13
"Wrote a strong"
replace
method in the String
class;String
class to figure out how to use it)(2 points) Consider the following method, which takes a
String
object as its only parameter:
public static void processString(String str) { for (int i = 0; i < str.length(); i++) { System.out.print(str.charAt(i) + " "); } System.out.println(); }
Describe briefly (in clear, non-technical terms) what this method does.
6 points total; individual-only
Consider the following code fragment:
Scanner scan = new Scanner(System.in); System.out.print("Enter three numbers: "); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); if (a <= b) { if (b > c) { System.out.println("diamond"); } else { System.out.println("ruby"); } System.out.println("pearl"); } else if (b >= c) { if (a < b) { System.out.println("copper"); } else if (b == c) { System.out.println("bronze"); } System.out.println("silver"); if (a < c) { System.out.println("gold"); } } else { System.out.println("penny"); if (a == b) { System.out.println("dime"); } else { System.out.println("nickel"); } } System.out.println("done");
(3 points) State the output that would result from each of the following sets of inputs:
3 3 3
3 4 5
3 5 2
5 4 3
5 4 7
4 4 3
(3 points) At least one of the println statements in the above code fragment will not be executed for any set of inputs. Identify the statement(s) and explain why it/they will never be executed.
6 points; individual-only
Write a static method called processIntegers
that takes two integers
x
and y
as parameters and does the following: (i) prints the
integers from x
to y
, separated by commas (or, more precisely, by
a comma and a space), and (ii) prints the total of those integers.
For example, processIntegers(6, 10)
should print
6, 7, 8, 9, 10 total = 40
The method should print nothing if the parameter x
is greater than
the parameter y
. If x
and y
have the same value, it should print
that value, along with a total that is equal to that value.
Notes:
if
statement somewhere in your code.7 points total; individual-only
(3 points) For each of the following indefinite loops, state how many iterations it will perform (i.e., how many times its body will be executed). If it is an infinite loop or if it performs no iterations, say so.
int x = 2; while (x < 16) { System.out.println(x); x += 2; }
int num = 5; while (num != 0) { num = num - 2; }
int num = 2; do { num--; } while (num > 2);
int num = 2; while (num > 2) { num--; }
int a = 12; int b = 4; do { a -= b; b--; } while (a > 2);
int x = 200; while (x > 1) { x = x / 2; }
(4 points) The following method prints the even numbers from
a
to b
:
public static void printEvens(int a, int b) { for (int i = a; i <= b; i++) { if (i % 2 == 0) { System.out.print(i + " "); } } System.out.println(); }
For example, printEvens(4, 11)
would print
4 6 8 10
If a
is greater than b, or if a
is odd and equal to b
,
the method does not print anything. Rewrite this method so that it uses a
while
loop instead of a for
loop.
6 points total; individual-only
The following code fragment repeatedly reads a number from the user until a number less than 5 is entered.
Scanner console = new Scanner(System.in); int num; do { System.out.print("Enter a number: "); num = console.nextInt(); } while (num >= 5);
Math.sqrt
method. (1 point)The code fragment below uses a boolean flag named validInput
to keep track of whether a valid input has been entered by the
user. Fill in the blanks to make this fragment equivalent to the
original version (i.e., the loop should repeat until the user
enters a number that is less than 5). (2 points)
Scanner console = new Scanner(System.in); int num; boolean validInput = _________; while (!validInput) { System.out.print("Enter a number: "); num = console.nextInt(); validInput = _____________; }
Note: Each blank should be replaced with either:
a boolean literal (true
or false
)
a boolean expression (e.g., num == 8
).
Submit your ps3_partI.pdf
file by taking the following steps:
If you still need to create a PDF file, open your file on Google Drive, choose File->Download->PDF document, and save the PDF file on your machine.
Click on the name of the assignment in the list of assignments on Gradescope. You should see a pop-up window labeled Submit Assignment. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Choose the Submit PDF option, and then click the Select PDF button and find the PDF file that you created. Then click the Upload PDF button.
You should see a question outline along with thumbnails of the pages from your uploaded PDF. For each question in the outline:
As you do so, click on the magnifying glass icon for each page and doublecheck that the pages that you see contain the work that you want us to grade.
Once you have assigned pages to all of the problems in the question outline, click the Submit button in the lower-right corner of the window. You should see a box saying that your submission was successful.
Important
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
If you are unable to access Gradescope and there is enough
time to do so, wait an hour or two and then try again. If you
are unable to submit and it is close to the deadline, email
your homework before the deadline to
cscis111-staff@lists.fas.harvard.edu
50-60 points total
10 points; pair-optional
Imagine that you have been asked to implement a program that computes the shipping charge for a single item purchased from an e-commerce website. The charge will be based on the following characteristics:
The total shipping charge is the sum of the item charge for the item plus an appropriate base charge that depends on the type of shipping.
We have implemented a preliminary unstructured solution to this problem, in which all of the code is found in the main() method. Your task for this problem is to turn it into a well-structured program.
Getting started
If you haven’t already done so, create a folder named ps3
for your work on this assignment. You can find instructions for
doing so here.
Download the following file: ShippingCharge.java
Make sure to put the file in your ps3
folder. If your browser
doesn’t allow you to specify where the file should be saved, try
right-clicking on the link above and choosing Save as... or Save
link as..., which should produce a dialog box that allows you to
choose the correct folder for the file.
As needed, open your ps3
folder using the File->Open Folder
or File->Open menu option in VS Code.
The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the file that you downloaded in the previous step.
Click on the name ShippingCharge.java
, which will open an editor
window for that file.
Read over the unstructured code that we’ve given you. Make sure that you understand it.
Run the program several times to see how it behaves for different inputs.
Restructuring the program
Once you have reviewed and run the unstructured program, take the steps needed to turn it into a fully structured one. In particular, you should:
1) Locate the conditional code in the original version of the program, and factor out any code that is common to multiple cases.
2) Add static methods to capture the structure of the program and to eliminate code duplication.
You are required to have at least four useful static methods besides
main
. All of these methods should take one or more parameters, and
at least three of them should return a value. Some of your methods
can be used to get and return an input from the user (see the
getChoice
method in our example program), but at least two of your
methods should do something other than just get an input from the
user (see the brewedPrice
, lattePrice
, computeTax
, and
displayPrices
methods in our example program).
Avoid putting too much code in the main
method, and make sure that
you don’t repeat code unnecessarily. In the final version of the
program, the main
method should provide a concise summary of the
entire program, and the program should have the same behavior as
the original unstructured version.
You may find it helpful to consult the related case studies that we covered in lecture and section. In particular, here are three files that illustrate how the code in coffee-price case study went from unstructured to structured:
40-50 points; individual-only
In this problem, you will implement a version of the popular word game called Wordle that runs in the console.
If you haven’t played Wordle yet, we encourage you to give it a try before you proceed!
Rules of the game
In Wordle, the user is given 6 chances to guess a 5-letter English word.
After each guess, the user is given feedback about how close their guess
was to the “mystery” word (the word they are trying to guess).
In the online version of Wordle, feedback about a guess is given using colored tiles. Because our version runs in the console, we’ll take an approach that relies purely on text characters for feedback.
For example, imagine that the mystery word is "depth"
and that the
user guesses "heart"
. To provide feedback on that guess, the program
would print the following:
[h] e _ _ [t]
Note that:
The 'e'
from "heart
” is displayed without modification because
it appears in the mystery word ("depth"
) and its position
in "heart"
(position 1) is the correct one because it matches the
position of the 'e'
in "depth"
.
The 'h'
and 't'
from "heart"
are displayed surrounded by square
brackets because these characters appear in "depth"
, but their
positions in "heart"
are not the correct ones.
The a
and r
from "heart"
are each replaced with an
underscore character (_
) because 'a'
and 'r'
do not appear
anywhere in "depth"
.
More details about how to correctly process a guess are provided below.
Here’s an example of what a full run of the program will look like:
Welcome to Wordle! The mystery word is a 5-letter English word. You have 6 chances to guess it. guess 1: stain _ [t] _ [i] _ guess 2: light _ [i] _ _ t guess 3: merit _ [e] _ [i] t guess 4: edict e d i c t Congrats! You guessed it!
If you haven’t already done so, create a folder named ps3
for your work on this assignment. You can find instructions for
doing so here.
Download the following files:
Make sure to put all three files in your ps3
folder. If your
browser doesn’t allow you to specify where a file should be
saved, try right-clicking on its link above and choosing Save
as... or Save link as..., which should produce a dialog box
that allows you to choose the correct folder for the file.
In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment. (Note: You must open the folder; it is not sufficient to simply open the file.)
The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the names of the files that you downloaded in the previous step.
Click on the name Wordle.java
, which will open an editor
window for that file. You will see that we’ve given you the
beginnings of the program.
Read over the starter code in Wordle.java
.
Note: The other two files that we’ve given you (WordList.java
and words.txt
) will be used to select the random word that the
user needs to guess. You should not modify either of those files.
In this task, you will implement several static methods, each of which processes one of more strings. At least some of these methods will be useful as helper methods in your implementation of Wordle.
Here are the methods that you should add to Wordle.java
:
a static method called includes
that takes two parameters: an
arbitrary String
object s
followed by a single char
c
. The
method should return the boolean literal true
if c
is found
somewhere in s
, and it should return the boolean literal false
otherwise. For example:
includes("hello", 'e')
should return true
, because
'e'
is found in position 1
of "hello"
includes("hello", 'l')
should return true
, because
'l'
is found in positions 2
and 3
of "hello"
includes("goodbye", 'x')
should return false
, because
'x'
is not found in any position of "goodbye"
.
This method should not do any printing; rather, it should return the appropriate boolean value.
Hint: One of the built-in String
methods that we discussed in
lecture would be very helpful here!
Testing your methods
You should test each method after your write it. Here are the steps
you should take:
Download the following file: Tester.java
Make sure to put it in your ps3
folder. If your
browser doesn’t allow you to specify where a file should be saved,
try right-clicking on the link above and choosing Save
as... or Save link as..., which should produce a dialog box
that allows you to choose the correct folder for the file.
The name Tester.java
should appear in the Explorer pane in
VS Code, along with the other files in your ps3
folder.
Click on the name of the file to open an editor
window for it.
In the provided main
method, we’ve included the following
sample test call for the includes
method:
boolean result = Wordle.includes("hello", 'e'); System.out.println("includes(\"hello\", 'e') returns " + result);
Note: Because we are calling a static method from another
class, we need to prepend the class name (Wordle
).
Right click on Tester.java
and choose Run to compile and run
the program. If VS Code reports problems in Tester.java
, that
probably means that there is an issue in your implementation of
the includes
method. In particular, make sure that you have
the correct header. Make whatever changes are needed to your
methods to get Tester.java
to compile and run.
Check to make sure that the provided test produces the correct result.
Add some additional test calls to ensure that your
implementation of includes
works correctly in all cases.
a static method called isAlpha
that takes an arbitrary String
object s
as its only parameter and returns true
if all of
the characters in s
are letters of the alphabet, and returns
false
otherwise. For example:
isAlpha("Hello")
should return true
because
all of the characters in "Hello"
are letters of the alphabet
isAlpha("Goodbye!")
should return false
because "Goodbye!"
includes a character ('!'
) that is not a letter of the
alphabet.
This method should not do any printing; rather, it should return the appropriate boolean value.
Hints:
To test if a character c
is a letter of the alphabet, you
may use a built-in method from the Character
class. The call
Character.isAlphabetic(c)
will return true
if c
is a letter
of the alphabet and false
otherwise.
You will need to process the string s
one character at a time.
The easiest way to do that is using a for
loop. For example,
here is a loop that prints the characters in the
string s
“vertically”, with each character on its own line:
for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); System.out.println(c); }
Test your method by adding test calls to your Tester
class as
described above.
a static method called numOccur
that takes two parameters: a
single char
c
followed by an arbitrary String
object
s
. The method should count and return the number of times that
c
occurs in s
. For example:
numOccur('l', "hello")
should return 2, because there are 2
occurrences of 'l'
in "hello"
numOccur('e', "hello")
should return 1, because there is 1
occurrence of 'e'
in "hello"
numOccur('x', "goodbye")
should return 0, because there are no
occurrences of 'x'
in "goodbye"
.
This method should not do any printing; rather, it should return the appropriate integer.
Test your method by adding test calls to your Tester
class as
described above.
a static method called numInSamePosn
that takes three
parameters: a single char
c
followed by two String
objects
s1
and s2
that you may assume have the same length. The
method should count and return the number of times that c
occurs in the same position in both s1
and s2
. For
example:
numInSamePosn('p', "apple", "maple")
should return 1,
because there is one position (position 2) at which
'p'
occurs in both "apple"
and "maple"
.
numInSamePosn('a', "apple", "maple")
should return 0,
because there are no positions at which 'a'
occurs
in both "apple"
and "maple"
. Note that the letter 'a'
occurs in both strings, but not at the same position.
numInSamePosn('a', "java", "mama")
should return 2,
because there are two position (positions 1 and 3) at which
'a'
occurs in both "java"
and "mama"
.
This method should not do any printing; rather, it should return the appropriate integer.
Test your method by adding test calls to your Tester
class as
described above.
In Wordle.java
, we have included an incomplete version of a method
called isValidGuess
that takes an arbitrary String
object guess
as its only parameter and returns a boolean
value.
The current version of the method always returns true
. Rewrite it so
that it tests the input string guess
and returns true
if it is a
valid guess for Wordle and false
otherwise. If guess
is not a
valid Wordle guess, you should also print an appropriate error
message (see below) before returning.
To be a valid guess in our version of Wordle, a string must:
If guess
does not have the correct length, you should print
the message:
Your guess must be 5 letters long.
and return false
If guess
has the correct length but includes characters that are not
letters of the alphabet, you should print the message:
Your guess must only contain letters of the alphabet.
and return false
.
If guess
is a string of exactly 5 letters, you should return true
without printing anything.
For example:
isValidGuess("hello")
should return true
isValidGuess("hi")
should print the first error message above
and return false
isValidGuess("what?")
should print the second error message above
and return false
isValidGuess("abcde")
should return true
. (Note: In the actual
online version of Wordle, "abcde"
would not be considered valid
because it is not one of the words in Wordle’s word list. In our
version of the game, any five-letter string is considered a valid
guess, regardless of whether it is an English word.)
Important: When implementing this method, you should take advantage of one of the helper methods that you implemented in Task 1.
Testing isValidGuess
As before, you can test this method by adding test calls to your
Tester
class.
Another option is to make calls to a separate static method
called readGuess
that we have provided in the Wordle
class, since
readGuess
reads a guess from the user and calls isValidGuess
to
determine if the guess is valid.
readGuess
takes two parameters: an integer that specifies the number
of the guess that is being read, and a Scanner
object that will be
used to read the guess. In Tester.java
, we have created a Scanner
object called console
that you can use for this purpose.
Here is one possible user interaction that should result from calling
Wordle.readGuess(1, console)
in Tester.java
and entering the
guesses that are underlined below:
guess 1: go Your guess must be 5 letters long. guess 1: what? Your guess must only contain letters of the alphabet. guess 1: hi! Your guess must be 5 letters long. guess 1: apple
In Wordle.java
, implement a method called processGuess
that takes
two parameters: a 5-character String
object guess
representing a
guess made by the user followed by a 5-character String
object mystery
representing the “mystery” word that the user is trying to guess.
The method should process the input string guess
and:
provide feedback to the user about how guess
compares to mystery
by printing the appropriate sequence of characters (see below)
return true
if guess
is equal to mystery
and false
otherwise.
Rules for providing feedback
The method should process the string guess
one character at a time
parameter and print the appropriate output for each character:
If the character at a given position of guess
matches the character
at the corresponding position in mystery
, you should print
the character itself without modification.
If the character at a given position of guess
does not match the
character at the corresponding position of mystery
but it is
needed elsewhere to get the word specified by mystery
, you
should print it surrounded by square brackets (e.g., `”[a]”). See
below for more detail about what it means for a character to be
needed elsewhere!
If the character is not needed elsewhere to get mystery
, you
should print an underscore character (_
).
For example, the call processGuess("heart", "depth")
should print the
following
[h] e _ _ [t]
and return false
. Note that the 'e'
in "heart"
matches the
'e'
in the corresponding position of "depth"
, the 'h'
and the
't'
are needed elsewhere in the mystery word, and the 'a'
and 'r'
are not needed elsewhere.
In addition, the method should:
Begin by printing two spaces so that the feedback string is properly indented.
Print a single space after whatever it prints for each character.
Print a newline at the end of the entire feedback string. An easy
way to do this is to use an “empty” println
statement:
System.out.println();
Return the appropriate boolean value as described above.
Initial version of the method
To begin, you should implement a simplified version of processGuess
in which a character in the guess is “needed elsewhere” whenever
(1) it does not match the character in the corresponding position of
the mystery word, and (2) it appears somewhere in the mystery word.
Such a simplified version won’t be fully correct in all cases (see Task 5 below), but it will be good enough for cases in which the user’s guess never includes two or more occurrences of the same character.
Here are some other test cases for this initial version of the method:
processGuess("stain", "edict")
should print:
_ [t] _ [i] _
and return false
.
processGuess("light", "edict")
should print:
_ [i] _ _ t
and return false
.
processGuess("merit", "edict")
should print:
_ [e] _ [i] t
and return false
.
processGuess("edict", "edict")
should print:
e d i c t
and return true
.
Note: You should be able to take advantage of at least one of the helper methods that you implemented in Task 1.
Test your processGuess
method by adding test calls to your
Tester
class.
main
methodAt the bottom of Wordle.java
, we have given you the beginnings of the
main
method. The code that we have provided:
creates a Scanner
that will be used to read the user’s inputs
calls the separate method printWelcome
to print a welcome message
for the user
creates an object of type WordList
that will represent the
collection of possible 5-letter words that the user could be asked to
guess
makes a call to a method called getRandomWord
in the WordList
object to choose the mystery word, and assigns it to a variable
called mystery
.
Add the code needed to obtain and process each of the user’s guesses.
You should use a loop that repeatedly gets a single guess and
processes it, continuing until the user guesses the mystery word or
until they have made 6 guesses, whichever comes first. Make sure
to take advantage of the existing methods – in particular,
readGuess
and processGuess
.
Then, after the loop has ended, you should print an appropriate closing message:
If the user successfully guessed the mystery word, you should print the following:
Congrats! You guessed it!
If the user made 6 guesses without guessing the mystery word, you should print a message with the following format:
Sorry! Better luck next time! The word was _____.
where you replace the blank line with the mystery word.
Testing the full program
Once you have completed the main
method, you are ready to try
playing the game!
When running the program, you must highlight Wordle.java
in the
list of files before using F5 to run the program. Another option is
to right-click on the name of the file in the Explorer pane and choose
Run or Run Java.
To get a repeatable mystery word for testing and debugging, you can specify what is known as a seed for the random-number generator that we use to select the random word. Here is one way to do so:
As needed, open the folder containing your code by using the File->Open Folder or File->Open menu option in VS Code.
If you don’t already have a Terminal pane at the bottom of the VS Code window, use the Terminal->New Terminal menu option to open one.
Enter the following command from the Terminal to compile your code:
javac Wordle.java
If executing this command produces error messages describing bugs in your code, fix them and try again. Repeat this process until your code compiles without any error messages.
Enter the following command from the Terminal to run your code with a random seed:
java Wordle seed
where you replace seed
with an integer. Here are some seeds and
their corresponding mystery words:
10: pearl 40: edict 60: enact 70: nippy 90: nutty 100: depth 112: towel
processGuess
This task is required for grad-credit students. It may be completed by other students for partial extra credit.
In this task, you will try to improve your processGuess
method
so that it will correctly handle cases in which a character appears
two or more times in the user’s guess.
Before proceeding, we recommend saving a copy of your current
version of Wordle.java
in another folder so that you don’t lose
your current version of processGuess
.
When a character appears more than once in the user’s guess (e.g., if
the user guesses "loyal"
, which has two 'l'
s), it becomes trickier to
determine whether a given instance of the repeated character should be
considered as being “needed elsewhere.”
For example, consider the following examples:
processGuess("loyal", "towel")
"towel"
"loyal"
The first 'l'
in "loyal"
doesn’t match the corresponding
character in "towel"
and there is an 'l'
in the mystery
word. Therefore, the version of processGuess
from Task 3 would
print the following feedback in this case:
[l] o _ _ l
However, the first 'l'
in the user’s guess is not truly
needed elsewhere since the guess already has an 'l'
in the only
position where it is needed – at the very end of the
word. Therefore, an improved version of processGuess
should
print the following instead:
_ o _ _ l
processGuess("piper", "nippy")
"nippy"
"piper"
In this case, there are two 'p'
s in the mystery word, and the
user’s guess also has two 'p'
s: one in a correct position, and one
that is in an incorrect position but is needed elsewhere. Therefore,
an improved version of processGuess
should print:
[p] i p _ _
Note: The original version of processGuess
would actually
handle this case correctly. You should make sure that your improved
version continues to do so!
processGuess("nanny", "enact")
"enact"
"nanny"
In this case, there is only one 'n'
in the mystery word but the
user’s guess has three 'n'
s, none of which is in a correct
position. Because only one of the three 'n'
s is needed
elsewhere, the call processGuess("nanny", "enact")
should print:
[n] [a] _ _ _
Note that the first 'n'
is surrounded by brackets because it is
needed elsewhere. But because there is only one 'n'
in the mystery
word, the other two 'n'
s are replaced with underscore characters.
Make whatever changes are needed to your processGuess
method so that
a character that isn’t in the correct position is only printed with
brackets around it if it is truly needed elsewhere to form the mystery
word. Otherwise, you should print an underscore character instead.
It can be challenging to satisfy all of the possible cases, but taking advantage of the helper methods that you wrote in Task 1 should allow you to at least handle cases like the first two examples above.
Important
If you chose to work on Problem 7 with a partner, both you and your partner should submit your own copy of your joint work, along with your individual work on the other two problems.
You should submit only the following files:
ShippingCharge.java
Wordle.java
You do not need to submit WordList.java
or words.txt
.
Here are the steps:
Click on the name of the assignment in the list of assignments. You should see a pop-up window with a box labeled DRAG & DROP. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Add your files to the box labeled DRAG & DROP. You can either drag and drop the files from their folder into the box, or you can click on the box itself and browse for the files.
Click the Upload button.
You should see a box saying that your submission was successful.
Click the (x)
button to close that box.
The Autograder will perform some tests on your files. Once it is done, check the results to ensure that the tests were passed. If one or more of the tests did not pass, the name of that test will be in red, and there should be a message describing the failure. Based on those messages, make any necessary changes. Feel free to ask a staff member for help.
Note: You will not see a complete Autograder score when you submit. That is because additional tests will be run later, after the final deadline for the submission has passed. For such problems, it is important to realize that passing all of the initial tests does not necessarily mean that you will ultimately get full credit on the problem. You should always run your own tests to convince yourself that the logic of your solutions is correct.
If needed, use the Resubmit button at the bottom of the page to resubmit your work. Important: Every time that you make a submission, you should submit all of the files for that Gradescope assignment, even if some of them have not changed since your last submission.
Near the top of the page, click on the box labeled Code. Then click on the name of each file to view its contents. Check to make sure that you see the code that you want us to grade.
Important
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
If you are unable to access Gradescope and there is enough
time to do so, wait an hour or two and then try again. If you
are unable to submit and it is close to the deadline, email
your homework before the deadline to
cscis111-staff@lists.fas.harvard.edu
Last updated on July 8, 2025.