This one I am Proud: But adding AM PM to clock!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    This one I am Proud: But adding AM PM to clock!

    All it is, is a simple program that asks to enter hour and minute and makes the clock tick, after ticking, when clock is viewed, it should read a minute ahead. Very proud of the results. The only issue is getting PM to fire when time ticks beyond 24 hours. Just a matter of time, but do tell me what you see happening, I am sure it is pretty simple...

    [CODE=JAVA]

    /**
    * @author Michael Kölling and David J. Barnes
    * @version 2007.10.03
    * @professor The Nice One
    * @programmer Just Me
    * @Lab 5
    */


    public class ClockDisplay
    {
    private NumberDisplay hours;
    private NumberDisplay minutes;
    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(1 2);
    minutes = new NumberDisplay(5 9);
    updateDisplay() ;
    }

    /**
    * Constructor for ClockDisplay objects. This constructor
    * creates a new clock set at the time specified by the
    * parameters.
    */
    public ClockDisplay(in t hour, int minute)
    {
    hours = new NumberDisplay(1 2);
    minutes = new NumberDisplay(5 9);
    setTime(hour, minute);
    }

    /**
    * This method should get called once every minute - it makes
    * the clock display go one minute forward.
    */
    public void timeTick()
    {
    minutes.increme nt();
    if(minutes.getV alue() == 0) { // it just rolled over!
    hours.increment ();
    }
    updateDisplay() ;
    }

    /**
    * Set the time of the display to the specified hour and
    * minute.
    */
    public void setTime(int hour, int minute)
    {
    hours.setValue( hour);
    minutes.setValu e(minute);
    updateDisplay() ;
    }

    /**
    * Return the current time...
    */
    public String getTime()
    {
    return displayString;
    }

    /**
    * Update the internal string that represents the display.
    */
    private String updateDisplay()
    {
    if(hours.getVal ue() < 12){

    return displayString = hours.getValue( ) + ": " +
    minutes.getValu e() + " AM";

    }

    else {

    return displayString = hours.getValue( ) + ": " +
    minutes.getValu e() + " PM";
    }
    }

    }

    [/CODE]

    Thanks for reading!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Why not make the hours a NumberDisplay(2 4)? A value < 12 indicates AM,
    the other values indicate PM. When the value of the display > 12 subtract 12
    from the value before displaying it.

    kind regards,

    Jos

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Originally posted by JosAH
      Why not make the hours a NumberDisplay(2 4)? A value < 12 indicates AM,
      the other values indicate PM. When the value of the display > 12 subtract 12
      from the value before displaying it.

      kind regards,

      Jos
      I'll try it JosAH, this might work. I was also coming over because I forgot to add additional code, specific to the NumberDisplay class, sorry about that:

      [CODE=JAVA]

      /**
      * @author Michael Kölling and David J. Barnes
      * @version 2007.10.03
      * @professor Still Nice One
      * @programmer Only Me
      * Lab 5
      */
      public class NumberDisplay
      {
      private int limit;
      private int value;

      /**
      * Constructor for objects of class NumberDisplay.
      * Set the limit at which the display rolls over.
      */
      public NumberDisplay(i nt rollOverLimit)
      {
      limit = rollOverLimit;
      value = 0;
      }

      /**
      * Return the current value.
      */
      public int getValue()
      {
      return value;
      }

      /**
      * Return the display value (that is, the current value as a two-digit
      * String. If the value is less than ten, it will be padded with a leading
      * zero).
      */
      public String getDisplayValue ()
      {
      if(value < 10) {
      return "0" + value;
      }
      else {
      return "" + value;
      }
      }

      /**
      * Set the value of the display to the new specified value. If the new
      * value is less than zero or over the limit, do nothing.
      */
      public void setValue(int replacementValu e)
      {
      if((replacement Value >= 0) && (replacementVal ue < limit)) {
      value = replacementValu e;
      }
      }

      /**
      * Increment the display value by one, rolling over to zero if the
      * limit is reached.
      */
      public void increment()
      {
      value = (value + 1) % limit;
      }
      }
      [/CODE]

      Will give it another whirl like I said. Wish me luck for Lab 6, looks easier, will post after submitting, only if I need to tweak it a bit, I may not like what it is doing:-)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Dököll
        I'll try it JosAH, this might work. I was also coming over because I forgot to add additional code, specific to the NumberDisplay class, sorry about that
        No need to apologize; I'd put the AM/PM logic in a class derived from your
        NumberDisplay class; that'd keep your main/driver logic clean.

        kind regards,

        Jos

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          #5
          Originally posted by JosAH
          No need to apologize; I'd put the AM/PM logic in a class derived from your
          NumberDisplay class; that'd keep your main/driver logic clean.

          kind regards,

          Jos
          Thanks!

          funny thing, I actually did start off by copying a chunk of code from the NumberDisplay class and made up Strings using the ClockDisplay class objects to attempt a completed task. I guess when AM popped up I was overjoyed and ignored minor details...

          I think I see what you're leading at:-)

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Dököll
            Thanks!

            funny thing, I actually did start off by copying a chunk of code from the NumberDisplay class and made up Strings using the ClockDisplay class objects to attempt a completed task. I guess when AM popped up I was overjoyed and ignored minor details...

            I think I see what you're leading at:-)
            What you wrote above indicates an enthousiasm for actual coding instead of
            designing the darn thing first. Never, ever touch that keyboard before you've
            finished your design. A design doesn't imply all sorts of fancy UML pictures,
            i.e. a few pieces of scribbling paper do fine too as long as you have thought
            over all corners of your program to be so that you won't face surprises when
            you finally do have to type in all that silly source code.

            kind regards,

            Jos

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              #7
              Originally posted by JosAH
              What you wrote above indicates an enthousiasm for actual coding instead of
              designing the darn thing first. Never, ever touch that keyboard before you've
              finished your design. A design doesn't imply all sorts of fancy UML pictures,
              i.e. a few pieces of scribbling paper do fine too as long as you have thought
              over all corners of your program to be so that you won't face surprises when
              you finally do have to type in all that silly source code.

              kind regards,

              Jos
              The very idea, JosAH...Design first is key...I am going with my guts thus far, and it looks like I am winning. You're very kind to amicably add your two cents in, I appreciate it...keep'em comin';-)

              And about silly coding! Lab6 is in the bag, the first time I ever studied each piece before looking at what to modify...just submitted it. The only one thus far with no issues. Perhaps somehow you were talking "sense" into me before I logged on to report job, seemingly, well-done (Prof will be the Judge but...)...

              Have a look:

              [CODE=JAVA]

              import java.util.Array List;

              /**
              * A class to maintain an arbitrarily long list of notes.
              * Notes are numbered for external reference by user.
              * In this version, note numbers start at 1.
              *
              * @author David J. Barnes and Michael Kölling.
              * @version 2007.10.15
              * @programmer Me
              * @professor Nice
              * @Lab 6
              */
              public class Notebook
              {
              // Storage for an arbitrary number of notes.
              private ArrayList<Strin g> notes;

              /**
              * Perform any initialization that is required for the
              * notebook.
              */
              public Notebook()
              {
              notes = new ArrayList<Strin g>();
              }

              /**
              * Store a new note into the notebook.
              * @param note The note to be stored.
              */
              public void storeNote(Strin g note)
              {
              notes.add(note) ;
              }

              /**
              * @return The number of notes currently in the notebook.
              */
              public int numberOfNotes()
              {
              return notes.size();
              }


              /**
              * Decided to use the search mechanism here to do the searching and printing for us:-)
              * Judging by the index information I thought it fiting to use the index number to figure out
              * how to make a comparison. Hence, comparing the index number against whatever is found=true
              */



              /**
              * Modified the index portion to reflect 1 as starting point.
              * Modified removeNote, showNote as well to reflect 1 as starting point...
              */


              public void listNotes(Strin g searchString)
              {
              int index = 1;
              boolean found = false;
              while(index < notes.size() && !found)
              {
              String note = notes.get(index );
              if (note.contains( searchString))
              {
              found=true;
              }
              else
              {
              index++;
              }
              }

              /**
              * Modified to reflect a 1, 2, 3 for 1st, 2nd, 3rd note.
              */


              if ((found) && index ==1)
              {
              System.out.prin tln("1" + ":" + notes.get(index ));
              }


              else if ((found) && index ==2)
              {
              System.out.prin tln("2" + ":" + notes.get(index ));
              }

              else if ((found) && index ==3)
              {
              System.out.prin tln("3" + ":" + notes.get(index ));
              }


              else
              {
              System.out.prin tln("Search term not Found, please try again!");
              }
              }

              /**
              * Modified the index portion to reflect 1 as starting point.
              */


              public void removeNote(int noteNumber)
              {
              if(noteNumber < 1) {
              // This is not a valid note number, so do nothing.
              }
              else if(noteNumber < numberOfNotes() ) {
              // This is a valid note number.
              notes.remove(no teNumber);
              }
              else {
              System.out.prin tln("Note number was not Found, please try again");
              }
              }




              /**
              * Show a note.
              * @param noteNumber The number of the note to be shown.
              * Modified the index portion to reflect 1 as starting point.
              */

              public void showNote(int noteNumber)
              {
              if(noteNumber < 1) {
              // This is not a valid note number, so do nothing.
              }
              else if(noteNumber < numberOfNotes() ) {
              // This is a valid note number, so we can print it.
              System.out.prin tln(notes.get(n oteNumber));
              }
              else {
              System.out.prin tln("Note number was not Found");
              }
              }
              }


              [/CODE]

              No problems it does what I want, no gimmicks. I will revisit this one to see what else it can do, perhpas I can make it work for other previous labs. There's an idea, yeah, That's it!

              Do indexes work with time? Or a better way to ask! Does each time slot have an index number, like 12:00 index 0, does this even make sense?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Erm, indexes in arrays and all sorts of Lists start at 0, not 1.

                kind regards,

                Jos

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  #9
                  Originally posted by JosAH
                  Erm, indexes in arrays and all sorts of Lists start at 0, not 1.

                  kind regards,

                  Jos
                  Now this makes sense, thanks Jos!

                  Comment

                  Working...