1. Member
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.

2. What part of this are you having trouble with?

3. Member
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 if-elses but it would be a loooong code.

4. Let's see that loooong code and maybe we can help you improve it.

db

5. Member
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)
{
System.out.print("v1 v2 v3 ? ");

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.");
}
}```

6. 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

7. Member
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

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

9. 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:

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

Jos

10. Member
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 +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?

11. 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 ;-)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•