You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code below is a simple 20 questions game that guesses an animal. In Runestone, you need to provide the input below the code before you run it. It is set to answer the questions with y and y. Run the code to see the result. Change the input to guess the other animals. It can only guess 3 animals. Let's add more!
626
+
The code below is a simple 20 questions game that guesses an animal. In Runestone, you need to provide the input below the code before you run it. It is set to answer the questions with y and y. Run the code to see the result. Change the input to guess the other animals. It can only guess 3 animals. Let's add more! Add a question and if/else statement on line 20 to distinguish a cat and a dog and on line 31 to distinguish a turtle and a bird. Change the input below the code to test your new questions and answers.
627
627
~~~~
628
628
import java.util.Scanner;
629
629
@@ -638,28 +638,35 @@ The Animal Guessing program that we will make uses the following decision tree:
638
638
String answer = scan.next(); // in other IDEs, use nextLine()
639
639
if (answer.equals("y"))
640
640
{
641
-
System.out.println("Is it a pet (y/n)?");
642
-
answer = scan.next();
643
-
if (answer.equals("y"))
644
-
{
645
-
System.out.println("I guess a dog! Click on run to play again.");
646
-
}
647
-
else
648
-
{
649
-
650
-
System.out.println("I guess an elephant! Click on run to play again.");
651
-
}
641
+
System.out.println("Is it a pet (y/n)?");
642
+
answer = scan.next();
643
+
if (answer.equals("y"))
644
+
{
645
+
// Uncomment the question and answer code below.
646
+
// Add in your question to distinguish cat vs dog
647
+
// System.out.println("change this question");
648
+
// answer = scan.next();
649
+
// Add another if/else to guess a cat vs dog
650
+
651
+
System.out.println("I guess a dog! Click on run to play again.");
652
+
}
653
+
else
654
+
{
655
+
System.out.println("I guess an elephant! Click on run to play again.");
656
+
}
652
657
}
653
658
else { // not a mammal
654
-
659
+
// Uncomment the question and answer code below.
660
+
// Add in your question to distinguish turtle vs bird
661
+
// System.out.println("change this question");
662
+
// answer = scan.next();
663
+
// Add another if/else to guess a turtle vs bird
664
+
655
665
System.out.println("I guess a bird! Click on run to play again.");
Copy file name to clipboardExpand all lines: _sources/Unit4-Iteration/topic-4-5-loop-analysis.rst
+94-2Lines changed: 94 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ Here is a complex loop. See if you can trace the code on paper by making a trac
75
75
Did your trace table look like the following?
76
76
77
77
.. figure:: Figures/whileLoopTrace.png
78
-
:width:150px
78
+
:width:300px
79
79
:align:center
80
80
:figclass:align-center
81
81
@@ -206,7 +206,7 @@ In the code above the largest value that allows the loop to run is 6 (which is t
206
206
:language: java
207
207
:autograde: unittest
208
208
209
-
How many stars are printed out by the following loops? How many times do the loops run? Calculate on paper before you run the code.
209
+
How many stars are printed out by the following loops? How many times do the loops run? Calculate on paper before you run the code. Trace through it with the Code Lens button.
210
210
~~~~
211
211
public class NestedLoops
212
212
{
@@ -249,7 +249,99 @@ In the code above the largest value that allows the loop to run is 6 (which is t
249
249
250
250
For the example above, the outer loop executes 4 - 0 + 1 = 5 times and the inner 9 - 0 + 1 = 10 times so the total is 5 * 10 = 50.
251
251
252
+
Non-rectangular Nested Loops
253
+
-------------------------------
252
254
255
+
In the nested loops we have seen so far, the inner loop always runs a set number of times. However, nested loops can be **non-rectangular** where the number of times the inner loop runs is dependent on the outer loop's variable. Here is an example of a non-rectangular nested loop (notice the ``j <= i`` ending condition in the inner loop):
256
+
257
+
.. code-block:: java
258
+
259
+
for (int i =1; i <=4; i++)
260
+
{
261
+
for (int j =1; j <= i; j++)
262
+
{
263
+
System.out.print("*");
264
+
}
265
+
System.out.println();
266
+
}
267
+
268
+
This code will print a triangle of stars instead of a rectangle because the inner loop runs i times and prints 1 star when i=1, 2 stars when i=2, etc.
269
+
270
+
.. code-block:: java
271
+
272
+
*
273
+
**
274
+
***
275
+
****
276
+
277
+
278
+
|CodingEx| **Coding Exercise**
279
+
280
+
281
+
.. activecode:: triangle-stars
282
+
:language: java
283
+
:autograde: unittest
284
+
285
+
How many stars are printed out by the following non-rectangular loops? Trace through it with the Code Lens button. Then, can you change the code so that the triangle is upside down where the first row has 5 stars and the last row has 1 star? Hint: make the inner loop count backwards from i to 0.
286
+
~~~~
287
+
public class NestedLoops
288
+
{
289
+
290
+
public static void main(String[] args)
291
+
{
292
+
for (int row = 0; row < 5; row++)
293
+
{
294
+
// Change the inner loop to count backwards from i to 0
295
+
for (int col = 0; col <= row; col++)
296
+
{
297
+
System.out.print("*");
298
+
}
299
+
System.out.println();
300
+
}
301
+
}
302
+
}
303
+
====
304
+
import static org.junit.Assert.*;
305
+
306
+
import org.junit.*;
307
+
308
+
import java.io.*;
309
+
310
+
public class RunestoneTests extends CodeTestHelper
311
+
{
312
+
@Test
313
+
public void testMain() throws IOException
314
+
{
315
+
String output = getMethodOutput("main");
316
+
String expect = "*****\n****\n***\n**\n*\n";
317
+
boolean passed = getResults(expect, output, "Expected output from main");
How many stars are printed out? How many times do the loops iterate? The outer loop runs 5 times and the inner loop runs 0, 1, 2, 3, 4, 5 times respectively. So, the number of stars printed are 0 + 1 + 2 + 3 + 4 + 5 = 15. Notice that this is the sum of a series of natural numbers from 0 to 5.
329
+
330
+
There is a neat formula to calculate the sum of n natural numbers: ``n(n+1)/2`` where n is the number of times the outer loop runs or the maximum number of times the inner loop runs. So, for the example above, the outer loop runs 5 times (and the inner loop runs 0 to a maximum of 5 times) so for n=5, the inner loop runs 5(5+1)/2 = 15 times.
331
+
332
+
This summation formula has a great story that goes back to the famous mathematician Carl Gauss. The story goes that when he was in elementary school in the 1780s, his teacher asked the class to add up all the numbers from 1 to 100. Gauss quickly discovered the pattern that the sum of the first and last numbers is 1 + 100 = 101, the sum of the second and second-to-last numbers is 2 + 99 = 101, and so on. So if you write the series 1 to 100 twice and pair things up, you can quickly that you have 100 pairs that sum to 101 and then you can divide by 2 to get down to just 1 series. Gauss's formula for the sum of the first n natural numbers ``n(n+1)/2`` works for any n. Try it with adding up 1 to 5 and 1 to 10 by pairing numbers until you memorize the pattern and the formula.
333
+
334
+
.. figure:: Figures/sumFormula.png
335
+
:width:400px
336
+
:align:center
337
+
:figclass:align-center
338
+
339
+
Figure 2: How Gauss quickly calculated the sum of the first 100 natural numbers.
340
+
341
+
342
+
.. note::
343
+
344
+
In non-rectangular loops, the number of times the inner loop runs can be calculated with the sum of natural numbers formula ``n(n+1)/2`` where n is the number of times the outer loop runs or the maximum number of times the inner loop runs.
0 commit comments