-
July 11th, 2009, 12:09 PM
#1
Quadratic Equations Java Help with My Program
Hi,
I got my quadratic equations program to run calculating the roots, but I can't seem to the following:
1. Print the quadratic equation from the coefficents.
//Note: I tired doing that by just saying System.out.println("The equation is: "+a.b^2 + b.x+c") but it didn't compile on my Command Prompt Window.
2. Use if statements to deal with the nature of the roots. (discriminants)
//Note: I tried but was unsuccessful in this part. I saw examples online but I don't know how to incorporate it, I never learned try/catch yet so I can't use that in my program.
Thanks coders who help me,
Coder752
Here's my code:
import java.util.Scanner;
public class Quadratic{
public static void main(String[] args){
System.out.println("Enter three coefficients");
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double root1= (-b + Math.sqrt( b*b - 4*a*c ) )/ (2*a);
double root2= (-b - Math.sqrt( b*b - 4*a*c ) )/ (2*a);
System.out.println("The roots are: "+ root1);
System.out.println("and: " + root2);
//System.out.println("The equation is: ");
//System.out.println(" +a.x^2 + b.x +c");
}
}
-
July 11th, 2009, 03:11 PM
#2
Re: Quadratic Equations Java Help with My Program
Originally Posted by coder752
//Note: I tired doing that by just saying System.out.println("The equation is: "+a.b^2 + b.x+c") but it didn't compile on my Command Prompt Window.
You have a stray double-quote in the middle of that string (or at the end, if you want the value rather than the equation as text).
2. Use if statements to deal with the nature of the roots. (discriminants)
//Note: I tried but was unsuccessful in this part. I saw examples online but I don't know how to incorporate it, I never learned try/catch yet so I can't use that in my program.
try/catch has nothing to do with 'if' statements.
If you're not failing every now and again, it's a sign you're not doing anything very innovative...
W. Allen
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 11th, 2009, 07:20 PM
#3
Re: Quadratic Equations Java Help with My Program
No I want the equation to be printed.
For example you input the values, u get the roots, then next step which I want is to get the quadratic equation from those coefficients.
And I saw examples online that had try/catch and I was just letting others know to not offer me help using those statements.
Regarding my question, coders are still welcome.
Plz help me.
Thanks to those who do.
-
July 12th, 2009, 04:02 AM
#4
Re: Quadratic Equations Java Help with My Program
Originally Posted by coder752
No I want the equation to be printed.
OK, so remove the stray double-quote from the middle of the string.
Regarding my question, coders are still welcome.
We're all coders here. You said you need to use 'if' statements to 'deal with the nature of the roots' - can you be a bit clearer - what does this mean, and what part of it do you need help with?
It is better to have an approximate answer to the right question than an exact answer to the wrong one...
J. Tukey
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 12th, 2009, 04:21 AM
#5
Re: Quadratic Equations Java Help with My Program
As well as removing the extra ", you'll need to put brackets around the calculation ie
Code:
System.out.println("The equation is: "+(a.b^2 + b.x+c));
-
July 12th, 2009, 05:03 AM
#6
Re: Quadratic Equations Java Help with My Program
Originally Posted by keang
As well as removing the extra ", you'll need to put brackets around the calculation
I thought he was saying he wanted the the equation as text, not its value - but the reply to my question isn't really clear
The limits of your language are the limits of your world...
L. Wittgenstein
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 13th, 2009, 06:38 AM
#7
Re: Quadratic Equations Java Help with My Program
I thought the same as dlorde, but coder752 mixed up algebra with Java syntax. I guess he/she meant to print the equation as text given the coefficients a, b and c. It could be done using something like:
Code:
System.out.format("The equation is: %f.x^2 + %f.x + %f%n", a, b, c);
Regarding the second question, you have to calculate the discriminant (b^2 - 4ac) and based on the result sign decide if the equation has two real roots, one real root or two complex roots. Try to think how you can put that in plain English and you should be able to write in down in Java. No need to use try/catch blocks.
-
July 13th, 2009, 10:24 AM
#8
Re: Quadratic Equations Java Help with My Program
Ok to clarify for all:
I've already got the program to ask the user to enter 3 values.
Then it outputs to the screen what the roots are after it computes it.
I want to know how to code the part where you output the equation once it computes in ax^2+bx+c form...for example so and so values inputted and equation like this for eg gets outputted to the screen: 5x^2+10x+25
(something like that)
I also want to know how to do the discriminants (if statements).
-
July 13th, 2009, 10:34 AM
#9
Re: Quadratic Equations Java Help with My Program
Also to add to what I just clarified.
Do I just add a new variable, say double d... and delete the double a, b, and c statements, or keep all the double a, b and c parts (see earlier code I had above)?
I searched on Google and found this chunk of code, now I just need help blending it with my code. Say for e.g.
//double d = Math.pow(b,2) - 4*a*c; //Discriminant
//double x1 = (-b + Math.sqrt(discriminat))/(2*a);
//double x2 = (-b - Math.sqrt(discriminat))/(2*a);
//double imaginary = Math.sqrt(-1);
//double x3 = (-b + (Math.sqrt(Math.abs(discriminat))))/(2*a);
//double x4 = (-b + (Math.sqrt(Math.abs(discriminat))))/(2*a);
if (d > 0 ){
System.out.println("There are two solutions:" +x1+ "and" +x2);
}
else if (discriminat == 0){
System.out.println("The solutions is:" +x1);
}
else if (discriminat < 0){
System.out.println("The solutions are"+ x3 + "i" + " and "+ x4 + "i");
}
}
}
But the fact that it uses x1, x2,x3,and x4 does that ruin my program in using a,b, and c? In my intuition, I feel that is like making two quadratic equations program in one file...so its confusing me.
-
July 13th, 2009, 10:54 AM
#10
Re: Quadratic Equations Java Help with My Program
Originally Posted by coder752
Ok to clarify for all:
I want to know how to code the part where you output the equation once it computes in ax^2+bx+c form...for example so and so values inputted and equation like this for eg gets outputted to the screen: 5x^2+10x+25
(something like that)
Perhaps it would be better if you gave a couple of examples of exactly what you want the output to look like for specified input values, because 'so and so values inputted' and 'something like that' output doesn't translate to code too well...
The most important single aspect of software development is to be clear about what you are trying to build...
B. Stroustrup
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 13th, 2009, 11:48 AM
#11
Re: Quadratic Equations Java Help with My Program
Originally Posted by coder752
I want to know how to code the part where you output the equation once it computes in ax^2+bx+c form...for example so and so values inputted and equation like this for eg gets outputted to the screen: 5x^2+10x+25
I gave you the answer already:
Originally Posted by jcaccia
... to print the equation as text given the coefficients a, b and c. It could be done using something like:
Code:
System.out.format("The equation is: %f.x^2 + %f.x + %f%n", a, b, c);
And to analyse the possible results by testing the discriminant:
Code:
...
double discriminant = b * b - 4 * a * c;
double b2a = -b / (2 * a);
if (discriminant > 0) {
double x = Math.sqrt(discriminant) / (2 * a);
System.out.format("There are two real solutions: %f and %f%n", (b2a + x), (b2a - x));
} else if (discriminant == 0) {
System.out.format("There is one real solution: %f%n", b2a);
} else if (discriminant < 0) {
double x = Math.sqrt(-discriminant) / (2 * a);
System.out.format("There are two complex solutions: %f + %fi and %1$f - %2$fi%n", b2a, x);
}
...
(Which is not too far from what you found googling.)
Originally Posted by coder752
But the fact that it uses x1, x2,x3,and x4 does that ruin my program in using a,b, and c? In my intuition, I feel that is like making two quadratic equations program in one file...so its confusing me.
that is confusing. What do you mean by "ruin my program"? Why do you feel you are making two quadratic equations programs in one?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|