Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > alias

Reply
Thread Tools

alias

 
 
NickName
Guest
Posts: n/a
 
      12-19-2006

Hendrik Maryns wrote:

>> [ ... ]
> >
> > What method to display date/time like mm/dd/yyyy or mm--dd--yyyy?
> > Sorry I did not go the trouble of digging it via language reference doc

>
> Have a look at Formatter:
> http://java.sun.com/j2se/1.5.0/docs/...Formatter.html
>
> There are also convenience methods in the outputstreams:
> System.out.format(...)
>
> H.
> --
> Hendrik Maryns
> http://tcl.sfs.uni-tuebingen.de/~hendrik/
> ==================
> http://aouw.org
> Ask smart questions, get good answers:
> http://www.catb.org/~esr/faqs/smart-questions.html


Thanks, got it.

 
Reply With Quote
 
 
 
 
NickName
Guest
Posts: n/a
 
      12-19-2006

Ian Wilson wrote:
> NickName wrote:


[ ...]

> 1) Separate the message from the IO function.
>
> String m;
> for (int i=0; i < 5; i++) {
> switch(i) {
> case 0:
> case 2:
> case 4: m = " is an even number"; break;
> case 1:
> case 3:
> case 5: m = " is an odd number"; break;
> default: m = " is neither an odd nor even number";
> }
> System.out.println(i+m)
> }


Beautiful and glad to learn about the "String m" usage, thanks.

>
> 2) Use a less clumsy test and fix the out-by-one error.
>
> System.out.println("whatever you want to say about 0");
> for (int i=1; i < 6; i++) {
> if (i%2 == 0) {
> System.out.println(i+" is even");
> } else {
> System.out.println(i+" is odd");
> }
> }


Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?
Similar ones? TIA.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      12-20-2006
NickName wrote:
> Daniel Pitts wrote:
> > NickName wrote:
> > OP [...]

