CS 145 Lecture 6 – Methods 2
Agenda
- what does this do?
- the separation between caller and callee
- building a snowman
- method chaining
What does this do?
public static String ?(??) { return text + " :)"; }public static ? ??(String text) { return text.length() == 0; }public static ? ??(String text) { int index = text.indexOf('.'); return text.substring(0, index + 1); }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("<html>");
System.out.println("<body>");
System.out.println("<h1>My first SVG</h1>");
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
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("</svg>");
System.out.println("</body>");
System.out.println("</html>");
}
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 "<circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius +
"\" stroke=\"black\" stroke-width=\"2\" fill=\"" + color + "\" />";
}
}
</pre>
Haiku
“Beware the blackHole()!”
But that method’s just legend.
It’s not in the docs…
But that method’s just legend.
It’s not in the docs…