#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    NYC
    Posts
    11
    Rep Power
    0

    help with class Time


    Trying to do the following problem but keep hitting roadblocks...

    The following class is a Time object.

    class Time {
    private int hour;
    private int minute;
    private int second;

    public Time( ) { // set hour, minute and second to zero }
    public Time(int hr, int min, int sec) { // set hour, minute and second }
    public void setTime(int hr , int min, int sec) { // set hour, minute and second }
    public void printTime( ) { //prints the time in the form of 0:0:0 }
    public void tick( ) { // increments the time by one second }
    };

    Implement (code) all the constructor and methods. Assume that a time of 00:00:00 is
    represented by 0:0:0 -- (use 0 to represent 00). Test the Time object. [HINT:] Use
    a for loop to test the tick( ) method.

    Here's what I have so far, but I'm just hitting a wall. Any help is greatly appreciated.


    public class Time {

    private int hour;
    private int minute;
    private int second;

    public Time(){
    int hour=0;
    int minute=0;
    int second=0;
    }

    public Time (int hr, int min, int sec){

    }

    public void setTime(int hr, int min, int sec){
    hour=hr;
    minute=min;
    second=sec;

    }

    public void printTime() {
    System.out.println("The time is " + hour +":"+ minute +":"+second);
    }

    public void tick(){
    second++;


    }

    public static void main(String [] args){


    Time t1 = new Time();
    t1.setTime(10,30,15);
    for(int i=0; i < 60; i++){
    t1.tick();
    t1.printTime();
    }




    }
    }
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    I'll give you a few hints. UNIX computer systems store the date as a "timestamp", which means they store the number of seconds that have elapsed since January 1, 1970 (I think). Whenever you need the current time, they just do some math with the timestamp and the starting date to figure out the current date and time. For instance: "Tue Jul 1 17:43:08 PDT 2003" is equals to
    "1057106588"

    Second hint, there are 86400 seconds in a single day (24 hours * 60 minutes * 60 seconds).

IMN logo majestic logo threadwatch logo seochat tools logo