Please tighten up these 10 lines of Java code

M

metaperl.etc

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.

I dont like calling print() one line and then println() on the next.
Something like printf would be nice.

Any other suggestions welcome.


package redick;

import redick.analysis.*;
import redick.node.*;

class Translation extends DepthFirstAdapter {

public void outASexpExpr(ASexpExpr node) {

System.out.print("arg1 ");
System.out.println(node.getArg1());

System.out.print("arg2 ");
System.out.println(node.getArg2());

System.out.print("dyad ");
System.out.println(node.getDyad());

Double arg1 = Double.parseDouble(node.getArg1().toString());
Double arg2 = Double.parseDouble(node.getArg2().toString());
Double sum = arg1 + arg2 ;

System.out.print("Their sum: ");
System.out.println(sum);

}

}
 
T

Thomas Fritsch

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.

I dont like calling print() one line and then println() on the next.
Something like printf would be nice.

Any other suggestions welcome.

System.out.print("arg1 ");
System.out.println(node.getArg1());

System.out.print("arg2 ");
System.out.println(node.getArg2());

System.out.print("dyad ");
System.out.println(node.getDyad());

Double arg1 = Double.parseDouble(node.getArg1().toString());
Double arg2 = Double.parseDouble(node.getArg2().toString());
Double sum = arg1 + arg2 ;

System.out.print("Their sum: ");
System.out.println(sum);
Why not use the + operator for strings/objects ?
(and BTW: the Double-handling can be shortened a bit, too)
System.out.println("arg1 " + node.getArg1());
System.out.println("arg2 " + node.getArg2());
System.out.println("dyad " + node.getDyad());
double arg1 = Double.parseDouble(node.getArg1());
double arg2 = Double.parseDouble(node.getArg2());
double sum = arg1 + arg2 ;
System.out.print("Their sum: " + sum);
 
Z

Zig

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.

well, you can make the compiler work a little harder by adding the import

import static java.lang.System.out;

(Java 1.5+ required)

Then you can rewrite
System.out.print
as just
out.print

I dont like calling print() one line and then println() on the next.
Something like printf would be nice.

As another commenter posted, you could just use
System.out.println("arg1 "+node.getArg1());

However, it might be a little cleaner for this case to and create another
method
(ignoring the convention in the part of this response)

private static void out(String prefix, Object arg) {
System.out.print(prefix);
System.out.print(' ');
System.out.println(arg);
}

So, you end up with
public void outASexpExpr(ASexpExpr node) {
out("arg1", node.getArg1());
out("arg2", node.getArg2());
out("dyad", node.getDyad());

double arg1 = Double.parseDouble(node.getArg1().toString());
double arg2 = Double.parseDouble(node.getArg2().toString());
double sum = arg1 + arg2 ;

out("Their sum:", Double.toString(sum));
}


HTH,

-Zig
 
P

Patricia Shanahan

Thomas said:
Why not use the + operator for strings/objects ?
(and BTW: the Double-handling can be shortened a bit, too)
System.out.println("arg1 " + node.getArg1());
System.out.println("arg2 " + node.getArg2());
System.out.println("dyad " + node.getDyad());
double arg1 = Double.parseDouble(node.getArg1());
double arg2 = Double.parseDouble(node.getArg2());
double sum = arg1 + arg2 ;
System.out.print("Their sum: " + sum);

That is how I would code it.

However, PrintStream does now have printf, and "import static
java.lang.System.out" would allow reference to "out" without qualification.

Patricia
 
T

Thomas Hawtin

Zig said:
well, you can make the compiler work a little harder by adding the import

import static java.lang.System.out;

Even before 1.5 you could write:

private static final java.io.PrintStream out = System.out;

Tom Hawtin
 
M

markacy

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.

I dont like calling print() one line and then println() on the next.
Something like printf would be nice.

Any other suggestions welcome.

package redick;

import redick.analysis.*;
import redick.node.*;

