06-04-2011, 01:18 PM
Code:
//QuadEqn.java
package p;
interface Quadratic
{
void process();
}
public class QuadEqn implements Quadratic
{
double a,b,c,d;
public QuadEqn(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
public void process()
{
d=b*b-(4*a*c);
if(d==0)
{
double r=-b/2*a;
System.out.println("Roots are equal.Root=\t"+r);
}
else
if(d>0)
{
double sq=Math.sqrt(d);
double r1=(-b+sq)/2*a;
double r2=(-b-sq)/2*a;
System.out.println("Root1=\t"+r1+"\nRoot2=\t"+r2);
}
else
System.out.println("Roots are imaginary\n");
}
}
//QuadM.java
import java.io.*;
import p.QuadEqn;
class QuadM
{
public static void main(String arg[])throws IOException
{
try
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the values of a,b,c\n");
double p=Double.parseDouble(dis.readLine());
double q=Double.parseDouble(dis.readLine());
double r=Double.parseDouble(dis.readLine());
QuadEqn ob=new QuadEqn(p,q,r);
ob.process();
}
catch(Exception e)
{
System.out.println("Error..."+e);
}
}
}
OUTPUT
E:\3msc\java\liya>java QuadM
Enter the values of a,b,c
1
3
2
Root1= -1.0
Root2= -2.0