Question
I need the program to ask the user to enter the correct amount of hours, minutes and seconds after they enter a incorrect value. The
I need the program to ask the user to enter the correct amount of hours, minutes and seconds after they enter a incorrect value. The program right now when a user enters a incorrect amount restarts completely and makes the user enter the hours and minutes and seconds again. The program should just ask for the correct value for where the user entered the wrong value and continue asking if they keep inputing the wrong value until they put the right value. If they put the right value then continue on to the next question.
import java.util.Scanner;
public class Clock
{
int hours;
int minutes;
int seconds;
String dayCycle;
public Clock(int hours, int minutes, int seconds, String dayCycle)
{
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.dayCycle = dayCycle;
}
public Clock()
{
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
this.dayCycle = "a.m.";
}
public String getDifference(Clock clock)
{
if(this.dayCycle.equals("p.m."))
return Math.abs(12+this.hours-clock.hours)+" hours, "+Math.abs(this.minutes-clock.minutes)+" minutes and "+Math.abs(this.seconds-clock.seconds)+" seconds";
else if(clock.dayCycle.equals("p.m."))
return Math.abs(this.hours-(12+clock.hours))+" hours, "+Math.abs(this.minutes-clock.minutes)+" minutes and "+Math.abs(this.seconds-clock.seconds)+" seconds";
else if(clock.dayCycle.equals("a.m.") && clock.hours==12)
return Math.abs(this.hours-(clock.hours-12))+" hours, "+Math.abs(this.minutes-clock.minutes)+" minutes and "+Math.abs(this.seconds-clock.seconds)+" seconds";
else if(this.dayCycle.equals("a.m.") && this.hours==12)
return Math.abs((this.hours-12)-(clock.hours))+" hours, "+Math.abs(this.minutes-clock.minutes)+" minutes and "+Math.abs(this.seconds-clock.seconds)+" seconds";
return Math.abs(this.hours-clock.hours)+" hours, "+Math.abs(this.minutes-clock.minutes)+" minutes and "+Math.abs(this.seconds-clock.seconds)+" seconds";
}
public void setTime(int hour, int minute, int second, String dayOfCycle)
{
this.hours=hour;
this.minutes=minute;
this.seconds=second;
this.dayCycle=dayOfCycle;
}
@Override
public String toString()
{
String hour, minute, second;
if(hours<10)
hour="0"+hours;
else
hour=hours+"";
if(minutes<10)
minute="0"+minutes;
else
minute=minutes+"";
if(seconds<10)
second="0"+seconds;
else
second=""+seconds;
return hour+":"+minute+":"+second+" "+dayCycle;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i=0;
while(true)
{
if(i>0)
{
System.out.println("Do you want to conitinue?Y/N");
String choice=sc.nextLine();
if(choice.equals("N"))
break;
}
i+=1;
try
{
System.out.println("Enter hour of clock A");
int hour1=sc.nextInt();
sc.nextLine();
if(hour1<0)
throw new Exception("Invalid hour! try Again");
System.out.println("Enter minute of clock A");
int minute1=sc.nextInt();
sc.nextLine();
if(minute1<0)
throw new Exception("Invalid minute! try Again");
System.out.println("Enter second of clock A");
int second1=sc.nextInt();
sc.nextLine();
if(second1<0 || second1>59)
throw new Exception("Invalid second! try Again");
System.out.println("Enter day cycle of clock A (a.m./p.m.)");
String dayCycle1=sc.nextLine();
if(!dayCycle1.equals("a.m.") && !dayCycle1.equals("p.m."))
throw new Exception("Invalid day of cycle! try Again");
System.out.println("Enter hour of clock B");
int hour2=sc.nextInt();
sc.nextLine();
if(hour2<0)
throw new Exception("Invalid hour! try Again");
System.out.println("Enter minute of clock B");
int minute2=sc.nextInt();
sc.nextLine();
if(minute2<0)
throw new Exception("Invalid minute! try Again");
System.out.println("Enter second of clock B");
int second2=sc.nextInt();
sc.nextLine();
if(second2<0 || second2>59)
throw new Exception("Invalid second! try Again");
System.out.println("Enter day cycle of clock B (a.m./p.m.)");
String dayCycle2=sc.nextLine();
if(!dayCycle2.equals("a.m.") && !dayCycle2.equals("p.m."))
throw new Exception("Invalid day of cycle! try Again");
Clock clk1=new Clock(hour1, minute1, second1, dayCycle1);
Clock clk2=new Clock(hour2, minute2, second2, dayCycle2);
System.out.println("Clock 1 time is: "+clk1);
System.out.println("Clock 2 time is: "+clk2);
System.out.println("Time difference between clock1 and clock2 i: "+clk1.getDifference(clk2));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started