Chapter 21- Using Generics – Case Study: Geometric Bunch
In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject. The subclasses are comparable. The wrapper includes three methods: showList(), findLocationElementWithLargestArea(), and sortElements()
Class: Driver
package csu.matos;
import java.util.ArrayList;
public class Driver {
/**
* Author: V. Matos
* Date: Spring-2012
* Goal: Various code fragments to demonstrate issues on
* Java Generics - Chapter 21.
*/
public static void main(String[] args) {
TestGeometricBunch test = new TestGeometricBunch();
test.main(null);
}//main
}
Class: TestGeometricBunch
package csu.matos;
/**
* Author: V. Matos
* Date: Spring-2012
* Goal: Generics Case STudy - In this example a class called
* GeometricBunch is made to wrap around a list of GeometricObjects.
* Circle and Rectangle are subclasses of GeometricObject. The
* subclasses are comparable. The wrapper includes three methods:
* showList(), findLocationElementWithLargestArea(), and sortElements()
*/
import java.util.ArrayList;
public class TestGeometricBunch {
public static void main(String[] arg) {
GeometricBunchGeometricObject> bunch = new GeometricBunchGeometricObject>();
Circle c1 = new Circle(1);
Circle c2 = new Circle(2);
Circle c3 = new Circle(3);
bunch.add(c1);
bunch.add(c2);
bunch.add(c3);
Rectangle r1 = new Rectangle(1, 20);
Rectangle r2 = new Rectangle(10, 2);
Rectangle r3 = new Rectangle(10, 0.5);
bunch.add(r1);
bunch.add(r2);
bunch.add(r3);
bunch.showList();
System.out.println("\nLargestArea at " + bunch.getLargest());
ArrayListGeometricObject> list2 = bunch.sortElements();
int i = 0;
for (GeometricObject g : list2) {
String objType = g.getClass().getSimpleName();
System.out
.printf("\n[%d]: %s \t %3.2f ", i++, objType, g.getArea());
}
}
}
Class: GeometricObject
package csu.matos;
public abstract class GeometricObject implements Comparable<GeometricObject> {
@Override
public int compareTo(GeometricObject o2) {
double area1 = this.getArea();
double area2 = o2.getArea();
if (area1 < area2)
return -1;
else if (area1 == area2)
return 0;
else
return 1;
}
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
// Construct a default geometric object
protected GeometricObject() {
dateCreated = new java.util.Date();
}
// create object with color and filled value
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
// Return color
public String getColor() {
return color;
}
// Set a new color
public void setColor(String color) {
this.color = color;
}
// Return TRUE is object is filled
public boolean isFilled() {
return filled;
}
public void setDateCreated(java.util.Date dateCreated) {
this.dateCreated = dateCreated;
}
// Set filled to either: true/false
public void setFilled(boolean filled) {
this.filled = filled;
}
// Get dateCreated
public java.util.Date getDateCreated() {
return dateCreated;
}
// Return a string representing this object
public String toString() {
return " created on " + dateCreated + "\n color: " + color
+ " and filled: " + filled;
}
// Abstract method getArea
public abstract double getArea();
// Abstract method getPerimeter
public abstract double getPerimeter();
}
Class: Rectangle
package csu.matos;
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
public String toString() {
return " > Rectangle " + getColor() + "\n" + super.toString()
+ "\n > Height: " + getHeight() + " Width: " + getWidth()
+ " Perimeter: " + getPerimeter() + " Area: " + getArea();
}
}
Class: Circle
package csu.matos;
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated()
+ " and the radius is " + radius);
}
public String toString() {
return " > Circle " + getColor() + "\n" + super.toString()
+ "\n > Radius: " + getRadius() + " Perimeter: "
+ getPerimeter() + " Area: " + getArea();
}
}
CONSOLE
[0]: > Circle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Radius: 1.0 Perimeter: 6.283185307179586 Area: 3.141592653589793
[1]: > Circle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Radius: 2.0 Perimeter: 12.566370614359172 Area: 12.566370614359172
[2]: > Circle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Radius: 3.0 Perimeter: 18.84955592153876 Area: 28.274333882308138
[3]: > Rectangle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Height: 20.0 Width: 1.0 Perimeter: 42.0 Area: 20.0
[4]: > Rectangle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Height: 2.0 Width: 10.0 Perimeter: 24.0 Area: 20.0
[5]: > Rectangle white
created on Fri Feb 10 12:21:02 EST 2012
color: white and filled: false
> Height: 0.5 Width: 10.0 Perimeter: 21.0 Area: 5.0
LargestArea at 2
[0]: Circle 3.14
[1]: Rectangle 5.00
[2]: Circle 12.57
[3]: Rectangle 20.00
[4]: Rectangle 20.00
[5]: Circle 28.27