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=coeffB*coeffB-(4*coeffA*coeffC); //Calculates the discriminant
xroot1=(-coeffB+(Math.sqrt(discriminant)))/(2*coeffA); //Calculates the first root of the polynomial
xroot2=(-coeffB-(Math.sqrt(discriminant)))/(2*coeffA); //Calculates the second root of the polynomial
//Output
System.out.println("The Equation is:"); //Displays the value for Coefficient C
System.out.println(coeffA+"x^2 + "+coeffB+"x + "+ coeffC); //Displays the equation to the user
System.out.println("The discriminant is: " + discriminant); //Displays the discriminant to the user
if(discriminant<0){
root1complex=-coeffB/(2*coeffA); //Calculates the first part of the complex root
root2complex=Math.sqrt(-discriminant)/(2*coeffA); //Calculates the second part of the complex root
System.out.println("The Equation has Imaginary roots."); //Displays to the user that there are imaginary roots
System.out.println("The Roots are: " + root1complex + "+" + root2complex + "i" + " and " + root1complex + "-" + root2complex + "i.");
if (coeffA==0){
System.out.println("*NaN stands for \"Not a Number.\"*");
} //Ends If
} //Ends If
else if(discriminant==0){
System.out.println("The Root is: " + xroot1 + "."); //Displays the root
if (coeffA==0){
System.out.println("*NaN stands for \"Not a Number.\"*");
} //Ends If
} //Ends If
else if (discriminant>0){
System.out.println("The Roots are: " + xroot1 + " and " + xroot2 +"."); //Displays the roots
if (coeffA==0){
System.out.println("*NaN stands for \"Not a Number.\"*");
} //Ends If
} //Ends If
System.out.println("Do you wish to perform another calculation?"); //Asks the user if they would like to perform another calculation
loop=sc.nextLine(); //Captures the next string input and stores it to the variable loop
} //Ends If
else if (loop.equals("n")){
System.out.println("Quit."); //Prints "Quit"
contloop=false; // Sets variable contloop to false thereby allowing therby allowing the program to exit the loop and finish
} //Ends If
else if ((!loop.equals("y")) || (!loop.equals("n"))){
System.out.println("Please enter \"y\" or \"n\"."); //Requests the user to enter a valid input
loop=sc.nextLine(); //Captures the next string input and stores it to the variable loop
} //Ends If
}while(contloop==true); //Ends Loop
System.exit(0); //Exits Java
} //End Try
catch (InputMismatchException e){
System.out.println("Invalid input format\nOnly numbers (decimals or intergers) are valid input formats.\nPlease restart program.");
} //End Catch
} //End Main
} //End Class
BigDecimal
instead ofdouble
. \$\endgroup\$