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.


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

Results 1 to 3 of 3

Thread: quadratic equation solver help!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Thanked 0 Times in 0 Posts

    Default quadratic equation solver help!

    I need to write a program that prints all real solutions to the quadratic equation "ax2 + bx + c = 0" i need to implementa class QuadraticEquation whose constructor recieves the coefficients of a, b and c and i need to supply methods getSolution1 and getSolution2 and the getSolution1 should return the smaller number. Lastly i also need to supply a method boolean hasSolution() that returns false if the Discriminant is negative. Any help would be greatly appreciated because I dont even have a clue of where to start

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: quadratic equation solver help!

  3. The Following User Says Thank You to copeg For This Useful Post:

    overlord (October 28th, 2011)

  4. #3
    Forum VIP
    Join Date
    Oct 2010
    My Mood
    Thanked 54 Times in 47 Posts
    Blog Entries

    Default Re: quadratic equation solver help!

    First hint, you're going to have to (For greatest ease) delete the whitespace before you attempt to decode its formula. Then you need to decide how your x2 is going to look like. EG, x^2 x2 xpow2.
    Then you can either complete the square, or just use the quadratic formula.

    As for your hasSolution method, it is going to need parameters a, b and c, like
    public static boolean hasSolution(double a, double b, double c)
      //Code not included

    Your main method is going to have to start with some kind of decoder, however it will be either really simple or medium difficulty depending on if you are going to allow more then 1 number coefficients. May I recommend using two-three lines for the quadratic formula,
    it will look much cleaner.

    Back to the decoder, since you know it will have the number x at the end of a and b, a quick for loop and second string will do the trick

     *Precondition: input is the user's input
    char c = input.charAt(0);
    int i = 1;
    String stringA = "";
    while(c != 'x')
      stringA += c;
      c = input.charAt(i);
    Then you need to skip 1 chars for the ^2 (Assuming you use that), then you need to check if is ax^2 + bx + c = 0, or ax^2 -bx + c, so
    double a = Double.valueOf(stringA);
    i += 1; //We are already at the x
    char operator = input.charAt(i);
    We can ignore the operator for right now.
    That is pretty much the first step, for decoding the rest, the logic will be
    for B:
    • Get all the chars before the second x
    • Convert the value into a double.
    • If operator1 is a subtraction sign, multibly b by -1
    • Increment i again to get past the x, and to the operator
    • Save the operator in operator

    For c,
    do what you did for a, except without looking for an operator and waiting for '=' instead of x.
    Last edited by Tjstretch; October 20th, 2011 at 11:55 AM.

  5. The Following User Says Thank You to Tjstretch For This Useful Post:

    overlord (October 28th, 2011)

Similar Threads

  1. Testing equation of a line
    By TimoElPrimo in forum Object Oriented Programming
    Replies: 8
    Last Post: February 23rd, 2011, 12:40 AM
  2. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 06:32 AM
  3. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 07:27 PM
  4. How to handle quadratic roots that don't exist?
    By kairojya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2009, 12:21 PM
  5. help!! quadratic program
    By dscrudato21xo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 18th, 2009, 05:13 PM

Tags for this Thread