I have been self-teaching myself java using a free guide I found online. This is the first program I have wrote just off the top of my head, looking at additional resources (such as this website) as little as possible.
I think the program is finally complete.
Is it any good or have I been making some bad mistakes and/or following a few bad practices when I am creating my code?
I want to make sure now so that I don't get into the habit of using bad practices.
By judging my current level with Java, would you do anything different to have a program of the same effect or not?
Here is my final code (with example coefficients given in the calculation(x, y, z) method):
Code:class QuadraticSolver {
static int aValue, bValue, cValue; // This class is available to all methods
static double plusAnswer, minusAnswer; // This class is availible to all methods
public static void main(String[] args) {
calculation(1, 1, -6); //This line is where the coefficients are entered
if (aValue <= 0) { // This is to ensure that 0 is not entered as a coefficients for the first term
System.out.println("0 is not a valid value for the coefficient of x\u00B2");
return;
}
else {
System.out.println("The quadritic equation entered was:");
System.out.print(aValue + "x" + "\u00B2");
if (bValue >= 0) { // These if statements ensure that + and - are not both displayed for a negative coefficient
System.out.print("+" + bValue + "x");
}
else {
System.out.print(bValue + "x");
}
if (cValue >= 0) {
System.out.println("+" + cValue);
}
else {
System.out.println(cValue);
}
}
System.out.println(" "); // This line puts a blank line inbetween the two sections of text on the output
System.out.println("It's roots are:");
System.out.println(plusAnswer + " and " + minusAnswer);
}
public static void calculation(int a, int b, int c) { // The start of the calculation method - the variables for the coefficients are parameters
aValue = a; // Because the coefficients are needed in both methods, they need to be accessible to both as a static variable (see the top comments). However, they also need to be a parameter so that the values can be entered with the method, by making the value both of these, both actions are fulfilled.
bValue = b;
cValue = c;
plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a); // This is the quadratic formula in Java syntax (positive)
minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a); // This is the quadratic formula in Java syntax (negative)
}
}

