Homework02_Plus.java
import java.util.Random;
/**
Example of interfaces based on CIS370 homework #2.
The <code>Shape</code> class and its subclasses have been augmented
to implement the <code>PrinterPlottable</code> interface. This
program shows how <code>Shape</code> objects can be referenced
through <code>PrinterPlottable</code> variables.
@author Walt Leipold
*/
public class Homework02_Plus
{
public static void main(String[] argv)
{
PrinterPlottable[] shapes = new PrinterPlottable[20];
Random rand = new Random();
for (int i=0; i<shapes.length; i++) {
double x = rand.nextInt(72);
double y = rand.nextInt(22);
double w = rand.nextInt(40);
double h = rand.nextInt(20);
switch(rand.nextInt(4)) {
case 0:
shapes[i] = new Line(x,y,x+w,y+h);
break;
case 1:
shapes[i] = new Rectangle(x,y,w,h);
break;
case 2:
shapes[i] = new Circle(x,y,w);
break;
case 3:
shapes[i] = new Ellipse(x,y,w,h);
break;
}
}
// Plot the objects.
PlotContext pc = new PlotContext(76,32);
String symbols = "+-@#%*O";
for (int i=0; i<shapes.length; i++) {
char c = symbols.charAt(i % symbols.length());
shapes[i].draw(pc,c);
}
System.out.println(pc);
// Print out the PrinterPlottable objects.
System.out.println();
for (int i=0; i<shapes.length; i++)
System.out.println(shapes[i]);
}
}