// Defines a Rectangle object that can be // displayed in an output file. import java.io.FileNotFoundException; public class Rectangle extends Shape { private int width; private int height; public Rectangle(char theSymbol, int xPos, int yPos, int theWidth, int theHeight) { super(theSymbol, xPos, yPos); width = theWidth; height = theHeight; } public int getWidth( ) { return width; } public int getHeight( ) { return height; } public String toString( ) { return String.format("%s %d %d", super.toString( ), width, height); } public void plot(Canvas theCanvas) { for(int j = y; j < y + height; j++) for(int i = x; i < x + width; i++) theCanvas.setPoint(symbol, i, j); } public static void main(String[ ] args) throws FileNotFoundException { // Create objects. Canvas c = new Canvas("output.txt", 100, 50); Rectangle r = new Rectangle('*', 35, 10, 22, 20); Rectangle s = new Rectangle('&', 20, 15, 25, 22); // Test Rectangle methods for r. System.out.println("Width for r is " + r.getWidth( )); System.out.println("Width for r is " + r.getHeight( )); System.out.println("toString output for r is"); System.out.println(r); System.out.println( ); // Test Rectangle methods for s. System.out.println("Width for s is " + s.getWidth( )); System.out.println("Width for s is " + s.getHeight( )); System.out.println("toString output for s is"); System.out.println(s); System.out.println( ); // Plot rectangles on canvas. r.plot(c); s.plot(c); // Write canvas to output file. c.write( ); } }