# teaching machines

## CS 245 Lecture 10 – Abstract Classes

October 3, 2013 by . Filed under cs245, fall 2013, lectures.

### Agenda

• what ?s
• the inheritance spectrum
• abstract classes
• writing a travel diary with Google Earth

### TODO

• Read on Generics in chapter 12 through section 12.3 in Core Java. 1/4 sheet.

### Travel Diary

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="seethrough"><PolyStyle><color>7fffffff</color></PolyStyle></Style>

<Placemark>
<name>Eurasian Smoketree</name>
<description>This tree is surreal. <img src="smoketree.jpg" /></description>
<Point>
<coordinates> -93.65057870772075,42.03081012742565</coordinates>
</Point>
</Placemark>

<Placemark>
<name>Triangle of Fate</name>
<description>At all costs, avoid traveling the unmarked edge of this triangle during the fall.  A female ginkgo lies on that edge, and female ginkgos are some of stinkiest trees known to man.<img src="ginkgo.jpg" /></description>
<LineString>
<tesselate>1</tesselate>
<coordinates> -93.65014025291298,42.02556822761216 -93.65031285507688,42.02551563374377 -93.65031551056221,42.0257240592268</coordinates>
</LineString>
</Placemark>

<Placemark>
<name>Baldcypress Alley</name>
<description>I love baldcypress trees.  They do not fit cleanly into an inheritance hierarchy, because they are both deciduous and coniferous.<img src="baldcypress.jpg" /></description>
<styleUrl>#seethrough</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates> -93.65289780478878,42.03059860529465 -93.6529014382725,42.03049484516234 -93.65207503713377,42.03049447620213 -93.65208168225243,42.03059931197005</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>

</Document>
</kml>


### Code

#### TravelDiary.java

package lecture10;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class TravelDiary {
private ArrayList<Placemark> placemarks;

public TravelDiary() {
placemarks = new ArrayList<Placemark>();
}

}

public void writeKML(String path) throws FileNotFoundException {
PrintWriter out = new PrintWriter(path);

out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
out.println("<Document>");
out.println("<Style id=\"seethrough\"><PolyStyle><color>7fffffff</color></PolyStyle></Style>\n");

for (Placemark placemark : placemarks) {
out.println(placemark.toKML());
}

out.println("</Document>");
out.println("</kml>");

out.close();
}
}

#### Placemark.java

package lecture10;

import java.util.ArrayList;

public abstract class Placemark {
private String name;
private String description;
private ArrayList<LatLon> coordinates;

public Placemark(String name, String description) {
this.name = name;
this.description = description;
coordinates = new ArrayList<LatLon>();
}

}

public String toKML() {
String s = "<Placemark>\n";
s += "<name>" + name + "</name>\n";
s += "<description>" + description + "</description>\n";
s += getSpecializedKML();
s += "</Placemark>\n";
return s;
}

public String getCoordinatesTag() {
String s = "<coordinates>";
for (LatLon c : coordinates) {
s += c + " ";
}
s += "</coordinates>\n";
return s;
}

public abstract String getSpecializedKML();
}

#### LatLon.java

package lecture10;

public class LatLon {
private double lat;
private double lon;

public LatLon(double lat,
double lon) {
this.lat = lat;
this.lon = lon;
}

public double getLat() {
return lat;
}

public double getLon() {
return lon;
}

public String toString() {
return lat + "," + lon;
}
}

#### PinPlacemark.java

package lecture10;

public class PinPlacemark extends Placemark {

public PinPlacemark(String name, String description) {
super(name, description);
}

@Override
public String getSpecializedKML() {
String s = "<Point>\n";
s += getCoordinatesTag();
s += "</Point>\n";
return s;
}
}

#### LinePlacemark.java

package lecture10;

public class LinePlacemark extends Placemark {
public LinePlacemark(String name,
String description) {
super(name, description);
}

@Override
public String getSpecializedKML() {
String s = "<LineString>\n<tesselate>1</tesselate>\n";
s += getCoordinatesTag();
s += "</LineString>\n";
return s;
}
}

#### RegionPlacemark.java

package lecture10;

public class RegionPlacemark extends Placemark {
public RegionPlacemark(String name,
String description) {
super(name, description);
}

@Override
public String getSpecializedKML() {
String s = "<styleUrl>#seethrough</styleUrl>\n";
s += "<Polygon>\n<outerBoundaryIs>\n<LinearRing>\n";
s += getCoordinatesTag();
s += "</LinearRing>\n</outerBoundaryIs>\n</Polygon>\n";
return s;
}
}

#### Main.java

package lecture10;

import java.io.FileNotFoundException;

public class Main {
public static void main(String[] args) throws FileNotFoundException {
TravelDiary diary = new TravelDiary();

// Cannot create instances of abstract things.
// new Placemark("Foo", "Boo");

PinPlacemark pin = new PinPlacemark("Eurasian Smoketree",
"This tree is surreal. <img src=\"smoketree.jpg\" />");

LinePlacemark line = new LinePlacemark("Triangle of Fate", "At all costs, avoid traveling the unmarked edge of this triangle during the fall.  A female ginkgo lies on that edge, and female ginkgos are some of stinkiest trees known to man.<img src=\"ginkgo.jpg\" />");

RegionPlacemark region = new RegionPlacemark("Baldcypress Alley",
"I love baldcypress trees.  They do not fit cleanly into an inheritance hierarchy, because they are both deciduous and coniferous.<img src=\"baldcypress.jpg\" />");
}