Comment in code to see what breaks.
public class Main {
public static void main(String[] args) {
// Human a = new Human("Fred");
// // OK to cast a subclass object as its superclass
// Animal b = a;
// System.out.println(b);
// Animal c = new Animal("Wanda");
// // ClassCastException on line 9:
// // Never cast a superclass object as its subclass
// Human d = (Human) c;
// System.out.println(c);
// int[] nums = new int[4];
// // Note: an array of numbers defaults to zeroes if not initialized
// System.out.println(nums[3]);
// // ArrayIndexOutOfBoundsException
// System.out.println(nums[4]);
// // Creates five null references; new needs to work with
// // the parenthesis to be an operator that creates an object
// Human[] humans = new Human[5];
// // Human habib = new Human("Habib");
// // humans[0] = habib;
// System.out.println(humans[0]);
// humans[0].eat();
// // ArithmeticException
// System.out.println(1/0);
// double[] quad = quadratic(1,2,3);
// // Intentionally throws an IllegalArgumentException if
// // no real number solutions (see code in quadratic method)
// System.out.println("{ " + quad[0] + ", " + quad[1] + " }");
}
public static double discriminant(double a, double b, double c) {
return b*b - 4*a*c;
}
public static double[] quadratic(double a, double b, double c) {
if (discriminant(a,b,c) < 0) { // Don't want to do Math.sqrt on negative number
throw new IllegalArgumentException("\nNo real solutions for: \na = " + a + ", b = " + b + ", c = " + c);
} else {
double[] solution = new double[2];
solution[0] = (-b - Math.sqrt(discriminant(a,b,c)))/(2 * a);
solution[1] = (-b + Math.sqrt(discriminant(a,b,c)))/(2 * a);
return solution;
}
}
}