'F' → Fullscreen
Today you build a fishing game in a new program named 1-20-fishing-trip. Ten casts, and on each one you choose shallow water or deep. By the end it reports your trip using all four of today's patterns.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
Your fish are random, so nobody's numbers will match. Check every run by hand instead: the program prints each fish as it lands, so add them up yourself and compare. You cannot check a loop whose answer you do not already know.
Random decides whether anything bites and how big it is.totalWeight is an accumulator. It adds the weight of every fish. Add your fish up by hand and check that the total matches.weight is declared. It is inside the loop, so it is brand new every cast — and that is fine, because totalWeight is the one that has to survive all ten. Be ready to say why totalWeight could not go inside.int totalWeight = 0;
Console.WriteLine("Ten casts. Shallow water bites often. Deep water bites less, but bigger.");Console.WriteLine();
for (int cast = 1; cast <= 10; cast++){ Console.Write($"Cast {cast}: shallow (1) or deep (2)? "); int spot = int.Parse(Console.ReadLine());
int weight = 0; int roll = Random.Shared.Next(1, 101);
if (spot == 1 && roll <= 75) { weight = Random.Shared.Next(1, 7); }
if (spot == 2 && roll <= 30) { weight = Random.Shared.Next(4, 16); }
if (weight > 0) { Console.WriteLine($" A {weight} lb fish!");
totalWeight = totalWeight + weight; } else { Console.WriteLine(" Nothing bites."); }}
Console.WriteLine();Console.WriteLine($"Total weight: {totalWeight} lb");fishCaught adds one. totalWeight adds the weight. They look almost the same and they answer completely different questions.if (weight > 0). A cast that catches nothing should not be counted, and it cannot be the biggest fish.biggest starts at 0, and here that is safe. The deck called starting a maximum at 0 the classic bug of this pattern. Work out why it cannot hurt this program — what is the lightest fish you can catch?int totalWeight = 0;int fishCaught = 0;int biggest = 0;
Console.WriteLine("Ten casts. Shallow water bites often. Deep water bites less, but bigger.");Console.WriteLine();
for (int cast = 1; cast <= 10; cast++){ Console.Write($"Cast {cast}: shallow (1) or deep (2)? "); int spot = int.Parse(Console.ReadLine());
int weight = 0; int roll = Random.Shared.Next(1, 101);
if (spot == 1 && roll <= 75) { weight = Random.Shared.Next(1, 7); }
if (spot == 2 && roll <= 30) { weight = Random.Shared.Next(4, 16); }
if (weight > 0) { Console.WriteLine($" A {weight} lb fish!");
totalWeight = totalWeight + weight; fishCaught = fishCaught + 1;
if (weight > biggest) { biggest = weight; } } else { Console.WriteLine(" Nothing bites."); }}
Console.WriteLine();Console.WriteLine($"Total weight: {totalWeight} lb");Console.WriteLine($"Fish caught: {fishCaught} of 10");Console.WriteLine($"Biggest: {biggest} lb");< instead of >, and a starting value of 999 instead of 0. Be ready to say why it starts that high.if (fishCaught > 0). No fish, no biggest, no smallest, and no average — there is nothing to report.fishCaught as an int, play a trip, and work out total ÷ fish on a calculator. Compare it with the average your program printed.int to double on line 2 and play again — the deck's one-word fix. Write down one wrong average and one right one. If your first trip happened to divide evenly, the bug hid; play until it does not.int totalWeight = 0;double fishCaught = 0;int biggest = 0;int smallest = 999;
Console.WriteLine("Ten casts. Shallow water bites often. Deep water bites less, but bigger.");Console.WriteLine();
for (int cast = 1; cast <= 10; cast++){ Console.Write($"Cast {cast}: shallow (1) or deep (2)? "); int spot = int.Parse(Console.ReadLine());
int weight = 0; int roll = Random.Shared.Next(1, 101);
if (spot == 1 && roll <= 75) { weight = Random.Shared.Next(1, 7); }
if (spot == 2 && roll <= 30) { weight = Random.Shared.Next(4, 16); }
if (weight > 0) { Console.WriteLine($" A {weight} lb fish!");
totalWeight = totalWeight + weight; fishCaught = fishCaught + 1;
if (weight > biggest) { biggest = weight; }
if (weight < smallest) { smallest = weight; } } else { Console.WriteLine(" Nothing bites."); }}
Console.WriteLine();Console.WriteLine($"Total weight: {totalWeight} lb");Console.WriteLine($"Fish caught: {fishCaught} of 10");
if (fishCaught > 0){ double average = totalWeight / fishCaught;
Console.WriteLine($"Biggest: {biggest} lb"); Console.WriteLine($"Smallest: {smallest} lb"); Console.WriteLine($"Average: {average:F1} lb");}else{ Console.WriteLine("Not one bite. Try shallow water next time.");}Three changes, one at a time. Write down exactly what happens, and put each one back before the next.
totalWeight = totalWeight + weight; to totalWeight = totalWeight + 1; and play a trip. Record the total weight and the average, and say which of the four patterns you accidentally wrote.smallest at 0 instead of 999. Record what it reports.fishCaught > 0 to fishCaught >= 0, then answer 3 on every cast — there is no spot 3, so you are casting onto dry land. Record all five report lines.Number 1 is the dangerous one, because it runs and looks fine. Say how long it would have taken you to notice if you had not been adding up your fish by hand. For number 3, say which of the five lines are starting values that leaked out, and look up what NaN stands for.
One real trip from the finished program. Your fish will be different, but your report has to agree with your fish.
Ten casts. Shallow water bites often. Deep water bites less, but bigger.
Cast 1: shallow (1) or deep (2)? 1 [Enter] A 5 lb fish!Cast 2: shallow (1) or deep (2)? 1 [Enter] A 2 lb fish!Cast 3: shallow (1) or deep (2)? 2 [Enter] Nothing bites.Cast 4: shallow (1) or deep (2)? 1 [Enter] A 2 lb fish!Cast 5: shallow (1) or deep (2)? 2 [Enter] Nothing bites.Cast 6: shallow (1) or deep (2)? 2 [Enter] Nothing bites.Cast 7: shallow (1) or deep (2)? 1 [Enter] Nothing bites.Cast 8: shallow (1) or deep (2)? 1 [Enter] A 2 lb fish!Cast 9: shallow (1) or deep (2)? 2 [Enter] Nothing bites.Cast 10: shallow (1) or deep (2)? 1 [Enter] A 1 lb fish!
Total weight: 12 lbFish caught: 5 of 10Biggest: 5 lbSmallest: 1 lbAverage: 2.4 lbAnswer the following questions before submitting your work.
biggest at 0 is safe in your fishing game, but it was a bug on the deck's temperature readings. Explain what is different about the data, and why a bug that only appears on some data is hard to catch.fishCaught to a double, your average printed with a .0 on the end, and that looks like a properly formatted answer. Explain what C# actually did, and why :F1 could not have rescued it.Submit four things to the dropbox: