-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment2
More file actions
59 lines (50 loc) · 2.13 KB
/
assignment2
File metadata and controls
59 lines (50 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// write a program that performs linear and quadratic calculations
import java.util.Scanner;
public class assignment2
{
public static void main(String [] args)
{
// set variables
double A, B, C, D, altRoot, root1, root2;
// set object kb to the main Scanner
Scanner kb = new Scanner(System.in);
//enter the values A, B, C, for quadratic formula/equation
System.out.println("Enter values for A, B and C");
A = kb.nextDouble();
B = kb.nextDouble();
C = kb.nextDouble();
// print the new full quadratic equation
System.out.println("The Quadratic Equation is ");
System.out.println(A+"x^2 + " + B+"x + " + C + " = 0");
// calculate discriminant from quadtratic formula
D = (B*B) - (4 * A * C);
System.out.println("The discriminant is " + D + " according to quadratic formula");
if (A == 0 && B == 0) // if both A and B equal 0 then the equation has no solution
{
System.out.println("The System Has No Solution ");
}
else if (A == 0) // if just A is 0 then there is an alternate root -C/B
{
altRoot = -C/B;
System.out.println("Both roots both equal -C/B because A = 0 ");
System.out.println("Therfore the altnernate root is " + altRoot);
}
else if (D < 0) // if D is less than 0 then the roots are imaginary
{
System.out.println("Roots are imaginary ");
}
else if (D > 0) // if D is greater than 0 then the roots are real and given by the quadtratic formula
{
root1 = -B + (Math.sqrt(D))/(2*A);
root2 = -B - (Math.sqrt(D))/(2*A);
System.out.println("The first root is " + root1);
System.out.println("The second root is " + root2);
}
else if (D == 0) // if D equals 0 then there is another alternate root equation -B/2A
{
altRoot = -B/(2*A);
System.out.println("The roots are both equal to -B/2A because discriminant = 0");
System.out.println("Therefore the alternate root is " + altRoot);
}
} // end main
} // end class