4 Replies - 17628 Views - Last Post: 26 November 2010 - 07:57 AM Rate Topic: -----

#1 Guest_Rachel*


Reputation:

Method for quadratic equation

Posted 25 November 2010 - 08:30 PM

Write a method called quadratic that solves quadratic equations and prints their roots. Recall that a quadratic equation is a polynomial equation in terms of a variable x of the form ax2 + b + c = 0.
Your method should accept the coefficients a, b, and c as parameters and should print the roots of the equation. You may assume that the equation has two real roots, though mathematically this is not always the case.

I am so confused at what to do. I think I have to start:
public static void quadratic (int a, int b, int c)

What would be my next step? It is confusing me because it's only a method and I've seen so many examples of an actual program.

Is This A Good Question/Topic? 0

Replies To: Method for quadratic equation

#2 and1dre   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 30-March 09

Re: Method for quadratic equation

Posted 25 November 2010 - 08:40 PM

I would break it up into parts. Solve each part of the quadratic equation separately(http://en.wikipedia.org/wiki/Quadratic_equation)

For example, do the b^2-4ac and store the answer in a variable X. Then take the square root of X..and so on.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Method for quadratic equation

Posted 25 November 2010 - 08:41 PM

Well, remember that the quadratic formula is relatively simple to use. It looks like this:

(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)

Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.

This post has been edited by Dogstopper: 25 November 2010 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Rachel*


Reputation:

Re: Method for quadratic equation

Posted 25 November 2010 - 09:29 PM

View PostDogstopper, on 25 November 2010 - 07:41 PM, said:

Well, remember that the quadratic formula is relatively simple to use. It looks like this:

(( -b ) +- sqrt(b^2 - 4*a*c))/(2*a)

Where the +- is Plus and minus. So, make two equations, one with the plus and one with the minus and send a b and c through it to get the roots.

Is this what I'm looking for?
public static void quadratic(int a, int b, int c){
    System.out.println((( -b ) + Math.sqrt(b^2 - 4*a*c))/(2*a));
    System.out.println((( -b ) - Math.sqrt(b^2 - 4*a*c))/(2*a));
}

Was This Post Helpful? 0

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Method for quadratic equation

Posted 26 November 2010 - 07:57 AM

Very close. Instead of
-b
use
-1*b


Also, you can't do b^2. Instead use the pow method.
Math.pow(b, 2)



You might also want to put in checks to make sure that Math.pow(b, 2) - 4 * a * c is greater than -1.

This post has been edited by Dogstopper: 26 November 2010 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1