← Back to course
Intro to CS 9-10 / Week 06 / Thursday
4/6
Week 06 · Procedures and Abstraction

Thursday

Libraries and random test values
// Splitting a big problem into pieces you can reuse
⏱ about 20 min

Thursday: Libraries and Random Test Values

A message arrives from the outpost next door. Their crew has shared a set of tested procedures called Greenhouse Helpers.

"It has a procedure that changes temperatures into a chart," Comet says. "We would not have to write it."

Wren reads the notes that came with it. "There is a license file too. We should read that before we use anything."

Then Comet has another idea. "We test checkPlant with numbers we pick. What if we let Beacon pick?"

Nova hovers over the list of test values. "Would you like a hint?" she asks.

"A random number helper can surprise you with values you would never think to choose."

Using code someone else wrote

In our story, Greenhouse Helpers is a set of procedures another crew wrote and tested.

Using existing correct algorithms as building blocks saves development time and testing.

You still need to know what each procedure does. Thanks to procedural abstraction, you do not need to know how.

You must also acknowledge code that came from someone else, including where it came from or who wrote it.

USING SHARED CODE WELL
  • Read the question.
  • Tap your answer.
Comet wants to use the chart procedure from Greenhouse Helpers. What must she know?
Beacon uses a procedure the next-door crew wrote. What should Beacon's documentation say?
Why might using a tested procedure make errors easier to find?

RANDOM(a, b)

RANDOM(a, b) returns a random whole number from a to b, including both. Each result is equally likely.

For example, RANDOM(1, 3) could return 1, 2 or 3.

Using random numbers means each run of a program may give a different result.

REPEAT 5 TIMES
{
  testLevel ← RANDOM(0, 100)
  DISPLAY(testLevel)
  DISPLAY(checkPlant(testLevel))
}
THINK ABOUT RANDOM
  • Read the question.
  • Tap your answer.
Which value could RANDOM(1, 6) never return?
RANDOM(1, 3) runs once. Is 2 more likely than 3?
You run the test loop twice. Will both runs show the same five numbers?
One run shows testLevel 12. What does checkPlant return for it?
StatementTrue or false?
RANDOM(0, 100) can return 0.?
RANDOM(0, 100) can return 100.?
Random test values replace the need to test the edge values you choose on purpose.?
You should acknowledge code that came from another source.?
WHY THIS EXERCISERandom values can surprise you, but careful developers still pick key test values on purpose.
Building on existing correct ____ saves development time.
RANDOM(a, b) gives a whole number from a to b, and each result is equally ____.
Try it
Use a six-sided number cube as RANDOM(1, 6). Roll it ten times and write each result.
Did every number show up? Talk about why ten rolls may not show them all.
On paper, sketch a spinner split into four equal parts labeled 1 to 4. Label it RANDOM(1, 4).

Sharp thinking. Tomorrow is Impact Friday: licenses, credit and sharing fairly.

← Wednesday