Solutions to the Try It Out Exercises in Java For Dummies, 8th Edition
by Barry Burd

Chapter 15: Fancy Reference Types

In this chapter:

Control Alt Delete

public interface Deletable {
    public void detele();
}


public class DeletableColumnOfNumbers extends ColumnOfNumbers
                                      implements Deletable {

    public DeletableColumnOfNumbers(double[] numbers) {
        super(numbers);
    }

    @Override
    public void detele() {
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = 0.0;
        }
    }

}


public class DeletableTable extends Table implements Deletable {

    public DeletableTable(String fileName) {
        super(fileName);
    }

    @Override
    public void detele() {
        for (int i = lines.size() - 1; i > 0; i--) {
            lines.remove(i);
        }
    }
}



public class Main {

    public static void main(String[] args) {
        double numbers[] = { 21.7, 68.3, 5.5 };
        var column = new DeletableColumnOfNumbers(numbers);

        deleteMe(column);
        displayMe(column);

        DeletableTable table = new DeletableTable("MyTable.txt");

        deleteMe(table);
        displayMe(table);
    }

    static void displayMe(Displayable displayable) {
        displayable.display();
        System.out.println();
    }

    static void summarizeMe(Summarizable summarizable) {
        System.out.println(summarizable.summarize());
        System.out.println();
    }

    static void deleteMe(Deletable deletable) {
        deletable.detele();
    }
}

Abstract Art

abstract public class Shape {
  int size;

  public Shape(int size) {
    this.size = size;
  }

  abstract public void show();
}

import static java.lang.System.out;

public class Square extends Shape {

  public Square(int size) {
    super(size);
  }

  @Override
  public void show() {
    out.print(" ");
    for (int i = 1; i < size; i++) {
      out.print("--");
    }

    out.println();

    for (int row = 1; row < size - 1; row++) {
      out.print("|");
      for (int i = 1; i < size; i++) {
        out.print("  ");
      }
      out.print("|");
      out.println();
    }

    out.print(" ");
    for (int i = 1; i < size; i++) {
      out.print("--");
    }
    out.println();
  }
}

import static java.lang.System.out;

public class Triangle extends Shape {

  public Triangle(int size) {
    super(size);
  }

  @Override
  public void show() {
    for (int row = 0; row < size; row++) {
      for (int i = 0; i < size - row; i++) {
        out.print(" ");
      }
      out.print("/");
      for (int i = size - row; i < size + row; i++) {
        out.print(" ");
      }
      out.print("\\");
      out.println();
    }
    out.print(" ");
    for (int i = 0; i < size * 2; i++) {
      out.print("-");
    }
    out.println();
  }
}

public class Main {
  public static void main(String[] args) {
    var square1 = new Square(5);
    square1.show();

    var triangle1 = new Triangle(7);
    triangle1.show();

    var square2 = new Square(3);
    square2.show();

    var triangle2 = new Triangle(4);
    triangle2.show();

    var triangle3 = new Triangle(2);
    triangle3.show();
  }
}

Gooey Art

import java.awt.Graphics;
import javax.swing.JFrame;

public class ArtFrame extends JFrame {
    Shape shape;

    public ArtFrame(Shape shape) {
        this.shape = shape;
        setTitle("Abstract Art");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        shape.paint(g);
    }
}

import java.awt.Color;
import java.awt.Graphics;

abstract public class Shape {
  int size;
  Color color;
  boolean isFilled;

  public Shape(int size, Color color, boolean isFilled) {
    this.size = size;
    this.color = color;
    this.isFilled = isFilled;
  }

  abstract public void paint(Graphics g);
}

import java.awt.Color;
import java.awt.Graphics;

public class Square extends Shape {

  public Square(int size, Color color, boolean isFilled) {
    super(size, color, isFilled);
  }

  @Override
  public void paint(Graphics g) {
    g.setColor(color);
    if (isFilled) {
      g.fillRect(size/2, size/2, size, size);
    } else {
      g.drawRect(size/2, size/2, size, size);
    }
  }

}


import java.awt.Color;
import java.awt.Graphics;

public class Circle extends Shape {

  public Circle(int size, Color color, boolean isFilled) {
    super(size, color, isFilled);
  }

  @Override
  public void paint(Graphics g) {
    g.setColor(color);
    if (isFilled) {
      g.fillOval(size/2, size/2, size, size);
    } else {
      g.drawOval(size/2, size/2, size, size);
    }
  }

}


import java.awt.Color;

public class Main {
  public static void main(String[] args) {
    var circle = new Circle(100, Color.BLUE, true);
    var square = new Square(200, Color.RED, false);

    var frame1 = new ArtFrame(circle);
    frame1.setSize(200, 200);
    frame1.setVisible(true);

    var frame2 = new ArtFrame(square);
    frame2.setSize(500, 500);
    frame2.setVisible(true);
  }
}