The main purpose of this initial laboratory exercise is to:



What to do - in just 31 easy steps!

1. Launch BlueJ (or your favorite IDE).

2. Create a new project called “lab1” in an appropriate folder,

e.g. "~/cmpu102/lab1/".



ask for help if you aren't sure what this means…

3. Among other ways, use Edit → New Class to create a new class called “Function”.

4. Double click the newly created class icon.

5. Replace the template code with 4 methods:

a) An empty, public constructor.
b) A public method called "imageOf", with a type double parameter "x" and a type double
  return type. Let its body be such that it always returns "Double.NaN". Recall
  that Double is a class, not a pod-type. NaN is short for Not A Number.
c) A public method called "root", with no parameters and a double
  return type. Let its body be such that it always return "Double.NaN".
d) A public method called "toString", with no parameters and a String
  return type. Let its body be such that it always return an empty
  String, simply a pair of double quotes "".

6. Hit the compile button. Correct any potential syntax errors before proceeding.

7. Create a new class called “LinearFunction”. This class will represent a linear function described by the equation y = m*x + b.

8. Make LinearFunction a subclass of Function and compile. Note how the inheritance relationship is made visible in the main BlueJ window. This is similar to the UML diagrams we did in class.

9. Add “m” and “b” private fields - of type double - to the LinearFunction class.

10. Add a constructor method to LinearFunction that takes double values “m” and “b” as arguments and assigns the fields of the same name to them. We didn't cover the this keyword, but it allows the compiler know which m is a parameter and which m is part of the class. A statement using this would look like

 this.m = m;

11. Add an “imageOf” method to LinearFunction. It should have the same signature as Function's method of the same name but return the image of the argument x according to the linear equation, i.e. it should return y = m*x + b.

12. Add a “root” method to LinearFunction. It should have the same signature as Function's method of the same name but return the value of x at which the function intercepts the x-axis, -b/m.

13. Add a “toString” method to LinearFunction. It should have the same signature as Function's method of the same name but return a String representation of the linear function in question. E.g. if m=2.0 and b=3.0, toString should return the string “y = 2.0x + 3.0”.

14. Compile and correct any errors.

15. Create a new class called “QuadraticFunction”. This class will represent a quadratic function described by the equation y = a*x^2 + b*x + c.

16. Make QuadraticFunction a subclass of Function, just like you did with LinearFunction.

17. Add variables “a”, “b” and “c”, each of type double, as private fields to the QuadraticFunction class.

18. Add a constructor method to QuadraticFunction that takes double values “a”, “b” and “c” as arguments, and assigns the fields of the same name to them. Again use the this keyword.

19. Add an “imageOf” method to QuadraticFunction. It should have the same signature as Function's method of the same name but return the image of the argument x according to the quadratic equation. I.e. it should return y = a*x*x + b*x + c.

20. Also called the “I AM ROOT” step… Add a “root” method to QuadraticFunction. It should have the same signature as Function's method of the same name but return the value of x at which the function intercepts the x-axis, i.e. return x such that y = a*x*x + b*x + c == 0. As noted in class, the solution is given by the quadratic formula (-b +/- sqrt(b^2-4ac)) / 2a.

 Use the solution given by (-b + sqrt(b^2-4ac)) / 2a, and the static method Math.sqrt() to compute the square root. We'll cover static in a subsequent lecture.

21. Add a “toString” method to QuadraticFunction. It should have the same signature as Function's method of the same name but return a String representation of the quadratic function in question. E.g. if a=2.0, b=3.0 and c=4.0, toString should return “y = 2.0x^2 + 3.0x + 4.0”.

22. Compile your code and correct any errors before proceeding.

23. Create a new class called “FunctionTester”. This class will be used to exercise the code written up to this point.

24. Create a public static void method named “getRootAndConfirm” that takes argument f of type Function (that you already created!), computes the root of f, the image of the root of f rootImage, and finally writes, i.e. uses System.out.println(), the results to the screen in the following format:

Function f has root r. f(r) = rootImage;

Here, f represents a textual description of argument Function f, as given by its toString() method; r represents the root of f, as computed by its root() method; and rootImage represents the image of r, as computed by object f's imageOf() method.

For instance, if f is the LinearFunction y = 1.0x + 3.0, the output should be:

Function y = 1.0x + 3.0 has root -3.0. f(-3.0) = 0.0

25. Create a main method (signature public static void main(String[] args)) to test the function classes. This method should do the following:

  1. Declare and initialize two double method-local variables named m and b. Let's initiliaze them to 1 and 6, respectively.
  2. Create an object, f1, of type Function and initialize it with a new LinearFunction object created with parameters m and b. Recall, the new keyword allows Java to reserve memory for the Function object.
  3. Call getRootAndConfirm on f1.
  4. Declare and initialize three type double method-local variables named a, qb,
    and c. Initialize them to 1, 4 and 4, respectively.
  5. Declare an object f2 of type Function and initialize it with a new
    QuadraticFunction object created with parameters a, qb and c.
  6. Call getRootAndConfirm on f2.

26. Compile and correct any errors.

27. Right click the FunctionTester class icon on BlueJ's main window and select void main(String[] args) from the dropdown menu. Press OK on the dialog that is presented to you, as we did in class yesterday.

28. Confirm the output on BlueJ's terminal window. It should read:

“Function y = 1.0x + 6.0 has root -6.0. f(-6.0) = 0.0 Function y = 1.0x^2 + 4.0x + 4.0 has root -2.0. f(-2.0) = 0.0”

As you can see, the toString(), root() and image() methods that were actually executed were the ones defined in the subclasses, although we passed a more generic Function object as an argument to getRootAndConfirm(). This is the beauty of OOP: inheritance + polymorphism.

29. Call either the Professor or a Coach to inspect your work and give you credit for completing the lab.

30. Compress the folder with the code you've written today, naming the file cmpu102-lab1-yourname.tar.gz. However, please substitute your name for yourname. The command to do this is (for me) is

"tar -czvf cmpu102-lab1-petelemieszewski.tar.gz cmpu102/lab1/"

31. Unless I can setup moodle or a submit script, email the tar file to me directly: pelemieszewski .

Congratulations, you have now completed CMPU-102's lab #1!