CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017

Lab 7: Quadratics

In this lab, you will create a class that allows one to use quadratic functions. You should start off with s1 driving and s2 navigating.

1. Introductions

Introduce yourself to your lab partner. Tell your lab partner about your plans for fall break.

Quadratic functions

In this lab you will familiarize yourself with the java representation of objects by working with a class which supports quadratic functions.

Complete the methods in Quadratic.java, as shown below. After you complete each method, demonstrate it to your instructor. Change the driver/navigator roles after each method. You will also need the Complex class from last week and the Line.java and Point2D.java files from the W: drive; copy these to your H: drive and make sure they are all in the same folder.
 ``` public class Quadratic { // three double instance variables representing a, b, and c. double a, b, c; // Constructor that takes three double parameters representing a, b, and c. public Quadratic(double a\$, double b\$, double c\$) { } // Constructor that copies a Quadratic public Quadratic(Quadratic q) { } // printable form of this quadratic public String toString() { return "0 x^2 + 0 x + 0"; } // returns a double representing quadratic function evaluated at x. public double f(double x) { return 0.0; } // returns the discriminant of this quadratic: (b^2 - 4 a c) public double discriminant() { return 0.0; } // returns a boolean that is true if the graph of the quadratic opens up public boolean concaveUp() { return false; } // returns a boolean that is true if the graph of the quadratic opens down public boolean concaveDown() { return false; } // returns an int that represents the number of real roots of this quadratic // 0 - both roots are complex when the discriminant is negative // 1 - both roots are equal and real when the discriminant is zero // 2 - both roots are real an distinct when the discriminant is positive public int realRootCount() { return 0; } // returns a Complex value that represents the first root of the polynomial // Hint: Use the discriminant and realRootCount functions public Complex root1() { return new Complex(0.0D, 0.0D); } // returns a Complex value that represents the first root of the polynomial // Hint: Use the discriminant and realRootCount functions public Complex root2() { return new Complex(0.0D, 0.0D); } // returns a Point2D representing the vetex of the parabola // Recall that the vertex of the parablola is given by (-b/(2a), f(-b/(2a))) public Point2D vertex() { return new Point2D(0.0, 0.0); } // returns a double that is the insantaneous slope of this Quadratic at x: 2 a x + b public double slope(double x) { return 0.0; } // returns a line that represents the derivative of this Quadratic at x // 2 a x + b is the slope // (x, f(x)) is a point on the line // Use point-slope equation to determine the y intercept public Line derivative(double x) { return new Line(0.0, 0.0); } // integral-ish helper method // Define F(x) = (a/3)x^3 + (b/2)x^2 + c x double F(double x) { return 0.0; } // returns a double representing the definite integral from x1 < x2 of this quadratic. // intgrate(x1,x2) returns F(x2) - F(x1). public double integrate(double x1, double x2) { return 0.0; } public static void main(String args[]) { Quadratic q1 = new Quadratic(1, -5, 6); System.out.println(q1); // 1.0 x^2 + -5.0 x + 6.0 System.out.println(q1.concaveUp()); // true System.out.println(q1.concaveDown()); // false System.out.println(q1.f(0)); // 6 System.out.println(q1.f(3)); // 0 System.out.println(q1.f(2)); // 0 System.out.println(q1.vertex()); // (2.5, -0.25) System.out.println(q1.f(q1.vertex().x)); // -0.25 System.out.println(q1.slope(4)); // 3 System.out.println(q1.integrate(0,2)); // 4.66666666 System.out.println(q1.discriminant()); // 1.0 System.out.println(q1.realRootCount()); // 2 System.out.println(q1.root1()); // 3.0 System.out.println(q1.root2()); // 2.0 System.out.println(q1.derivative(1)); // y = -3.0 x + 5.0 System.out.println("- - - - - - - - - - - - - - - - -"); Quadratic q2 = new Quadratic(-1,-2,-3); System.out.println(q2); // -1.0 x^2 + -2.0 x + -3.0 System.out.println(q2.concaveUp()); // false System.out.println(q2.concaveDown()); // true System.out.println(q2.f(0)); // -3 System.out.println(q2.f(3)); // -18 System.out.println(q2.f(2)); // -11 System.out.println(q2.vertex()); // (-1,-2) System.out.println(q2.f(q2.vertex().x)); // -2 System.out.println(q2.slope(4)); // -10 System.out.println(q2.integrate(0,2)); // -12.66666666 System.out.println(q2.discriminant()); // -8 System.out.println(q2.root1()); // -1.0+1.4142135623730951i System.out.println(q2.root2()); // -1.0-1.4142135623730951i System.out.println(q1.realRootCount()); // 2 System.out.println(q2.derivative(1)); // y = -4.0 x + -2.0 } } ```

When you are finished, email your lab files to your lab partner and instructor.

Copyright © 2017, David A. Reimann. All rights reserved.