For problem 1, part 2, can our method print the perimeter?
No! It should return the perimeter. It should not do any printing.
For problem 1, part 2, I’m getting an error. It says “incompatible types: possible lossy conversion from double to int”.
This probably means that the return type that you specified in the method header is incorrect. Make sure that it reflects the type of the value that you are attempting to return.
For problem 2, part 2, problem e, why does it tell us to note the single quotes?
The single quotes indicate that the value is of type char, rather
than of type String. Make sure that the expression that you give
as an answer produces a char rather than a String.
What are you looking for in parts 1-4 of problem 6? Do we need to give you the revised code fragment for each part?
You should just give us the revised conditions. In other words, for
each part, with what condition would you replace the original
condition (num >= 50)? Don’t forget to use continuation conditions
(conditions that tell the loop whether to continue) rather than
termination conditions (conditions that tell the loop when to stop).
For problem 6, part 5, are we allowed to add lines of code (e.g., an
if statement)?
For full credit, you shouldn’t add any statements – you should just
fill in the blanks. Don’t forget that you can assign any boolean
expression to a boolean variable. One option would be a boolean
literal (true or false). In addition, you can assign an
expression that involves a boolean operator. For example, in the
notes, we do the following:
boolean isEven = (num % 2 == 0);
You may want to consider filling in at least one of the blanks with
a boolean expression involving num.
In my revised ShippingCharge program, I’m trying to add a method that obtains input from the user. Can you remind me how to do that?
The code that we’ve given you creates a Scanner object on
the first line of the main method, as required.
If you want to use the Scanner in another static method, you’ll
need to pass the object to that method as a parameter. For example,
here’s a simple method that takes a Scanner object as a parameter
and uses it to read an integer from the user:
public static int myMethod(Scanner console) {
int result = console.nextInt();
return result;
}
See our example program for other
examples of methods that take a Scanner as a parameter. Note that
it would also be possible for a method to take to take additional
parameters if necessary. For example, here is a method that takes
two parameters, a Scanner and an int:
public static int getVal(Scanner console, int valType) {
if (valType == 1) {
return 3;
} else {
int num = console.nextInt();
return 5 * num;
}
}
Make sure that your revised program does not create more than
one Scanner object.
In my revised ShippingCharge program, the compiler is giving me an error message saying that it cannot find one of my variables. Any suggestions?
Make sure that you declare the variable in a location that gives it a large enough scope.
For example, here is some code that does not declare the variable
val properly:
int num = console.nextInt();
if (num < 5) {
int val = 10;
} else {
val = 15;
}
Note that the variable val is declared inside the block belonging
to the if, and thus its scope is limited to that block. As a
result, it is not available when we try to use it in the block
belonging to the else.
To fix this code, we need to declare val before the if
statement, as follows:
int num = console.nextInt();
int val;
if (num < 5) {
val = 10;
} else {
val = 15;
}
In my revised ShippingCharge program, the compiler is giving me an error message saying that one of my variables might not have been initialized. I’ve tried changing the scope of the variable, but nothing seems to work. Any suggestions?
This error can result if you initialize a variable inside an if or
else block, and then evaluate that variable somewhere else. For
example:
int var;
if (some condition) {
var = 5;
}
System.out.println(var);
The compiler is pointing out that if the condition is not true, then the variable will not be assigned a value – and thus the println statement will fail.
In order to eliminate this error, you need to make whatever changes are necessary to ensure that the variable will always be assigned a value before it is evaluated. For instance, we could fix the above example in one of two ways:
assign an initial, default value when the variable is declared:
int var = 3;
if (some condition) {
var = 5;
}
System.out.println(var);
add an else clause that assigns a value to var when the
condition is not true:
int var;
if (some condition) {
var = 5;
} else {
var = 3;
}
System.out.println(var);
In my revised ShippingCharge program, the compiler is giving me an error message saying that one of my methods is missing a return statement. It looks to me like I have all of the return statements I need. What am I doing wrong?
This error can result if all of your return statements are in if
or if-else blocks. For example:
public static int getVal(int valType) {
if (valType == 1) {
return 3;
} else if (valType == 2) {
return 5;
}
}
The compiler will complain, because if neither (valType == 1) nor
(valType == 2) is true, then the method will not return a value.
In order to eliminate this error, you should make sure that one of
your return statements is outside an if or if-else blocks. In
the example above, changing the else if to an else would do the
trick:
public static int getVal(int valType) {
if (valType == 1) {
return 3;
} else {
return 5;
}
}
In my revised ShippingCharge program, I’ve written a method that uses
a Scanner to get a value from the user. I need that value in two
different places, and therefore I call the method in both places.
However, when I run the program, it ends up asking the user to enter
the value twice. How can I avoid that?
You need to make sure that this method is not called twice in a given run of the program. (It’s okay for there to be multiple calls to this method in your code, but only one of those calls should actually occur in a given execution of the program.)
When you write a method to get a value from the user, the method
should return that value, and any line of code that calls the method
should store the return value in a variable. For example, in our
example program, our getChoice
method gets the user’s choice and returns it. In the main method,
we call this method, and we store the return value in a variable:
int choice = getChoice(console);
In this case, we don’t need the choice in any method other than the
main method. However, if we did need the choice in one or more
other methods, we would pass it into those methods – just as we
pass the value of the variable price into the computeTax method
in our example program:
double tax = computeTax(console, price);
In my revised ShippingCharge program, I’ve written a method that
returns a value. However, that value doesn’t seem to be available in
the main method after the method returns. Any suggestions?
Make sure that you aren’t throwing away the return value. For
example, here is an incorrect version of a fragment form the main
method in our coffee-price program:
double price = 0.0;
if (choice == BREWED) {
brewedPrice(console); // XXX: incorrect!
} else if (choice == LATTE) {
lattePrice(console); // XXX: incorrect!
}
Note that the method calls to brewedPrice and lattePrice throw
away their return values, because they don’t use them in any way.
Here is the correct version:
double price = 0.0;
if (choice == BREWED) {
price = brewedPrice(console);
} else if (choice == LATTE) {
price = lattePrice(console);
}
In this version, the return value of each method call replaces the
method call and is assigned to the variable price, so that the
value can be used later on in the main method.
You should do something similar when you call a method that returns a value.
(By the way, it’s worth noting that although the lattePrice method
uses a variable called price to store the correct price, this is a
completely separate variable from the variable price in the main
method. Therefore, we still need to have the method return the
price, and we need to use that return value in some way in the
main method.)
In one of my methods for Task 1 of Problem 8, I’m getting
a StringIndexOutOfBoundsException. What am I doing wrong?
Don’t forget that the characters in a string have index values that go from 0 to length - 1, where length is the number of characters in the string. The exception means that your code is using an index from outside that range.
Can the methods that we write for Problem 8 read values from the console?
No! You should write your methods so that the necessary values are passed in as parameters.
In Task 3 of Problem 8, my processGuess method returns the correct
boolean value (true or false) when I test it in my Tester
class, but when I test it in the context of the larger program,
it always returns false. Why might this be happening?
One possible explanation is that you may be trying to compare
strings using the == operator. This won’t work, because strings are
objects, and the == operator only compares the memory addresses
of objects. To compare the internals of two objects, you need
to use a method (the equals method) to compare them.
Using == can work when you test your method from your Tester
class if your test calls are using string literals. For example,
imagine that we have the following test call:
boolean result = Wordle.processGuess("edict", "edict");
Because we have two literals for the same word, the Java
interpreter uses a single String object for both of them.
As a result, both parameters of processGuess end up referring to
the same object:
+-----------------+ | +-----+ | | guess | ------------>[ String object for "edict" ] | +-----+ | ^ | | / | +-----+ | / | mystery | ----------/ | +-----+ | +-----------------+
And when we use == to compare guess and mystery in this
case, we get a value of true because both variables hold the
same memory address.
However, when you call processGuess from your Wordle program,
the strings involved are not string literals that you are
hard-coding into your program. Rather, one of the strings (the
mystery word) is obtained from the WordList that we have
provided. The other string (the guess) is obtained from the
user. As a result, even if the two strings happen to represent
the same word, there are two separate String objects in
memory:
+-----------------+ | +-----+ | | guess | ------------>[ String object for "edict" ] | +-----+ | | | | +-----+ | | mystery | ------------>[ String object for "edict" ] | +-----+ | +-----------------+
And when we use == to compare guess and mystery in this
case, we get a value of false because the variables hold two
different memory addresses.
In order to test if two strings are equivalent, we need to use the
equals method instead of ==, since it will compare the
internals of the two strings.
Part of my program doesn’t do what it’s supposed to do, and I can’t figure out why. Any suggestions?
You need to perform some debugging! In other words, you need to step through your program to see how the values of your variables are changing and where your logic errors are.
There are at least two ways to do this:
When I try to run my code, I get an error saying that it can’t find one of the other classes – something that looks like this:
Exception in thread "main" java.lang.NoClassDefFoundError: WordList
Why is this happening?
This probably means that you haven’t properly followed the instructions in the Getting started section at the beginning of Problem 8.
In particular, make sure that all of the files are in the same folder, and that you use the File->Open Folder menu option in VSCodium to open the folder containing your files. Note that you MUST open the folder and not just open the individual files.
To make sure things are correctly configured, we recommend that you do the following:
Use File->Close Folder or File->Close Editor to close any open files in VSCodium.
Shut down the VSCodium program.
Make sure that all of your files are in the same folder.
Start up VSCodium again.
Use File->Open Folder to open the folder containing your files.
As needed, click on the name of the file that you want to edit from the list of files in the Explorer Pane on the left-hand side of VSCodium.
Some other things to check:
Make sure that your files do NOT include a package statement at the top. For example, if you see something like this:
package ps3; // remove this!
at the top of a file, you should remove it.
Make sure that the only import statements at the top of your
file are for java.util.*. All other import statements
should be removed.
When I submit my code on Gradescope, it tells me that I’m
failing a test for Task 4 because the program throws an exception
of type java.util.NoSuchElementException from within a Scanner
method that is being called by readGuess. Why is this happening?
This probably means that your main method is doing one or both
of the following:
making too many calls to readGuess. Once the user has
guessed the mystery word, you should NOT continue to call
readGuess.
closing the Scanner object (the one that we create at the
start of main and pass into readGuess) too soon. It should
only be closed at the very end of the program, after all calls
to readGuess have been made.
For Task 4, I have written a loop that performs 6 repetitions. How can I end the loop early if the user guesses the mystery word in fewer than 6 guesses?
One option is to use a break statement to break out of the loop
early when the user guesses the mystery word.
Another option is to use an indefinite loop (e.g., a while loop)
whose condition includes a boolean variable that keeps track of
whether the user has guessed the mystery word.
Sometimes when I run the program, the program behaves unexpectedly – e.g., telling me that my guess is invalid before I even enter a guess, or repeatedly asking for a new guess without giving me the chance to enter anything. Why is this happening?
This can happen if you try to run the program when you are still in the middle of a previous run. Either complete that previous run or use Ctrl-C to end it. Then, once the previous run is completed, you should be able to start a new run of the program and have it behave as expected.
I’m using the random-seed option for testing that you suggest at the end of Task 4. After performing a given run, I realized that I needed to make some changes to my code. However, after I made those changes and tried to rerun the program using the same random seed, the program didn’t seem to take into account the changes that I made to the code. Why is this happening.
Make sure that you save and re-compile your code after every change that you make:
javac Wordle.java
Then, enter a second command to run the program with a particular random seed. For example, to use a random seed of 10, you would do:
java Wordle 10
After you make any changes to your code, you must reenter both of these commands from the Terminal to compile your modified code and run it. If you only enter the second command (the one needed to run the program), you will still be running the old version of your code!
In Task 5, I’m having trouble getting processGuess to handle
all of the possible cases. Any suggestions?
This is a challenging problem, and you shouldn’t worry if you can’t get it to work correctly in all cases.
Try tracing through concrete cases on paper and think about how you can tell whether a given character that isn’t in the correct position is truly needed elsewhere to form the mystery word. Taking advantage of one or more of the helper methods that you wrote in Task 1 should help with this.
One important thing to check: Make sure that any changes that you make
as part of Task 5 don’t cause your processGuess method to fail
the simpler test cases from Task 3 that the original version of
processGuess was designed to handle! Task 3 is worth more
points than Task 5, so it’s more important that you are able to
correctly handle those test cases.
Last updated on July 2, 2026.