Source of TestCircles.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to demonstrate the equals method using the Circle class.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class TestCircles {

 11:     public static void main(String[] args) {
 12:         // Create variables
 13:         Circle c1, c2, c3;
 14:         String c2String;
 15:         Object other = null;
 16:         Circle c5 = null;

 18:         // Introduce yourself
 19:         System.out.println("I report on equality of circles!");
 20:         System.out.println();
 21:         System.out.println("NOTE: I expect to crash "
 22:                 + "with a NullPointerException!");
 23:         pause();

 25:         // Calculate results
 26:         c1 = new Circle(10.0);
 27:         c2 = new Circle(5.5);
 28:         c3 = new Circle(5.5);
 29:         c2String = c2.toString();

 31:         // Report results
 32:         System.out.println(c1 + " == " + c2 + " is " + (c1 == c2));
 33:         System.out.println(c1 + " == " + c3 + " is " + (c1 == c3));
 34:         System.out.println(c2 + " == " + c3 + " is " + (c2 == c3));
 35:         pause();
 36:         System.out.println(c1 + ".equals(" + c2 + ") is " + c1.equals(c2));
 37:         System.out.println(c1 + ".equals(" + c3 + ") is " + c1.equals(c3));
 38:         System.out.println(c2 + ".equals(" + c3 + ") is " + c2.equals(c3));
 39:         pause();
 40:         System.out.println(c2 + ".equals(\"" + c2String + "\") is "
 41:                 + c2.equals(c2String));
 42:         System.out.println(c2 + ".equals(" + other + ") is " 
 43:                 + c2.equals(other));
 44:         System.out.println(c2 + ".equals(" + c5 + ") is " + c2.equals(c5));
 45:         System.out.println("...about to crash...");
 46:         System.out.println(c5 + ".equals(" + other + ") is " 
 47:                 // NOTE: this will CRASH the program -- that's expected
 48:                 + c5.equals(other));
 49:     }
 50:     
 51:     private static final Scanner KBD = new Scanner(System.in);

 53:     /**
 54:      * Prompt the user and wait for them to press the enter key
 55:      */
 56:     private static void pause() {
 57:         System.out.println();
 58:         System.out.print("...press enter...");
 59:         KBD.nextLine();
 60:         System.out.println();
 61:     }

 63: }