Code Year Lesson 2, Part 2

Yikes, I’ve been woefully negligent of my coding lately! Admittedly, it’s a task that I end up avoiding. For whatever reason, thinking about the coding often fills me with anxiety; I know that the work will be a little bit tedious and will often be difficult for me to process. I imagine it’s natural to avoid tasks that feel uncomfortable, but I think I have to do a better job of either a.) finding joy in the process or b.) becoming a little bit more disciplined. I’d like to rely on option a), but I have a feeling that the mean option b) may have to take the wheel.

Technically, I’m a week behind. I just finished up the “activity” for Lesson 2, which was about defining functions in Java.

Do you remember those word problems in math class? You know, the ones that were like:

“Jimmy drove 100 miles at sixty miles per hour to meet Carmen. Carmen is 500 miles away driving at 30 miles per hour. Calculate how long it will take Jimmy to meet Carmen. Throw in the number of corn nuts Jimmy will munch on as he drives to meet Carmen.”

I mean, I guess they weren’t exactly like that, but I remember walking away from each of those problems with an exasperated, “WHO CARES?” Plus, it was always so difficult for me to wrap my mind around the spatial-temporal connection. ¬†What if Jimmy had to pull over for a flat tire? Why is Carmen such a slow driver?

In any case, the “functions” activity for Code Year initially terrified me as it reminded me of those old word problems…

My task was to create a program that calculated New York taxi cab fare. It started nicely enough:

Declare a variable! Yeah! I’ve done this before! For those of you who don’t remember, declaring a variable really just means saving it into the program. Sweet. Now the program knows the baseFare is $2.50.

Oh, and that little yellow “caution” sign at the start of line three of my code was to scold me for forgetting a semicolon. Unfortunately, I forgot to screencast my “good job!” when I fixed the syntax.

Then, we had to bring back our knowledge of functions. To refresh your memory, functions are those programs that you create so that you don’t have to repeat certain lines of script. A function is a program that you probably want to be running in the background or that you want to use multiple times.

So, here’s the function I defined to calculate the taxi fare. For whatever reason, it was really difficult for me to keep the variables straight initially. Somehow, defining the base fare BEFORE I established the function that finds the final taxi fare seemed confusing.

I mean, for someone used to thinking in a narrative form, this would be kind of like giving the cast of characters at the front of a book before the book even begins. I suppose that’s not so weird, but casts of characters typically only appear at the beginnings of plays. When I’m reading a novel, I somehow don’t expect this to happen. In the same way, I somehow didn’t expect this to happen in functions either.

Of course, in math, you must define your variables before you solve your problems, so I suppose if I was used to thinking in a “math-brained” way, this would have been more intuitive.

Now, the lesson grew increasingly tricky as we had to include a parameter in our function. This parameter allows the function to define more specific characteristics of the function. In this case:

I had to include the parameter for the miles traveled in the taxi to determine its total cost. Without defining that parameter, I would have been unable to execute the cost of the taxi ride. In the initial function, that total fare would have only been determined according to the base fare. That, of course, is incomplete since the rest of the fare is determined by the cost of additional miles and, of course, the number of miles traveled.

See how this is reminiscent of those old word problems?

But look! We had to deal with one more factor:

That’s right: the cost of the fare CHANGES during certain hours of the day. I guess it’s about time I start calculating those corn nut delays, right?

As you can see, I didn’t end up getting the final function right. Frankly, I’m still a little stumped on this one. I think I made a simple syntax error, but I’m pretty sure I set it up correctly. All I had to do was add in an “if/else” statement to determine that final fare. However, again, something went wrong…

This, of course, took me a while to figure out and, with the help of forums that are part of the Codeacademy lessons, I finally got to this point. However, it was that kind of “backwards” thinking that threw me off again. Why couldn’t the night surcharge declared at the beginning of the function just operate as the factor needed to do the calculation? Couldn’t the computer just “figure it out?”

Obviously, the answer to that is “no.” As a programmer, it is your job to define all the variables necessary to make sure the end result comes out accurately. If I think about it more, it’s somewhat similar to the process of writing a paper. A writer should not assume that the reader just “gets” his argument. He has to go through the pains of explicating it, justifying his point and showing evidence. Something similar is happening here: I have to explicate the problem and show all my tracks to guarantee that the computer “gets it.” Otherwise, he’s stumped!

I suppose he is a little stumped at present, considering the fact that my syntax is somehow wrong. At times like these, I want to apologize for any student’s English grammar that I’ve ever scoffed at. It’s incredibly difficult to remember these grammatical rules when learning a new language! I suppose I’ve always taken that for granted.

Next week, I’ll complete the work I was supposed to do this week on Conditionals in Java. Get stoked!

Leave a Reply

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