# Help with equation

• 03-16-2011, 07:03 PM
_Jk_
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.
• 03-16-2011, 07:03 PM
KevinWorkman
What part of this are you having trouble with?
• 03-16-2011, 07:14 PM
_Jk_
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 if-elses but it would be a loooong code.
• 03-16-2011, 08:16 PM
DarrylBurke
Let's see that loooong code and maybe we can help you improve it.

db
• 03-17-2011, 05:32 PM
_Jk_
Ok here´s the code:

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.");         } }```
• 03-17-2011, 05:52 PM
JosAH
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,

Jos
• 03-17-2011, 06:00 PM
_Jk_
Sorry not very good in english. Could you show a little example of what you just said
• 03-17-2011, 06:26 PM
DarrylBurke
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
• 03-17-2011, 06:27 PM
JosAH
Quote:

Originally Posted by _Jk_
Sorry not very good in english. Could you show a little example of what you just said

I already expected such a reply; it has nothing to do with English but I wrote a little spoiler for you:

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);         } }```
kind regards,

Jos
• 03-17-2011, 08:39 PM
_Jk_
@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 +x-2,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?
• 03-17-2011, 09:15 PM
JosAH
Quote:

Originally Posted by _Jk_
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?

I left that as an exercise for the reader (*)

kind regards,

Jos

(*) I love writing that ;-)