Tuesday, July 28, 2009

Help me with my program...?

I want the output like this:





Example output:


Enter coefficient a: 4


Enter coefficient b: 4


Enter coefficient c: 1


Quadratic _expression: 4x2 + 4x + 1


The roots are real: x1 = -0.5 ; x2 = -0.5


It is a perfect square.


Evaluating the _expression:


Enter x: 2


Result : 25





Enter coefficient a: 2


Enter coefficient b: 5


Enter coefficient c: 10


Quadratic _expression: 2x2 + 5x + 10


The roots are imaginary.


Evaluating the _expression:


Enter x: 1


Result : 17





Enter coefficient a: 1


Enter coefficient b: 0


Enter coefficient c: -1


Quadratic _expression: x2 + 0x + -1


The roots are real. x1 = -1 ; x = 1


It is not a perfect square.


Evaluating the _expression:


Enter x: 5


Result : 24





I can't do it like the output above...pls edit





import java.lang.Math;


import java.util.*;


import java.io.*;


import java.text.*;





class Quadratic {





public static void main( String args[] ) {





float fA,fB,fC;


float fRoot1, fRoot2;


float fDiscriminant;


String sLine;





// Print welcome message.





System.out.println("Welcome to The Quadratic Equation Solver");


System.out.println("--------------------...





// Prompt user for coefficients in quadratic equation.





System.out.println("Please Enter coefficients for equation");


System.out.println("a.x^2 + b.x + c");





System.out.print("Coefficent a: ");


sLine = getTextFromConsole();


fA = Float.valueOf(sLine).floatValue();





System.out.print("Coefficent b: ");


sLine = getTextFromConsole();


fB = Float.valueOf(sLine).floatValue();





System.out.print("Coefficent c: ");


sLine = getTextFromConsole();


fC = Float.valueOf(sLine).floatValue();





// Print details of quadratic equation to screen.





System.out.println("The equation you have entered is : ");


System.out.println(+fA+".x^2 + "+fB+".x + "+fC);





// Check for degenerate roots (i.e., fA = fB = zero).





if ( fA==0 %26amp;%26amp; fB==0 ) {


System.out.println("Cannot solve " + fC +" = 0.0");


return;


}





if ( fA==0 %26amp;%26amp; fB !=0 ) {


fRoot1 = -fC/fB;


System.out.println("Degenerate root : Root = "+ fRoot1);


return;


}





// Compute discriminant of quadratic equation.





fDiscriminant = fdiscriminant(fA,fB,fC);





// Case for two real roots.





if ( fDiscriminant %26gt;= 0.0 ) {





// Compute : two real roots .....





fRoot1 = (float)(-fB/2.0/fA-(float)Math.sqrt(fDis... /


2.0 / fA );


fRoot2 = (float)(-fB/2.0/fA+(float)Math.sqrt(fDis... /


2.0 / fA);





// Format output to two decimal places of accuracy ...





DecimalFormat formatRoot = new DecimalFormat( "##.##" );


String output1 = formatRoot.format( fRoot1 );


String output2 = formatRoot.format( fRoot2 );





// Print results to screen ....





System.out.println("Two real roots : Root1 : " + output1 );


System.out.println(" Root2 : " + output2 ) ;


} else {





// Compute : two complex roots .....





fRoot1 = (float) (-fB/2.0/fA);


fRoot2 = (float) (Math.sqrt(-fDiscriminant)/2.0/fA);





// Format output to two decimal places of accuracy ...





DecimalFormat formatRoot = new DecimalFormat( "##.##" );


String output1 = formatRoot.format( fRoot1 );


String output2 = formatRoot.format( fRoot2 );





System.out.println("Two complex roots");


System.out.println("Root1 : " + output1 + "+" + output2 + "i");


System.out.println("Root2 : " + output1 + "-" + output2 + "i");


}


}











static float fdiscriminant(float fA, float fB, float fC) {


float fReturn;





fReturn= (float)(fB*fB-4.0*fA*fC);


return fReturn;


}











static String getTextFromConsole() {


String inLine = "";





// Create buffered reader for keyboard input stream....





BufferedReader inStream = new BufferedReader (


new InputStreamReader(System.in));





// Try to read input from keyboard ....





try {


inLine = inStream.readLine();


} catch (IOException e) {


System.out.println("IOException: " + e);


}





return inLine;


}


}

Help me with my program...?
i gt the program to run as what your output is. Pls email for the solution. i cant post the coding here, i think some part of the codes is missing. thanks.
Reply:its perfect

pear

No comments:

Post a Comment