• Post Reply Bookmark Topic Watch Topic
  • New Topic

Headfirst Fullmoons exercise

 
Alberto Fontova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
What is the compilation error you are getting?
I compiled and ran the program you posted successfully.

regards,
amit
 
Alberto Fontova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!