Most examples in this book are short and focus on just enough code to demonstrate an idea. But most programs are larger than that. Seeing how a larger program is constructed, and learning to write them yourself, is an important part of learning to program. Periodically, we will examine a Case Study β a full fledged program. Our first such Case Study is a program to calculate elapsed time.
One way computers often track time is as a number of seconds since some known point. If you know 1342 seconds have elapsed, you can calculate how many minutes, hours and days have elapsed. We are going to construct a program that does that calculation.
Programs are not written by sitting down and typing all of the code. Typically you have to do some design work, and then you write the code piece by piece, testing each part as you go.
If you can not solve a problem by hand, you can not solve it with a computer. The computer can do the math faster, but writing a program is literally writing the instructions for how to solve a problem. So if you are stuck, first solve the problem by hand. Then worry about trying to turn that solution into code.
the division and remainder operators should allow me to convert something like 83 seconds to 1 minute by dividing by 60. Taking the remainder will tell be 23 seconds are left over. Similarly, I can convert minutes to hours and by dividing by 60 and then taking the remainder to find the left over minutes.
How will you calculate those values? the division and remainder operators should allow me to convert something like 83 seconds to 1 minute and 23 seconds by dividing by 60
Now it is time to start writing code. Not all the code, just enough to verify I am starting with the right value(s). We donβt know how to get input as the program is running yet, so I will hard code in the value 1312 seconds for now. Here is my first attempt:
When I run this program, I see that it prints out 1312 seconds. So I know I am starting with the right value. That is a good first step! If there were errors, I would want to fix those before trying to continue on with the program.
Now I need to figure out how to convert that to hours, minutes, and seconds. Letβs tackle one part at a time. First I will worry about how many minutes have elapsed. (I am choosing to work from the smallest unit to the largest unit, but you could also choose to work in the other direction.)
I know that there are 60 seconds in a minute. So I can divide totalSeconds by 60 to get minutes But I also need to know how many leftover seconds there are when I do the division. I can use the remainder operator % to do that. Here is my next attempt:
When I run this program, I see that it prints out 21 totalMinutes and 52 leftoverSeconds. That is the correct answer! Letβs try out another starting value. Change the 1312 to 8734. If the program is working right, it should tell you that is 145 totalMinutes and 34 leftoverSeconds
Notice that I have two different variables that refer to an amount of seconds. totalSeconds refers to the total number of seconds that have elapsed. leftoverSeconds refers to the number of seconds that are left over after I have calculated the number of minutes. It is important to keep track of what each variable represents. Picking good names for variables is an important part of keeping this straight! Although it is tempting to use short variable names like seconds, it is often better to use longer names that are more descriptive to avoid confusion.
Now I need to figure out how to convert the number of minutes to hours and minutes. I know that there are 60 minutes in an hour. So I can divide totalMinutes by 60 to get hours. And I can use the remainder operator to get the number of minutes left over. Here is my next attempt:
Try running this program. Also try running it with the Codelens feature to watch it run line by line. Then try changing the starting number of seconds and make sure that it still appears to give the right answers.
Once we are convinced the coding is working, we can clean it up and get rid of the extra output. It was useful to see the value of totalMinutes, but we donβt actually need to print it in the final program. This version of the program keeps all of the
The process we used to develop this program is known as incremental development - building a program in small steps instead of all at once. The key ideas are:
Start with a working program and make small changes or additions one at a time.
The goal is to have confidence in all of of the code other than your most recent changes. That way you know exactly what to blame if the program stops working as expected. If you do get stuck along the way, you can always go back to the last working version of the code and start over from there.
Breaking large problems into a series of smaller, simpler problems is a powerful general problem solving technique and a critical habit in programming.
Early on as a developer, you should not write more than a few lines of code without stopping to test that they are working as you intended. If there is an error in something you have written, focus on fixing what you have (or back tracking and trying something else). Trying to add more logic to code that isnβt working will just make finding and fixing the issue harder.
int days = hours / 24;
int leftoverHours = hours % 24;
Yes, that is correct. The number of days is the total number of hours divided by 24. The number of leftover hours is the total number of hours modulo 24.
int days = hours / 60;
int leftoverHours = hours % 60;
There are not 60 hours in a day.
int days = totalHours / 24;
int leftoverHours = totalHours % 24;
The program currently does not have a variable named totalHours. It might be smart to rename hours to that, but assume we are sticking with the current variable names.
int days = hours % 24;
int leftoverHours = hours / 24;
The number of days is the total number of hours divided by 24. The number of leftover hours is the total number of hours modulo 24.