Yeah, the class names were just from the tutorial. I usually like to stay more organized. Anyways thank you! That really helped. I was struggling with how to get integer values to compare and how to compare. Once I got a vague idea of how to do it. I just typed it in and after a few syntax corrections from the IDE and it prompting me to make the int values Static I got it to work. I didn't make it as fancy as yours but here is the code of mine that I got to work just the way I wanted.
class apples{
public static void main(String[] args) {
tuna tunaObject = new tuna();
int alarmHours = 7;
int alarmMinutes = 7;
int alarmSeconds = 7;
int counter = 0;
while (alarmHours!=tuna.getHour() || alarmMinutes!=tuna.getMinute() || alarmSeconds!=tuna.getSecond()){
{
++counter;
tunaObject.setTime((int)((Math.random()*23)+1), (int)((Math.random()*59+1)), (int)((Math.random()*59)+1));
System.out.println(tunaObject.toMilitary());
}
}
System.out.println("it took "+ counter+ " tries");
}
}
public class tuna {
public static int hour;
public static int minute;
public static int second;
public void setTime(int h, int m, int s){
hour = ((h>=0 && h<24) ? h : 0);
minute= ((m>=0 && m<60) ? m : 0);
second = ((s>=0 && s<60)? s : 0);
}
public static int getHour() {
return hour;
}
public static int getMinute() {
return minute;
}
public static int getSecond() {
return second;
}
public String toMilitary(){
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
I got
...
22:56:54
05:37:22
08:28:40
04:14:27
22:11:26
07:07:07
it took 85992 tries
Now that we've got that done. I feel a little bit accomplished. Haha, I'm still taking babysteps with this java thing. Anyways if anybody has any input on how to make this code more efficient? Or how they would have solved it. I'd love to see your code and read your explanation.
Thanks guys!