Time adder – Java example

Time adder – Java example


Hello everybody, welcome to the draw for example section in this section, I’m going to set up a time adder so first I would give the current time in hour minute and a second and then I would like to Move time forward by inputting the hour minute and second to move forward after that, I will do the evaluation of the new time by using the addition of time So let let us see how it can be done I would like to input some Pharos to representative current time. And as well as the Time to add so I need a scanner object So first, I import the scanner package Okay, so after Importing the package I can use the scanner object later So first I would like to Create a scanner object reference called scan Ok the variable name called scan is storing the object reference to a scanner object and here system not in is the Input argument for the scanner class, which means I’m going to receive input from the keyboard After that I can ask the user to input the current time For example I can ask the user to input the current owl so I need a variable to store the value received but user input Assume that the user would input an integer so I use a Integer variable called current hour to store the user input So I can do similar things for the minute and the second Okay, I have three variables to store the current hour minute and second So the next step is to ask the time to go forward. So I would ask the user to input the hour, minute, and second to go forward The process is quite similar to the previous part, so I can use the copy and paste action And of course, I need some new variables to store the time to go forward. So I would use forwardHour to store the time of hour to go forward forwardMinute and forwardSecond. So these three variables would accept the user input about the time to go forward So I I can go to the real calculation part First I need some variables to store the final answer I call the answer to the hour answerHour So I can do something similar to store the minute and the second for the answer I would initialize all these three variables to zero When we do the addition of time, it is possible that we have some result which is larger than 60 Minutes or 60 seconds. In this case, we would have a carry digit. So we need the variables to store the carry digit So we would have two variables to store the carry digit for the second and the minute addition It is possible that the time adding result would lead to another day. So it is quite sensible to have a variable to consider the situation. I would use a boolean variable to address this issue For now, we assume that we don’t meet the situation So we set the variable isNextDay to be false Okay, we can do the addition. So the second value would be equal to the summation of the current second and the forward second Of course, we have to address the issue of the second value being larger than 60 Okay, we can see that if the answer for the second is larger than or equal to 60, we need to make it less than 60. So I need to subtract 60 from the original answer about the second and We also need to address the carry digit by setting the carrySecond to be equal to one We can do similar things for the minute part from my answer Okay since it is possible that we have a carry digit from the … in the second answer, this carry digit will be converted to one minute So I have to add carrySecond in the answer part of the minute section, okay? So if there is no carry digit, this variable will still be zero Okay, I do similar things with the second answer to the minute answer Put a similar thing for the hour answer So we have to consider the case when the sum of the minutes is larger than or equal to 60. If it is the case, there is a carry digit from the minute answer, which is carried to the hour part of the answer. So we would add one hour to the answer if it is the case Okay I need to add some changes here as well So the conditional statement here would address the issue when the hour part of the answer is larger than or equal to 24. It means that we will go to the next day So I would set isNextDay variable to be true So after that, I can print the answer by considering whether it is going to become the next day or not Okay, this is the answer. If the sum of the two time instants would go to the next day, I would print out the hour, minute, and second on the next day If we don’t go to the next day, we can print out similar things But in this case, we don’t need to mention the next day because we still get the answer on the same day Okay, let … let us try to run it. Save it and run it Suppose the current hour is Three, the minute is 15, the second is 45 The time to go forward is, for example, six hours 45 minutes and 30 seconds, so the answer would be Ten o’clock one minute and 15 seconds. How can we get this answer? We handle the addition of the second first 45 plus 30 would be 75, so it means we would have a carry digit from the second part of the answer And after handling the carry digit, the second would become 15 So we will have one minute to the answer part current minute is 15 and the minute to add is 45. So these two values with then add up to 60. Together with the carry digit from the second part which which which is converted to one, we would have one minute as the answer We have to carry the the digit coming from the minute section to the hour section. So three plus six is originally nine But after we handle the addition issue we would have 10 as the hour answer So this is one of the possible cases of the time addition. We would have similar treatment when we want to subtract time from another time My approach is intuitive and straightforward, but it … it may not always be the best solution or the most efficient solution If you can think about any idea to improve my code, please feel free to do so on the comment section below the video If you like this video, please give me a like and please subscribe to my channel Thank you for watching

Leave a Reply

Your email address will not be published. Required fields are marked *