A website about programming wouldn’t be very useful if I only showed snippets of code, and never a real-world example. In this post, I’m going to reveal code for an electricity calculator… and here’s how it works.

The user starts up the program, types in some information, and the program tells the user some dollar values. Here’s what the program looks like, after a user asks it the ridiculous question “What would happen if I left my 1250 Watt Microwave Oven running full-blast, all year around?”

electricity-cs

As you can see, the intended use of the program is not to scare parents by telling kids to leave their microwaves running 24/7. The intended use is whatever you want it to be, because freedom is the number one benefit of programming.

So let’s take a look at the code, piece by piece:

So what do we have here?

The using directive at the top instructs my compiler to import, or load the libraries for, the respective System class and commands. Without that line, I wouldn’t be able to use any of the Console.ReadLine or Console.WriteLine commands without prefixing each of them with System. – which obviously would get cumbersome and make the code look not-so-pretty.

The program name is ElectricityCS – I wrote multiple versions of this program (one in Visual Basic and one in C#), so I can easily distinguish which one I’m loading from my filesystem. The namespace keyword (and associated { block } of code) tells the compiler that all code within the block is part of the ElectricityCS namespace. This is one example of scope, similar to a class or subroutine, but in a level above class.

Next we define the Program class and the main subroutine, which give the program an entry point. This was described in detail during the What’s In YOUR App? series of posts.

 

The first line defines a label called beginning. From anywhere in the program, I can say goto beginning; and my code will jump up to the beginning label. Labels are not used as much now as they were in the past (mainly because they were a pseudo-subroutine methodology in a world of primarily spaghetti code). In fact, Java doesn’t even support goto commands or labels. However, I’m giving an example so in case you ever come across labels, you’ll know what they are.

Next we define four doubles: centsPerkWh, powerDraw, hoursPerDay, and daysPerMonth. These will be used to store the data that the user inputs:

The first thing you’ll notice is a new type of block, called a try/catch block. In a try/catch block, code execution is attempted; however, if execution fails for any reason (or an exception is thrown), the code jumps to the bottom of the try part of the block, where it latches onto the catch part. You’ll see the catch part shortly.

We ask the user for values to fill each of the four doubles. However, because the user is technically typing in Strings, we have to do an explicit conversion to doubles with Convert.ToDouble(String);.

This block of code simply rehashes the information that the user typed in. This is good for troubleshooting, since if the output was all zeroes, we’d know there’s an issue with our code somewhere.

 

This block of code does some simple arithmetic to calculate, based on the inputted values, the kWh used/month, $/month, and $/year. It then outputs this information to the user, which is really the whole point of the program.

Here we have the console telling the user to “Press ENTER to exit.” It waits for the user to press enter (otherwise the program would exit immediately after printing the information). These are things that programmers sometimes forget to do. In this case, when the program is tested, the tester would have gotten annoyed that the program exits before anyone could read the output.

Then you can see the catch part of the block! This is what happens when the try part fails. In other words, any time the user enters garbage data (or tries to enter “Bob” for a numeric value, for example,) the code will jump to the catch part of the block, which instructs the program to (essentially) yell at the user and start over from scratch.

Finally, there are several } characters representing the closure of the try/catch, void Main, class Program, and namespace blocks of code, respectively.

If you are so inclined, you can download the entire source code file here for further in-depth analysis.

Share

Comments are closed.

Post Navigation