Results 1 to 11 of 11
Thread: Help with equation
 03162011, 06:03 PM #1Member
 Join Date
 Mar 2011
 Posts
 5
 Rep Power
 0
Help with equation
Hi, I need some help in making a program that reads three values (a, b and c) from a quadratic equation and then writes in this manner (a x^2 + b x + c = 0).
examples:
a b c ? 0 1 2,5
the equation is: x – 2,5 = 0
a b c ? 2 1 0
the equation is: 2,0 x^2  x = 0
a b c ? 3,25 1 2
the equation is: 3,25 x^2 + x – 2,0 = 0
a b c ? 1 0 1
the equation is: x^2 + 1,0 = 0
Any kind of help is appreciated.
 03162011, 06:03 PM #2
What part of this are you having trouble with?
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 03162011, 06:14 PM #3Member
 Join Date
 Mar 2011
 Posts
 5
 Rep Power
 0
Well is the part that if any of the values are zero we have to omit them and the x's, if any of them is equal to 1 we must leave them out but print the x's and if the values are positive or negative. The only idea that i had was with ifelses but it would be a loooong code.
 03162011, 07:16 PM #4
Let's see that loooong code and maybe we can help you improve it.
db
 03172011, 04:32 PM #5Member
 Join Date
 Mar 2011
 Posts
 5
 Rep Power
 0
