teaching machines

CS 145 Lecture 6 – Methods 2

February 10, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

What does this do?

  1. public static String ?(??) {
      return text + " :)";
    }

  2. public static ? ??(String text) {
      return text.length() == 0;
    }

  3. public static ? ??(String text) {
      int index = text.indexOf('.');
      return text.substring(0, index + 1);
    }

  4. public static ? ??(Random generator, Scanner in) {
      return generator.nextInt() == in.nextInt();
    }

Code

.

<h4>Area.java</h4>
<pre class="code">
package preexam1;

public class Area {

  public static void main(String[] args) {
    double r1 = 5;
    
//    java.awt.Toolkit.getDefaultToolkit().beep();
    
    double area = getArea(8);
    System.out.println(r1);
  }
  
  public static double getArea(double radius) {
    radius = Integer.MAX_VALUE;
    return Math.PI * radius * radius;
  }
}
</pre>
<h4>Snowperson.java</h4>
<pre class="code">
package preexam1;

import java.util.Random;

public class Snowperson {
  
  public static void main(String[] args) {
    
    System.out.println("&lt;html&gt;");
    System.out.println("&lt;body&gt;");
    System.out.println("&lt;h1&gt;My first SVG&lt;/h1&gt;");
    System.out.println("&lt;svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"&gt;");
    
    Random generator = new Random();
    
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));
    System.out.println(getSnowperson(generator.nextInt(400), 50));

    
    System.out.println("&lt;/svg&gt;");
    System.out.println("&lt;/body&gt;");
    System.out.println("&lt;/html&gt;");
  }
  
  public static String getSnowperson(int x, int y) {
    int headRadius = 15;
    int torsoRadius = 20;
    int bottomRadius = 30;
    
    String bottom = getCircleTag(x, y + headRadius + 2 * torsoRadius + bottomRadius, bottomRadius, "green");
    String torso = getCircleTag(x, y + headRadius + torsoRadius, torsoRadius, "green");
    String head = getCircleTag(x, y, headRadius, "green");

//    String joined = head + torso + bottom;
//    return joined;
    
    return head + torso + bottom;
  }

  public static String getCircleTag(int x, int y, int radius, String color) {
    return "&lt;circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius + 
           "\" stroke=\"black\" stroke-width=\"2\" fill=\"" + color + "\" /&gt;";
  }
}
</pre>

Haiku

“Beware the blackHole()!”
But that method’s just legend.
It’s not in the docs…