Search code examples
javamathcalculatoralgebra

Java Math Completing the square calculator


Hey im new to the whole programming scene im just a high schooler taking a java computer science course. Im trying to test out my abilities with what I know so far and to try understand stuff and practice. So im trying to make a couple math calculators for certain things. Im working on one to complete the square. So like (2 + 4x^2)^2 will come out to be (4 + 16x + 4x^2). My problem is that I cant get it to fully work, and the code is kinda klanky.

import java.util.Scanner;

public class SquaringDoubles {

    public static void main(String[] args) {

        //declaring
        Scanner input = new Scanner(System.in);

        //inputs
        System.out.println("Enter in the double with this format ( A + B )^2");

        System.out.print("A --> ");
        double A = input.nextInt();

        System.out.print("B --> ");
        double B = input.nextInt();
        input.close();

        System.out.println("You're Equation: " + A + " + " + B + "x");

        //Math
        //A + C + B
        double A2 = Math.pow(A, 2);
        double B2 = Math.pow(B, 2);
        double C = 2 * (A * B);

        //final
        System.out.print("You answer: ");
        System.out.println(A2 + " + "+ C + "x" + " + "+ B2+ "x^2");
    }
}

Solution

  • Inserting is the simplest general approach to get from one formula to an equivalent equation of form

    Y(x) = A2 + B2x + C2(x)^2
    

    As there are three variables(A2,B2,C2) we need three equations to solve the system. To get those equations we can simply put three x of our choice and their calculated Y(x) into the form we want. And solve the system of equations.

    So in essence we calculate Y(x) for three x of our choice and just stick them into the formula. One can take any (defined) value but some make life easier.

    So

    X=0 is the first candidate as it eliminates everything with x and directly gives you A2.

    Y(0)= C2*(0)^2+B2*(0)+A2 = A2
    A2 = Y(0)
    

    x=1: you get

     Y(1)= C2*(1)^2+B2*(1)+A2 = C2+B2+A2
    

    x=-1: you get

    Y(-1)= C2*(-1)^2+B2*(-1)+A2= C2-B2+A2
    

    Eliminating C2:

    Y(-1)+B2-A2 = Y(1) -B2 -A2
    -> 2*B2=Y(1)-Y(-1)
    B2=(Y(1)-Y(-1))/2
    

    Finaly calculate C2 by inserting into C2+B2+A2=Y(1):

    C2=Y(1) -B2 -A2
    

    So in General - for any given(valid) equation to get to the form A2+B2·x+C2·x²:

    • A2 = Y(0)
    • B2=(Y(1)-Y(-1))/2
    • C2=Y(1) -B2 -A2 = (Y(1)+Y(-1))/2 - Y(0)

    In your example Y(1) = Y(-1) due to the square, so

    Y(x) = (A + B * (x)^2)^2
    
    Y(1) = (A + B * (1)^2)^2   =(A+B)^2  // x=1
    Y(-1) = (A + B * (-1)^2)^2 =(A+B)^2  // x=-1
    B2 =(Y(1)-Y(-1))/2 =0
    

    And so

    C2= Y(1) -A2 = (A+B)^2 - A2
    

    So for (A + B*(x)^2)^2:

    • A2 = Y(0) = A^2
    • B2=0
    • C2=Y(1) - Y(0) = (A+B)^2 - A2

    Code:

    public static void main(String[] args) {
    
            //declaring
            Scanner input = new Scanner(System.in);
    
            //inputs
            System.out.println("Enter in the double with this format (A + B(x)^2)^2");
    
            System.out.print("A --> ");
            double A = input.nextInt();
    
            System.out.print("B --> ");
            double B = input.nextInt();
            input.close();
    
            System.out.println("You're Equation: (" + A + " + " + B + "x^2)^2");
    
            //Math
            //A + C + B
            double A2 = Math.pow(A, 2);
            /**  old code:
             * 
            double B2 = Math.pow(B, 2);
    
            double C = 2 * (A * B);
            */     
            /** replacement : */
            //Y(1)=(A + B*(1)^2)^2 = (A+B)^2
            //Y(-1)=(A + B*(-1)^2)^2 = (A+B)^2
            //B2 = (Y(1)-Y(-1))/2 = ((A+B)^2 -(A+B)^2)/2 = 0
            double B2=0; // it is always 0 in this case
            //Y(1)=(A + B*(1)^2)^2 = (A+B)^2
            double C2=(A+B)*(A+B) - A2; //Y(1) -A2 
            //final
            System.out.print("You answer: ");
            System.out.println(A2 + " + " + B2 + "x" + " + " + C2 + "x^2");
        }
    

    I added some comments, to show what is going on and how to do similar with other equation.