Question
In JAVA please. Please answer all questions completely. Will LIKE if satisfied. All information has been provided. public class Roots { private Complex4 rootA; private
In JAVA please. Please answer all questions completely. Will LIKE if satisfied. All information has been provided.
public class Roots { private Complex4 rootA; private Complex4 rootB; public Roots(Quadratic quad) { findRoots(quad.getA(), quad.getB(), quad.getC()); } public void findRoots(double a, double b, double c) { rootA = new Complex4(); rootB = new Complex4(); double sqrt = b*b - (4 * a * c); double real = -b / (2*a); if (sqrt } else { // only real part rootA.setRealPart(real + (Math.sqrt(sqrt) / (2*a))); rootB.setRealPart(real - (Math.sqrt(sqrt) / (2*a))); } } public String toString() { return "Root A: " + rootA + " Root B: " + rootB; } // accessors public Complex4 getRootA() { return rootA; } public Complex4 getRootB() { return rootB; } } |
public class Complex4 { private double realPart = 0; private double imaginaryPart = 0; public Complex4(double a, double b) { realPart = a; imaginaryPart = b; } public Complex4() {} // accessor methods public void setRealPart(double value) { realPart = value; } public void setImaginaryPart(double value) { imaginaryPart = value; } public double getRealPart() { return realPart; } public double getImaginaryPart() { return imaginaryPart; } // operators public void addComplex(Complex4 c) { realPart = realPart + c.getRealPart(); imaginaryPart = imaginaryPart + c.getImaginaryPart(); } public void subtractComplex(Complex4 c) { realPart = realPart - c.getRealPart(); imaginaryPart = imaginaryPart - c.getImaginaryPart(); } public String toString() { return "" + realPart + "+" + imaginaryPart + "i"; } // NOTE: this is comparing real numbers for equality // Does the code below have to be revised to ensure it will work correctly? public boolean equals(Object anotherObject) { Complex4 temp; boolean same = false; if (anotherObject instanceof Complex4) { temp = (Complex4) anotherObject; if (realPart == temp.getRealPart() && imaginaryPart == temp.getImaginaryPart()) same = true; } return same; } } // Complex4 class |
In this part you will create a Quadratic class for dealing with quadratic functions. A quadratic function is a function of the form ax2+bx+c. Create a Quadratic class with the following methods: - public Quadratic(double a, double b, double c) Set up a quadratic function with the given coefficients. - public Quadratic add(Quadratic other): This method should add the other Quadratic function to the current quadratic function and return the result as a new Quadratic. - public Quadratic subtract(Quadratic other): This method should subtract the other Quadratic function from the current quadratic function and return the result as a new Quadratic. public Roots findRoots(): This method should use the quadratic formula. to find the roots of the current quadratic function, returning an instance of the Roots class containing those roots. The Roots class is provided to you along with the project write-up and contains a Complex4 variable for storing each of the two possible roots. - public String toString(): This method should return a String representation of the current quadratic function, e.g. 9x24x+1. For up to 5 extra credit points, format the string as you would if you were writing the equation by hand; for example, x2+1 or 5x24x+3 instead of x2+0x+1 or 5x2+4x+3 - public boolean equals(Object other): This method should return true if the current quadratic function is equal to other quadratic function and false otherwise. Note: Since the coefficients are doubles, comparing them directly may not work due to floating point imprecision. Instead of directly testing if two numbers are equal, check if their difference is within some tolerance, e.g. 0.0001. - A TestQuadratic Class: This class should contain a main function which demonstrates that all the methods in the Quadratic class work correctly via well-documented, well-formatted tests of the methods defined in the Quadratic class. - The TestQuadratic class should implement a main method that instantiates a number of quadratic functions, and tests the operations defined in the Quadratics class on the quadratic functions you instantiate (that is, add 2 quadratic functions, subtract two quadratic functions, and test to determine if two quadratic functions are equal or not). - Your main method in TestQuadratic should print out the each of the results of each addition and subtraction using the toString method defined in the Quadratic class - When testing to determine if two quadratic equations are equal, your main method should print out the two quadratic equations being compared with the toString method defined in the Quadratic class, and the result of the comparison (that is, True or False). - Your main method should print out the Roots found by the findRoots() method using the toString method you define for Roots or other classes you define and use. You may want to include getters and setters for the coefficients in your implementation of Quadratic
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started