1

I'm planning on creating a calculator for physics that would run off of a few equations. But, I realized that it would be a lot of code.

With the equation v = x/t (just one of many I want to include) , there's already three possible equations.

v = x/t     x = vt       t = x/v

What I was planning to have the program do is:

-Ask the user what equation they're going to use

-Ask what variable is missing

-Solve for it with a matching equation

My question is whether or not there is a way I can format the code more efficiently. Without knowing how, it seems like running a lot of very similar code for each variant of an equation.

I'm planning to create this using multiple classes, if it isn't clear.

  • Not sure if I got it right. Does the user provide any values for the variables or should the program only display the form of the equation that is resolved to the variable the user has entered? – Frank Puffer May 26 '16 at 15:54
  • Possible duplicate of [How to Solve Equations with java?](http://stackoverflow.com/questions/1431885/how-to-solve-equations-with-java) – cyroxis May 26 '16 at 16:56

2 Answers2

1

There's 2 approaches I can think of that would make the most sense.

The first more traditional way would be to make a bunch of classes for each kind of equation you wanted to include.

public class Velocity implements Equation{
    public double solveT(double v, double x){
        if(v != 0)
            return x / v;
        else
            return 0; //or whatever value is appropriate
    }

    public double solveX(double v, double t){
        return v * t;
    }

    public double solveV(double t, double x){
        if(t != 0)
            return x / t;
        else
            return 0; //or whatever value is appropriate
    }
}

This keeps all of your different equations separate, and if you define an empty Equation interface you can substitute different Equation objects as needed. The drawback is that you'd have a lot of classes to keep track of, and you would have to make sure that the Equation object you're trying to call methods on is the correct instance, i.e. trying to call solveX() on a Density instance that doesn't have a solveX() method. However, having each class separate is a nice way to organize and debug.

The other approach is using Java8 lambdas:

interface twoTermEq{
    double solve(double a, double b);
}

public class Calculator{
    public double solveTwoTermEq(twoTermEq eq, double a, double v){
        eq.solve(a, b);
    }
}

public static void main(String[] args){
    twoTermEq velSolveX = (t, v) -> return t * v;
    twoTermEq velSolveT = (x, v) -> v != 0.0 ? return x / v : 0.0;
    twoTermEq velSolveV = (x, t) -> t != 0.0 ? return x / t : 0.0;
    //define as many equations as needed...

    Calculator c = new Calculator();
    //select which equation to run, collect user input
    ....
    //do the calculation
    double result = c.solveTwoTermEq(velSolveX, t, v);
}

This lets you define your equations all in one place and doesn't need a boatload of classes. You could similarly define interfaces for ThreeTermEq, FourTermEq, etc., as well as solveThreeTermEq(), solveFourTermEq(), etc. methods for the Calculator class. The drawback here is that it might become more difficult to maintain and organize, and I believe there's an upper limit on how big a class file can be; if a class file becomes too big it won't compile, which could happen if you've defined tons of equations.

For me the choice would come down to how I wanted to organize the code; if I wanted to only include a (relatively) small number of (relatively) simple equations, I would probably use lambdas. If I wanted to include every physics equation across as many physics topics as possible, I'd probably use classes.

Either way, there's going to have to be some similar code written for different permutations of an equation - I don't think there's really any way around that. You could try for a novel approach using a bunch of Objects to try to circumvent that, but I think that would be overwrought and not worth the effort; it's not like flipping variables around is hard.

NAMS
  • 983
  • 7
  • 17
1

You would probably be best off using some kind of symbolic math toolbox. Maple and MatLab are good languages/environments for working with equations, as they recognize symbolic math and can manipulate equations fairly easily. Java does not have any built in libraries for this, and it is difficult to find any libraries that would support a 'Computer Algebra System' to manipulate the equations for you. You might want to look at JAS (Java Algebra System), but I'm not sure that will do what you're looking to do. Most likey, you will need to solve for each variable by hand and build functions for each individual expression.

If you're sticking with Java, this is how I would go about it. In terms of code formatting, I would just create one Equation class that holds an array of all the variations of a given equation. The variations (i.e. V=I*R, I=V/R, R=V/I) would all be passed into the constructor for the class. A solve method could then be implemented that takes the requested variable to be solved for, the other variables and their values (distinguished by two arrays- one for characters and one for values)

Usage could be as follows:

Equation ohmsLaw = new Equation(new String[] {"V=I*R", "I=V/R", "R=V/I"});
double resistance = ohmsLaw.solve('R', new char[] {'I', 'V'}, new double[] {0.5, 12.0});

You would need to write a little bit of symbolic parsing, but that makes it fun, right?

May or may not have been the answer you were looking for, but hopefully it's some help. Good luck!