CS 1: Lecture 8 – Methods
Dear students,
First, let’s a new data type to our repertoire: a random number generator. If I tell you that the name of this type is Random
, could you create a new variable that is capable of generating random numbers?
You should be able to. We can tell by its capitalization that it is a class, rather than a primitive, and its should be declared and assigned like this:
Random generator = new Random();
Okay, we have a random number generator. Now how do we use it? In particular, how do we find information on to use it? I can think of a few ways:
- Search the web for
java 8 CLASSNAME
. - Create a variable of the desired type, then use Eclipse’s autocompletion to see what methods are available.
- Click inside the name of a class in Eclipse, and hit
Shift-F2
to bring up the documentation.
We will use Random
many times this semester, because random behavior adds an element of surprise to otherwise boring programs. But now we move on to the next big idea of this class. We enter the Computer as Chef chapter of our lives together.
Sometimes we write code that we want to execute again and again. Let’s see an example of this. We will generate some random circle art using the SVG protocol. An outlined circle looks like this in SVG:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <circle cx="CENTERX" cy="CENTERY" r="RADIUS" stroke="black" stroke-width="2" fill="rgb(RED, GREEN, BLUE)"/> </svg>
The capitalized words are just placeholders. The first three are in pixel units, and the last three are in [0, 255]. Let’s start by filling them in with random values. We’ll use the Random
class this time, because unlike Math.random
, it can generate int
s for us.
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
Random generator = new Random();
int originX = generator.nextInt(1000);
int originY = generator.nextInt(800);
int radius = generator.nextInt(600);
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
System.out.println("</svg>");
Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on.
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
Random generator = new Random();
// First circle
int originX = generator.nextInt(1000);
int originY = generator.nextInt(800);
int radius = generator.nextInt(600);
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
// Second circle
originX = generator.nextInt(1000);
originY = generator.nextInt(800);
radius = generator.nextInt(600);
r = generator.nextInt(256);
g = generator.nextInt(256);
b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
// ...
System.out.println("</svg>");
We’ll find that the code quickly gets away from us if we simply copy and paste. Instead, we factor out the repeated code to a method. For the time being, our programs with multiple methods will have this structure:
public class ClassWithMultipleMethods {
public static void main(String[] args) {
// ...
helperMethodA();
// ...
helperMethodB();
// ...
}
public static void helperMethodA() {
// ...
}
public static void helperMethodB() {
// ...
}
}
Let’s make a method that generates a random circle and call it a bunch of times! We’ll move the code outside of main
into a helper method. That helper method will have a very narrow purpose: generate one circle. Then in main
, we’ll call our own method a bunch of times.
public class Circles {
public static void main(String[] args) {
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
System.out.println("</svg>");
}
public static void generateCircle() {
Random generator = new Random();
int originX = generator.nextInt(1000);
int originY = generator.nextInt(800);
int radius = generator.nextInt(600);
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
}
This is much better. There’s a mantra in software development that’s worth repeating here: don’t repeat yourself.
One thing that’s less than awesome, however, is that every time we invoke generateCircle
, we create a brand new Random
. It seems smarter for us to just create one up front, and then share it. But this brings in an issue of variable scope. We’ll solve it using parameters, we allow us to toss values from the caller to the callee.
We’ll then look at the reverse direction, going from callee back to caller.
Here’s your TODO list to complete before next class:
- Fix any homework 1 issues today. I sent an unofficial grading email early this morning. I will re-run for an official grade early tomorrow.
- Attend a peer review session during lab next week to claim your Blugolds.
- Read section 1.4 and chapter 3. On a quarter sheet, write down 2-3 questions or observations related to the reading. For one of these items, consider writing a method that does something interesting.
See you next class!
P.S.
It’s time for a haiku!
What’s insanity?
It’s doing something again
With the same result
P.P.S. Here’s the code we wrote together in class…
CircleMural.java
package lecture0922;
import java.util.Random;
public class CircleMural {
public static void main(String[] args) {
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
generateCircle();
System.out.println("</svg>");
}
public static void generateCircle() {
Random generator = new Random();
int centerX = generator.nextInt(2000);
int centerY = generator.nextInt(900);
int radius = generator.nextInt(100);
int r = generator.nextInt(128) + 128;
int g = generator.nextInt(128) + 128;
int b = generator.nextInt(56) + 200;
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
}
}
RandomPoints.java
package lecture0922;
import java.util.Random;
public class RandomPoints {
public static void main(String[] args) {
Random generator = new Random();
int nInsides = 0;
int nPoints = 1000000;
for (int i = 0; i < nPoints; ++i) {
double x = generator.nextDouble() * 2 - 1;
double y = generator.nextDouble() * 2 - 1;
double hypot = Math.hypot(x, y);
if (hypot <= 1) {
nInsides += 1;
}
// System.out.printf("%.3f,%.3f%n", x, y);
}
System.out.println(4 * nInsides / (double) nPoints);
}
}
Svg.java
package lecture0922;
import java.util.Random;
public class Svg {
public static void main(String[] args) {
System.out.printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">%n");
for (int i = 0; i < 20000; ++i) {
generateCircle();
}
System.out.printf("</svg>%n");
}
public static void generateCircle() {
Random generator = new Random();
int centerX = generator.nextInt(1000);
int centerY = generator.nextInt(500);
int radius = generator.nextInt(50);
int r = generator.nextInt(56) + 200;
int g = generator.nextInt(128);
int b = generator.nextInt(128);
System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
}
}