>
> > import static java.lang.System.*;
> > public class OddsAndEvens {
> > public static final int MAX_COUNT = 5;
> > public static boolean isEven(int number) {
> > return (number & 1) == 0;
> > }
> > public static void main(String[] args) {
> > for (int i = 0; i < MAX_COUNT; ++i) {
> > out.println(i + " is an " + (isEven(i) ? "even" : "odd") + "
> > number");
> > }
> > }
> > }
> > Probably the shortest way to write this and still have it readable.

>
> Very nice and thanks for introducing the System package here. More
> questions,
> For the LINE of
> public static final int MAX_COUNT = 5;
> why not simply int MAX_COUNT = 5;
> ? // since it's it's already at the top level of the OddsAndEvens
> class.


int MAX_COUNT = 5; woudl create a new integer for every object create
in OddsAndEvens. In this particular case, that doesn't matter much,
but if you have a constant value that is the same accross 10000
objects, it can start to add up.

the "static" keyword tells the compiler that the memory and value is
associated with the class, not individual instances of the class. The
"final" keyword tells the compiler to not let anyone accidently change
the value of this constant. It also allows the compiler to optimize.

>
> Please elaborate on the "(number & 1) == 0", though the & symbol is
> supposed to mean something like Evaluation AND (binary). TIA.
>


In binary, if a number is a multiple of two, then its lowest
signifigant bit is 0, otherwise the bit is one.
We can use that knowledge to help us determine the "evenness" of a
number. Since an even number is a number which contains two as a
factor, we can test the lowest bit to tell us where a number is odd or
even.
1 is the bitmask for the lowest bit. n & 1 will return the value of
the lowest bit.
for example:
n | BIN |n&1|
0 | 0000 | 0 | even
1 | 0001 | 1 | odd
2 | 0010 | 0 | even
3 | 0011 | 1 | odd
4 | 0100 | 0 | even

Hope this helps.
- Daniel.

 
Reply With Quote
 
Ian Wilson
Guest
Posts: n/a
 
      12-20-2006
NickName wrote:
> Ian Wilson wrote:
>
>> NickName wrote:
>>
>> 2) Use a less clumsy test and fix the out-by-one error.
>>
>> System.out.println("whatever you want to say about 0");
>> for (int i=1; i < 6; i++) {


Most people[1] regard 0 as even. If you are one of them, you could
replace the above two lines with:
for (int i=0; i < 6; i++) {

>> if (i%2 == 0) {
>> System.out.println(i+" is even");
>> } else {
>> System.out.println(i+" is odd");
>> }
>> }

>
>
> Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?


Yes.

The value of `a%b` is the remainder after integer division of a by b. I
recall Patricia Shanahan saying that the Java remainder operator (`%`)
is almost the same as the usual modulo operator but there is some subtle
distinction which I forget. (P.S. my recollection may be inaccurate, it
often is

http://en.wikipedia.org/wiki/Modulo_operation

> Similar ones?


I'm too new to Java to have a list of "interesting" operators handy
I find O'Reilly's "Learning Java" helpful.

Just reading this newsgroup regularly is an excellent way of discovering
better ways to do things in Java.


[1] Roulette table operators don't.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-20-2006
Ian Wilson wrote:
....
> The value of `a%b` is the remainder after integer division of a by b. I
> recall Patricia Shanahan saying that the Java remainder operator (`%`)
> is almost the same as the usual modulo operator but there is some subtle
> distinction which I forget. (P.S. my recollection may be inaccurate, it
> often is

....

The difference is in the extension to negative numbers.

One of the examples in the JLS is

(-5)%3 produces -2

This is correct for remainder, because it maintains consistency with
division. I would expect (N mod 3) to be represented by an integer in
the range 0 through 2 for any integer N.

Patricia
 
Reply With Quote
 
NickName
Guest
Posts: n/a
 
      12-20-2006

Daniel Pitts wrote:
> NickName wrote:
> > Daniel Pitts wrote:
> > > NickName wrote:
> > > OP [...]
> > > }

> >
> > Very nice and thanks for introducing the System package here. More
> > questions,
> > For the LINE of
> > public static final int MAX_COUNT = 5;
> > why not simply int MAX_COUNT = 5;
> > ? // since it's it's already at the top level of the OddsAndEvens
> > class.

>
> int MAX_COUNT = 5; woudl create a new integer for every object create
> in OddsAndEvens. In this particular case, that doesn't matter much,
> but if you have a constant value that is the same accross 10000
> objects, it can start to add up.
>
> the "static" keyword tells the compiler that the memory and value is
> associated with the class, not individual instances of the class. The
> "final" keyword tells the compiler to not let anyone accidently change
> the value of this constant. It also allows the compiler to optimize.
>
> >
> > Please elaborate on the "(number & 1) == 0", though the & symbol is
> > supposed to mean something like Evaluation AND (binary). TIA.
> >

>
> In binary, if a number is a multiple of two, then its lowest
> signifigant bit is 0, otherwise the bit is one.
> We can use that knowledge to help us determine the "evenness" of a
> number. Since an even number is a number which contains two as a
> factor, we can test the lowest bit to tell us where a number is odd or
> even.
> 1 is the bitmask for the lowest bit. n & 1 will return the value of
> the lowest bit.
> for example:
> n | BIN |n&1|
> 0 | 0000 | 0 | even
> 1 | 0001 | 1 | odd
> 2 | 0010 | 0 | even
> 3 | 0011 | 1 | odd
> 4 | 0100 | 0 | even
>
> Hope this helps.
> - Daniel.


Your explanation is perfect. Thank you.

 
Reply With Quote
 
NickName
Guest
Posts: n/a
 
      12-20-2006

Ian Wilson wrote:
> NickName wrote:
> > Ian Wilson wrote:
> >
> >> NickName wrote:
> >>
> >> 2) Use a less clumsy test and fix the out-by-one error.
> >>
> >> System.out.println("whatever you want to say about 0");
> >> for (int i=1; i < 6; i++) {

>
> Most people[1] regard 0 as even. If you are one of them, you could
> replace the above two lines with:
> for (int i=0; i < 6; i++) {
>
> >> if (i%2 == 0) {
> >> System.out.println(i+" is even");
> >> } else {
> >> System.out.println(i+" is odd");
> >> }
> >> }

> >
> >
> > Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?

>
> Yes.
>
> The value of `a%b` is the remainder after integer division of a by b. I
> recall Patricia Shanahan saying that the Java remainder operator (`%`)
> is almost the same as the usual modulo operator but there is some subtle
> distinction which I forget. (P.S. my recollection may be inaccurate, it
> often is
>
> http://en.wikipedia.org/wiki/Modulo_operation
>
> > Similar ones?

>
> I'm too new to Java to have a list of "interesting" operators handy
> I find O'Reilly's "Learning Java" helpful.
>
> Just reading this newsgroup regularly is an excellent way of discovering
> better ways to do things in Java.
>
>
> [1] Roulette table operators don't.


Great. Thank you.

 
Reply With Quote
 
NickName
Guest
Posts: n/a
 
      12-20-2006

Patricia Shanahan wrote:
> Ian Wilson wrote:
> ...
> > The value of `a%b` is the remainder after integer division of a by b. I
> > recall Patricia Shanahan saying that the Java remainder operator (`%`)
> > is almost the same as the usual modulo operator but there is some subtle
> > distinction which I forget. (P.S. my recollection may be inaccurate, it
> > often is

> ...
>
> The difference is in the extension to negative numbers.
>
> One of the examples in the JLS is
>
> (-5)%3 produces -2
>
> This is correct for remainder, because it maintains consistency with
> division. I would expect (N mod 3) to be represented by an integer in
> the range 0 through 2 for any integer N.
>
> Patricia


Thank you.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference/alias in perl vs reference/alias in C++ grocery_stocker C++ 9 05-24-2008 04:32 AM
XP X64 Pro, IE7, Delphi 2007, IntraWeb, MS Access, ODBC Alias problem: Insufficient memory for this operation. Alias: SomeDatabase. Skybuck Flying Windows 64bit 13 01-09-2008 07:34 PM
Is it possible to define an alias of a type? TigerJade VHDL 4 08-05-2005 08:25 AM
what's incorrect ALIAS =?ISO-8859-15?Q?Stefan_Schw=E4rzler?= VHDL 1 08-04-2005 01:55 PM
[vhdl] how to wire two signals together? alias not adequate Khashishi VHDL 3 09-22-2004 10:36 PM



Advertisments