4 Replies - 731 Views - Last Post: 22 April 2012 - 08:21 PM Rate Topic: -----

#1 aliaslam7163  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 12

help to build this java code for quadratic equation

Posted 22 April 2012 - 06:21 PM

package edu.bellevuecollege.cs210.quadraticequation;
import java.util.*;


public class QuadraticEquation {

	
	public static void main(String[] args) {
		
		double a, b ,c; //values obtained from keyboard to satisfy a binomial equation
		
		System.out.println("Enter in the value of a = ");
		a = getinput();
		
		System.out.println("Enter in the value of b = ");
		b = getinput();
			
		System.out.println("Enter in the value of c = ");
		c = getinput();
		
		double numerator = Math.pow(b, 2) - (4*a*c);
		double x1 = ((-1 * B)/> + Math.sqrt(numerator)) / (2 * a);
		double x2 = ((-1 * B)/> - Math.sqrt(numerator)) / (2 * a); 
		
		if(numerator > 0){
			System.out.println("X is " + x1);
			System.out.println("X is " + x2);
		}
		else if (numerator == 0){
			System.out.println("X is " + x1);
		}
		
		
	}
	
	
	public static int getinput()
	{
		Scanner input; //bringing in the scanner class to obtain keyboard inputs from user
		input = new Scanner(System.in);
		
		return input.nextInt();
		
		
	}

}


*Edited: add code tags. Please :code:

This post has been edited by pbl: 22 April 2012 - 07:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help to build this java code for quadratic equation

#2 aliaslam7163  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 12

Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 06:29 PM

the smiley face is suppose to be b but i dont know why its displaying a smiley character
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 07:53 PM

View Postaliaslam7163, on 22 April 2012 - 09:29 PM, said:

the smiley face is suppose to be b but i dont know why its displaying a smiley character

Ya the browser nterprets b ) as a smiley don't worry about it or avoid to use a single B as variable name

And what is your question/problem ?
If my secondary scholl maths are rigth it -b +/ sqrt(b^2 - 4ac) not 1 - b

If your numerator is < 0 your sqrt() will fail

Sorry -1 * b == -b
Was This Post Helpful? 0
  • +
  • -

#4 aliaslam7163  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 12

Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 07:53 PM

well the quadratic function is implemented in the code as 2 steps. how can i fix that if my sqrt is less than zero. how can i stop it from failing.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: help to build this java code for quadratic equation

Posted 22 April 2012 - 08:21 PM

Perform the calculation of X1 and X2 inside the if() that test the numerator so you won't do it if < 0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1