#need help why is alarm triggering twice

85 messages · Page 1 of 1 (latest)

oak estuary
#

java ''
/*

  • Student Name: Peter Drakulic
  • Lab Professor: Professor Daniel Cormier
  • Due Date: June 16
  • Description: This Java class retrieves and formats the current local time.
    */
    package assignment2;

/** AlarmClock extends Clock and handles alarm functionality,
*including setting and checking alarm times.

  • @author - Peter Drakulic

  • @version 1.1

  • @since java 20

  • @see "Clock class for detail"
    */
    public class AlarmClock extends Clock
    {
    //creates variables alarmHours and alarmMinutes
    int alarmHours;
    int alarmMinutes;

    /** This method sets the alarm time by assigning the provided values for hours

    • and minutes to the corresponding variables.
    • @param hours
    • @param minutes
      */

    public void setAlarm(int hours, int minutes)
    {
    alarmHours = hours;
    alarmMinutes = minutes;
    }

       /**
      * The provided code snippet retrieves the current time,
      * compares it to a set alarm time, and if the current time has reached or surpassed the alarm time, 
      * it returns a string indicating the time followed by the word "Alarm."
      * @return String Return Hours and minutes and Alarm.
      */
    

    public String getTime()
    {
    String clockHours = getHours();
    String clockMinutes = getMinutes();
    int clockHoursInt = Integer.valueOf(clockHours);
    int clockMinutesInt = Integer.valueOf(clockMinutes);

       /*This code checks the time and alarm, 
       *resets the alarm if the condition is met, 
       *and returns the time with or without the "Alarm" suffix.
       */
      if (clockHoursInt >= alarmHours && clockMinutesInt >= alarmMinutes)
      {
      
          alarmHours = 0;
          alarmMinutes = 0;
          
          
           return clockHoursInt + ":" + clockMinutesInt + " Alarm";
      }
      else
      {
          return clockHoursInt + ":" + clockMinutesInt;
      } 
     
    }
    

}

weary minnowBOT
#

This post has been reserved for your question.

Hey @oak estuary! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#
java ''
/*
* Student Name: Peter Drakulic
* Lab Professor: Professor  Daniel Cormier
* Due Date: June 16
* Description: This Java class retrieves and formats the current local time.
*/
package assignment2;

/** AlarmClock extends Clock and handles alarm functionality,
*including setting and checking alarm times.
* @author - Peter Drakulic
* @version 1.1 
* @since java 20
* @see "Clock class for detail" 
*/
public class AlarmClock extends Clock 
{    
	//creates variables alarmHours and alarmMinutes
	int alarmHours;
	int alarmMinutes;
	
	/** This method sets the alarm time by assigning the provided values for hours 
	* and minutes to the corresponding variables.
	* @param hours
	* @param minutes
	*/
	
	public void setAlarm(int hours, int minutes)
	{
		alarmHours = hours;
		alarmMinutes = minutes;
	}
	
	/**
	* The provided code snippet retrieves the current time,
	* compares it to a set alarm time, and if the current time has reached or surpassed the alarm time, 
	* it returns a string indicating the time followed by the word "Alarm."
	* @return String Return Hours and minutes and Alarm.
	*/
	public String getTime() 
	{
		String clockHours = getHours(); 
		String clockMinutes = getMinutes();
		int clockHoursInt = Integer.valueOf(clockHours);
		int clockMinutesInt = Integer.valueOf(clockMinutes);
		
		/*This code checks the time and alarm, 
		*resets the alarm if the condition is met, 
		*and returns the time with or without the "Alarm" suffix.
		*/
		if (clockHoursInt >= alarmHours && clockMinutesInt >= alarmMinutes)
		{
			
			alarmHours = 0;
			alarmMinutes = 0;
			
			
			return clockHoursInt + ":" + clockMinutesInt + " Alarm";
		}
		else
		{
			return clockHoursInt + ":" + clockMinutesInt;
		} 
		
	}
}
north arrow
#

What exactly is happening?

#

And I don't see anything triggering here

oak estuary
#

why twice alarm

north arrow
#

You haven't shared the code printing that

oak estuary
weary minnowBOT
north arrow
#

that command is for formatting existing code

#

And I think that message is too long

oak estuary
#

the bot is thinkin

#

/*

  • Student Name: Peter Drakulic
  • Lab Professor: Professor Daniel Cormier
  • Due Date: June 16
  • Description: AlarmClockDemo displays clocks with current time and class names.
    */
    package assignment2;