Ok here´s the code:
Java Code:import java.util.*; public class Problem { public static void main(String[] args) { Scanner teclado = new Scanner(System.in); System.out.print("v1 v2 v3 ? "); double a = teclado.nextDouble(); double b = teclado.nextDouble(); double c = teclado.nextDouble(); if (a>1  a<0) { if (b>1) { if (c>1) System.out.println("equation is: " + a + "x^2 + " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + a + "x^2 + " + b + "x = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 + " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 + " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + a + "x^2 + " + b + "x " + c + " = 0"); } if (b<1) { if (c>1) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + a + "x^2 " + b + "x " + c + " = 0"); } else if (b==0) { if (c>1) System.out.println("equation is: " + a + "x^2 + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + a + "x^2 = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 " + c + " = 0"); else if (c<1) System.out.println("equation is: " + a + "x^2 " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is: " + a + "x^2 + x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + a + "x^2 + x = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 + x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2 + x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + a + "x^2 + x " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is: " + a + "x^2  x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + a + "x^2  x = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2  x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + a + "x^2  x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + a + "x^2  x " + c + " = 0"); } } else if (a==0) { if (b>1) { if (c>1) System.out.println("equation is: " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + b + "x = 0"); else if (c==1) System.out.println("equation is: " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + b + "x " + c + " = 0"); } if (b<1) { if (c>1) System.out.println("equation is: " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: " + b + "x " + c + " = 0"); } else if (b==0) { if (c>1) System.out.println("equation is: " + c + " = 0"); else if (c==0) System.out.println("equation is: 0 = 0"); else if (c==1) System.out.println("equation is: " + c + " = 0"); else if (c==1) System.out.println("equation is: " + c + " = 0"); else if (c<1) System.out.println("equation is: " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is: x + " + c + " = 0"); else if (c==0) System.out.println("equation is: x = 0"); else if (c==1) System.out.println("equation is: x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x " + c + " = 0"); else if (c<1) System.out.println("equation is: x " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is:  x + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x = 0"); else if (c==1) System.out.println("equation is:  x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x " + c + " = 0"); else if (c<1) System.out.println("equation is:  x " + c + " = 0"); } } else if (a==1) { if (b>1) { if (c>1) System.out.println("equation is: x^2 + " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: x^2 + " + b + "x = 0"); else if (c==1) System.out.println("equation is: x^2 + " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2 + " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: x^2 + " + b + "x " + c + " = 0"); } if (b<1) { if (c>1) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2 " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is: x^2 " + b + "x " + c + " = 0"); } else if (b==0) { if (c>1) System.out.println("equation is: x^2 + " + c + " = 0"); else if (c==0) System.out.println("equation is: x^2 = 0"); else if (c==1) System.out.println("equation is: x^2 + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2 " + c + " = 0"); else if (c<1) System.out.println("equation is: x^2 " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is: x^2 + x + " + c + " = 0"); else if (c==0) System.out.println("equation is: x^2 + x = 0"); else if (c==1) System.out.println("equation is: x^2 + x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2 + x " + c + " = 0"); else if (c<1) System.out.println("equation is: x^2 + x " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is: x^2  x + " + c + " = 0"); else if (c==0) System.out.println("equation is: x^2  x = 0"); else if (c==1) System.out.println("equation is: x^2  x + " + c + " = 0"); else if (c==1) System.out.println("equation is: x^2  x " + c + " = 0"); else if (c<1) System.out.println("equation is: x^2  x " + c + " = 0"); } } else if (a==1) { if (b>1) { if (c>1) System.out.println("equation is:  x^2 + " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x^2 + " + b + "x = 0"); else if (c==1) System.out.println("equation is:  x^2 + " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2 + " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is:  x^2 + " + b + "x " + c + " = 0"); } if (b<1) { if (c>1) System.out.println("equation is:  x^2 " + b + "x + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2 " + b + "x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2 " + b + "x " + c + " = 0"); else if (c<1) System.out.println("equation is:  x^2 " + b + "x " + c + " = 0"); } else if (b==0) { if (c>1) System.out.println("equation is:  x^2 + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x^2 = 0"); else if (c==1) System.out.println("equation is:  x^2 + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2 " + c + " = 0"); else if (c<1) System.out.println("equation is:  x^2 " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is:  x^2 + x + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x^2 + x = 0"); else if (c==1) System.out.println("equation is:  x^2 + x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2 + x " + c + " = 0"); else if (c<1) System.out.println("equation is:  x^2 + x " + c + " = 0"); } else if (b==1) { if (c>1) System.out.println("equation is:  x^2  x + " + c + " = 0"); else if (c==0) System.out.println("equation is:  x^2  x = 0"); else if (c==1) System.out.println("equation is:  x^2  x + " + c + " = 0"); else if (c==1) System.out.println("equation is:  x^2  x " + c + " = 0"); else if (c<1) System.out.println("equation is:  x^2  x " + c + " = 0"); } } else System.out.println("Equation is invalid."); } }
 03172011, 04:52 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,519
 Blog Entries
 7
 Rep Power
 20
Yuck, try to implement a little method that prints out one term of the polynomial given the coefficient and the exponent and apply that method on each term of the polynomial. That by itself will reduce the size of your code enormously.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03172011, 05:00 PM #7Member
 Join Date
 Mar 2011
 Posts
 5
 Rep Power
 0
Sorry not very good in english. Could you show a little example of what you just said
 03172011, 05:26 PM #8
It rather appears that you aren't aware of how to write and use methods, apart from the main(...) method. Here's where you can learn more about that:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Or start at the top of that section of the tutorial and work your way down.
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
db
 03172011, 05:27 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,519
 Blog Entries
 7
 Rep Power
 20
I already expected such a reply; it has nothing to do with English but I wrote a little spoiler for you:
Java Code:public class T { private static String printExp(int exp) { String r= ""; if (exp > 0) r= "x"; if (exp > 1) r+= "^"+exp; return r; } private static String printCoef(double coef) { if (coef > 0) return "+"+coef; return ""+coef; } private static void printTerm(double coef, int exp) { if (coef == 0) return; if (coef == 1) if (exp == 0) System.out.print("1"); else System.out.print(""+printExp(exp)); else if (coef == 1) if (exp == 0) System.out.print("+1"); else System.out.print(printExp(exp)); else System.out.print(printCoef(coef)+printExp(exp)); } public static void main(String[] args) { printTerm(2, 2); printTerm(3, 1); printTerm(1, 0); } }
Joscenosillicaphobia: the fear for an empty beer glass
 03172011, 07:39 PM #10Member
 Join Date
 Mar 2011
 Posts
 5
 Rep Power
 0
@JosAH Many thanks, i now have completed the little rest of the code but there is one thing left.
When I put 0 1 2,5
The output is +x2,5 = 0
Since I don´t need that plus sign in the beginning do I have to create another metod for this or add some code on an existing metod?
 03172011, 08:15 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,519
 Blog Entries
 7
 Rep Power
 20
Similar Threads

equation
By bobo67 in forum New To JavaReplies: 5Last Post: 09062010, 06:40 PM 
Need help with math equation
By annabellastorm in forum New To JavaReplies: 4Last Post: 01102010, 05:12 PM 
differential equation RK4
By arvindmer in forum New To JavaReplies: 3Last Post: 01082009, 01:27 PM 
Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11122008, 03:29 AM 
Help with an equation in java
By coco in forum New To JavaReplies: 1Last Post: 07312007, 07:47 AM
Bookmarks