I’m having trouble with problem 2.4. Can you give us any additional hints?
As the hint for the problem states, your expression for part 4 should only need two arithmetic operators (+, -, *, /, or %). Think about how we’ve used these operators in other contexts, and see if you can combine them to give the desired result.
Note that you are also welcome to use an expression with more than two arithmetic operators, but it is possible to come up with an expression that only uses two.
When I try to run a program, I get a message that says “Cannot find a class with the main method.” Why is this happening?
Make sure that your program includes a main
method with the
required header. If there are any syntax errors in the header of
the main
method, the compiler will give you this message.
When I try to run a program, I get a message that says “Build failed, do you want to continue?” Why does this happen?
There are two types of cases that can produce this message:
If you see problems listed for the program that you’re trying to run, you need to fix them before proceeding. Click Cancel, fix the problems, and then try to rerun the program.
Note that clicking on the description of a given problem will typically highlight the place in your code that is causing the problem.
If there are no problems listed for the program you are trying to run, the message probably stems from the fact that there are problems in another file that it is in the same folder. In that case, you should be able to click the Proceed button to run your program.
I’m a bit confused about the starter code that you’ve given us for Problems 8, 9, and 11. Can you explain it?
In the starter code for these problems, the main
method begins
with lines of code that read in the necessary values from the user
and assign them to the appropriate variables. You do not need to
understand how this code works. You just need to realize that
after these lines have been executed, the variables will have been
assigned the values that the user enters. You should then use these
variables to perform the task(s) specified in the problem.
Notes:
You should not change the lines of code that we’ve given you in any way.
The code that you write should go within the existing main
method, just below the large block comment that summarizes
what you need to do.
When I run one of programs for Part II, the program prints the correct prompt and waits for me to enter a value, but I don’t seem to be able to enter the value. What am I doing wrong?
You may need to click on the Terminal area at the bottom of VS Code in order to enter a value that the program is waiting for you to input.
For one of the problems from Part II, the results that I’m getting are not correct. Do you have any suggestions?
Make sure that you are making your computations as precise as possible. See the implementation guidelines in the assignment handout for more detail. Also, don’t forget that there are two types of division – integer division and floating-point division. Make sure that you use the correct type of division for a given problem.
See the next question for a related issue.
For part II, some of my results seem to be off by a very small amount. For example, on problem 9, when I enter leg values of 20 and 17, the final number in the output should be 26.0, but I get 26.000000000000004. Do I need to worry about this type of thing?
No. If the difference between the expected and computed values is very small, you don’t need to worry about it. The floating-point representation used for real numbers is naturally imprecise, and there’s no easy way around that imprecision.
When we implement the formulas that you’ve given us, is it okay to use different variables?
Yes! The formulas do not need to be implemented exactly as given in the assignments. Make whatever changes to the formula are needed to compute the length as precisely and accurately as possible.
Is it possible that some of the components of the figure will not require nested loops?
Yes. If a given component is always one line in height –
regardless of the value of the scale factor – you may not need
nested loops to represent it. One example of this in our
DrawTorch
program is the rim of the torch handle, which is
always one line in height. See the drawRim()
method for the
actual code.
However, multi-line components of the figure (e.g., the flame or
handle of the torch) must be implemented using nested loops –
either explicitly, or by calling the printChars
method. Don’t
forget that at least one component of the figure must explicitly
use nested loops.
What about cases in which we need to print a single copy of a character on a given line? Do we need a loop for that?
First, make sure that you only need one copy of that character for all sizes of the figure (i.e., for all values of the scale factor).
For example, in all sizes of the handle in DrawTorch
, there is only
one vertical bar along the edges of the handle. That means that you do
not need to use a loop to print a given vertical bar.
However, if there is more than one occurrence of the character at larger sizes, you should use a loop to print that character – even at the default size. Doing so will make it easier for you to convert your default-size version to your scaleable version.
I’m trying to use a nested loop to draw a portion of the figure in which the number of occurrences of a given character changes from line to line. I can’t seem to get it to work. Do you have any hints?
If the number of occurrences changes from line to line, make sure that you have developed a formula for how the number of occurrences is related to the line number, and that you are using that formula in the header of the nested loop. See the notes – and question 6 of Part I – for examples of how to use a table to develop the necessary formula.
Also, don’t forget that your nested loops should be simple repetition loops that follow this template (or a similar one):
for (int i = 0; i < N; i++) { // line or line(s) to repeat }
where you replace N with the number of repetitions – which may be a formula based on the line number.
To what degree should we be using procedural decomposition?
You should use it:
to capture the structure of your program. See our DrawTorch
and DrawTorch2
programs for examples of this. They are
available in their entirety in the examples associated with the
lecture notes on methods on the course website.
to avoid duplication of larger blocks of code. For example, if two portions of the figure are identical, you should use a single method for both of them.
I’m having trouble figuring out how to use the scale-factor constant to make my existing code scaleable. Do you have any suggestions?
You can use tables here as well. For example, consider our DrawTorch program. When its scale factor is 2, the top of the torch handle looks like this:
|::::::| |::::|
We saw in lecture that the formula for the number of colons on a given line is
colons = -2*line + 8
When the scale factor is 3, the top of the torch handle looks like this:
|::::::::::| |::::::::| |::::::|
If we determined the formula for the number of colons at this size (using the same approach taken in lecture), we would get the following formula:
colons = -2*line + 12
Given these two formulas, we can determine how the various numbers in the formulas depend on the value of the scale factor. First, we notice that both formulas multiply the line number by -2. This means that the -2 does not depend on the scale factor. The other number in the formula does depend on the scale factor, and we can determine how by using the following table:
scale-factor second number in formula 2 8 3 12
Using the same procedure shown in lecture, we can see that the first number in the formula is 4 times the scale factor. Thus, the general formula for the number of colons is:
colons = -2*line + 4*SCALE_FACTOR
and thus we would replace -2*line + 8
in DrawTorch
with
-2*line + 4*SCALE_FACTOR
in DrawTorch2
.
See the notes for other examples of incorporating the scale factor.
Can you give an example of how Newton’s algorithm works?
To compute the square root of a number x, Newton’s algorithm begins with some estimate of the square root. We’ve asked you to use x/2 for this original estimate. Newton’s algorithm then repeatedly improves the estimate by replacing the current estimate - call it est - with the value that is obtained by averaging est with x / est. (See the assignment for the formula.)
For example, let’s say that we’re computing the square root of
10. That means that x is 10, and our initial value for est
would be 10/2 = 5. Then we would begin the process of gradually
improving the estimate – using a for
loop to make the requested
number of improvements.
The first improvement would compute
5 + 10 ---- 5 5 + 2 7 --------- = ----- = --- = 3.5 2 2 2
and so we would update est to be 3.5 and print that new value.
The second improvement would compute
3.5 + 10 ---- 3.5 ----------- = 3.178571428571429 2
and so we would update est to be 3.178571428571429 and print that new value.
This process would continue until we have made the number of improvements specified by the user.
Last updated on June 27, 2025.