Results 1 to 5 of 5
  1. #1
    TomFahey is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Problem with BlueJ Exercise

    Hello,

    So I'm new to these forums, and in fact new to Java (I'm teaching myself, and started a few days ago). I'm using "Objects First With Java", the excellent BlueJ tutorial, and I've been really enjoying it so far. I should add, I have no CS background at all, in fact, I'm a student on a gap year that's about to start a Physics course at university this coming year (so I have a relatively strong math/physics background at the high school/A-Level standard).

    However, introductions aside, I've come across what is, I must begrudgingly admit, the first wall, in the form of one of the "challenge exercises" in the book. It relates to a clock display that starts out as a 24hr affair, and you're tasked with making it analogue. Here is what I'm at so far:


    Java Code:
    
    /**
     * The ClockDisplay class implements a digital clock display for a
     * European-style 24 hour clock. The clock shows hours and minutes. The 
     * range of the clock is 00:00 (midnight) to 23:59 (one minute before 
     * midnight).
     * 
     * The clock display receives "ticks" (via the timeTick method) every minute
     * and reacts by incrementing the display. This is done in the usual clock
     * fashion: the hour increments when the minutes roll over to zero.
     * 
     * ^^ Student note: I've attempted to modify this 24hr clock to a 12hr one. I've tried to do this
     * by implementing a boolean field "AM", which when true will cause "AM" to be added to the 
     * displayString or when false will cause "PM" to be added instead. I've also added a method that
     * should read the displayString at 11:59 and basically make sure when the clock changes to 12:00
     * it correctly alters the suffix in the display.
     * 
     * @author Michael Kölling and David J. Barnes
     * @version 2011.07.31
     */
    public class ClockDisplay
    {
        private NumberDisplay hours;
        private NumberDisplay minutes;
        private String displayString;    // simulates the actual display
        private boolean AM;
        
        /**
         * Constructor for ClockDisplay objects. This constructor 
         * creates a new clock set at 12:00am.
         */
        public ClockDisplay()
        {
            AM= true;
            hours = new NumberDisplay(13);
            minutes = new NumberDisplay(60);
            setTimeDefault();
        }
    
        /**
         * Constructor for ClockDisplay objects. This constructor
         * creates a new clock set at the time specified by the 
         * parameters.
         */
        public ClockDisplay(int hour, int minute, String am)
        {
            hours = new NumberDisplay(13);
            minutes = new NumberDisplay(60);
            updateDisplay();
            setTime(hour, minute, am);
        }
    
        /**
         * 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();
            }
            updateDisplay();
        }
        
       /**
         * Set the time of the display to 12:00
         */
        public void setTimeDefault()
        {
            hours.setValue(12);
            minutes.setValue(00);
            updateDisplay();
        }
    
        /**
         * Set the time of the display to the specified hour, minute and choose AM or PM.
         */
        public void setTime(int hour, int minute, String am)
        {
            hours.setValue(hour);
            minutes.setValue(minute);
            if (am == "AM") {
                AM = true;
            }
            else if (am == "PM") {
                AM = false;
            }
            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()
        {
           if (AM = true) {
               displayString = hours.getDisplayValue() + ":" + 
                            minutes.getDisplayValue() + "AM";
                        }
           else if (AM = false) {
               displayString = hours.getDisplayValue() + ":" + 
                            minutes.getDisplayValue() + "PM";
                        }
        }
        
        
        /**
         * Change boolean expression over from AM to PM at 11:59
         */
            private void changeAMPM()
            {
                if (displayString == "11:59PM") {
                    AM = true;
                }
                
                else if (displayString == "11:59AM") {
                    AM = false; 
                }
    }
    /**
     * Set the boolean variable "AM" to false.
     */
    public void setPM()
    {
        AM = false;
    }
    /**
     * Set the boolean variable "AM" to true.
     */
    public void setAM()
    {
        AM = true;
    }
    }

  2. #2
    TomFahey is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem with BlueJ Exercise

    I've added a small note in the comment at the top, labelled "student note", that explains my approach to the exercise. The problem I'm getting is that my boolean variable "AM", is just not working at all. I suspect that I'm doing something wrong with my mutator methods (the two methods at the very end), as boolean variables have not been dealt with in much detail so far, and I'm sort of stepping on uncovered ground so far. I realise there are several ways to actually implement an analogue clock (such as letting the clock work internally as a 24hr clock, and simply changing the display string), but the exercise specifies that the limits of the hours and minutes objects, created from the NumberDisplay class, must be 13 and 60, respectively.

    I have no problem with just starting from scratch and trying to tackle the exercise with a different approach - as I said, the approach I've tried to implement relies on inferences I've made rather than techniques the book has covered so far - however, I quite like my approach, and would essentially like to know if the basic idea works, and I've just messed up somewhere in the code. Any help would be greatly appreciated - as a British student who's gone through high school/sixth form with the total b*llocks that is the British "IT" gcse-level work (my school didn't offer programming or anything like that, or in fact any kind of higher level IT course at all - like I said, I'm a physics student), I have no sources to go to, save the internet :(

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Problem with BlueJ Exercise

    Java Code:
    if (am == "AM") {
    Compare objects with equals method. I haven't looked at your code closely but when you want to change am/pm you might be ale to simplify your code with
    Java Code:
    AM = ! AM;

  4. #4
    TomFahey is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem with BlueJ Exercise

    Thank you Junky! Yes, while trawling the web for forums, I did come across that method, which would work very well for the switch-over at 11:59, but initially wasn't sure how to use it in the setTime method. Having thought about it again since reading your post, I've written a code, however, I'm still having the same problem - the AM variable is just refusing to change, it seems stuck on "true"! it's only when I call my "setAM()" or "setPM()" methods that it changes (I use the object inspector to check), and even then, as soon as I call the "updateDisplay()" method, it swaps right back to "AM". I'm perplexed, as I can see nothing in the updateDisplay() method that would cause such behaviour...

  5. #5
    TomFahey is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Problem compiling with BlueJ
    By Morgan in forum Other IDEs
    Replies: 8
    Last Post: 03-09-2013, 06:58 AM
  2. Bar chart Bluej problem
    By Manny123 in forum New To Java
    Replies: 1
    Last Post: 10-25-2012, 12:43 AM
  3. bluej challenge exercise 1.16
    By gatehrdy in forum New To Java
    Replies: 5
    Last Post: 03-07-2012, 08:54 AM
  4. Is the problem with my code or bluej?
    By farahm in forum Advanced Java
    Replies: 2
    Last Post: 03-26-2011, 04:00 PM
  5. bluej hashset problem ;s
    By truant420 in forum Other IDEs
    Replies: 2
    Last Post: 12-07-2010, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •