Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*****************JAVA******************** A one-variable quadratic expression is

ID: 665317 • Letter: #

Question

*****************JAVA********************

A one-variable quadratic expression is an arithmetic expression of the form ax2+bx +c, where a, b, and c are some fixed numbers (called coefficients) and x is a variable that can take on different values. 1. Specify, design and implement a class named Quadratic that can store information about a quadratic expression. The constructor should set all three coefficients to zero, and another method should allow you to change these coefficients. There should be accessor methods to retrieve the current values of the coefficients. There should also be a method to allow you to evaluate the quadratic expression at a particular value of x, that is, the method has one parameter x and returns the value of the expression ax2+bx +c. Also write the following methods to perform these indicated operations: public static Quadratic sum(Quadratic q1, Quadratic q2) // Post-condition: The return value is the quadratic expression obtained by adding // q1 and q2. For example, the c coefficient of the return value is the sum of q1’s c // coefficient and q2’s c coefficient. public static Quadratic scale(double r, Quadratic q2) // Post-condition: The return value is the quadratic expression obtained by // multiplying each of q’s coefficients by the number r. public boolean equals(Object q) // Post-condition: returns true if and only if q refers to a Quadratic object and its // state is the same as this Quaratic object. public Quadratic clone() // Post-condition: returns a separate copy of this Quadratic object. public String toString() // Post-condition: returns a string representation of this Quadratic object. 2. Implement a separate class named QuadraticDemo that contains a main method that creates Quadratic objects and demonstrates Quadratic methods. Every method except the constructor need be demonstrated.

Explanation / Answer

