HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

How does this quadratic formula program look?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisprogramlookdoeshowquadraticformula

Problem

I'm new to Java and this is my first program. I'm trying to verify that my thought processes are good.

Let me know of any improvements infor the code, whether it's too few/too many comments, poorly structured code, etc.

```
import javax.swing.*;
import java.math.*;
import java.util.*;

class Root
{
public static void main(String[] args)
{
//Variable & Constant Declaration
double coeffA=0.0; //Variable for Coefficient A
double coeffB=0.0; //Variable for Coefficient B
double coeffC=0.0; //Variable for Coefficient C
double xroot1=0.0; //Variable for the first root
double xroot2=0.0; //Variable for the second root
double discriminant=0.0; //Varaible for the discriminant
double root1complex=0.0; //Variable for part a of the complex root
double root2complex=0.0; //Variable for part b of the complex root
boolean contloop=true; //Variable for continuing loop
String loop="y"; //Variable for input choice of continuing loop

Scanner sc = new Scanner(System.in); //An object to read from the keyboard

try{
do{

if (loop.equals("y")){

//Input
System.out.println("Please enter the value for the first coefficient: "); //Asks the user to input the value for the A term
coeffA=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffA

System.out.println("Please enter the value for the second coefficient: "); //Asks the user to input the value for the B term
coeffB=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffB

System.out.println("Please enter the value for the third coefficient: "); //Asks the user to input the value for the C term
coeffC=sc.nextDouble(); //Captures the keyboard input and stores it to the variable coeffC

//Calculations
discriminant=coeffBcoeffB-(4coeffA*coeffC); //Calculates the discrimi

Solution

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.

Code Snippets

//Ends Loop
// Quadratic formula from Optimized Math Algorithms p. 46

Context

StackExchange Code Review Q#9571, answer score: 7

Revisions (0)

No revisions yet.