Tuesday, January 20, 2015

Aaron's Java Blag 3: Electric Boogaloo



Today I learned enough to make the program do different things based on calendar events. Soon I’ll learn how to make a robot butler, and have him bring me tea at specific times. 

The first “new” thing I used in the code was importing a calendar. You can’t see it in the image of the code, but it says at the top import java.util.Calendar; and import java.util.GregorianCalendar;. These just allowed me to pull two classes already premade in Java into my code: the calendar and the Gregorian calendar. The book wasn’t clear on why I needed both, so I’ll just assume the latter is so the former is formatted correctly.

The next things that were covered were if-else statements, although you might see I’d already been using those last time, as I already knew a bit about how they worked. According to the book and my experiences with them, these basically check to see if a condition is met, and executes different processes depending on whether it is or is not met (Bryant Control Flow).

The next thing I learned, which was completely new to me, was the switch statement. They are similar and yet different to if-else statements, and the book says “A switch block evaluates any value of the type used as its argument… switch blocks support any number of execution paths versus the two paths (true and false) of an if statement” (Bryant Control Flow). As soon as I read that, I figured that this would be good for anything that happened on multiple days, as it could work much faster than going through each day individually and finding what to say. You’ll see what I mean on my code.

The only flaw is that it implies there are people who start their homework before Sunday.
As you can see, I used a combination of the two statements I learned to determine what to say based on the date and the time. As I only needed to sort hours into adjacent groups for their time of the day, I went ahead and used if-else statements, to cover the large range (since I could just do all the numbers smaller than a certain other number, as the groups I needed were in numerical order). With the day, I used switch statements, as the days I have Robotics are not all next to each other. With this new statement, I didn’t need to go through for every day and write a specific if statement for it, so time was saved.

Here’s what I got when I ran the program:

Now I’ve got a program to tell me everything I need to do in the day! No more needing to remember things!

…Incidentally, that leads into today’s Amazing Thought-Provoking QuestionTM: Do you think having electronic devices that help us remember things are beneficial, or have they just forced us to rely on technology to remember the most basic things? I personally think that they’re useful if you have many things to remember, but you shouldn’t overuse them. If I ever actually need this program to tell me whether I have Robotics or not, that means I’m developing memory issues or something.

Bryant, Jay. Java 7 for Absolute Beginners. Berkeley, CA: Apress, 2012. Books 24/7. Web. 20 Jan. 2015.

2 comments:

  1. Wow, this is totally new to me, I've never seen this before. this is a very skilled thing to know how to do, very nice in explanations as well.

    ReplyDelete
    Replies
    1. Thanks Kiarra! In reality I barely know any Java, but I hope to learn more from this book and maybe take a class on it at some point.
      Chances are you've seen a Java program before and just not realized it. For example, there are many programs on websites that run off of Java code, like a tuner that I use to tune my saxophone. Those codes are much better than mine and in a different format, so you don't need to run them through Eclipse.

      Delete