diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Java-Assignment-005.iml b/Java-Assignment-005.iml index b46c0dd..e1006ff 100644 --- a/Java-Assignment-005.iml +++ b/Java-Assignment-005.iml @@ -5,6 +5,7 @@ + \ No newline at end of file diff --git a/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..529bc23 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,54 @@ +import java.util.Scanner; +/** + * + * @author Trevor Hartman + * @author Eliot Rodriguez + * + * @since version 1.0 + * February 22, 2024 + * + */ +public class Quadratic { + static Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) { + System.out.println("Calculate the roots of the quadratic equation ax^2+bx+c=0 by inputting three integers."); + System.out.println("Input a value for a: "); + if (!scanner.hasNextInt()) { //validates the input to ensure that it is an integer + String invalidInt = scanner.next(); //makes the invalid input the value of invalidInt + System.out.println(invalidInt + " is an invalid input");//Uses invalidInt to display to the user that the input is invalid + return; + } + int a = scanner.nextInt(); + if (a==0) { + System.err.println("To avoid division by 0, a cannot be 0"); + return; + } + System.out.println("Input a value for b: "); + if (!scanner.hasNextInt()) { + String invalidInt = scanner.next(); + System.out.println(invalidInt + " is an invalid input"); + return; + } + int b = scanner.nextInt(); + System.out.println("Input a value for c: "); + if (!scanner.hasNextInt()) { + String invalidInt = scanner.next(); + System.out.println(invalidInt + " is an invalid input"); + return; + } + int c = scanner.nextInt(); + double stepOne = Math.pow(b, 2.0) - (4.0 * a * c); //evaluating the square root of b^2-4ac + if (stepOne > 0) { //avoids taking the square root of a negative number + double stepTwo = (double) -b+(Math.sqrt(stepOne))/2*a; //evaluating the formula for -b + + double stepThree = (double) -b-(Math.sqrt(stepOne))/2*a;//evaluating the formula for -b - + System.out.printf("With the given inputs the roots are = %f, %f", stepTwo, stepThree); + } + else if (stepOne == 0) { //For when there is only one solution + double stepFour = (double) -b/2*a; //Since stepOne equals 0, this would have to be the equation + System.out.printf("With the given inputs the root is %f", stepFour); + } else { + System.out.println("There is no real solution"); //there would have to be no real solutions for it to get to this part + } + } +} \ No newline at end of file