Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How do you use as an array as an argument (http://www.velocityreviews.com/forums/t390640-how-do-you-use-as-an-array-as-an-argument.html)

Hakusa@gmail.com 02-05-2007 11:14 PM

How do you use as an array as an argument
 
I have this class Term (of the algebraic kind) and I want it to accept
an array of variables and an array of coefficients (in case I get
something like 2x*2y). I have no problems with the constructor or
anything else, but I can't figure out how to make the constructor
accept an array as an argument without resorting to arbitrary array
creation and then deleting the array later. It feels unnecessary.


Mark Space 02-05-2007 11:27 PM

Re: How do you use as an array as an argument
 
Hakusa@gmail.com wrote:
> I have this class Term (of the algebraic kind) and I want it to accept
> an array of variables and an array of coefficients (in case I get
> something like 2x*2y). I have no problems with the constructor or
> anything else, but I can't figure out how to make the constructor
> accept an array as an argument without resorting to arbitrary array
> creation and then deleting the array later. It feels unnecessary.
>


I'm honestly not sure what you are asking. Some examples of how it
works/you want it to work would be helpful.

Do you want overloaded constructors?

Term( Double t ) {} // One coefficient
Term( Double [] ts ) {} // Multiple coefficients

This gives you a Term that could be constructed with either one number,
or an array of numbers:

Term t1 = new Term( 10.1 );
Term t2 = new Term( Double [] = {10.1, 12.2} };

Don't quote me on the syntax, I didn't check it and I don't often use
anonymous arrays ;-)

Mark Rafn 02-06-2007 12:21 AM

Re: How do you use as an array as an argument
 
Hakusa@gmail.com <Hakusa@gmail.com> wrote:
>I have this class Term (of the algebraic kind) and I want it to accept
>an array of variables and an array of coefficients (in case I get
>something like 2x*2y). I have no problems with the constructor or
>anything else, but I can't figure out how to make the constructor
>accept an array as an argument without resorting to arbitrary array
>creation and then deleting the array later.


I'm very confused. An array is just another type, and there's no arbitrary
creation needed. There's no deleting either, of course - unused objects get
garbage collected eventually, which is true of arrays just like any type.

Are you talking about the caller? You don't like having to do
someMethod(new Term[] { term1, term2, term3 });
because you think it's a waste to new up an array?

1) Get over it, unless you have actual profiles showing it to be a problem.
object creation is cheap, including (small) array creation.

2) The jdk1.5 vararg syntax makes this feel a bit nicer, as you can declare
public void someMethod(Term foo...) { ... }
and call it as
someMethod(term1, term2, term3);
This will create an array under the covers, so there's no performance
difference, just a convenience to the programmer.

3) If you're taking multiple arrays, say of variables, exponents, and
coefficients, please consider making a class that consists of one of each,
and passing just an array of that type. It's way cleaner.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>

jupiter 02-06-2007 03:15 AM

Re: How do you use as an array as an argument
 

<Hakusa@gmail.com> wrote in message
news:1170717295.253036.43580@a34g2000cwb.googlegro ups.com...
>I have this class Term (of the algebraic kind) and I want it to
>accept
> an array of variables and an array of coefficients (in case I get
> something like 2x*2y). I have no problems with the constructor or
> anything else, but I can't figure out how to make the constructor
> accept an array as an argument without resorting to arbitrary
> array
> creation and then deleting the array later. It feels unnecessary.
>


The constructor isn't creating the arrays if it's receiving them as
parameters. Those are references to the objects (arrays) on the
heap.

There is no need to manually delete (set to null) an array because
it gets removed from the heap automatically when it goes out of
scope. But there is nothing wrong with setting an object to null.
It just isn't something to worry about. If your constructor
constructs properly then you have what you need.





Hakusa@gmail.com 02-06-2007 03:56 AM

Re: How do you use as an array as an argument
 
OK, time for a more specific example of what I'm trying to do:

I've already created classes for a variable (which includes a base
(String) and int exponent). I'm now creating a class called Term that
contains the variable and coefficient classes so I can do different
algebraic, although simple, stuff. Later I want to make an expressions
class to do even more things.

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} );


Mark Rafn 02-06-2007 06:02 AM

Re: How do you use as an array as an argument
 
Hakusa@gmail.com <Hakusa@gmail.com> wrote:
>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.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>

Rocky.Rock1@gmail.com 02-06-2007 06:46 AM

Re: How do you use as an array as an argument
 
Hi Huksana how r u

From Rocky


Hakusa@gmail.com 02-06-2007 04:15 PM

Re: How do you use as an array as an argument
 
On Feb 6, 1:02 am, d...@dagon.net (Mark Rafn) wrote:
> Hak...@gmail.com <Hak...@gmail.com> wrote:
> >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.
> --
> Mark Rafn d...@dagon.net <http://www.dagon.net/>


I'm still relatively new to Java, so I only think I understood what
you said, but there's a reason I want Tern to be a class and not
implementable interface. A variable has a base and an exponent.
Coefficients aren't involved in the same processes as so I only want
them to be part of the Term class. The Term class can accept multiple
coefficients, but automatically simplifies them to one. The big idea
is that the computer could eventually do the following piece of
algebra.

5x^2*4b (the computer multiplies them together
20bx^2

And later I could have it catch up with my Algebra class at school.
But because all of that was all one term, I want to treat it as one
object for when I get expressions in there.

I hope this has explained it enough so that you understand or can tell
me why I'm wrong. Thank you.


Oliver Wong 02-06-2007 05:05 PM

Re: How do you use as an array as an argument
 

<Hakusa@gmail.com> wrote in message
news:1170734214.472954.267340@h3g2000cwc.googlegro ups.com...
> OK, time for a more specific example of what I'm trying to do:
>
> I've already created classes for a variable (which includes a base
> (String) and int exponent). I'm now creating a class called Term that
> contains the variable and coefficient classes so I can do different
> algebraic, although simple, stuff. Later I want to make an expressions
> class to do even more things.
>
> 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} );


This is a typical exercise when learning about parsing and compiler
construction, so you might be interested in doing some reading on compiler
theory to see how others have solved the problem in the past, and whether
you might be able to reuse their ideas in your solution.

I don't have any specific recommendations (everybody seems to recommend
the so called "Dragon Book", but I haven't read it myself, having learnt
this stuff only through my professor's proprietary notes), but you can get
started at the Wikipedia article http://en.wikipedia.org/wiki/Parsing and in
particular, look at the diagram at
http://en.wikipedia.org/wiki/Image:Parsing-example.png

- Oliver



Andy Dingley 02-06-2007 05:40 PM

Re: How do you use as an array as an argument
 
On 6 Feb, 17:05, "Oliver Wong" <o...@castortech.com> wrote:

> I don't have any specific recommendations (everybody seems to recommend
> the so called "Dragon Book", but I haven't read it myself,


Anything in 2006 that really did require me to dust off my Dragon Book
would also give me The Fear. It's a great book and everyone ought to
read it sometime, but to actually _need_ it these days for any
"typical" commercial coding problem makes me suspect that something
about the intended solution is bizarrely over-complicating things.



All times are GMT. The time now is 12:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.