/** The "AlarmClockDemo" class creates instances of different clocks

  • and displays their current time and class name.

  • @version 1.1

  • @since java 20

  • @see "AlarmClock class for detail"
    */
    public class AlarmClockDemo
    {

      public static void main(String[] args)
      {
            //The code creates an array of Clock objects and assigns a new Clock instance to the first element.
            Clock[] clocks = new Clock[4];
            clocks[0] = new Clock();
            
            //Creates a WorldClock object with a -4 hour time offset and adds it to the "clocks" array.
            WorldClock wClock = new WorldClock(-4);
            clocks[1] =  wClock;
            
            
            //This code creates an AlarmClock instance, sets the alarm to 17:01, and assigns it to the "clocks" array.
            AlarmClock aClock = new AlarmClock();
            aClock.setAlarm(8,36);
            clocks[2] = aClock;
            
            clocks[3] = aClock;
            
            
            //This loop prints the time and class name of each clock object in an array.
            for (int i = 0; i < clocks.length; i++) 
            {
                
               String time = clocks[i].getTime();
               System.out.println(time + ", " + clocks[i].getClass().getSimpleName());
                 
            }
            
            
            
        
        
        
        
        
        
        
     }
    

}

north arrow
#

that command is a bit weird

weary minnowBOT
#
/*
 * Student Name: Peter Drakulic
 * Lab Professor: Professor  Daniel Cormier
 * Due Date: June 16
 * Description: AlarmClockDemo displays clocks with current time and class names.
 */    
package assignment2;
    
/** The "AlarmClockDemo" class creates instances of different clocks 
 * and displays their current time and class name.
* @version 1.1 
* @since java 20
* @see "AlarmClock class for detail" 
*/
    public class AlarmClockDemo 
    {
        
        public static void main(String[] args)
        {
              //The code creates an array of Clock objects and assigns a new Clock instance to the first element.
              Clock[] clocks = new Clock[4];
              clocks[0] = new Clock();
              
              //Creates a WorldClock object with a -4 hour time offset and adds it to the "clocks" array.
              WorldClock wClock = new WorldClock(-4);
              clocks[1] =  wClock;
              
              
              //This code creates an AlarmClock instance, sets the alarm to 17:01, and assigns it to the "clocks" array.
              AlarmClock aClock = new AlarmClock();
              aClock.setAlarm(8,36);
              clocks[2] = aClock;
              
              clocks[3] = aClock;
              
              
              //This loop prints the time and class name of each clock object in an array.
              for (int i = 0; i < clocks.length; i++) 
              {
                  
                 String time = clocks[i].getTime();
                 System.out.println(time + ", " + clocks[i].getClass().getSimpleName());
                   
              }
              
              
              
          
          
          
          
          
          
          
       }
    
}
north arrow
#

both clocks[2] and clocks[3] is your AlarmClock

oak estuary
#

ok thanks

weary minnowBOT
# oak estuary ok thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

oak estuary
#

nnow i lost hte word alarm in the output

#

8:57 Alarm, AlarmClock

#

alarm is missing now

north arrow
#

What exactly did you change?

oak estuary
north arrow
#

Are you getting an error?

oak estuary
north arrow
#

What exactly is the output you get?

#

And is the program stopping?

oak estuary
#

09:17, Clock
09:17, WorldClock
9:17, AlarmClock

north arrow
#

I have an idea what could have gone wrong

#

You have that:

Clock[] clocks = new Clock[4];
#

but you only have 3 clocks, right?

oak estuary
north arrow
#

then maybe change it to

Clock[] clocks = new Clock[4];
#

I assume that in your loop, it fails because it is trying to access clocks[3] which is null

#

normally, that would show an error message but I assume your IDE suspends the program at that point

north arrow
#

What's the error message?

oak estuary
# north arrow What's the error message?

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "assignment2.Clock.getTime()" because "clocks[i]" is null
at assignment2/assignment2.AlarmClockDemo.main(AlarmClockDemo.java:41)

north arrow
#

Can you show your current code again?

oak estuary
#

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "assignment2.Clock.getTime()" because "clocks[i]" is null
at assignment2/assignment2.AlarmClockDemo.main(AlarmClockDemo.java:41)

north arrow
#

I feel like you didn't recompile the code properly

oak estuary
#

/*

  • Student Name: Peter Drakulic
  • Lab Professor: Professor Daniel Cormier
  • Due Date: June 16
  • Description: This Java class retrieves and formats the current local time.
    */
    package assignment2;

