Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Alarm clock won't tick, can't call method because of dereferencing??

  1. #1
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Alarm clock won't tick, can't call method because of dereferencing??

    public class ClockDisplay
    {
        private NumberDisplay hours;        // different class
        private NumberDisplay minutes;     //"                   "
        private String displayString;    // simulates the actual display
        private boolean turnAlarmOff;
        private boolean turnAlarmOn;
        private int alarmHour;
        private int alarmMinute;
     
        /**
         * Constructor for ClockDisplay objects. This constructor 
         * creates a new clock set at 00:00.
         */
        public ClockDisplay()
        {
            hours = new NumberDisplay(24);
            minutes = new NumberDisplay(60);
            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 alarmH, int alarmM)
        {
            hours = new NumberDisplay(24);
            minutes = new NumberDisplay(60);
            setTime(hour, minute);
        }
     
        /**
         * This method should get called once every minute - it makes
         * the clock display go one minute forward.
         */
        public void timeTick()                                                      //cannot get this method to work for setTurnAlarmOn method.
        {
            minutes.increment(); 
            if(minutes.getValue() == 0) {  // clock "rolls over" 60 sec "tick"
                hours.increment();
            }
            if(alarmHour == hours.getValue() && alarmMinute == minutes.getValue()){
                System.out.println("The alarm is ringing.");
            }
            updateDisplay();
        }
     
        /**
         * Set the time of the display to the specified hour and
         * minute.
         */
        public void setTime(int hour, int minute)
        {
            hours.setValue(hour);
            minutes.setValue(minute);
            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()
        {
            int hour = hours.getValue();
            String suffix;
            if(hour >= 12) {
                suffix = " pm";
            }   
            else {
                suffix = " am";
            }
            if(hour >= 12) {
                hour -= 12;
            }
            if(hour == 0) {
                hour = 12;
            }
            displayString = hour + ":" + 
                            minutes.getDisplayValue() + suffix;
        }
     
        /**
         * Set the alarm clock time
         */
        public void setTurnAlarmOn(int alarmH, int alarmM)           //Would like this alarmMinute to follow the timeTick
        {
            int hGood = 0;
            int hMGood = 0;
            if(alarmHour == 0){
                alarmHour = 12; 
                hGood = 1;
            }
            if(alarmHour >= 0 && alarmHour <= 23){
                alarmHour = alarmH;  
                hGood = 1;
            }
            else {
                System.out.println("Please input a valid time.");
            }
            if(alarmHour >= 13 && alarmHour <=23){
                alarmHour -= 12;
                hGood = 1;
            }
            else { 
                System.out.println("Please input a valid time.");
            }
            if(alarmMinute >=0 && alarmMinute <= 59) {
                alarmMinute = alarmM;
                hMGood = 1;
            }
            else{
                System.out.println("Please input a valid time.");
            }
            if(hGood == 1 && hMGood == 1){
                turnAlarmOn = true;
            }
                else{
                    turnAlarmOn = false;
            }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    Can I add a screen shot of just that method with the compiler displaying the error message?

    If not, it has my call to the method alarmMinutes.getValue(); and says "int cannot be dereferenced." I just want my alarm clock to use the same timeTick method as the original clock (NumberDisplay class). If I can't get the clocks to sync then I don't have an alarm clock because it will only go off when the original clock "ticks" to the alarm time. So frustrating, but I'm continuing to read, test, and learn. Thx for the comment.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    call to the method alarmMinutes.getValue();
    I do not see a variable: alarmMinutes in the code. Are you sure that is the variable name?
    I do see a variable: alarmMinute is that what is in the error?
    says "int cannot be dereferenced."
    Where is the statement with the getValue method calll? You can not call a method on a primitive variable like an int.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    The alarmMinute is in this class but I was trying to compare it to the original clock minute. In this program there are two classes NumberDisplay (which displays the number string) and ClockDisplay (which uses the data in the NumberDisplay to show us the current time). I was trying to sync the two clocks together and that's why I was calling that method hour.getValue() and minute.getValue() to see if I could sync the clocks. I know, I'm an idiot. I applaud all the programmers out there because this stuff can be very very frustrating!!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    There is no need to call a method to get the value of a primitive. The variable itself contains its value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    Ok, cool. I guess that's what the problem is then. So, in my constructor and field I have the variables NumberDisplay hours and NumberDisplay minutes, in my timeTick method, should I replace the (alarmHour == minutes.getValue()) && (alarmMinute == minutes.getValue()) with NumberDisplayHours and NumberDisplayMinutes? I've tried that and it doesn't work, I also tried getTime.hour/s and getTime.minute/s, doesn't work. I feel like I try things and then end up messing up something else and it's a vicious circle. If you don't mind sharing what helped you learn this stuff the most, I'd be very grateful. I watch/read how people explain what they're doing because sometimes someone explains it in a slightly different manner and it actually makes sense to me. Thanks for helping me.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    it doesn't work
    Please explain.
    Copy the full text of the error messages and paste it here so we can see what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    Quote Originally Posted by Norm View Post
    Please explain.
    Copy the full text of the error messages and paste it here so we can see what happened.
    "int cannot be dereferenced."

    I figured out what the problem was. Well, I didn't, but someone asked me why I was trying to create another clock instead of creating another "feature," or time. Basically I was doing way more than needed and all I needed to do was to create a boolean statement inside the method that I created called turnAlarmOn() and create a boolean method for turnAlarmOff(). So I tried this:

    public void setTurnAlarmOn(int alarmH, int alarmM)
    {
    if((alarmH >=0 && alarmH <=23) && (alarmM >=0 && alarmM <=59)){
    alarmHour = alarmH;
    alarmMinute = alarmM;
    isOn= true;
    }
    else {
    System.out.println("Input error. Please set alarm to valid time.");
    }
    }
    -----------------------------------------------------------------------------------------------
    /**
    * Turn the alarm off
    */
    public void turnAlarmOff()
    {
    isOn = false;
    }

    In the timeTick() method I added one piece:

    if(isOn){
    if((alarmHour == hours.getValue()) && (alarmMinute == minutes.getValue())){
    System.out.println("The alarm is ringing.");
    }
    }
    updateDisplay();

    It worked. Unfortunately for me, I take WAY too long to figure things out and my assignment was late.

  10. #10
    Junior Member
    Join Date
    Aug 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Alarm clock won't tick, can't call method because of dereferencing??

    Excellent methods that help me
    employee monitoring
    Last edited by Arnetta; August 28th, 2021 at 04:40 AM.

Similar Threads

  1. alarm clock gui
    By hhm136 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2014, 01:03 PM
  2. Need some help with a tick tack toe game
    By Jploganlant in forum Java Theory & Questions
    Replies: 5
    Last Post: April 14th, 2014, 04:47 PM
  3. Alarm Clock (How to figure out how much time left till alarm will sound)
    By MrMario in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2013, 12:58 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM