From 185191a3bcf4f9156382edccc81f8fc97e023055 Mon Sep 17 00:00:00 2001 From: Ricardo M <95rikym@gmail.com> Date: Sun, 25 Feb 2024 12:06:30 -0800 Subject: [PATCH] done with assignment 005 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 +++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 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..ea71d50 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..8a5c7d3 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,58 @@ +import java.util.Scanner; + +public class Quadratic { + + public static void main(String[] args) { + + int a; + int b; + int c; + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter an amount for a: "); + if (!scanner.hasNextDouble()) { + String word = scanner.next(); + System.err.println(word + " is not a number"); + return; + } + a = scanner.nextInt(); + + System.out.print("Enter an amount for b: "); + if (!scanner.hasNextDouble()) { + String word = scanner.next(); + System.err.println(word + " is not a number"); + return; + } + b = scanner.nextInt(); + + System.out.print("Enter an amount for c: "); + if (!scanner.hasNextDouble()) { + String word = scanner.next(); + System.err.println(word + " is not a number"); + return; + } + c = scanner.nextInt(); + + quadriticEquation(a, b, c); + + + } + + public static void quadriticEquation(int a, int b, int c) { + + double discriminant = (b * b) - (4 * a * c); + double divide2A = (2 * a); + + if (discriminant < 0 || divide2A == 0) { + // Discriminant is negative & Division by zero, no real roots + System.out.println("No real roots exist for the given quadratic equation."); + } else { + double plusX = ((-b) + Math.sqrt(discriminant)) / divide2A; + double minusX = ((-b) - Math.sqrt(discriminant)) / divide2A; + + System.out.printf("The solutions are: x1 = %.02f, x2 = %.02f", plusX, minusX); + } + + } + +}