patternjavaMinor
Alarm clock with "ringing" functionality
Viewed 0 times
alarmringingwithclockfunctionality
Problem
I have a question regarding a program that is to run an alarm clock and display the time (HH:MM AM/PM) and also trigger the alarm and display "ringing" at 12:00 AM.
I am a beginner in Java and I am a little lost. I believe I have most of the skeletal work done. This is much like another question asked on here but I could not find an answer to my problems. So I apologize for the similarity. I am also to create a "dummy driver" in order to test the method as one of the requirements for this program is to be able to be read by a general driver someone else has created. Therefore, all of my variables and methods must be left the way I have them in order to be read by said driver. I wish I could be a little more specific in what I need assistance with but any feedback you can give me is much appreciated.
This is what I've got so far:
```
//dummy driver
package program5;
public class Program5 {
public static void main(String[] args) {
AlarmClock alarm = new AlarmClock();
System.out.println(alarm.getHour() + ":" + alarm.getMinute() + alarm.getAmOrPM());
System.out.println(alarm.getAlarmHour() + ":" + alarm.getAlarmMinute() + alarm.getAlarmAmOrPm());
System.out.println(alarm.isIsAlarmRinging());
}
}
package program5;
public class AlarmClock {
private int hour;
private int minute;
private String amOrPm;
private int alarmHour;
private int alarmMinute;
private String alarmAmOrPm;
private boolean isAlarmRinging;
//constructor:
AlarmClock() {
hour = 12;
alarmHour = 12;
minute = 0;
alarmMinute = 0;
alarmAmOrPm = new String();
amOrPm = new String();
isAlarmRinging = false;
}
//getters:
public int getHour() {
if ((hour >= 1) && (hour = 0) && (minute = 12){
isAlarmRinging = true;
}
return isAlarmRinging;
}
//setters
public void setHour(int newHour) {
if ((newHour >= 1) && (newHour = 0) && (newMinute = 1) && (newAlarmHour = 0) && (newAlarmMinute 12) {
hour = 1;
}
}
public void advanceHours(int hoursToAdvance) {
}
public void setTime(int newHour, int newMinute, St
I am a beginner in Java and I am a little lost. I believe I have most of the skeletal work done. This is much like another question asked on here but I could not find an answer to my problems. So I apologize for the similarity. I am also to create a "dummy driver" in order to test the method as one of the requirements for this program is to be able to be read by a general driver someone else has created. Therefore, all of my variables and methods must be left the way I have them in order to be read by said driver. I wish I could be a little more specific in what I need assistance with but any feedback you can give me is much appreciated.
This is what I've got so far:
```
//dummy driver
package program5;
public class Program5 {
public static void main(String[] args) {
AlarmClock alarm = new AlarmClock();
System.out.println(alarm.getHour() + ":" + alarm.getMinute() + alarm.getAmOrPM());
System.out.println(alarm.getAlarmHour() + ":" + alarm.getAlarmMinute() + alarm.getAlarmAmOrPm());
System.out.println(alarm.isIsAlarmRinging());
}
}
package program5;
public class AlarmClock {
private int hour;
private int minute;
private String amOrPm;
private int alarmHour;
private int alarmMinute;
private String alarmAmOrPm;
private boolean isAlarmRinging;
//constructor:
AlarmClock() {
hour = 12;
alarmHour = 12;
minute = 0;
alarmMinute = 0;
alarmAmOrPm = new String();
amOrPm = new String();
isAlarmRinging = false;
}
//getters:
public int getHour() {
if ((hour >= 1) && (hour = 0) && (minute = 12){
isAlarmRinging = true;
}
return isAlarmRinging;
}
//setters
public void setHour(int newHour) {
if ((newHour >= 1) && (newHour = 0) && (newMinute = 1) && (newAlarmHour = 0) && (newAlarmMinute 12) {
hour = 1;
}
}
public void advanceHours(int hoursToAdvance) {
}
public void setTime(int newHour, int newMinute, St
Solution
Calendar
You created an alarmclock, which is really just a time-representation. A
You can also look into the Java 8 time API but I think that might not be appropriate yet.
Enums
When you have a limited set of different possibilities, your mind should immediately wander to enums. This way you can represent your AM/PM by saying
Indentation
Stick to conventions. Some people indent a block with one tab (or 4 spaces), some with 2 spaces. This will keep the code readable for everyone.
Methods without a meaningful body
Look at your code here:
With formatting this becomes this:
Can you tell what's wrong here? Note that there are several methods that have this construct!
Assignment vs Comparison
This is a very common mistake:
What you do here is set
Keep in mind:
Unused parameters
In your method
Likewise in method
You created an alarmclock, which is really just a time-representation. A
Calendar has both a date and time component. I strongly, strongly suggest you use that so you can let your user define how he wants his date represented and let the API handle fancy stuff like timezones, summer hours, etc.You can also look into the Java 8 time API but I think that might not be appropriate yet.
Enums
When you have a limited set of different possibilities, your mind should immediately wander to enums. This way you can represent your AM/PM by saying
DayPart.AM and DayPart.PM instead of the literal string "AM" which could mistakenly be used as "aM", "am", "AX", etc. It will provide compile-time safety and is a lot more pleasant to work with.Indentation
Stick to conventions. Some people indent a block with one tab (or 4 spaces), some with 2 spaces. This will keep the code readable for everyone.
Methods without a meaningful body
Look at your code here:
public int getHour() {
if ((hour >= 1) && (hour <= 12)) {
}
return hour;
}With formatting this becomes this:
public int getHour() {
if ((hour >= 1) && (hour <= 12)) {
}
return hour;
}Can you tell what's wrong here? Note that there are several methods that have this construct!
Assignment vs Comparison
This is a very common mistake:
if (isAlarmRinging = true)What you do here is set
isAlarmRinging to true, you never compare it. Keep in mind:
- Comparison:
==.
- Assignment:
=.
Unused parameters
In your method
setTime you never use the parameter newMinute.Likewise in method
setAlarmTime you never use newAlarmAmOrPm.Code Snippets
public int getHour() {
if ((hour >= 1) && (hour <= 12)) {
}
return hour;
}public int getHour() {
if ((hour >= 1) && (hour <= 12)) {
}
return hour;
}if (isAlarmRinging = true)Context
StackExchange Code Review Q#47921, answer score: 6
Revisions (0)
No revisions yet.