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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 15 of 15

Thread: help!! quadratic program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation help!! quadratic program

    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 ax^2 + bx + c = 0. The formula for solving a quadratic equation is:

    x= -b + or - the square root of b^2 - 4ac/2a

    Please use the example where a =1, b= -7, c=10

    so the roots are 5 and 2.

    i am unsure of how to program this can anyone figure it out??


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    help pleasee

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    What have you done so far?

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    i wrote the algorithm and started...

    algorithm is
    1. get the numbers for variables a,b and c from the given equation 0= ax^2 + bx +c.
    2. declare the variables with their given numbers.
    3.plug the numbers into the variables for the equation x = -b + the square root of b^2 - 4ac/2a
    and solve for x.
    4.plug the numbers into the variables for the equation x = -b - the square root of b^2 - 4ac/2a
    and solve for x.
    5.print the roots for both equations.
    public class quadratic 
    {
       public static void main (String[] args){
     
          int variable a = 1;
          int variable b = -7;
          int variable c = 10;
     
    system.out.println(compute roots(equation);
    }
     
    public static int(compute roots(variable a, variable b, variable c); {
     
       int roots = variable -b + math.sqrt((variable b * variable b) - (4 * a * c)/(2*a));
     
         return root;
    }
    }
    I know im missing the other equation for the root with subtracting the square root, and i dont know how to say my given equation im using, 0 = x2 -7x +10...in order to declare my ints. maybe you can help me where I went wrong?
    Last edited by helloworld922; October 14th, 2009 at 11:37 PM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    You're java syntax is a bit off Also, it's not possible for a method to return 2 values, so I removed the helper method. Don't know if you have to handle imaginary roots, I left that out. I would also highly recommend using doubles when computing quadratic roots

    public class Quadratic 
    {
       public static void main (String[] args){
               double a = 1;
               double b = -7;
               double c = 10;
              // compute positive root
              double positive_root = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
              // compute negative root
              double negative_root = (-b-Math.sqrt(b*b-4*a*c))/(2*a);          
              system.out.println(positive_root + "\n"+negative_root);
         }
    }

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    thanks!! do i have to show that i got the variable a b and c from a certain formula or no??

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    ... they are the coefficients to the quadratic equation. In other words, you (or the user) gets to choose them. I don't see much use in this program, it can only solve one quadratic equation

    Unless your assignment specifically told you to find the solution to only one equation, I would make it so the user could input any values of a,b, and c, and also add support for imaginary roots.

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    okayy so maybe you can help me make it so the user can be in control using the scanner??

  9. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    actuallly i would prefer if it included methods and returned them this way...

    can you look this one over and make any changes to fix it pleaseee
    import java.util.*; //so that I can use Scanner
     
    public class quadratic
    {
       public static void main (String[] args)
       {
          Scanner in = new Scanner(System.in);
     
          System.out.print("Please type a quadratic equation, which you would like to find out the roots.");
     
               double quadratic = in.nextDouble();
               double variablea = in.nextDouble();
               double variableb = in.nextDouble();
               double variablec = in.nextDouble();
     
               // calling the method
               double quadraticanswer = quadratic1(variablea, variableb, variablec);
            }
            // compute positive root
              public static double quadratic1(double a, double b, double c) {
              double positiveroot = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
              return positiveroot;
            }
     
              // compute negative root
              public static double quadratic2(double a, double b, double c) {
              double negativeroot = (-b-Math.sqrt(b*b-4*a*c))/(2*a);
              return negativeroot;
              }
    }
    Last edited by helloworld922; October 15th, 2009 at 06:40 PM.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    I think you might run into a problem by naming a variable quadratic and the class quadratic... not positive on this. As a general rule, always name classes starting with upper case letters, and variables/methods starting with lower case letters (the one exception being constructors, they must have the same name as the class, so should start with an upper-case letter).

  11. #11
    Junior Member
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: help!! quadratic program

    Use following code
    Scanner keyb = new Scanner(in);
    int a = keyb.nextInt();
    int b = keyb.nextInt();
    int c = keyb.nextInt();
    double d = pow(b,2);
    double f = sqrt(d-(4*a*c));
    double g = (-b+f)/2;
    double h = (-b-f)/2;
    out.printf("The Roots are %1.2f,%1.2f\n",g,h);


    This code works, trust me. It's even formattted.
    Last edited by helloworld922; October 15th, 2009 at 08:11 PM.

  12. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: help!! quadratic program

    To be honest, you need to follow a good naming convention as helloworld said...
    public class _ {
     
    	private static int _ = 6;
     
    	public _(){
    		String _ = "Hello";
    		System.out.println(_ + this._);
    	}
     
    	public static void main(String[] args) {
    		_ _ = new _();
    		_._ = 60;
    		System.out.println(_._);
    		System.out.println(_._(_));
    	}
     
    	public String _(_ _){
    		 return Integer.toString(_._);
    	}
     
    }
    Last edited by Freaky Chris; October 16th, 2009 at 09:09 AM.

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    :O

    public class Ø
    {
         public Ø()
         {
              System.out.println("No!");
         }
    }

  14. #14
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help!! quadratic program

    Hmm, I have the same problem. Here's my code:

    import java.util.Scanner;
     
    public class Assignment2
    {
       public static void main (String[] args)
       {
          Scanner in = new Scanner(System.in);
     
    		System.out.println("This program computes the roots of a Quadratic Equation.");
     
    		System.out.println();
     
     
               System.out.println("Please input coefficient A.");
               double coeffA = in.nextDouble();
               System.out.println("Please input coefficient B.");
               double coeffB = in.nextDouble();
               System.out.println("Please input coefficient C.");
               double coeffC = in.nextDouble();
     
               // calls the method
               double quadraticAnswer1 = quadratic1(coeffA, coeffB, coeffC);
               double quadraticAnswer2 = quadratic2(coeffA, coeffB, coeffC);
     
               System.out.println("The roots of the equation are " + quadraticAnswer1 + " and " + quadraticAnswer2);
            }
              // computes positive root
              public static double quadratic1(double a, double b, double c) {
              double posRoot = (-b+Math.sqrt(b*b-4*a*c))/(2*a);
              return posRoot;
            }
     
              // computes negative root
              public static double quadratic2(double a, double b, double c) {
              double negRoot = (-b-Math.sqrt(b*b-4*a*c))/(2*a);
              return negRoot;
              }
    }

    But when I run the Program, my program says this: "The results are NaN and NaN" What the Hell does NaN mean?

  15. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help!! quadratic program

    You tried to take the square root of a negative number. NaN stands for "not a number", which technically isn't true. It's just not a real number, but there are imaginary roots.