Results 1 to 1 of 1

Stackoverflow questions that the original poster continually deletes
Wrie a method printRoots that given 3 terms as input(a,b,c) in that order prints their roots
We have the following given information
1. 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
2. If **b²4ac** ****equals 0****, the program should print. “The equation has one X” where X is the only root
3. 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?
_____________________________
Java Code: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); int a=reader.nextInt(); System.out.println("Enter the value of a"); int b=reader.nextInt(); System.out.println("Enter the value of b"); int c=reader.nextInt(); System.out.println("Enter the value of c"); //now compte the discrimintat d double discrimintant = d; double X,Y; //root 1 & root 2, respectively // is the step double X,Y necessary? double d = (b*b)(4.0*a*c); if (d > 0.0){ d = Math.sqrt(d); System.out.println("The two roots are X and Y"); double X = (b + d)/(2.0 * a ); //X= root 1, which is larger double Y = (b  d)/(2.0 *a); //Y= root 2, which is the smaller root System.out.println("Root 1" = X "and" "Root 2" "=" Y); } else{ if (d==0.0) //then...how to write? System.out.println("The equation has one root X")//where X is the only root double X = (b + 0.0)/(2.0 * a);//repeated root System.out.println("Root" "=" X); } else{ if(d < 0.0) System.out.println("The equation has two roots (X1 + Y1i) and (X2 +Y2i)"); // where i represents the square root of negative 1 double X1 = b/(2*a); double Y1 = (Math.sqrt(C))/(2*a); double X2 = b/(2*a); double Y2 = ((Math.sqrt(C)))/(2*a); double Y2 = ((Math.sqrt(C)))/(2*a); System.out.println("Root 1" "=" (X1 + Y1i) "and" "Root 2" "=" (X2 +Y2i)"); } } }
Question 2:
computer science  make a java code that prints the roots of a quadratic equation  Stack Overflow
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.
__________________________________________________ _________
Java Code: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 } } }
Accepted answer:
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).
Java Code://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; } }
`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.
__________________________________________________ _
Third question:
Creating a java program that given 3 terms as input(a,b,c) in that order prints their roots  Stack Overflow
Write a java code for the nth root?
Calculate the nth root y^n instead of y²( the square root). Write a method called root which takes as input two values: double x and int n and calculates the nth rootof x. (assume that x is greater than or equal to 0.)
You may find it useful to write a method pow which takes as input a double x and an int n and calculate x^n. This will simplify the code in the root method
**Can someone help me with this code. I have done the following work below but am completely new to Java. For that reason, I need help correcting my code.**
Java Code:public class nthRootfinder{ public static double root(double x, int n){ //a method which takes as input two values and calculates the nth root of x double start = x > 1.0; double end = x > 1.0; boolean accurate = false; while(!accurate){ //true double middle = (start + end) / 2; double middletoEnd = Math.pow(middle, double)n); if (Math.abs(middleToEnd  x) <=0.000001) return middle; } else{ if (middleSquared > x) end = middle; } else{ start = middle; } } }
Similar Threads

Eclipse deletes and replaces code when I highlight
By Text in forum EclipseReplies: 0Last Post: 05072012, 10:10 PM 
Java installation continually repeats
By BobW56 in forum Forum LobbyReplies: 1Last Post: 01022011, 12:21 AM
Bookmarks