Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Object Oriented Programming

When I was setting up my game engine, and even when developing my game, I took the time to create a nice class hierarchy. It took a little extra time. I did not go overboard. There may have been 15 classes tops. And some of those were small subclasses.

During a final round of testing, I found some of the text was coming out wrong. That was disturbing as the text worked fine in some scenarios. I traced it back to find some code doing extra work to get the job done right. To fix the global problem, I moved that code to a base class. Then everyone got the fix. I did have to tweak a few instances where the specific code made some wrong assumptions. But I essentially fixed the problem in one location. Bonus.

As an aside, I find the Java Collections Framework very helpful in developing games. Now I cannot imagine how I lived without them. Oh yeah. I used arrays. Painful. Luckily there is no more of that pain in my projects.

File Parsing

I have an idea for a new Java program which requires information about my colleges schedule of classes. Specifically I want to know what all the Computer Science classes are, and what the prerequisites for each are. This information is readily available in the latest schedule of classes. The chore is to get that information into my program. How do you do that? You write a program of course.

To start with, the schedule of classes is a huge PDF file. I used Adobe Acrobat to export this file to plain ASCII text. Then I wrote a small Java program to read in the file one line at a time. I coded in some rules to parse the lines to figure out which ones were computer science courses. So far I can detect all the CIS courses fine. For now I have the code just grabbing the text of the prerequisites for each class.

The next step is for me to build some structure which can easily represent the relationship between classes. Perhaps this is a big tree. I don't know. I do know that I must first read in all the information. Then I can make the connections in my tree. I already have a Java class called Course. And I have coded most of the logic into my Java class Parser. There are a lot more Java class before I have all the information that I want.

Sorry I have been a little cryptic about my eventual application which will require all this information. Perhaps when I am done I will upload the application here. For now I am keeping it on the down low. I am finding it hard to schedule time to work on my Java programming. As soon as my Java college class ended, there was nothing else driving me to code. And get this. The university does not offer a Java Programming 2 class. Go figure.

Name Clash

In my Java programming college class, we are learning more and more about classes. Specifically we are learning about access modifiers for instance variables and methods. One thing that is pretty clear is that if you have an instance variable in a class, and the variable name is myVar, then you cannot have another instance variable in the same class with the same variable name.

Within the class, you can reference an instance variable with just its name. You can optionally prepend the variable name with the keyword “this”. However in this scenario, “this” which represents the current object is already implied. So for most if not all of the time, the “this” keyword is omitted.

We are also learning about accessors and mutators in our programming class. Mutators, or setters, take an argument and set the instance variable to the value of that argument. Here is where some confusion in class came up. Support we have an instance variable in our class named “email”. What should we name the formal parameter of the setEmail method?

Most of the time, the simplest and most obvious variable name is the best one. So I thought I would name the formal argument to the mutator setEmail to be just “email”. But here is the problem and question. Does the local variable email, which is the formal parameter to the method in the class, clash with the instance variable of the same name?

The best way to answer questions like this is to try it out. It compile fine. So it must be ok. The only tricky part comes when you must set the instance variable to the formal parameter passed into the method. How do you do that if they are named the same? The trick is to qualify the instance variable with the “this” keyword. Then you get code that looks like this (no pun intended):

public void setEmail(String email)
{
this.email = email;
}

Now this may not be the best practice, as there might be some ambiguity as to which variable the name email refers to. So our instructor insisted that we name the formal parameter newEmail, or something else distinct from the instance variable name. That sounds logical. It was still cool to find out you could have two variables of the same name within the class.