CS 145 Lecture 4
September 19, 2011 by Chris Johnson. Filed under cs145, fall 2011, lectures.
Agenda
- finish up KML example
- don’t repeat yourself
- method anatomy
- method’s beauty:
- minimize bug potential
- encapsulate small, testable units
- make calling code more readable
- some more examples
Code
Placemarker.java
package lecture;
import java.util.Scanner;
public class Placemarker {
public static void main(String[] args) {
System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
System.out.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
System.out.println("<Document>");
System.out.println("<Style id=\"seethrough\"><PolyStyle><color>7fffffff</color></PolyStyle></Style>");
handlePlacemark();
handlePlacemark();
handlePlacemark();
System.out.println("</Document>");
System.out.println("</kml>");
}
public static void handlePlacemark() {
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
String name = in.nextLine();
System.out.print("Description: ");
String description = in.nextLine();
System.out.print("Lon lat: ");
double lon = in.nextDouble();
double lat = in.nextDouble();
System.out.println("<Placemark>");
System.out.println("<name>" + name + "</name>");
System.out.println("<description>" + description + "</description>");
System.out.println(" <Point>");
System.out.println("<coordinates>" + lon + ", " + lat + "</coordinates>");
System.out.println("</Point>");
System.out.println("</Placemark>");
}
}
GeometryFun.java
package lecture;
public class GeometryFun {
public double getAreaOfCircle(double radius) {
double area = radius * radius * Math.PI;
return area;
}
}
TODO
- Work on preassignment 1
- We’ve discussed all of chapter 1. Try crushing the Practice-It exercises.
Haiku
she makes great omelettes
but ev’ryday, Robot Mom?
a method addict
show