• February 28th, 2013, 10:26 AM
phantom777
I have to write a read method for a quadratic class where a quadratic is entered in the form ax^2 + bx + c. The description for the class is this:

Add a read method that asks the user for an equation in standard format and set the three instance variables correctly. (So if the user types 3x^2 - x, you set the instance variables to 3, -1, and 0). This will require string processing you have done before. Display the actual equation entered as is and properly labeled as expected output.

I was able to do the ax^2 part by using string manipulation and if else statements. But I am not sure how to do the bx and c parts of the equation because of the sign that could be in front of bx and c. Here is how I did the ax^2 part of the method.

Code Java:

```  public void read() { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a quadratic equation in standard format."); String formula = keyboard.next(); String a = formula.substring(0, formula.indexOf("x^2")); int a2 = Integer.parseInt(a); if (a2 == 0) { System.out.println("a = 0"); } else if (a2 == 1) { System.out.println("a = 1"); } else { System.out.println("a = " + a2); } }```

Feel free to make edits and write new code for the method.
Any help would be greatly appreciated.
• February 28th, 2013, 10:33 AM
Norm
What is the syntax of the full expression?
Will it always have three expressions separated by sign operators? Could some of the parts be omitted? say two expressions and one sign operator?
<exp> <sign> <exp> <sign> <exp>

You need to parse the full expression and break it into sub parts: expressions<exp> and signs<sign>

What tokens make an <exp>
what tokens are valid <sign>
• February 28th, 2013, 11:23 AM
phantom777
It could be any various combination of things it could be ax^2-c, bx+c, ax^2-bx+c, ax^2. It's supposed to work for all the different possibilities. But the only signs will be + and - . But a,b, and c will always be whole integers. And the number represented by a will always be with x^2 and so on. Also if the equation was x+3, a must be set equal to 0, b would be set equal to 1 and c would be 0.
• February 28th, 2013, 11:35 AM
Norm
The choices are one of these three?
<expr>
<expr> <sign> <expr>
<expr> <sign> <expr> <sign> <expr>

Then the code needs to be able to parse out <expr> and <sign> from the String that was entered.
• February 28th, 2013, 11:51 AM
phantom777
Yes that is the case it seems. But I am unsure of how to do that.
• February 28th, 2013, 11:56 AM
Norm
What characters make up an <expr>? Is there an implied * between a and x in ax?
What characters make a <sign>
What separates an <expr> from a <sign>
• February 28th, 2013, 12:47 PM
phantom777