Last active
November 12, 2020 13:02
-
-
Save bowmana/9bc2795679f6addaf2d844a0c724255b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Alex Bowman | |
* HW#10 | |
* Professor Silvestri | |
* 12/12/19 | |
*/ | |
package quadratic; | |
public class Quadratic { | |
private double a, b, c; | |
public Quadratic(double a, double b, double c) { | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
} | |
public int noOfRoots() { | |
double quad = b * b - 4 * a * c; | |
if (quad == 0) | |
return 1; | |
else if (quad > 0) | |
return 2; | |
else | |
return 0; | |
} | |
public double getRoot1() { | |
double d = b * b - 4 * a * c; | |
d = Math.sqrt(d); | |
double quad = (-b + d) / (2 * a); | |
return quad; | |
} | |
public double getRoot2() { | |
double d = b * b - 4 * a * c; | |
d = Math.sqrt(d); | |
double quad = (-b - d) / (2 * a); | |
return quad; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Alex Bowman | |
* HW#10 | |
* Professor Silvestri | |
* 12/12/19 | |
*/ | |
package quadratic; | |
import java.util.Scanner; | |
public class QuadraticDriver { | |
private final static String TITLE = "Quadratic Equation Solver V1.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
//********************************************** | |
// Put as many methods you need here | |
//********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter coefficients of the Quadratic: "); | |
double a = sc.nextDouble(); | |
double b = sc.nextDouble(); | |
double c = sc.nextDouble(); | |
sc.nextLine(); // Clear Keyboard | |
Quadratic q = new Quadratic(a, b, c); | |
int noOfRoots = q.noOfRoots(); | |
if (noOfRoots == 2) { | |
double r1 = q.getRoot1(); | |
double r2 = q.getRoot2(); | |
System.out.printf("Two Roots = %.3f, %.3f\n", r1, r2); | |
} | |
else if (noOfRoots == 1) { | |
double r = q.getRoot1(); | |
System.out.printf("One Root = %.3f\n", r); | |
} | |
else { | |
System.out.println("No Real Roots\n"); | |
} | |
} | |
//********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
// Do not change the main method | |
public static void main(String args[]) { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment