Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Quadratuc Read Method

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.

```
{
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.  Reply With Quote

3. ## Re: Quadratuc Read Method

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>  Reply With Quote

4. ## Re: Quadratuc Read Method

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.  Reply With Quote

5. ## Re: Quadratuc Read Method

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.  Reply With Quote

6. ## Re: Quadratuc Read Method

Yes that is the case it seems. But I am unsure of how to do that.  Reply With Quote

7. ## Re: Quadratuc Read Method

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>  Reply With Quote

8. ## Re: Quadratuc Read Method

A <expr> would be made up of either ax^2, bx, or c. a, b, and c are represented by integers in the actual equation. A <sign>would be represented by a plus or minus. Nothing separates an <expr> from a <sign>. The user inputs an equation such as this example: 3x^2-16x+8  Reply With Quote

9. ## Re: Quadratuc Read Method

The real expressions that are entered will have numbers where the discussion so far has talked about a,b,c.
Will the next character after the optional leading number be x?

All of that should now define what an <expr> can be made of.
Do you have enough now to write the code to parse an <expr>?  Reply With Quote

10. ## Re: Quadratuc Read Method

Yes the next character would be an x in the cases of a and b, but not for c. I'm getting there though.  Reply With Quote