Google Answers Logo
View Question
 
Q: Java Programming - Quadratic Equations ( No Answer,   2 Comments )
Question  
Subject: Java Programming - Quadratic Equations
Category: Computers > Programming
Asked by: java_design-ga
List Price: $15.00
Posted: 29 Nov 2005 05:34 PST
Expires: 30 Nov 2005 09:47 PST
Question ID: 598931
Design and develop a Java program that continuously computes and
displays value(s) for x, given quadratic equations (i.e. a
second-order polynomials) of the form:

ax2 + bx + c = 0 
where the values for the coefficients a, b and c are supplied by the
user, and are assumed to be integers within the range of -100 to 100.
To control the loop use a menu interface. The menu should include two
options: "Calculate quadratic" and "End".

Note that to solve a quadratic equation we must calculate the roots.
This can be done using the quadratic formula:

root 1 = (-b + sqrt(b2-4ac)) / 2a

root2 = (-b - sqrt(b2-4ac)) / 2a

Example:

x2 + 2x - 8 = 0 
a= 1, b = 2, c = -8 
 
roots = (-2 +or- sqrt(22-4x1x-8)) / 2x1 
      = (-2 +or- sqrt(4+32)) / 2 
root1 = (-2 + 6)/2 = 4/2 = 2.0 
root2 = (-2 - 6)/2 = -8/2 = -4.0 
 
x = 2.0 or -4.0 

However, there are certain special consideration to be taken into account:

If a and b are both zero there is no solution (this is referred to as
the degenerate case):
-8 = 0?
a= 0, b = 0, c = -8
(degenerate case)

If a is zero and b is non zero the equation becomes a linear equation.

2x - 8 = 0
a= 0, b = 2, c = -8
(Linear equation)

root = -c/b = 8/2 = 4.0
x = 4.0

If the value for the term b2 - 4ac (the discriminant) is negative
there is no solution (conventionally we cannot find the square root of
a negative number!):
x2 + 2x + 8 = 0
a= 1, b = 2, c = 8

roots = (-2 +or- sqrt(22-4x1x8)) / 2x1
      = (-2 +or- sqrt(4-32)) / 2
      = (-2 +or- sqrt(-28))
Negative discriminant therefore no solution.

If the discriminant is 0 then there are two identical solutions, i.e.
only one solution (root) need be calculated:
x2 + 4x + 4 = 0
a= 1, b = 4, c = 4

roots = (-4 +or- sqrt(42-4x1x4)) / 2x1
      = (-4 +or- sqrt(16-16)) / 2
(Discriminant = 0, there fore only one solution)
root = -4/2 = -2
x = -2.0

Output, where appropriate, should be accurate to at least several decimal places.

Please try to include explanations where appropriate.

Clarification of Question by java_design-ga on 29 Nov 2005 05:39 PST
The program must be written in Java 1.5 !!!
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java Programming - Quadratic Equations
From: scubajim-ga on 29 Nov 2005 08:41 PST
 
I'd do it for $200.  I wouldn't do it for $15.
Subject: Re: Java Programming - Quadratic Equations
From: cjdavis-ga on 29 Nov 2005 10:23 PST
 
import	java.io.*;
public class quadratic
{
    public int a = 0,b = 0,c = 0;
    public int flag=0;
    public double r1=0,r2=0;
    public quadratic()
    {
        do
        {
            
        System.out.println("\n\n\n\n\nType 1 to Calculate quadratic equation");
        System.out.println("Type 3 to END");
        int choice = getChoice();
        
        switch(choice)
        {
        case 1:
        a = inputABC("a");
        b = inputABC("b");
        c = inputABC("c");

        if((Math.pow(b,2)-4*a*c)<0)
        {System.out.println("\nNegative discriminant therefore no solution!");}
        else if((Math.pow(b,2)-4*a*c)==0)
        {
        r1 = getRoot1(a,b,c);
        System.out.print("x= "+r1);   
        }
        else if(a==0&&b==0)
        {System.out.println("\nDegenerate Case");}
        else if(a==0)
        {double u = -c/b;
         System.out.println("The root is "+u);}
        else
        {
        r1 = getRoot1(a,b,c);
        r2 = getRoot2(a,b,c);
        System.out.print("x= "+r1+" or "+r2);
        }
        break;
        case 3:
        flag = 1;
        break;
        default:
        System.out.println("\nThat is not an option!  Try Again.");
        break;
        }
          
        }while(flag==0);
    }
    
    public int inputABC(String s2)
    {
        InputStreamReader stdin =
				new InputStreamReader(System.in);
		BufferedReader console =
				new BufferedReader(stdin);
		int i1=0;
		String	s1;
		try
		{
			System.out.print("Enter interger for "+s2+": ");
			s1 = console.readLine();
			i1 = Integer.parseInt(s1);
		}
		catch(IOException ioex)
		{
			System.out.println("\nInput error");
			System.exit(1);
		}
		catch(NumberFormatException nfex)
		{
			System.out.println("\"" + 
				nfex.getMessage() + 
				"\" is not numeric");
			System.exit(1);
		}
		return(i1);
    }
    public int getChoice()
    {
        InputStreamReader stdin =
				new InputStreamReader(System.in);
		BufferedReader console =
				new BufferedReader(stdin);
		int i2=0;
		String	s2;
		try
		{
			System.out.print("User's Choice: ");
			s2 = console.readLine();
			i2 = Integer.parseInt(s2);
		}
		catch(IOException ioex)
		{
			System.out.println("Input error");
			System.exit(1);
		}
		catch(NumberFormatException nfex)
		{
			System.out.println("\"" + 
				nfex.getMessage() + 
				"\" is not numeric");
			System.exit(1);
		}
		return(i2);
    }
    public double getRoot1(int x, int y, int z)
    {
        double root1 = (-y + Math.sqrt((Math.pow(b,2))-(4*x*z)))/(2*x);
        return(root1);
    }
        public double getRoot2(int x, int y, int z)
    {
        double root2 = (-y - Math.sqrt((Math.pow(b,2))-(4*x*z)))/(2*x);
        return(root2);
    }
     public static void main(String[] args)
    {
        quadratic qd = new quadratic();
    }
    
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy