my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Headfirst Fullmoons exercise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Headfirst Fullmoons exercise" Watch "Headfirst Fullmoons exercise" New topic
Author

Headfirst Fullmoons exercise

Alberto Fontova
Greenhorn

Joined: Oct 26, 2007
Posts: 2
Hi, I've been working through the headfirst book and really liking it & finding Java to be very cool!!

I think i missed something fundamental though about static variable.

I solved this exercise & got the program to compile & run fine.

Code-

import java.util.*;

import static java.lang.System.out;

class FullMoons {

static int DAY_IM = 1000*60*60*24;

public static void main (String [] args) {
Calendar c= Calendar.getInstance();

c.set(2004,0,7,15,40);

long day1 = c.getTimeInMillis();

for (int x=0; x<60;x++) {
day1+= (DAY_IM *29.52);

c.setTimeInMillis(day1);

out.println(String.format("full moon on %tc",c));
}

}
}

Then i started messing around and tried to create a class I could call in main along with some other stuff just for fun.

code-

class FullMoons {

static int DAY_IM = 1000*60*60*24;

public static void main (String [] args) {
Calendar c= Calendar.getInstance();

c.set(2004,0,7,15,40);

long day1 = c.getTimeInMillis();

for (int x=0; x<60;x++) {
day1+= (DAY_IM *29.52);

c.setTimeInMillis(day1);

out.println(String.format("full moon on %tc",c));
}

}
}

This won't compile. When I take DAY_IM out it will compile. I think it has something to do with DAY_IM being static but I don't know the why.

Thanks for the help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

"day1+= (DAY_IM *29.52);"

The result of (DAY_IM * 29.52) is a double, not a long. If you cast this back to a long it should work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 515
Hello,
What is the compilation error you are getting?
I compiled and ran the program you posted successfully.

regards,
amit


Regards,
Amit
Alberto Fontova
Greenhorn

Joined: Oct 26, 2007
Posts: 2
Can't believe I missed that! Thanks for the help.

Amit, message from the compiler was something like "...expected". It's working now. Thanks for the reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Headfirst Fullmoons exercise