Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Please tighten up these 10 lines of Java code

Thread view: 
metaperl.etc@gmail.com - 14 Aug 2007 04:06 GMT
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);

    }

}
Thomas Fritsch - 14 Aug 2007 04:25 GMT
> 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
[quoted text clipped - 20 lines]
>     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);

Signature

Thomas

Patricia Shanahan - 14 Aug 2007 04:42 GMT
>> 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
[quoted text clipped - 30 lines]
>     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
Zig - 14 Aug 2007 04:40 GMT
> 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
Thomas Hawtin - 14 Aug 2007 05:35 GMT
> 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
markacy - 14 Aug 2007 09:05 GMT
On 14 Sie, 05:06, "metaperl....@gmail.com" <metaperl....@gmail.com>
wrote:
> 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
[quoted text clipped - 33 lines]
>
> }

Rewrite it in Python/Jython :D

Cheers,
Marek
Roedy Green - 14 Aug 2007 09:11 GMT
On Tue, 14 Aug 2007 03:06:39 -0000, "metaperl.etc@gmail.com"
<metaperl.etc@gmail.com> wrote, quoted or indirectly quoted someone
who said :

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

You don't want Doubles. You want doubles.
See http://mindprod.com/applet/converter.html
for how to convert Strings to double and back.

see http://mindprod.com/jgloss/intvsinteger.html
to understand the difference between int and Integer, and analogously
double vs Double.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Boris Stumm - 14 Aug 2007 17:40 GMT
> 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));
}
Daniel Pitts - 14 Aug 2007 21:12 GMT
On Aug 13, 8:06 pm, "metaperl....@gmail.com" <metaperl....@gmail.com>
wrote:
> 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)));
metaperl - 15 Aug 2007 18:38 GMT
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.
Chris Smith - 15 Aug 2007 23:56 GMT
> Many thanks for all the helpful replies. I have studied this thread
> closely and implemented a few of them. In looking through Norvig's
[quoted text clipped - 4 lines]
>
> 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.

Signature

Chris Smith

Thomas Hawtin - 16 Aug 2007 05:36 GMT
> 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
Chris Smith - 16 Aug 2007 06:55 GMT
> > 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.

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,

Signature

Chris Smith

Chris Smith - 16 Aug 2007 07:00 GMT
> 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.

Signature

Chris Smith



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.