Code others wrote
After this: Import a module, use sqrt and a random number, and read one page of documentation.
On this page · 6 sections · 3 checks
You need the square root of 16. You could write a loop that tries numbers until one, multiplied by itself, is close enough. Someone wrote that loop long ago, tested it on millions of numbers, and made it faster than yours would be. Your program can use theirs.
Bringing in a module
A is a file of functions and values that someone else wrote. The line import mathimport java.util.Scanner; at the top of a program brings one in; the word for that line is import. After it, you reach what the module holds by writing its name, a dot, and the function's name: math.sqrt(16).You have copied lines like it since lesson 1; now you know what they do. In Java a module is called a package, and java.util is the one that holds Scanner, Random and the list tools. What a module holds is reached by its name, a dot, and the function's name: Math.sqrt(16). Math itself needs no import line, as the note below says.
The modules that come with the language, with nothing to install, are called the . Maths, random numbers, dates, files and text are all in it, and this tutorial has used it already: the tools for reading files came from there.
The maths module
- math.sqrt(16)Math.sqrt(16) is 4.0. It is a decimal number even though 16 is a perfect square, because
sqrtalways gives a decimal number. - math.piMath.PI is a value, not a function: 3.14159 and so on. There are no brackets after it.
- Rounded to two decimals it is 3.14. round(math.pi, 2) does it.Math.round rounds to a whole number only, so multiply by 100, round, and divide by 100.0.
import mathroot = math.sqrt(16)print(root)print(round(math.pi, 2))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Change 16 to 2 and run it again. The square root of 2 has no end; sqrt gives the first sixteen or so digits.
Random numbers
A game needs a dice. random.randint(1, 6)dice.nextInt(1, 7) gives a whole number from 1 to 6, a different one each time it is called. Both ends are included: 1 and 6 can come up.The first number is included and the second is left out, as with the end of a range, so 7 never comes up; write 7 to get 1 to 6. The tool is made once, Random dice = new Random(), and asked again and again.
import randoma = random.randint(1, 6)b = random.randint(1, 6)print(a)print(b)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The two numbers in the trace lie between 1 and 6. A trace has to be the same every time it is stepped through, so the tracer fixes the starting point of the random numbers before the run. Your own runs do not fix it: press Run a few times below and watch the numbers change.The Java code does the same with new Random(0). Take the 0 out, and each run gives new numbers. The trace band shows the pair from the Python run for both languages; a Java run from the same starting point gives its own pair.
Reading the documentation
The standard library is large, and nobody knows all of it. The skill is to find a function when you need one. Here is one lookup, in words.
You want to round a number down. Does the maths module have that? Open the documentation page for mathMath. The page is a list of functions with a sentence each. Scan the names for something like "floor". math.floor(x): "Return the floor of x, the largest integer less than or equal to x."Math.floor(double a): "Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer." The sentence says what goes in, what comes back, and what kind of value it is. Then try it in a two-line program:
import mathprint(math.floor(2.7))2
public class Main { public static void main(String[] args) { System.out.println(Math.floor(2.7)); }}2.0
The two languages agree on the value and disagree on the kind: Python's floor gives a whole number, Java's gives a decimal number, 2.0. The documentation said so; a program that needs a whole number in Java converts it with (int). Find the page, scan the names, read the one sentence, check the kind of result, try it: that is the whole skill.
Check yourself
A program wants a random number. Which line, at the top of the file, brings in the tools for that?
Which numbers can random.randint(1, 6)dice.nextInt(1, 7) give?
What does this program print?
import mathprint(math.sqrt(25))print(math.sqrt(25) * 2)public class Main { public static void main(String[] args) { System.out.println(Math.sqrt(25)); System.out.println(Math.sqrt(25) * 2); }}Honesty notes
- Say what a module and the standard library are, and bring a module in with an import line.
- Use the maths module for a square root (always a decimal number) and pi.
- Get a random whole number from 1 to 6 (randint includes both ends in Python; nextInt leaves the second out in Java, so it says 7).
- Look a function up in the documentation: find the page, scan the names, read the one sentence, check the kind of result, try it.