2 Replies - 1639 Views - Last Post: 31 January 2012 - 08:05 AM Rate Topic: -----

#1 Saya_26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-November 11

Digital Clock program - problems with specifying each month's date

Posted 30 January 2012 - 12:43 PM

I have extended a digital clock program to display time with day, date, month and year in BlueJ. The program makes use of cooperating objects. I used integer values (1-12) for specifying the month (did the same for days as well). My problem is when it comes to specifying the dates of each month. I specified this in timetick() method which ticks the minute. So far, it seems to work.. But other times it doesn't. I just wanted to know if i was going in the direction with this.

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private DayDisplay days;
    private DateDisplay dates;
    private MonthDisplay months;
    private NumberDisplay years;
    private String displayString;    // simulates the actual display
    
    /**
     * Constructor for ClockDisplay objects. This constructor 
     * creates a new clock set at 00:00.
     */
    public ClockDisplay()
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        days = new DayDisplay(8);        
        months = new MonthDisplay(13);
        dates = new DateDisplay(32); 
        years = new NumberDisplay(3000);
        updateDisplay();
    }

    /**
     * Constructor for ClockDisplay objects. This constructor
     * creates a new clock set at the time specified by the 
     * parameters.
     */
    public ClockDisplay(int hour, int minute, int day, int date, int month, int year)
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        days = new DayDisplay(8);        
        months = new MonthDisplay(13);
        dates = new DateDisplay(32);
        if(dates.getValue() == 0){     //when user enters the value as 0, the program sets the value to 1 instead
            dates.setValue(1); }
        years = new NumberDisplay(3000);        
        setTime(hour, minute, day, date, month, year);
    }

    /**
     * This method should get called once every minute - it makes
     * the clock display go one minute forward.
     */
    public void timeTick()
    {
        minutes.increment();
        if(minutes.getValue() == 0) {  // it just rolled over!
            hours.increment();
            if(hours.getValue() == 0) {
                days.increment();
                dates.increment();
                if(dates.getValue() == 0) {
                    dates.setValue(1);     // To set the value to 1 after it rolls over the limit.
                    months.increment();                         
                    if(months.getValue() == 3 || months.getValue() == 5 || months.getValue() == 7 || months.getValue() == 8 || months.getValue() == 10 || months.getValue() == 12) {        // Condition statement for specifying months with 31 days in a year.
                        dates = new DateDisplay(32); 
                        if(dates.getValue() == 0){
                           dates.setValue(1);}}
                           else if(months.getValue() == 4 || months.getValue() == 6 || months.getValue() == 9 || months.getValue() == 11) {      // Condition statement for specifying months with 30 days in a year.
                              dates = new DateDisplay(31);
                              if(dates.getValue() == 0){
                                 dates.setValue(1);}}
                                 else if(months.getValue() == 2){       //Condition statement for specifying the month of February with 28 days, doesn't take leap year into account.
                                    dates = new DateDisplay(29);
                                    if(dates.getValue() == 0){
                                       dates.setValue(1);}}
                                       if((months.getValue() == 0) && (dates.getValue() == 1)) {
                                          years.increment();
                    }                        
                }
            }      
        }
        updateDisplay();
    }

    /**
     * Set the time of the display to the specified hour and
     * minute.
     */
    public void setTime(int hour, int minute, int day, int date, int month, int year)
    {
        hours.setValue(hour);
        minutes.setValue(minute);
        days.setValue(day);
        dates.setValue(date);
        months.setValue(month);
        years.setValue(year);
        updateDisplay();
    }

    /**
     * Return the current time of this display in the format HH:MM.
     */
    public String getTime()
    {
        return displayString;
    }
    
    /**
     * Update the internal string that represents the display.
     */
    private void updateDisplay()
    {
        displayString = hours.getDisplayValue() + ":" + 
                        minutes.getDisplayValue() + "    " + 
                        days.getDisplayValue() + " " + 
                        dates.getDisplayValue() + " " + 
                        months.getDisplayValue() + " " + 
                        years.getDisplayValue();
    }
}



Any suggestions or opinions on the matter will be appreciated. ^-^

Is This A Good Question/Topic? 0
  • +

Replies To: Digital Clock program - problems with specifying each month's date

#2 M4trixSh4d0w  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 07-May 10

Re: Digital Clock program - problems with specifying each month's date

Posted 30 January 2012 - 02:29 PM

just wanted to know..
            hours = new NumberDisplay(24);  //Why is this 24, which is correct..

034         minutes = new NumberDisplay(60);  

035         days = new DayDisplay(8);        //But this is 8, which is 1 more than in a week  

036         months = new MonthDisplay(13);   //this too

037         dates = new DateDisplay(32);  //maybe this also



and down at line 64 it's 1 more than what you put in the comment
and with line 60.
Was This Post Helpful? 0
  • +
  • -

#3 Saya_26  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-November 11

Re: Digital Clock program - problems with specifying each month's date

Posted 31 January 2012 - 08:05 AM

Yes that's because, it takes values from 0 and not from 1. So if i would have specified 31, it will only display values from 0 - 30. ^-^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1