-1

I have posted this question before. But have made a huge edit to it. And would like to ask for help in correcting my steps since my java code is not compiling.

write a method printRoots that given 3 terms as input, representing a,b, and c in that order prints their roots.

We have the following given information

**If b²-4ac is a positive number, your program should print “The two roots are X and Y” where X is the larger root and Y is the smaller root

If b²-4ac equals 0, the program should print. “The equation has one X” where X is the only root

If b²-4ac is a negative number, the program should print.” The equation has two roots(-X1 + Y1i) and (-X2 and Y2i)**

The term can be determined based on:

If b^2 - 4ac is a negative number, then the quadratic equation becomes: (-b+/- √C)/2a -This means the equation can be simplified to become (-b+/- √Ci)/2a where the square root is not a positive number Calculate the coefficient and print that(i.e X1 is -b/2a and Y1 is sqrt(-C)/2i)

Note: Not allowed to use Scanners for this question

Is it possible for someone to review my program and tell me where I have gone wrong and do i just remove my scanners to make it a program without scanners? How do i enter a,b,c then??

Give note: you should NOT use Scanner inside this method. To test your method you can use SCanner in the main method OR you can hard code values into your code from the main method and then call the method printRoots with those inputs or ouputs.


import java.util.Scanner;//delete this part after 
public class findingRoots {
  public static void main(String[] args)
    {
  }
    public static double printRoots (){ //should it be double here or int? 
      //read in the coefficients a,b,and c 
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter the value of a");
    int a=reader.nextInt();
    System.out.println("Enter the value of b");
    int b=reader.nextInt();
    System.out.println("Enter the value of c");
    int c=reader.nextInt();        
    //now compte the discrimintat d 
     double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
     d=Math.sqrt(discriminant);
     double X,Y; //root 1 & root 2, respectively
     // is the step double X,Y necessary? 
     double d = (b*b)-(4.0*a*c);

       if (discriminant > 0.0){ 
       X = (-b + d)/(2.0 * a ); //X= root 1, which is larger 
       **//do i put int or double in front of X?** 
       Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root 
           String root1 = Double.toString(X)
       String root2 = Double.toString(Y)
   System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
 }
 else{
   if (discriminant==0.0) 
     X = (-b + 0.0)/(2.0 * a);//repeated root
     String root2 = Double.toString(X)
     System.out.println("The equation has one root X:"  + root1);//where X is the only root 
 }
 if(discriminant < 0.0){
    X1 = -b/(2*a);
    Y1 = (Math.sqrt(-C))/(2*a);
    X2 = -b/(2*a);
    Y2 = (-(Math.sqrt(-C)))/(2*a);
    String root1 = Double.toString(X1)
    String root2 = Double.toString(Y1)
    String root3 = Double.toString(X2)
    String root4 = Double.toString(Y2)
    System.out.println("The equation has two roots:" + (root1 + (root2)"i") "and" + (root3 + (root4)"i");
   // where i represents the square root of negative 1 
 }
}
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Jenny Fer
  • 21
  • 1
  • 2
  • 7
  • 1
    Perhaps the `**` at the front of the line is a compile problem. But better still, you can tell us what sort of errors you are getting. We can help you understand those messages. – Lee Meador Jun 03 '13 at 19:49
  • 1
    You may want to go to http://codereview.stackexchange.com/ – OldProgrammer Jun 03 '13 at 19:49
  • 2
    This is not really javascript related is it? – John b Jun 03 '13 at 19:49
  • 1
    `**//do i put int or double in front of X?** ` will probably give you a compile time failure. However, if you're having compile time problems, you should include the failure message in the question. – Aurand Jun 03 '13 at 19:50
  • 1
    One coding hint you might want to know is that when there are 3 options we usually do `if () {} else if () {} else {}` with conditions inside the parentheses and code in the braces. – Lee Meador Jun 03 '13 at 19:51
  • Reading the problem description, you _can_ use `Scanner` in the `main()` method to get the coefficients and pass them to your `printRoots()` method. So that's an answer to one of your questions. – Reinstate Monica -- notmaynard Jun 03 '13 at 19:52
  • Duplicate of [Creating a java program that given 3 terms as input(a,b,c) in that order prints their roots](http://stackoverflow.com/questions/16899771/creating-a-java-program-that-given-3-terms-as-inputa-b-c-in-that-order-prints) – user207421 Jun 04 '13 at 00:13
  • deleted code re-shown. – Hovercraft Full Of Eels Jun 04 '13 at 16:37
  • Original poster again deleted code. Again, I've rolled it back. I've also re-posted the question as an answer so that the original poster cannot delete it. – Hovercraft Full Of Eels Jun 04 '13 at 17:01

1 Answers1

0

Ok, so there a quite a bit wrong with the code. Here is a running version of your program. (or at least as close as I can get to what you are trying to do).

//import java.util.Scanner;//delete this part after 
public class findingRoots {
    public static void main(String[] args) { 
        double temp = printRoots(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
    }
    public static double printRoots (int a, int b, int c){ //should it be double here or int? 
       //read in the coefficients a,b,and c 
       //now compte the discrimintat d 
       double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
       double d=Math.sqrt(discriminant);
       double X = 0,Y = 0; //root 1 & root 2, respectively
       // is the step double X,Y necessary? 
       d = (b*b)-(4.0*a*c);

       if (discriminant > 0.0) { 
           X = (-b + d)/(2.0 * a ); //X= root 1, which is larger 
           //do i put int or double in front of X?** 
           Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root 
           String root1 = Double.toString(X);
           String root2 = Double.toString(Y);
           System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
       }
       else if (discriminant==0.0){
           X = (-b + 0.0)/(2.0 * a);//repeated root
           String root2 = Double.toString(X);
           System.out.println("The equation has one root X:"  + root2);//where X is the only root 
       }
       else if (discriminant < 0.0){
           double X1 = -b/(2*a);
           double Y1 = (Math.sqrt(-c))/(2*a);
           double X2 = -b/(2*a);
           double Y2 = (-(Math.sqrt(-c)))/(2*a);
           String root1 = Double.toString(X1);
           String root2 = Double.toString(Y1);
           String root3 = Double.toString(X2);
           String root4 = Double.toString(Y2);
           System.out.println("The equation has two roots:" + root1 + root2  + "and" + root3 + root4);
           // where i represents the square root of negative 1 
      }
      return -1;
    }
}

To run this code simply type: java findingRoots 1 2 3 where 1 2 3 are your a, b, c values respectively.

This is not working the way you want it to (i assume). This should get you a start as to trouble shooting though as it can now run. Let's see what you can do from here.

ageoff
  • 2,798
  • 2
  • 24
  • 39
  • Compare my code to your code. Make sure you end each statement with a ";". Do you see after all of my statements i have a ";"? This is required, just remember to put that after your statements. – ageoff Jun 03 '13 at 20:25
  • I had realized I had forgotten to put the ; after my statement. I had removed by comment since I had realized that. Though thank you once again for a great response – Jenny Fer Jun 03 '13 at 20:26
  • It has finally COMPILED! =p THANK YOU for ur kind help. I had been stuck on this for a while – Jenny Fer Jun 03 '13 at 20:27
  • @JennyFer You are welcome. Make sure you look through and understand why your code was not compiling though. You had syntax (it wont compile) and still have logic (you will get the wrong result when it does compile) errors in your code that need to be worked out. – ageoff Jun 03 '13 at 20:31