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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Quadratuc Read Method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

     
    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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>
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Quadratuc Read Method

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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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>
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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>?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. Problem understanding read method.
    By Truck35 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 12th, 2013, 08:42 PM
  2. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  3. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  4. Contain method to read array
    By theaveguy in forum Loops & Control Statements
    Replies: 3
    Last Post: February 27th, 2012, 12:32 AM
  5. Contain method to read array
    By theaveguy in forum Java Theory & Questions
    Replies: 8
    Last Post: February 25th, 2012, 02:53 PM