7. Design a Java class for quadratic equations. Solution public class QuadraticE
ID: 3640782 • Letter: 7
Question
7. Design a Java class for quadratic equations.Explanation / Answer
public class QuadraticEquations { private double aConstant; private double bConstant; private double cConstant; public QuadraticEquations(double pA, double pB, double pC) { aConstant = pA; bConstant = pB; cConstant = pC; } public double getA() { return aConstant; } public double getB() { return bConstant; } public double getC() { return cConstant; } public boolean hasSolutions() { if(2 * aConstant != 0) { if(((bConstant*bConstant) - 4 * aConstant * cConstant ) >= 0) { return true; } } return false; } public double getFirstSolution() { double firstSolution = aConstant; if(hasSolutions()) { firstSolution = (-bConstant + Math.sqrt(bConstant * bConstant - 4 * aConstant * cConstant))/(2 * aConstant); } return firstSolution; } public double getSecondSolution() { double firstSolution = aConstant; if(hasSolutions()) { firstSolution = (-bConstant - Math.sqrt(bConstant * bConstant - 4 * aConstant * cConstant))/(2 * aConstant); } return firstSolution; } public String getSolutions() { if(!hasSolutions()) { return "The equation " + aConstant + "x + " + bConstant + "x + " + cConstant + "x Has no solutions"; } else { return "the first solution is x = " + getFirstSolution() + " the second solution is x = " + getSecondSolution(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.