27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

String, text, numbers, I/O I part<br />

int n=10; /*Number of seconds to wait*/<br />

/*initialize you input stream*/<br />

...<br />

/*Now start you while loop*/<br />

long lStart = (new Date()).getTime();<br />

long lCurrent = (new Date()).getTime();<br />

while((lCurrent-lStart < n*1000) && (Input.available()==0)){<br />

Thread.currentThread.sleep(100); /* This will give JVM time to other threads */<br />

lCurrent = (new Date()).getTime();<br />

}<br />

********************************************<br />

You could simply count number of steps inside the loop keeping in mind that each<br />

step takes 100ms but I think using dates would be a bit more precise method.<br />

Alex Shlega<br />

Q: I tried to write a program where I imported the java.math.* package...But sin()<br />

didn't work!<br />

It looked something like this.<br />

import java.math.*;<br />

public class ...{<br />

public Fnx (double x){<br />

double answer = sin(x) - 2;<br />

return answer;<br />

}<br />

...<br />

}<br />

But sin() didn't work! I had to write Math.sin(), at which point I could just comment the<br />

import statement out.<br />

What am I doing wrong?<br />

Answer: You're confusing what the import statement does, and what Math is.<br />

Math is a class in the package java.lang, just as System is.<br />

java.math is a package containing two classes: BigDecimal and BigInteger.<br />

When you call Math.sin, you're using a class method in the Math *class*, just as<br />

when you call System.out.println () you're using an instance variable called "out"<br />

which belongs to the System class.<br />

Your import statement merely means you can use BigDecimal and BigInteger without<br />

prefixing them with java.math. - ie "new java.math.BigInteger()" becomes just "new<br />

BigInteger".<br />

You can't get away from putting Math.sin in your class.<br />

--<br />

Jon Skeet - http://www.pobox.com/~skeet<br />

file:///F|/350_t/350_tips/stings_text__date_numbers_io-I.htm (6 of 7) [2002-02-27 21:19:16]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!