Skip to content

Instantly share code, notes, and snippets.

@rumaisaabdulhai
Created February 27, 2021 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumaisaabdulhai/4b31001c21eb43b9ba387a420858d9c1 to your computer and use it in GitHub Desktop.
Save rumaisaabdulhai/4b31001c21eb43b9ba387a420858d9c1 to your computer and use it in GitHub Desktop.
Week 5 Second Optional HW Problem
import java.util.Arrays;
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
// version that is not interactive
double[] roots = findRoots(1, 3, 2);
// if we have roots
if (roots != null)
System.out.println(Arrays.toString(roots));
// if we do not have roots
else
System.out.println("This quadratic equation has no real roots.");
// interactive version
findRoots();
}
// version that is not interactive
public static double[] findRoots(double a, double b, double c) {
double disc = calcDisc(a, b, c);
System.out.println("The quadratic equation is y = " + a + "x^2 + " + b + "x + " + c);
if (disc < 0) {
return null;
}
else {
double r1 = calcRoot1(a, b, c);
double r2 = calcRoot2(a, b, c);
return new double[]{r1, r2};
}
}
// interactive version that requires user input
public static void findRoots() {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to calculating the roots of a quadratic equation!");
System.out.println("Remember that the form of a quadratic equation is y = ax^2+bx+c");
System.out.println("Enter the coefficient a of this quadratic equation (ex. 2.34)- ");
String a = input.nextLine();
double coefa = Double.parseDouble(a);
System.out.println("Enter the coefficent b of this quadratic equation- ");
String b = input.nextLine();
double coefb = Double.parseDouble(b);
System.out.println("Enter the coefficient c of this quadratic equation- ");
String c = input.nextLine();
double coefc = Double.parseDouble(c);
double disc = calcDisc(coefa, coefb, coefc);
// if discriminant is less than 0
if (disc < 0) {
System.out.println("This quadratic equation has no real roots. This quadratic equation has complex roots (imaginary).");
double realPart = calcRealPart(coefa, coefb, coefc);
double imagPart = calcImagPart(coefa, coefb, coefc);
System.out.println("This quadratic equation has complex roots of " + realPart + " + " + imagPart + "i" + " and " + realPart + " - " + imagPart + "i");
}
// if discriminant is greater than or equal to 0
else {
System.out.println("This quadratic equation has real roots.");
double r1 = calcRoot1(coefa, coefb, coefc);
double r2 = calcRoot2(coefa, coefb, coefc);
System.out.println("The roots of the quadratic equation y = " + coefa + "x^2 + " + coefb + "x + " + coefc + " are " + r1 + " and " + r2 + ".");
}
input.close();
}
// Calculates the discriminant of the quadratic equation
public static double calcDisc(double a, double b, double c) {
return b*b - 4*a*c;
}
// Calculates the first root of the quadratic equation
public static double calcRoot1(double a, double b, double c) {
double disc = calcDisc(a, b, c);
return (-b + Math.sqrt(disc)) / (2*a);
}
// Calculates the second root of the quadratic equation
public static double calcRoot2(double a, double b, double c) {
double disc = calcDisc(a, b, c);
return (-b - Math.sqrt(disc)) / (2*a);
}
// Calculates the real part of the root of the quadratic equation
public static double calcRealPart(double a, double b, double c) {
double r4= -b/(2*a);
return r4;
}
// Calculates the imaginary part of the root of the quadratic equation
public static double calcImagPart(double a, double b, double c) {
double disc = calcDisc(a, b, c);
return (Math.sqrt(-disc))/(2*a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment