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 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 /** * 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) { GeometricBunch<GeometricObject> bunch = new GeometricBunch<GeometricObject>(); 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()); ArrayList<GeometricObject> 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 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 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 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); return " >>> Rectangle " + getcolor() + "\n" + super.tostring() + "\n >>> Height: " + getheight() + " Width: " + getwidth() + " Perimeter: " + getperimeter() + " Area: " + getarea(); Class: Circle 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); return " >>> Circle " + getcolor() + "\n" + super.tostring() + "\n >>> Radius: " + getradius() + " Perimeter: " + getperimeter() + " Area: " + getarea(); CONSOLE [0]: >>> Circle white >>> Radius: 1.0 Perimeter: 6.283185307179586 Area: 3.141592653589793 [1]: >>> Circle white >>> Radius: 2.0 Perimeter: 12.566370614359172 Area: 12.566370614359172 [2]: >>> Circle white >>> Radius: 3.0 Perimeter: 18.84955592153876 Area: 28.274333882308138 [3]: >>> Rectangle white >>> Height: 20.0 Width: 1.0 Perimeter: 42.0 Area: 20.0 [4]: >>> Rectangle white >>> Height: 2.0 Width: 10.0 Perimeter: 24.0 Area: 20.0 [5]: >>> Rectangle white >>> 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