Quadratic Formula Java Program
Jul 19, 2013 Introduction This is a tutorial for solving quadratic equations in java. The program is given below that solves the QE. The program is not extendable.
You are missing a ' after else in the else body: else //needs this bracket root1 = (-B + discriminant) / (2.A); root2 = (-B - discriminant) / (2.A); And then this code is executed regardless of the case: System.out.println('The roots of the quadratic equation are n' + root1 + 'and' + root2); So since root1 wasn't computed in the case where there were no real roots, it remains uninitialized. Setting root1 to zero at the start of the method is a poor solution since you need to change your if else block logic. If this else statement is used only root1 will be computed, and root2 wont be. You are probably testing it so that there are no real roots, and then it wont compute root1 but it will compute root2 instead. Instead of just saying there are no real roots, you could get the square root of the absolute value of the discriminant and then just show the output including the imaginary unit i. That's just an implementation issue though.
Quadratic Formula Java Code
I hope that helps. Tell us some more. Upload in Progress.
Upload failed. Please upload a file larger than 100x100 pixels. We are experiencing some problems, please try again. You can only upload files of type PNG, JPG, or JPEG. You can only upload files of type 3GP, 3GPP, MP4, MOV, AVI, MPG, MPEG, or RM.
Java Program That Solves Quadratic Formula
You can only upload photos smaller than 5 MB. You can only upload videos smaller than 600MB. You can only upload a photo (png, jpg, jpeg) or a video (3gp, 3gpp, mp4, mov, avi, mpg, mpeg, rm). You can only upload a photo or a video. Video should be smaller than 600mb/5 minutes.
Photo should be smaller than 5mb.
I agree to everything, Matthew Flaschen wrote, but like to add some more things: You don't declare and initialize your variables with dummy values upfront. Instead, you restrict the scope as much as possible, and delay the initialization, if possible, to first usage. You have way too many comments.
Assume the developer reading your code knows Java, and can read standard library documentation. Thus, you do not need comments like: //Variable for Coefficient A (or equivalent for any other variable) //Variable & Constant Declaration //An object to read from the keyboard (clear from standard library docs) You also don't need the end comments: //Ends Loop for the same reason. It's an essential part of the language syntax, and many editors let you toggle between the start and end brace.
You have some small repetition, which you can factor out to a variable or method, like the part about NaN. A more realistic comment is something like: // Quadratic formula from Optimized Math Algorithms p. 46 This is silly here, since the quadratic formula is well known. But it would be useful for more novel algorithms.
Also, don't repeat calculations like Math.sqrt(discriminant) or 2.coeffA. Use an intermediate variable. I would also separate the input, calculation, and output phases into different methods.
The last condition statement is definitely wrong: else if ((!loop.equals('y')) (!loop.equals('n'))) It should be &&, not. Why choose a do while conditional statement? If you just wanna make sure it will be run at least one time, use a while loop. The main function are is large; split it to small function. You will maybe find that some code can be reused. For a comment like this: Scanner sc = new Scanner(System.in); //An object to read from the keyboard Everyone knows what this code does, so no need to comment. You comment simple code which already explains itself, then you repeat yourself.
Remember the 'DRY' rule; don't let comments repeat yourself. I don't quite understand the algorithmic part.
Maybe you can add one MathUtil class to help you do the algorithm. I'll definitely try to do that for my next program. Things kinda blended together when I got going.:D Pretty much I googled things for answers because my teacher, like so many people's, melts my mind. Plus, she doesn't teach fast enough for my tastes. I was homeschooled so I mainly look up how to do things and learn from examples before she teaches it. So my code is going to look poor for a little while. And beginning or not, it should be perfect.
Quadratic Formula Java Program
Because, well it just should be.:) – Mar 7 '12 at 17:43.