class Translation extends DepthFirstAdapter {

public void outASexpExpr(ASexpExpr node) {

System.out.print("arg1 ");
System.out.println(node.getArg1());

System.out.print("arg2 ");
System.out.println(node.getArg2());

System.out.print("dyad ");
System.out.println(node.getDyad());

Double arg1 = Double.parseDouble(node.getArg1().toString());
Double arg2 = Double.parseDouble(node.getArg2().toString());
Double sum = arg1 + arg2 ;

System.out.print("Their sum: ");
System.out.println(sum);

}

}

Rewrite it in Python/Jython :D

Cheers,
Marek
 
B

Boris Stumm

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.

I dont like calling print() one line and then println() on the next.
Something like printf would be nice.

With Java 1.5:

class Translation extends DepthFirstAdapter {

private void printf(String f, Object... o) {
System.out.format(f, o);
}

public void outASexpExpr(ASexpExpr node) {
printf("arg1 %s", node.getArg1());
printf("arg2 %s", node.getArg2());
printf("dyad %s", node.getDyad());
double arg1 = Double.parseDouble(node.getArg1().toString());
double arg2 = Double.parseDouble(node.getArg2().toString());
printf("Their sum: %f", (arg1 + arg2));
}
 
D

Daniel Pitts

Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.
Actually, You can't and shouldn't extend System.out.
You can't extend it because its not a class, but a reference to a
static object.
You shouldn't extend it, because extending something just to type
less defeats the purpose of inheritance and polymorphism.
I dont like calling print() one line and then println() on the next.
Something like printf would be nice.
You can build the string before hand, or as people have suggested, you
can use string concatenation.
Any other suggestions welcome.
[snip]

Suggestions: Make ASexpExpr have an array (or collection) of Args,
rather than arg1 and arg2. If you find yourself adding a number to
the end of a variable, then it is most likely better to have it as an
array or collection.

After that, you can have getArg(int whichArg) and others:

public double getDoubleArg(int whichArg) {
return Double.parseDouble(getStringArg(whichArg));
}
public String getStringArg(int whichArg) {
return String.valueOf(getArg(whichArg));
}



So your sum becomes:
System.out.println("Their sum: " + (node.getDoubleArg(0) +
node.getDoubleArg(1)));
 
M

metaperl

Many thanks for all the helpful replies. I have studied this thread
closely and implemented a few of them. In looking through Norvig's
JScheme (an old 1.4 version), I noticed he abbreviated writing to
output this way:

PrintWriter output = new PrintWriter(System.out, true);

Then he could simply do output.print() or whatever.
 
C

Chris Smith

metaperl said:
Many thanks for all the helpful replies. I have studied this thread
closely and implemented a few of them. In looking through Norvig's
JScheme (an old 1.4 version), I noticed he abbreviated writing to
output this way:

PrintWriter output = new PrintWriter(System.out, true);

Then he could simply do output.print() or whatever.

If the goal is to avoid typing, why wrap another layer around the
stream?

PrintStream output = System.out;

Yes, PrintStream is deprecated; but it's not like you actually avoid it
by wrapping it in something else. You just pretend like you avoid it,
and I'm not a fan of pretending.
 
T

Thomas Hawtin

Chris said:
Yes, PrintStream is deprecated; but it's not like you actually avoid it
by wrapping it in something else. You just pretend like you avoid it,
and I'm not a fan of pretending.

Perhaps PrintStream should be deprecated, but it isn't. I believe the
argument is that because so much uses (like everything that uses
System.out), it's not really practical to tell people that they should
modify all their code not to use it.

Tom Hawtin
 
C

Chris Smith

Thomas Hawtin said:
Perhaps PrintStream should be deprecated, but it isn't.

Oops. I remembered so clearly that it was, that I didn't even bother to
check before posting. That must have been a particularly vivid dream of
mine!

Thanks,
 
C

Chris Smith

Thomas Hawtin said:
Perhaps PrintStream should be deprecated, but it isn't.

Ah, okay! So apparently the constructors (not the class) for
PrintStream were deprecated in Java 1.1 when the Reader/Writer system
was first introduced. In Java 1.2, they were undeprecated, and remain
so to this day.

Deprecating the contructors seems more reasonable than deprecating the
class, but I suppose it makes it impossible to use System.setErr or
System.setOut in nearly any useful way without triggering deprecation
warnings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top