Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Quadratic
- // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in an object-oriented program
- import java.text.DecimalFormat;
- public class Quadratic
- {
- private int numRealRoots;
- private double a, b, c, x1, x2, determinant, aos, vertexX, vertexY;
- private DecimalFormat fmt = new DecimalFormat("0.###");
- private String vertex;
- /** Creates a Quadratic with instance data set to zero by default */
- public Quadratic()
- {
- a = 0;
- b = 0;
- c = 0;
- }
- /** Creates a Quadratic with a, b and c values
- * @param a the coefficient of x squared
- * @param b the coefficient of x
- * @param c the constant */
- public Quadratic(Double aValue, Double bValue, Double cValue)
- {
- a = aValue;
- b = bValue;
- c = cValue;
- }
- /** Returns the number of real roots based on the value of the determinant
- * @return the number of real roots */
- public int getNumRealRoots()
- {
- determinant = getDeterminant();
- if (determinant < 0)
- {
- numRealRoots = 0;
- }
- else if (determinant == 0)
- {
- numRealRoots = 1;
- }
- else if (determinant > 0)
- {
- numRealRoots = 2;
- }
- return numRealRoots;
- }
- /** Calculates and returns the value of the determinant
- * @return the value of the determinant */
- public double getDeterminant()
- {
- determinant = (Math.pow(b, 2) - (4 * a * c));
- return determinant;
- }
- /** Uses the quadratic formula to calculate and return the positive root
- * @return the positive root */
- public double getPositiveRoot()
- {
- double posRoot = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
- return posRoot;
- }
- /** Uses the quadratic formula to calculate and determine the negative root
- * @return the negative root */
- public double getNegativeRoot()
- {
- double negRoot = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
- return negRoot;
- }
- /** Calculates the real and imaginary parts of the first root and returns as a String
- * Ex: -5 + 3.25i
- * @return one of the complex roots as a String */
- public String getImaginaryRoot1()
- {
- double pos = Math.sqrt(determinant);
- String imagRootOne = pos + "i";
- return imagRootOne;
- }
- /** Calculates the real and imaginary parts of the second root and return as a String
- * Ex: -5 - 3.25i
- * @return the other complex root of the equation as a String */
- public String getImaginaryRoot2()
- {
- double neg = Math.sqrt(-determinant);
- String imagRootTwo = neg + "i";
- return imagRootTwo;
- }
- /** Retrieves the axis of symmetry from the quadratic function */
- public double getAxisOfSymmetry()
- {
- aos = -b / (2 * a);
- return aos;
- }
- /** Calculates the vertex of the quadratic function */
- public String getVertex()
- {
- vertexX = aos;
- vertexY = ((a * Math.pow(aos, 2)) + (b * aos) + (c));
- vertex = vertexX + ", " + vertexY;
- return vertex;
- }
- /** Builds an intelligent String that neatly displays on separate lines:
- * The number and kind of roots
- * The first root
- * The second root (except in the case of a double root)
- * return the number and type of roots along with the roots each on its own line */
- public String toString()
- {
- String s;
- // retrieves the determinant from the getDeterminant() method
- determinant = getDeterminant();
- System.out.println("Determinant: " + determinant);
- // rules for the value of the determinant
- if (determinant < 0)
- {
- // 0 real roots
- s = "There are 2 imaginary roots and " + getNumRealRoots() + " real roots, and they are: \nImaginary Root #1: " +
- getImaginaryRoot1() + "\n" + "Imaginary Root #2: " + getImaginaryRoot2() + "\n";
- s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
- s += "The vertex of the equation is: " + getVertex() + "\n\n";
- }
- else if (determinant == 0)
- {
- // 1 real root
- s = "There is " + getNumRealRoots() + " real root, and it is: " + getPositiveRoot() + "\n";
- s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
- s += "The vertex of the equation is: " + getVertex() + "\n\n";
- }
- else if (determinant > 0)
- {
- // 2 real roots
- s = "There are " + getNumRealRoots() + " real roots, and they are: \nPositive Root: " + getPositiveRoot() + "\n" +
- "Negative Root: " + getNegativeRoot() + "\n";
- s += "\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry() + "\n";
- s += "The vertex of the equation is: " + getVertex() + "\n\n";
- }
- else
- {
- s = "";
- }
- return s;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Quadratic
- // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in an object-oriented program
- import java.util.Scanner;
- public class QuadraticClient
- {
- public static void main(String args[])
- {
- double a, b, c;
- Quadratic eqtn;
- String again;
- Scanner sc = new Scanner(System.in);
- boolean running = true;
- System.out.println("Welcome to the Quadratic Program!");
- System.out.println("Standard Form of a Quadratic: Ax^2+Bx+C=0");
- System.out.println("--------------------------------------------------------------------------------");
- while(running)
- {
- System.out.println("\nEnter values for A, B, and C:");
- System.out.print("A = ");
- a = sc.nextDouble();
- System.out.print("B = ");
- b = sc.nextDouble();
- System.out.print("C = ");
- c = sc.nextDouble();
- eqtn = new Quadratic(a, b, c);
- System.out.println(eqtn.toString());
- System.out.print("Again? (Y or N): ");
- again = sc.next();
- if (again.equalsIgnoreCase("y"))
- {
- // program will continue
- }
- // farewell message
- else if (again.equalsIgnoreCase("n"))
- {
- System.out.println("Bye bye!");
- running = false;
- }
- // if they type something else besides "y" or "n"
- else
- {
- System.out.println("What was that? Please type in Y or N or the program will continue.");
- again = sc.next();
- }
- }
- }
- }
- /*
- Welcome to the Quadratic Program!
- Standard Form of a Quadratic: Ax^2+Bx+C=0
- --------------------------------------------------------------------------------
- Enter values for A, B, and C:
- A = 2
- B = 4
- C = 2
- Determinant: 0.0
- There is 1 real root, and it is: -4.0
- The axis of symmetry of the equation is: -1.0
- The vertex of the equation is: -1.0, 0.0
- Again? (Y or N): y
- Enter values for A, B, and C:
- A = 1
- B = -1
- C = -6
- Determinant: 25.0
- There are 2 real roots, and they are:
- Positive Root: 3.0
- Negative Root: -2.0
- The axis of symmetry of the equation is: 0.5
- The vertex of the equation is: 0.5, -6.25
- Again? (Y or N): y
- Enter values for A, B, and C:
- A = 5
- B = 8
- C = 19
- Determinant: -316.0
- There are 2 imaginary roots and 0 real roots, and they are:
- Imaginary Root #1: NaNi
- Imaginary Root #2: 17.776388834631177i
- The axis of symmetry of the equation is: -0.8
- The vertex of the equation is: -0.8, 15.8
- Again? (Y or N): y
- Enter values for A, B, and C:
- A = 12
- B = 13
- C = 14
- Determinant: -503.0
- There are 2 imaginary roots and 0 real roots, and they are:
- Imaginary Root #1: NaNi
- Imaginary Root #2: 22.427661492005804i
- The axis of symmetry of the equation is: -0.5416666666666666
- The vertex of the equation is: -0.5416666666666666, 10.479166666666668
- Again? (Y or N): n
- Bye bye!
- Press any key to continue . . .
- */
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // QuadraticProcedural
- // Spec: Given coefficients of a quadratic (a, b, c) calculate and output the number of real roots and the roots themselves in a procedural program
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class QuadraticProcedural
- {
- private static int numRealRoots;
- private static double x1, x2, determinant, aos, vertexX, vertexY;
- private static DecimalFormat fmt = new DecimalFormat("0.###");
- private static String vertex;
- /** Returns the number of real roots based on the value of the determinant
- * @return the number of real roots */
- public static int getNumRealRoots(double a, double b, double c)
- {
- determinant = getDeterminant(a, b, c);
- if (determinant < 0)
- {
- numRealRoots = 0;
- }
- else if (determinant == 0)
- {
- numRealRoots = 1;
- }
- else if (determinant > 0)
- {
- numRealRoots = 2;
- }
- return numRealRoots;
- }
- /** Calculates and returns the value of the determinant
- * @return the value of the determinant */
- public static double getDeterminant(double a, double b, double c)
- {
- determinant = (Math.pow(b, 2) - (4 * a * c));
- return determinant;
- }
- /** Uses the quadratic formula to calculate and return the positive root
- * @return the positive root */
- public static double getPositiveRoot(double a, double b, double c)
- {
- double posRoot = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
- return posRoot;
- }
- /** Uses the quadratic formula to calculate and determine the negative root
- * @return the negative root */
- public static double getNegativeRoot(double a, double b, double c)
- {
- double negRoot = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / 2 * a;
- return negRoot;
- }
- /** Calculates the real and imaginary parts of the first root and returns as a String
- * Ex: -5 + 3.25i
- * @return one of the complex roots as a String */
- public static String getImaginaryRoot1(double a, double b, double c)
- {
- double pos = Math.sqrt(determinant);
- String imagRootOne = pos + "i";
- return imagRootOne;
- }
- /** Calculates the real and imaginary parts of the second root and return as a String
- * Ex: -5 - 3.25i
- * @return the other complex root of the equation as a String */
- public static String getImaginaryRoot2(double a, double b, double c)
- {
- double neg = Math.sqrt(-determinant);
- String imagRootTwo = neg + "i";
- return imagRootTwo;
- }
- /** Retrieves the axis of symmetry from the quadratic function */
- public static double getAxisOfSymmetry(double a, double b, double c)
- {
- aos = -b / (2 * a);
- return aos;
- }
- /** Calculates the vertex of the quadratic function */
- public static String getVertex(double a, double b, double c)
- {
- vertexX = aos;
- vertexY = ((a * Math.pow(aos, 2)) + (b * aos) + (c));
- vertex = vertexX + ", " + vertexY;
- return vertex;
- }
- public static void main(String args[])
- {
- double a, b, c;
- String again;
- Scanner sc = new Scanner(System.in);
- boolean running = true;
- System.out.println("Welcome to the Quadratic Program!");
- System.out.println("Standard Form of a Quadratic: Ax^2+Bx+C=0");
- System.out.println("--------------------------------------------------------------------------------");
- while(running)
- {
- System.out.println("\nEnter values for A, B, and C:");
- System.out.print("A = ");
- a = sc.nextDouble();
- System.out.print("B = ");
- b = sc.nextDouble();
- System.out.print("C = ");
- c = sc.nextDouble();
- // retrieves the determinant from the getDeterminant() method
- determinant = getDeterminant(a, b, c);
- System.out.println("Determinant: " + determinant);
- // rules for the value of the determinant
- if (determinant < 0)
- {
- // 0 real roots
- System.out.print("There are 2 imaginary roots and " + getNumRealRoots(a, b, c) + " real roots, and they are: \nImaginary Root #1: " +
- getImaginaryRoot1(a, b, c) + "\n" + "Imaginary Root #2: " + getImaginaryRoot2(a, b, c) + "\n");
- System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
- System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n\n");
- }
- else if (determinant == 0)
- {
- // 1 real root
- System.out.print("There is " + getNumRealRoots(a, b, c) + " real root, and it is: " + getPositiveRoot(a, b, c) + "\n");
- System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
- System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n\n");
- }
- else if (determinant > 0)
- {
- // 2 real roots
- System.out.print("There are " + getNumRealRoots(a, b, c) + " real roots, and they are: \nPositive Root: " + getPositiveRoot(a, b, c) + "\n" +
- "Negative Root: " + getNegativeRoot(a, b, c) + "\n\n");
- System.out.print("\nThe axis of symmetry of the equation is: " + getAxisOfSymmetry(a, b, c) + "\n");
- System.out.print("The vertex of the equation is: " + getVertex(a, b, c) + "\n");
- }
- else
- {
- System.out.print("");
- }
- System.out.print("Again? (Y or N): ");
- again = sc.next();
- if (again.equalsIgnoreCase("y"))
- {
- // program will continue
- }
- // farewell message
- else if (again.equalsIgnoreCase("n"))
- {
- System.out.println("Bye bye!");
- running = false;
- }
- // if they type something else besides "y" or "n"
- else
- {
- System.out.println("What was that? Please type in Y or N or the program will continue.");
- again = sc.next();
- }
- }
- }
- }
- /*
- Welcome to the Quadratic Program!
- Standard Form of a Quadratic: Ax^2+Bx+C=0
- --------------------------------------------------------------------------------
- Enter values for A, B, and C:
- A = 2
- B = 4
- C = 2
- Determinant: 0.0
- There is 1 real root, and it is: -4.0
- The axis of symmetry of the equation is: -1.0
- The vertex of the equation is: -1.0, 0.0
- Again? (Y or N): 1
- What was that? Please type in Y or N or the program will continue.
- 4
- Enter values for A, B, and C:
- A = 1
- B = -1
- C = -6
- Determinant: 25.0
- There are 2 real roots, and they are:
- Positive Root: 3.0
- Negative Root: -2.0
- The axis of symmetry of the equation is: 0.5
- The vertex of the equation is: 0.5, -6.25
- Again? (Y or N): y
- Enter values for A, B, and C:
- A = 23
- B = 24
- C = 26
- Determinant: -1816.0
- There are 2 imaginary roots and 0 real roots, and they are:
- Imaginary Root #1: NaNi
- Imaginary Root #2: 42.61455150532503i
- The axis of symmetry of the equation is: -0.5217391304347826
- The vertex of the equation is: -0.5217391304347826, 19.73913043478261
- Again? (Y or N): y
- Enter values for A, B, and C:
- A = 75
- B = -43
- C = 3
- Determinant: 949.0
- There are 2 real roots, and they are:
- Positive Root: 2767.719135056202
- Negative Root: 457.2808649437978
- The axis of symmetry of the equation is: 0.2866666666666667
- The vertex of the equation is: 0.2866666666666667, -3.163333333333335
- Again? (Y or N): n
- Bye bye!
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement