From 9063cf210897d85bda4e79c2c46cc82a13cf20dd Mon Sep 17 00:00:00 2001 From: Molly-19 Date: Sun, 25 Feb 2024 21:47:07 -0800 Subject: [PATCH] Molly Severdia Assignment 005 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 +++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 53 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/Quadratic.java 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..1174bde --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,53 @@ +import java.util.Scanner; + +public class Quadratic { + + public static void main(String[] args){ + + Scanner in = new Scanner(System.in); + double a, b, c; + System.out.println("Solve the quadratic equation ax\u00B2 + bx +c = 0"); + + System.out.println("Enter a value for a: "); + if (in.hasNextDouble()){ + a = in.nextDouble(); + if (a == 0){ + System.err.println("a cannot be 0"); + return; + } + } else { + System.err.println("Value must be a real number."); + return; + } + + System.out.println("Enter a value for b: "); + if (in.hasNextDouble()){ + b = in.nextDouble(); + } else { + System.err.println("Value must be a real number."); + return; + } + + System.out.println("Enter a value for c: "); + if (in.hasNextDouble()){ + c = in.nextDouble(); + } else { + System.err.println("Value must be a real number."); + return; + } + + // calculate determinant and solution(s) + double det = Math.pow(b,2) - 4*a*c; + if (det < 0){ + System.err.println("There are no real number solutions."); + } else if (det == 0){ + double solution = -b / (2*a); + System.out.printf("The solution is %f", solution); + } else { + double sol1, sol2; + sol1 = (-b - Math.sqrt(Math.pow(b,2)) - 4*a*2)/(2*a); + sol2 = (-b + Math.sqrt(Math.pow(b,2)) - 4*a*2)/(2*a); + System.out.printf("The solutions are %f and %f", sol1, sol2); + } + } +}