2 Replies - 1318 Views - Last Post: 31 January 2011 - 02:21 PM Rate Topic: -----

#1 austinsmauston   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 29-October 09

Help With Quadratic Equation Method

Posted 31 January 2011 - 01:28 PM

I'm not sure how to return the number of real roots. This all compiles except the last line. How would I set it up to return the number of real roots?
	
	/**
	 * postcondition: both arrays cannot be null
	 * postcondition: each one must have appropiate size
	 * postcondition: coeffecient cannot be 0
	 * @param c coeffecients of quadratic equation
	 * @param r roots produced by solving quadratic equation
	 * @return the number of real roots
	 * postcondition: if there is 1 distinct solution, that solution will be returned in both positions of the array roots 
	 */
	public static int solve(double[] c, double[] r)
	{
		if (c == null)
			throw new IllegalArgumentException("null input array");
		if (r == null)
			throw new IllegalArgumentException("root is null");		
		if (c.length != 3)
			throw new IllegalArgumentException("cannot be longer than 3");
		if (r.length !=2)
			throw new IllegalArgumentException("cannot be more than 2");
		if (c[0]== 0)
			throw new IllegalArgumentException("coeffecient cannot be 0");
		
		double discriminant = Math.pow(c[1], 2) - 4*c[0] * c[2];
		//one real root
		if ( discriminant == 0)
		{
			r[0] = (-1 * c[1] + Math.sqrt(discriminant)) / (2 * c[0]);
			System.out.println("There is one root.");
		}
		else 
		{
			//two real roots
			if (discriminant > 0)
			{
				r[0] = (-c[1] + Math.sqrt(discriminant)) / (2 * c[0]);
				r[1] = (-c[1] - Math.sqrt(discriminant)) / (2 * c[0]);
				System.out.println("There are two roots.");
			}
		}
			
		return discriminant;
	}




Is This A Good Question/Topic? 0
  • +

Replies To: Help With Quadratic Equation Method

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Help With Quadratic Equation Method

Posted 31 January 2011 - 01:54 PM

What specific errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 glandrum101   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: Help With Quadratic Equation Method

Posted 31 January 2011 - 02:21 PM

The reason the last line throws an error is because you specified at the start of the method that it is suppose to return an int value type, but discriminant is a double.

Your error checking at the top is incorrect. You have it checking if it has more than three values in the array by doing != 3, which should be >3 and != 2, which should be changed to <=2.

Currently, your implementation will return the discriminant value. If you want it to return the real roots then you need to create a class that can hold root values and use that as your return type, or you need to return an array.

This post has been edited by glandrum101: 31 January 2011 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1