By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Passing "<", ">", "=", etc as an argument to a method?

P: n/a
Hello,

I am just learning Java and am trying to write a method that does
something like

//===========================================
public Static List find(double[] array,double val,String relationalOp)
{

List list = new ArrayList();
for (int m = 0;m < array.length;m++) {
// The following line is the problem
if (array[m] relationalOp val) {
list.add(array[m]);
}
}
return list;
}
//===========================================

Is it possible to use an argument directly in an if statement like
that?

I am trying to avoid having to rewrite the same method over and over
again, once for each relational operator "==", "<", ">", "<=", ">=",
not to mention all of their "not" versions.

What is the best way to do this?

Thank you for any help.

Eric
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Eric A. Forgy" <fo***@uiuc.edu> wrote in message
news:3f**************************@posting.google.c om...
Hello,

I am just learning Java and am trying to write a method
that does something like

//===========================================
public Static List find(double[] array,double val,String relationalOp)
{

List list = new ArrayList();
for (int m = 0;m < array.length;m++) {
// The following line is the problem
if (array[m] relationalOp val) {
list.add(array[m]);
}
}
return list;
}
//===========================================

Is it possible to use an argument directly in an if statement
like that?

No, it is not possible in languages like Java, C++, or C#, but it *is*
possible in some scripting languages.

I am trying to avoid having to rewrite the same method
over and over again, once for each relational
operator "==", "<", ">", "<=", ">=", not to mention all
of their "not" versions.

What is the best way to do this?


You need to map each operator string to a method that performs the required
task, and to which you also pass the required arguments [or, in this case,
operands]. You can design a solution varying in complexity and
sophistication; probably the very simplest is something like:

if (relationalOp.equals("<"))
doLessThan(value1, value2)
else if (relationalOp.equals("="))
doEquals(value1, value2)
...

You get the picture, I'm sure.

I hope this helps.

Anthony Borla

Jul 17 '05 #2

P: n/a
While it was 28/11/03 1:37 am throughout the UK, Anthony Borla sprinkled
little black dots on a white screen, and they fell thus:

<snip>
if (relationalOp.equals("<"))
doLessThan(value1, value2)
else if (relationalOp.equals("="))
doEquals(value1, value2)
...


Why not make relationalOp a number, and save the overhead of doing
string comparison?

Define

final static int EQ = 1;
final static int GT = 2;
final static int GTEQ = 3;
final static int LT = 4;
final static int LTEQ = 5;
final static int NOTEQ = 6;

and pass these values in.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #3

P: n/a

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bq**********@sun-cc204.lut.ac.uk...
While it was 28/11/03 1:37 am throughout the UK, Anthony
Borla sprinkled little black dots on a white screen, and they
fell thus:

<snip>
if (relationalOp.equals("<"))
doLessThan(value1, value2)
else if (relationalOp.equals("="))
doEquals(value1, value2)
...


Why not make relationalOp a number, and save the overhead
of doing string comparison?

Define

final static int EQ = 1;
final static int GT = 2;
final static int GTEQ = 3;
final static int LT = 4;
final static int LTEQ = 5;
final static int NOTEQ = 6;

and pass these values in.


Indeed, I fully agree ! However, my response was tailored to the OP's code:
'relationalOp' was a 'String' type.

I *did*, however, mention that:

'You can design a solution varying in complexity and
sophistication...'

and your solution is, most certainly, in this latter group :) !

Cheers,

Anthony Borla
Jul 17 '05 #4

P: n/a
Stewart Gordon wrote:
While it was 28/11/03 1:37 am throughout the UK, Anthony Borla sprinkled
little black dots on a white screen, and they fell thus:

<snip>
if (relationalOp.equals("<"))
doLessThan(value1, value2)
else if (relationalOp.equals("="))
doEquals(value1, value2)
...

Why not make relationalOp a number, and save the overhead of doing
string comparison?

Define

final static int EQ = 1;
final static int GT = 2;
final static int GTEQ = 3;
final static int LT = 4;
final static int LTEQ = 5;
final static int NOTEQ = 6;

