Created
February 12, 2015 15:57
-
-
Save devpruthvi/f09cebd7eb853ae6f966 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| class Complex | |
| { | |
| private float real; | |
| private float imaginary; | |
| public void display() | |
| { | |
| if(imaginary > 0) | |
| System.out.println(real + "+" + imaginary + "i"); | |
| else | |
| System.out.println(real + "-" + -imaginary + "i"); | |
| } | |
| public float getReal() | |
| { | |
| return real; | |
| } | |
| public float getImaginary() | |
| { | |
| return imaginary; | |
| } | |
| public void setReal(float real) | |
| { | |
| this.real = real; | |
| } | |
| public void setImaginary(float imaginary) | |
| { | |
| this.imaginary = imaginary; | |
| } | |
| } | |
| public class Quad { | |
| public static void main(String[] args) | |
| { | |
| float a,b,c; | |
| Scanner keyboard = new Scanner(System.in); | |
| System.out.println("Enter the coefficients of x^2,x^1, x^0: "); | |
| a = keyboard.nextFloat(); | |
| b = keyboard.nextFloat(); | |
| c = keyboard.nextFloat(); | |
| keyboard.close(); | |
| double discriminant = b*b - 4*a*c; | |
| if(discriminant < 0) | |
| { | |
| float real = -b/(2*a); | |
| float imaginary = (float) Math.sqrt(-discriminant)/(2*a); | |
| Complex root1 = new Complex(); | |
| Complex root2 = new Complex(); | |
| System.out.println("The roots are complex:"); | |
| root1.setReal(real); | |
| root1.setImaginary(imaginary); | |
| root1.display(); | |
| root2.setReal(real); | |
| root2.setImaginary(-imaginary); | |
| root2.display(); | |
| } | |
| else if(discriminant > 0) | |
| { | |
| float root1 = (float) (-b+Math.sqrt(discriminant))/(2*a); | |
| float root2 = (float) (-b-Math.sqrt(discriminant))/(2*a); | |
| System.out.println("The roots are real and unequal: " + root1 + " " + root2); | |
| } | |
| else if(discriminant > 0) | |
| { | |
| float root1 = (float) (-b+Math.sqrt(discriminant))/(2*a); | |
| System.out.println("The roots are real and equal: " + root1 + " " + root1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment