/** * Driver.java * Updated: 06 Nov 206 dgg - convert to use Scanner class * Created: Tue May 15 02:25:25 2000 * * @author David Green * @version * Vers: 1.0.5 2006/11/16 sma -- Documentation standards for Java source code */ import java.io.*; import java.util.*; public class Driver { static Scanner scanner; /** * The main method for the command line. */ public static void main(String[] args) throws IOException { if ( args.length > 0 ) { /* * if any arguments, give ID and exit */ System.out.println( "Shape Demonstration - May 2001 - David Green\n" + "Handles circle, square, rectangle shapes\n" + "Driver modified November 2006 to use Scanner class\n" + "See http://www-ece.eng.uab.edu/DGreen/java_ex/CDE.html for " + "more information." ); return; } scanner = new Scanner( System.in); Vector v = new Vector( 10, 5 ); String line; /* commands are * * command [parameters] * -or- * command * parameter * parameter * * The driver is not "production" quality but sufficient for exercising * the model. */ while (scanner.hasNext() ) { try { line = scanner.next(); if ( line.equals( "circle" ) ) { v.add( makeCircle() ); } else if ( line.equals( "square" ) ) { v.add( makeSquare() ); } else if ( line.equals( "rectangle" ) ) { v.add( makeRectangle() ); } else if ( line.equals( "resize" ) ) { resize( v ); } else if ( line.equals( "print" ) ) { print( v ); } else if ( line.equals( "quit")) { System.exit(0); } else { System.out.println("Bad input - command."); } } catch (EOFException e) { System.exit(0); } catch (IOException e) { System.out.println("Bad input - command."); scanner.nextLine(); /* *skip over rest of line */ } } } /** * Method to make a circle. */ static Circle makeCircle() throws IOException { String name = scanner.next(); int radius = scanner.nextInt(); return new Circle( name, radius ); } /** * Method to make a square. */ static Square makeSquare() throws IOException { String name = scanner.next(); int side = scanner.nextInt(); return new Square( name, side ); } /** * Method to make a rectangle. */ static Rectangle makeRectangle() throws IOException { String name = scanner.next(); int width = scanner.nextInt(); int length = scanner.nextInt(); return new Rectangle( name, width, length ); } /** * Method to resize all the shapes. */ static void resize( Vector v ) throws IOException { int amount = scanner.nextInt(); for ( int i = 0; i < v.size(); i++ ) { ( (Shape) v.get(i) ).resize( amount ); } } /** * Method to print. */ static void print( Vector v ) { /* * print out list of known shapes */ for ( int i = 0; i < v.size(); i++ ) { System.out.println( composeInfo( (Shape) v.get(i) ) ); } /* * print out coverage */ for ( int i = 0; i < v.size(); i++ ) { for ( int j = 0; j < v.size(); j++ ) { if ( i!=j && covers( (Shape)v.get(i), (Shape)v.get(j) ) ) { System.out.println( ( (Shape) v.get(i)).getName() + " covers " + ( (Shape) v.get(j)).getName() ); } } } } /** * Method to return the information about a specific shape. */ static String composeInfo( Shape s ) { return s.getName() + ": " + s.getArea(); } /* note: covers can NOT be a property of the object since it * would cause circular object definitions * One approach would be to create a class (object) that * understands this graphic information or perhaps even * coerce the AWT to help but we will just include create * a method to capture the information for this limited * set of shapes. */ /** * Method to check if any shape covers another one. */ static boolean covers( Shape s1, Shape s2 ) { /* * simple version... */ /* * if s1 is circle, it can only cover itself (or nothing) */ if ( s1 instanceof Circle ) { if ( ! ( s2 instanceof Circle ) ) return false; else return ((Circle) s1).getRadius() == ((Circle) s2).getRadius(); } /* if s1 is square, it covers circle if it's side is >= 2x radius * it covers square if it's side is >= other side * it covers rectangle if it's side >= both sides */ if ( s1 instanceof Square ) { if ( s2 instanceof Circle ) return ((Square)s1).getSide() >= ( ((Circle) s2).getRadius() * 2 ); if ( s2 instanceof Square ) return ((Square) s1).getSide() >= ( (Square) s2).getSide(); if ( s2 instanceof Rectangle ) return ((Square) s1).getSide() >= ((Rectangle) s2).getWidth() && ((Square) s1).getSide() >= ((Rectangle) s2).getLength(); } /* * if s1 is rectangle, it covers circle if both sides >= 2x radius * it covers square if both sides >= side * it covers rectangle if corresponding side > side */ if ( s1 instanceof Rectangle ) { if ( s2 instanceof Circle ) return ((Rectangle) s1).getWidth() >= ( ((Circle) s2).getRadius()*2 ) && ((Rectangle) s1).getLength() >= ( ((Circle) s2).getRadius()*2 ); if ( s2 instanceof Square ) return ((Rectangle) s1).getWidth() >= ((Square) s2).getSide() && ((Rectangle) s1).getLength() >= ((Square) s2).getSide(); if ( s2 instanceof Rectangle ) return ((Rectangle) s1).getWidth() >= ((Rectangle) s2).getWidth() && ((Rectangle) s1).getLength() >= ((Rectangle) s2).getLength(); } System.out.println( "Error - can't happen" ); return false; } }