and pass these values in.


Even better: use polymorphism. E.g.

// WARNING: Uncompiled, untested code
public class Helper
{
public static final Finder EQUALS = new EqualsFinder();
public static final Finder GREATER_THAN = new GreaterThanFinder();
// etc.

public static List find(double[] array, double val, Finder finder)
{
List result = new ArrayList();
for (int i = 0; i < array.length; i++)
{
if (finder.satisfies(array[i], val))
{
result.add(new Double(array[i]));
}
}
}

public static interface Finder
{
public boolean satisfies(double candidate, double val);
}

public static class EqualsFinder
{
public boolean satisfies(double candidate, double val)
{
return (candidate == value);
}
}

public static class GreaterThanFinder
{
public boolean satisfies(double candidate, double val)
{
return (candidate > value);
}
}

}

Ray

Jul 17 '05 #5

P: n/a

Hi Eric,
I am just learning Java and am trying to write a method that does
something like

//===========================================
public Static List find(double[] array,double val,String relationalOp)


It's a good example method. Unfortunately, Java is not very good at
handling such cases, because it does not treat methods (or operations)
as normal values. You cannot directly pass a method as an argument to
another method.

The closest you could get would be to use anonymous classes as a
substitute, as in the following (beware, very verbose)!

import java.util.*;

public class Main {

abstract static class DoubleOp
{
abstract boolean test(double x, double y);
}

public static List find(double[] array,double val, DoubleOp op){
List list = new ArrayList();
for (int m = 0;m < array.length;m++) {
// The following line is the problem
if (op.test(array[m], val)) {
list.add(new Double(array[m]));
}
}
return list;
}

public static void main(String[] args) {
double[] numbers = new double[]{ -10, -1, 0, 2, 5, 11 };

DoubleOp leq = new DoubleOp() {
boolean test(double x, double y) {
return x < y;
}
};
List negative = find(numbers, 0, leq);
System.out.println("Negative numbers: " + negative);

DoubleOp neq = new DoubleOp() {
boolean test(double x, double y) {
return x != y;
}
};
List nonZero = find(numbers, 0, neq);
System.out.println("Non-zero numbers: " + nonZero);
}
}
It is much easier if you can use a language that support methods as
values. For instance, here is the same code in the Nice language, which
is an extension of Java with many features, including method values (I
am one of the developers of Nice. You can find more about it at
http://nice.sf.net)

List<double> find(double[] array,double val,(double,double)->boolean op)
{
List<double> list = new ArrayList();
for (int m = 0;m < array.length;m++) {
// The following line was the problem
if (op(array[m], val)) {
list.add(array[m]);
}
}
return list;
}

void main(String[] args)
{
double[] numbers = [ -10, -1, 0, 2, 5, 11 ];

let negative = find(numbers, 0, (double x,double y)=> x < y);
System.out.println("Negative numbers: " negative);

let nonZero = find(numbers, 0, (double x,double y)=> x != y);
System.out.println("Non-zero numbers: " nonZero);
}
Some comments:

(double,double)->boolean
This is the type of a method, which has two arguments of type
double, and returns a boolean.

let negative =
This is a way to declare a local variable, letting the compiler
find what is its type.

(double x,double y)=> x < y
This is an expression that represents the '<' comparison on doubles.
One way to generalize your method find is to give it only two arguments:
the array of number, and a method with _one_ argument of type double,
returning a boolean to say if that number should be included in the
results. This generalization is what we call 'filter'. So this code
could be simply rewritten as:

void main(String[] args)
{
double[] numbers = [ -10, -1, 0, 2, 5, 11 ];

let negative = filter(numbers, (double x)=> x < 0);
System.out.println("Negative numbers: " negative);

let nonZero = filter(numbers, (double x)=> x != 0);
System.out.println("Non-zero numbers: " nonZero);
}
Enjoy!

Daniel

The Nice programming language: http://nice.sf.net

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.