I've already created classes for a variable (which includes a base
(String) and int exponent).
This makes little sense. A Variable should not have an exponent, it should
have exponents applied to it in various equations or operations. In the case
of 4x*x - 3x + 12 = 0, the Variable is "x". It has two different uses, with
different exponents.
I'm now creating a class called Term that contains the variable and
coefficient classes so I can do different algebraic, although simple, stuff.
I think you want a Term to have the exponent in addition to the coefficient.
Right now, I'm doing trying to instantiate the Terms class. The
following lines of code that were partially inspired by the solutions
I got here. The following lines of code work. Thank you.
Variable var1 = new Variable("x");
Term term1 = new Term( new int[] {2}, new Variable[] {var1} );
You're overcomplexifying by trying to avoid designing a class structure.
Don't do that - take the time to think about your object model, rather than
passing around arrays of primitives that the caller needs to build and keep in
sync with each other. Consider:
interface Term
Constant implements Term (a constant value)
Variable implements Term (a simple variable to be solved for or evaluated)
Operator (an operator, like exponentiation, division, sin, etc.)
Expression implements Term (Operator operator, Term... operands)
Equation (Term, Term)
Relation (equals, greater than, etc.)
Inequality (Term, Relation, Term)
With a sufficient set of Operators, you can encode just about anything you want.
You may want convenience subclasses of CompoundTerm, like
PolynomialTerm (Constant coefficient, Term base, Constant exponent)
and since Constant has constructors for double and long, and Variable has a
String constructor, you can have convenience constructors on PolynomialTerm
PolynomialTerm(double coefficient, String variable, double exponent)
and so on.
You'll end up being able to do something, after creating enough constructors
to do the right thing with strings and typed parameters, like
Variable x = new Variable("x");
Variable y = new Variable("y");
new Inequality(
new Expression("+", new PolynomialTerm(4, x, 2),
new PolynomialTerm(7, new Expression("*", x, y), 1),
new PolynomialTerm(-14, y, 2)),
Relation.valueOf(">="),
Constant.PI);
Which is "4x^2 + 7xy -14y^2 >= PI".
It's also fairly amenable to constructing things as you parse strings.