CS 145 Lecture 8 – Methods Cont’d

Dear students,

Last time we started generating some circle art, but we stopped with only one circle. Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on. 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!

Next we’ll write a method that generates a random letter, perhaps used to assign someone to a group. We’ll visualize what happens in memory when we execute methods. This will lead us to the notion of a variable’s scope, which is the region of code in which a variable may be accessed. Reaching across scopes is impossible, but methods provide a vehicle for circumventing the Wall of Braces: return values and parameters.

Here’s your TODO list to complete before next class:

  • An unofficial homework 1 grade report has been sent to you. Fix your homework 1 issues and resubmit before Monday. 50% of your issues are solved by noticing details like spaces and punctuation. 95% of your issues have been discussed numerous times on Piazza.
  • To receive credit for homework 1, you must attend a feedback/review session during the next lab.
  • The next lab is posted.

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

“Don’t repeat yourself”
Grandpa loved to tell me that
Mostly at the end

P.P.S. Here’s the code we wrote together in class…

Circles.java

package lecture0923;

import java.util.Random;

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();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();    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 originX = generator.nextInt(800);
    int originY = generator.nextInt(600);
    int radius = generator.nextInt(100);
    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);
  }
}

Grouper.java

package lecture0923;

import java.util.Random;

public class Grouper {
  public static void main(String[] args) {
    System.out.println("Person 1 is in group..." + getRandomLetter());
    
    System.out.print("Person 2 is in group...");
    char c = getRandomLetter();
    System.out.println(c);

    System.out.print("Person 3 is in group...");
    System.out.println(getRandomLetter());
  }
  
//  public static void printRandomLetter() {
//    Random generator = new Random();
//    int n = generator.nextInt(26); // a number in [0, 25]
//    char randomLetter = (char) (n + 'A');
//    System.out.println(randomLetter);
//  }
  
  public static char getRandomLetter() {
    Random generator = new Random();
    int n = generator.nextInt(26); // a number in [0, 25]
    char randomLetter = (char) (n + 'A');
    return randomLetter;
    //System.out.println(randomLetter);
  }
}

RandomUtilities.java

package lecture0923;

import java.util.Random;

public class RandomUtilities {
  public static int getRandomInRange(int lo, int hi) {
    Random generator = new Random();
    int span = hi - lo + 1;
    int randomNumber = generator.nextInt(span) + lo;
    return randomNumber;
  }
  
  public static void main(String[] args) {
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));
    System.out.println(getRandomInRange(9, 10));

  }
}

Comments

  1. Robert Prescott says:

    Circles!

Leave a Reply

Your email address will not be published. Required fields are marked *