Skip to content

Commit d81230a

Browse files
committed
Merge branch 'master' of https://github.com/bhoffman0/CSAwesome
2 parents ca015f3 + bd9b1a7 commit d81230a

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

_sources/Unit3-If-Statements/topic-3-3-if-else.rst

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,149 @@ You can use curly braces (``{}``) to enclose a nested if and have the else claus
417417
In fact many experienced Java programmers `always` use curly braces, even when
418418
they are not technically required to avoid this kind of confusion.
419419

420+
Math.random() in if Statements
421+
-------------------------------
422+
423+
The ``Math.random()`` method returns a random number between 0.0 and 1.0. You can use this method with ``if`` statements to simulate a coin flip or an event occuring a certain percentage of the time. For example, if you want to simulate a coin flip, you can check if the random number is less than 0.5 (halfway between 0 and 1) to simulate a 50% chance of heads or tails:
424+
425+
.. code-block:: java
426+
427+
if (Math.random() < 0.5)
428+
{
429+
System.out.println("Heads");
430+
}
431+
else
432+
{
433+
System.out.println("Tails");
434+
}
435+
436+
If you want to simulate an event occuring 90% of the time, you can check the random number to see if it is less than 0.9 (90% of the way between 0 and 1):
437+
438+
.. code-block:: java
439+
440+
if (Math.random() < 0.9)
441+
{
442+
// 90% of the time
443+
System.out.println("Event happened");
444+
}
445+
else
446+
{
447+
// 10% of the time
448+
System.out.println("Event did not happen");
449+
}
450+
451+
452+
453+
|Exercise| **Check your understanding**
454+
455+
.. mchoice:: mcq-rnd-ifs
456+
:practice: T
457+
458+
The weather report says there is approximately 25% chance of rain today. Which of the following if statements would print Rain or No Rain to simulate a day with the correct percentages following the weather report?
459+
460+
- .. code-block:: java
461+
462+
if (Math.random() < 0.25) { System.out.println("Rain"); }
463+
464+
+ Correct! This code will print "Rain" 25% of the time.
465+
466+
- .. code-block:: java
467+
468+
if (Math.random() > 0.75) { System.out.println("Rain"); }
469+
470+
+ Correct. This code will print "Rain" 25% (1 - .75) of the time.
471+
472+
- .. code-block:: java
473+
474+
if (Math.random() > 0.25) { System.out.println("Rain"); }
475+
476+
- Incorrect. This code will print "Rain" 75% of the time.
477+
478+
- .. code-block:: java
479+
480+
if (Math.random() < 0.75) { System.out.println("No Rain"); }
481+
482+
+ Correct! This code will print "No Rain" 75% of the time, so it will rain 25% of the time.
483+
484+
485+
|CodingEx| **Coding Exercise**
486+
487+
.. activecode:: randomShapes
488+
:language: java
489+
:autograde: unittest
490+
:datafile: turtleClasses.jar
491+
492+
Add an if/else statement that uses Math.random() to do a coin flip to decide whether to call yertle.turnRight() or yertle.turnLeft. Run the code to see the turtle draw a random shape.
493+
~~~~
494+
import java.util.*;
495+
import java.awt.*;
496+
497+
public class RandomTurns
498+
{
499+
public static void main(String[] args)
500+
{
501+
World world = new World(500,400);
502+
Turtle yertle = new Turtle(world);
503+
504+
// This is a loop that runs 10 times (you will learn to write loops in
505+
// Unit 4)
506+
for(int i = 1; i <= 10; i++)
507+
{
508+
yertle.forward(20);
509+
510+
// Write an if/else statement that uses
511+
// Math.random() to do a coin flip (50%) to choose
512+
// between yertle.turnRight() or turnLeft()
513+
514+
515+
516+
517+
518+
519+
} // end of loop
520+
world.show(true);
521+
}
522+
}
523+
====
524+
import static org.junit.Assert.*;
525+
526+
import org.junit.*;
527+
528+
import java.io.*;
529+
530+
public class RunestoneTests extends CodeTestHelper
531+
{
532+
public RunestoneTests()
533+
{
534+
super("RandomTurns");
535+
}
536+
537+
@Test
538+
public void testCodeContainsIf()
539+
{
540+
boolean ifCheck = checkCodeContains("if", "if");
541+
assertTrue(ifCheck);
542+
}
543+
@Test
544+
public void testCodeContainsElse()
545+
{
546+
boolean ifCheck2 = checkCodeContains("else", "else");
547+
assertTrue(ifCheck2);
548+
}
549+
@Test
550+
public void testCodeContainsRandom()
551+
{
552+
boolean ifCheck2 = checkCodeContains("Math.Random()", "Math.random()");
553+
assertTrue(ifCheck2);
554+
}
555+
@Test
556+
public void testCodeContains5()
557+
{
558+
boolean ifCheck2 = checkCodeContains(".5", ".5");
559+
assertTrue(ifCheck2);
560+
}
561+
}
562+
420563
|Groupwork| Programming Challenge : 20 Questions
421564
------------------------------------------------
422565

0 commit comments

Comments
 (0)