Thursday, October 30, 2008

Teaching Object-Oriented Programming

Since a lot of our students come here with very little computer experience, we had our beginning programming course split into two courses.  The first half focused on variables and control structures, and the second half focused on object-oreinted programming with C++.  Some time last year we decided to teach a scripting language (Perl) in the first course due to it's simplicity -- hello world is only one line.  We then created a two course series in object-oriented programming in Java.  This would allow us to make sure our students had a solid object-oriented background and three classes to develop their programming and problem solving skills, something that takes a lot of practice.

During the summer, I spent a lot of time creating labs and planning lectures for this new Java course.  After my Java class today, I had a discussion with a few struggling students.  After listening to their comments, I realized I had made a mistake on how I organized the class.  I decided to follow what most textbooks suggest and even brag about, focus on objects right at the beginning.  To me, this sounded like the best way to explain objects and classes.  An object is just like an object in the real world.  It has a nice interface and you just tell it what to do and it does it.

Today, I realized a major problem with this. They are still trying to grasp the concept of methods, and now they have to put the method in one file and the call in another.  They are still trying to grasp the concept of a variables, and now they put the declaration at the top of a class, and then use them inside the methods.  Plus, when they want to use a variable they are probably writing their main method and the variable is hidden in another file.  On top of all of this, they are trying to solve a problem.  Try working out a math problem on a bunch of different puzzle pieces and then put the pieces together in the right order to form a solution.  Thinking back to my beginning programming class, I remember struggling with the same thing when I first learned about objects and classes.

So here is my solution.  First, teach them about variables and how to use them.  Next focus on types, and then control structures, methods, and arrays.  Then present the idea of an object and class.  But first, as just a bunch of different variables that are related (like a struct).  What might work best is an example of keeping track of students -  their names, ids, and addresses.  Isn't it a lot of work to declare a string for each student and a int for their id and another string for their address and somehow keep them related?  Wouldn't it be nice to have all of those grouped together in an "array"?  Well we can, it's called an object.  Make the member variables public and use them like any other variable.  Next explain how it would be nice if the address edit method was somehow grouped with the student as well.  Then explain the constructor, and work towards encapsulation.

This way, the student first learns the basics without the need to memorize where everything goes.  They also learn object-oriented programming the same way it was invented, which shows the value of object-oriented programming.  Show them the structured programming way, and then show them how object-oriented programming improves upon it and simplifies the code.  By thinking that the why of oo-programming is to complex to explain, I actually made oo-programming more confusing.

Now I have to reorganize my class for next semester.

2 comments:

  1. I agree that teaching about objects first thing can be very confusing. I'm currently taking my first Java class and am still struggling with methods. There is a lot to learn up front and then throwing objects on top of that as well doesn't make programming java any less confusing. I'm interested in seeing how your class for next semester does compared to your old way of introducing objects.

    ReplyDelete
  2. It is very interesting, there are different ways in how to teach. Even though at BYUH somehow there are no books recommended for the programming classes, I think that books are good help. I understand that at BYUH there a a lot of students who come from islands or from the middle of the bushes, so they can't afford buying to many books, but I think that if the school also think like that, the other students end up screwed up because they don't have a a book as a learning tool.Education is priceless.As student I think that any method would work fine, however in class the students should have a time to practice and apply what they just learned. let's say, the teacher is talking about method. they should have sometime to do least one problem in class by themselves. It is the idea that our minds are like a bottle, with a small opening of the bottle. it has to be inserted in small amounts otherwise it spills out. It is the same for the students, they have a limit of information that they can handle, more than that limit they will get lost, lose focus in class, or fall sleep. Once a math teacher told me that "if the students have questions, it is because they are understanding what is being taught, or trying to, but if they have no question it means that they have no idea what is going on"

    ReplyDelete