import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * A class used to test the * functionality of the Quadratic class. * * Date: 9-22-14 * * @author Ryan Hochmuth & Jason Griffith * */ public class QuadTest { /*********************** * Quadratics **********************/ private static Quadratic quad1 = new Quadratic(); private static Quadratic quad2 = new Quadratic(); // quad3 is the sum of quad1 and quad2 private static Quadratic quad3 = new Quadratic(); /*********************** * Output Variables **********************/ private static int testNum = 0; private static String output = ""; /** * Gives a description of what * the program is doing. */ public static void intro() { System.out.println("This program is used to read data " + "from a text file and use that data " + "to create new Quadratics, then do " + "various calculations with them. "); } /** * Does the required calculation, * creates a clone of the first quadratic, * checks if they are aliases, then checks * if the two quadratics have the same coefficients. */ public static void calculations(double xVal, double scale, String line, boolean wrongInput) { // Do calculations and set up the output if (!wrongInput) { output += "*************************************************************" + " " + "Test # " + testNum + " " + line + " " + "-----------" + " " + "The first quadratic is: " + quad1.toString() + " " + "The value of the first quadratic expression when x = " + xVal + " is: " + (int)quad1.evalExpression(xVal) + " " + "The first quadratic after scaling with a multiplier of " + scale + " is: " + Quadratic.scale(scale, quad1).toString() + " "; switch ((int)quad1.getRootNum()) { case 0: output += "The number of roots for the first quadratic is: 0"; break; case 1: output += "The number of roots for the first quadratic is: " + (int)quad1.getRootNum() + " The value of the root is: " + " " + " Value of root 1: " + (int)quad1.getRootOne(); break; case 3: output += "The number of roots for the first quadratic is infinite."; break; default: output += "The number of roots for the first quadratic is: "+(int)quad1.getRootNum() + " The values of those roots are: " + " " + " Value of root 1: " + (int)quad1.getRootOne() + " " + " Value of root 2: " + (int)quad1.getRootTwo(); break; } output += " " + "The second quadratic is: " + quad2.toString() + " " + "The quadratic which is the sum of the first and the second quadratics is: " + quad3.toString() + " " + "A clone of the first quadratic is: " + quad1.clone(quad1).toString() + " "; if (quad1.equals(quad1.clone(quad1))) output += "The first quadratic and the clone are not aliases, but are equal to each other." + " "; else output += "The first quadratic and the clone are not aliases, and are not equal to each other." + " "; } else { output += "*************************************************************" + " " + "Test # " + testNum + " " + line + " " + "-----------" + " " + "Improper data entered for this test." + " "; } } /** * Parses a String line into * Separate tokens. * @param line - The input String */ public static void parse(String line) { double xVal = 0.0; double scale = 0.0; String[] tokens = line.split("\s+"); // Splits line by consecutive spaces testNum++; // Increment the number of tests (used for output) if (tokens.length != 8) // Returns if input line has too few digits { calculations(xVal, scale, line, true); // Run the calculations method return; } // Increment through all 8 input digits for(int x = 0; x < 8; x++) { try { // Depending on which input character is being // processed, set the values for the quadratics switch (x) { case 0: quad1.setCoefA(Double.parseDouble(tokens[x])); break; case 1: quad1.setCoefB(Double.parseDouble(tokens[x])); break; case 2: quad1.setCoefC(Double.parseDouble(tokens[x])); break; case 3: xVal= Double.parseDouble(tokens[x]); break; case 4: scale= Double.parseDouble(tokens[x]); break; case 5: quad2.setCoefA(Double.parseDouble(tokens[x])); break; case 6: quad2.setCoefB(Double.parseDouble(tokens[x])); break; case 7: quad2.setCoefC(Double.parseDouble(tokens[x])); break; } // End switch } // End try catch (NumberFormatException e) { e.printStackTrace(); calculations(xVal, scale, line, true); // Run the calculations method return; } } // End for loop // Set quad3 to the sum of quad1 and quad2 quad3 = Quadratic.sum(quad1, quad2); calculations(xVal, scale, line, false); // Run the calculations method } /** * Create the output of information * generated by this program. */ public static void output() { // The BufferedWrite to write the text file BufferedWriter writer = null; // The file to write to File outputFile = new File("src/output.txt"); // Attempt to write to the file try { // Pair the BufferedWriter with the proper file writer = new BufferedWriter(new FileWriter(outputFile)); // Write the prepared output String to the file writer.write(output); } catch (IOException e) { e.printStackTrace(); } finally { try { // Close the BufferedWriter writer.close(); } catch(Exception e) { e.printStackTrace(); } } } } Quadratic.java public class Quadratic implements Cloneable { // The coefficients of this quadratic private double coefA; private double coefB; private double coefC; /** * Default Constructor */ public Quadratic() { this.coefA = 0; this.coefB = 0; this.coefC = 0; } /** * Creates a new Quadratic with set coefficients. * @param coefA * @param coefB * @param coefC */ public Quadratic(double coefA, double coefB, double coefC) { this.coefA = coefA; this.coefB = coefB; this.coefC = coefC; } /** * Evaluate this quadratic with the * given value of x. * @param x - the value of x * @return int - the evaluated answer */ public double evalExpression(double x) { return (coefA * Math.pow(x, 2)) + (coefB * x) + coefC; } /** * Add the two given quadratics together. * @param q1 - the first quadratic * @param q2 - the second quadratic * @return Quadratic - the result of q1 + q2 */ public static Quadratic sum(Quadratic q1, Quadratic q2) { return new Quadratic(q1.getCoefA() + q2.getCoefA(), q1.getCoefB() + q2.getCoefB(), q1.getCoefC() + q2.getCoefC()); } /** * Scale the given quadratic by the given number. * @param mult - the multiplier to scale by * @param q - the quadratic to scale * @return Quadratic - the scaled quadratic */ public static Quadratic scale(double mult, Quadratic q) { return new Quadratic(q.getCoefA() * mult, q.getCoefB() * mult, q.getCoefC() * mult); } /** * Get how many real roots this quadratic has. * @return int - the number of roots this quadratic has */ public double getRootNum() { if (coefA == 0 && coefB == 0 && coefC == 0) return 3; else if (coefA == 0 && coefB == 0 && coefC != 0) return 0; else if (coefA == 0 && coefB != 0) return 1; else if (coefA != 0 && Math.pow(coefB, 2) < 4 * coefA * coefC) return 0; else if (coefA != 0 && Math.pow(coefB, 2) == 4 * coefA * coefC) return 1; else if (coefA != 0 && Math.pow(coefB, 2) > 4 * coefA * coefC) return 2; else return 0; } /** * Get the first root of this quadratic. * @return int - the first root */ public double getRootOne() { double posRoot = (-coefB + (Math.sqrt((coefB * coefB - ((4 * coefA * coefC)))))) / (2 * coefA); double negRoot = (-coefB - (Math.sqrt((coefB * coefB - ((4 * coefA * coefC)))))) / (2 * coefA); if (getRootNum() > 0) { if (getRootNum() == 1) { if (coefA == 0 && coefB != 0) { posRoot = (-coefC/coefB); negRoot = (-coefC/coefB); } else if (coefA != 0 && Math.pow(coefB, 2) == 4 * coefA * coefC) { posRoot = (-coefB / (2 * coefA)); negRoot = (-coefB / (2 * coefA)); } } if (posRoot 0) { if (getRootNum() == 1) { if (coefA == 0 && coefB != 0) { posRoot = (-coefC/coefB); negRoot = (-coefC/coefB); } else if (coefA != 0 && Math.pow(coefB, 2) == 4 * coefA * coefC) { posRoot = (-coefB / (2 * coefA)); negRoot = (-coefB / (2 * coefA)); } } if (posRoot > negRoot) return posRoot; else return negRoot; } else return 0; } /** * Returns the String equivalent * of this quadratic. * @return String - the quadratic as a String */ public String toString() { if (coefB > 0) { if (coefC > 0) return (int)coefA + "x^2 + " + (int)coefB + "x + " + (int)coefC; else return (int)coefA + "x^2 + " + (int)coefB + "x - " + (int)coefC * -1; } else if (coefB < 0) { if (coefC > 0) return (int)coefA + "x^2 - " + (int)coefB * -1 + "x + " + (int)coefC; else return (int)coefA + "x^2 - " + (int)coefB * -1 + "x - " + (int)coefC * -1; } else if (coefB == 0) { if (coefC > 0) return (int)coefA + "x^2 - " + (int)coefB * -1 + "x + " + (int)coefC; else return (int)coefA + "x^2 - " + (int)coefB * -1 + "x - " + (int)coefC * -1; } else { return (int)coefA + "x^2 + " + (int)coefB + "x + " + (int)coefC; } } /** * Checks to see if the given Object * is of type Quadratic. */ public boolean equals(Object obj) { if (obj instanceof Quadratic) { Quadratic quad = (Quadratic)obj; if (coefA == quad.getCoefA() && coefB == quad.getCoefB() && coefC == quad.getCoefC()) return true; else return false; } else return false; } /** * Creates a clone of the given Quadratic. * @param quad - The Quadratic to clone * @return clone - The new cloned Quadratic */ public Quadratic clone(Quadratic quad) { Quadratic clone = null; try { clone = (Quadratic)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } /** * @return the coefA */ public double getCoefA() { return coefA; } /** * @param coefA the coefA to set */ public void setCoefA(double coefA) { this.coefA = coefA; } /** * @return the coefB */ public double getCoefB() { return coefB; } /** * @param coefB the coefB to set */ public void setCoefB(double coefB) { this.coefB = coefB; } /** * @return the coefC */ public double getCoefC() { return coefC; } /** * @param coefC the coefC to set */ public void setCoefC(double coefC) { this.coefC = coefC; } }