• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Headfirst Fullmoons exercise

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic