# Polymorphism

• Goal of this Homework

Reinforce your knowledge of inheritance and polymorphism

• Relevant Material: Chapter 13 in book

• Preparation

Save the following files in your project directory (preferably, make a directory called "hw8" in your "cs170" directory, and save these files there (right click and select save target as)

You can also get the files using the command:

```    cp  /home/cheung/teaching/web/170/Projects/hw8/Handouts/*   ~/cs170/hw8
```

In the remainder of the homework description, I will assume that you have saved these file in your ~/cs170/hw8 directory.

• If you use BlueJ

• If you are using BlueJ for the lab, you must open hw8 as "Non BlueJ"

• Homework Assignment

• The file MathToolBox.class that is part of the handout, contains the compiled object code of the following class:

 ``` public class MathToolBox { public double f(double x) { return(x); } /** * solveEquation(double a, double b) */ public double solveEquation(double a, double b) { double leftPoint, rightPoint, midPoint; // Interval points double y_leftPoint, y_rightPoint, y_midPoint; // Function values /* --------------------------------------------------- Sanity check: sign f(a) and f(b) must be different --------------------------------------------------- */ if ( (f(a) > 0 && f(b) > 0) || (f(a) < 0 && f(b) < 0) ) { System.out.println("Error: interval not guaranteed to contain a zero"); return(0); // We need to return something.... } /* --------------------------------------------------- The Bisection Algorithm --------------------------------------------------- */ leftPoint = a; rightPoint = b; while ( rightPoint - leftPoint > EPSILON ) { midPoint = (leftPoint + rightPoint)/2.0; // Find the middle y_midPoint = f(midPoint); y_rightPoint = f(rightPoint); if ( (y_midPoint < 0 && y_rightPoint < 0) || (y_midPoint > 0 && y_rightPoint > 0) ) { rightPoint = midPoint; } else { leftPoint = midPoint; } } return (leftPoint + rightPoint)/2.0; } public static final double EPSILON = 0.00000001; // Minimum accuracy } ```

• This is the bisection technique that we have studied in this webpage: click here

Except that:

• the method solveEquation() is re-written as an instance method

• and solveEquation() invokes an instance method f() defined inside the MathToolBox class (see above program)

You did not receive the source code for the MathToolBox and you do NOT need it.

So, do NOT try to write the MathToolBox class using the above text.

You do NOT need to do anything with the MathToolBox class except for having a copy of the program MathToolBox.class in your project directory.

The following describes what you need to do for the project

• Write a class named Solve in the file Solve.java that "uses" the MathToolBox class to solve the equation:
```	x5 - 3 x2 - 245 = 0
```

• Complete the program in the file hw8.java that create a Solve object to solve:

```	x5 - 3 x2 - 245 = 0
```

You must assign the asnwer to the variable ans that is (already) defined. The value of this variable will be printed for grading purposes.

• Turnin

• Turn in the homework with the following command:
```   /home/cs170001/turnin   ~/cs170/hw8/hw8.java    hw8
/home/cs170001/turnin   ~/cs170/hw8/Solve.java  hw8a
```

You must turn in the file with the correct associated code !