/** AlarmClock extends Clock and handles alarm functionality,
*including setting and checking alarm times.

  • @author - Peter Drakulic

  • @version 1.1

  • @since java 20

  • @see "Clock class for detail"
    */
    public class AlarmClock extends Clock
    {
    //creates variables alarmHours and alarmMinutes
    int alarmHours;
    int alarmMinutes;

    /** This method sets the alarm time by assigning the provided values for hours

    • and minutes to the corresponding variables.
    • @param hours
    • @param minutes
      */

    public void setAlarm(int hours, int minutes)
    {
    alarmHours = hours;
    alarmMinutes = minutes;
    }

    /**

    • The provided code snippet retrieves the current time,

    • compares it to a set alarm time, and if the current time has reached or surpassed the alarm time,

    • it returns a string indicating the time followed by the word "Alarm."

    • @return String Return Hours and minutes and Alarm.
      */
      public String getTime()
      {
      String clockHours = getHours();
      String clockMinutes = getMinutes();
      int clockHoursInt = Integer.valueOf(clockHours);
      int clockMinutesInt = Integer.valueOf(clockMinutes);

      /*This code checks the time and alarm,
      *resets the alarm if the condition is met,
      *and returns the time with or without the "Alarm" suffix.
      */
      if (clockHoursInt >= alarmHours && clockMinutesInt >= alarmMinutes)
      {

        alarmHours = 0;
        alarmMinutes = 0;
        
        
        return clockHoursInt + ":" + clockMinutesInt + " Alarm";
      

      }
      else
      {
      return clockHoursInt + ":" + clockMinutesInt;
      }

    }
    }

north arrow
#

I meant the main class

oak estuary
#

/*

  • Student Name: Peter Drakulic
  • Lab Professor: Professor Daniel Cormier
  • Due Date: June 16
  • Description: AlarmClockDemo displays clocks with current time and class names.
    */
    package assignment2;

/** The "AlarmClockDemo" class creates instances of different clocks

  • and displays their current time and class name.

  • @author - Peter Drakulic

  • @version 1.1

  • @since java 20

  • @see "AlarmClock class for detail"
    */
    public class AlarmClockDemo
    {

      public static void main(String[] args)
      {
            //The code creates an array of Clock objects and assigns a new Clock instance to the first element.
          Clock[] clocks = new Clock[4];
            clocks[0] = new Clock();
            
            //Creates a WorldClock object with a -4 hour time offset and adds it to the "clocks" array.
            WorldClock wClock = new WorldClock(-4);
            clocks[1] =  wClock;
            
            
            //This code creates an AlarmClock instance, sets the alarm to 17:01, and assigns it to the "clocks" array.
            AlarmClock aClock = new AlarmClock();
            aClock.setAlarm(17,1);
            clocks[2] = aClock;
            
            
            
            //This loop prints the time and class name of each clock object in an array.
            for (int i = 0; i < clocks.length; i++) 
            {
                
               String time = clocks[i].getTime();
               System.out.println(time + ", " + clocks[i].getClass().getSimpleName());
                 
            }
            
            
            
        
        
        
        
        
        
        
     }
    

}

#

sent

north arrow
#

yeah that's as expected

#

because Clock[] clocks = new Clock[4]; has space for 4 clocks

#

but you only put in 3

#

and when looping, you try to access all 4

#

which doesn't work as the last one doesn't exist

oak estuary
#

so?

north arrow
#

So you'd need to change the Clock[] clocks = new Clock[4] to Clock[] clocks = new Clock[3]

oak estuary
#

thats not what i want

north arrow
#

?

oak estuary
#

this is my output 09:24, Clock
09:24, WorldClock
9:24, AlarmClock

#

what i want is

#

09:24, Clock
09:24, WorldClock
9:24, alarm AlarmClock

north arrow
#

yes

oak estuary
#

this is not working 09:24, Clock

#

if (clockHoursInt >= alarmHours && clockMinutesInt >= alarmMinutes)
{

        alarmHours = 0;
        alarmMinutes = 0;
        
        
        return clockHoursInt + ":" + clockMinutesInt + " Alarm";
north arrow
#

and if you change the number of clocks to 3, you should get your expected output

oak estuary
#

i literally did

north arrow
#

and the issue is that it doesn't display the word alarm?

oak estuary
#

no

#

it dosent

north arrow
#

you have aClock.setAlarm(17,1); in your main

#

What is that supposed to do?

oak estuary
#

get the current time

north arrow
#

?

oak estuary
#

get the current time

north arrow
#

Should that not set the alarm time?

#

Also, what is Clock?

oak estuary
#

huuh

north arrow
#

I have no idea what your Clock class does

#

but setAlarm doesn't look like it would get the current time

#

it looks more like it would set the alarm time

oak estuary
#

it does

#

one sec

north arrow
oak estuary
#

it worked

#

had to put the current time in the brackets next to set alarm