Introducing OOP concepts
From CompWiki
Most of the meaningful comments from week 1 of the workshop. Code examples referred to by "zip" files cannot be uploaded here unfortunately.
Assignment 1
by geetanjali dewan - Monday, 31 March 2008, 11:30 AM
regarding oop, I start off with that cookie cutter analogy for explaining oop, cookie dough as memory and each cookie as an instance.So they understand.
Then :objects:example student:data-name, date of birth, marks and the functions-total,avg and display.
Classes:a set of related objects.A class body implements operations on the interface& instance variables.then data and operation of a class can be declared as public, protected and private.
Data abstraction by classes explained briefly.
inheritance - Inheritance maybe used to define a separate subclass of employee for each different type of employee-scientist, engineer etc.Each subclass automatically inherits attributes and methods of employee but can provide additional attributes and methods.Attributes as no. of projects, duration etc can be stored in the scientist class.Methods such as reviewing project status can be added to scientist class.
Subclass can also override a method from the superclass if they want to provide more specialized behaviour for the method.If the subclass method has same name , parameter list and return type as superclass method, then method override occurs.The code below is understood by people with no background at all in programming.
Later they look at access rights and if the supervisor has more access rights than employee then supervisor class extends employee so it gets all the buttons of the emplyees in addition to his own.
class employee { int empno; String name; String address; employee(int eno, String ename, String addr) { empno = eno; name = ename; address = addr; } disp() { System.out.println("eno" + empno + "name" + name); } } class worker extends employee { int hrsworked; worker(int eno, String ename, String addr, int hr) { super(eno, ename, addr); hrsworked = hr; } } class Test { public static void main ( String argv[]) { worker W1 = new worker(1,"abc", "xyz", 5); W1.disp(); } } Polymorphism-this is easily understood even when there are multiple a\search methods with same name but different parameter lists and performing different actions as Search (String name) or Search (int id, int salary)
by Peter Napthine - Thursday, 3 April 2008, 10:26 AM
CarYard.zip
These masteries are new to me, as up till now I have really only learnt and applied the SL skills. Hence I have found it challenging to grapple with these week 1 mastery factors and problems, especially the assignment problem. What I have learnt through this is that the best way of understanding them is by doing practical examples yourself (and by discussing problems/solutions with others), not by reading up on encapsulation, inheritance, polymorphism, parsing, hierarchical composite data structures, etc - which can be confusing. By solving problems and working them through then the concepts become clearer.
Hence, I have learnt that I would teach these skills by setting small practical activities and challenges (such as the ones we had this week) so that the students can learn through doing.
I would consider using some of these examples/activities, but also other ones as suggested by other participants on the course, or even new ones, trying to address contexts that my students can relate too. If the theme of the task interests them, then this will also help with motivation to solve the problem.
by Richard Jones - Sunday, 6 April 2008, 11:35 AM
Hi Peter
Thanks for your contribution; it is often good to be reminded of how these things look to a person who is seeing them through new eyes. It helps us get back to how our students are feeling.
Employee Project
by Thomas Wozniak - Thursday, 27 March 2008, 05:50 PM
I would say that the first way of creating the Employee project would show mastery (of course with some explanation) of two aspects, Encapsulation and Polymorphism. Encapsulation - each class has the data members and methods it needs for the project. Polymorphism - each class has a pay() method that works for that type of employee. The only class where that's different is the Casual employee, but still it gets the job done.
Inheritance - I would say that this is a trivial use of inheritance, due to the fact that the Employee class is really just a shell and the other classes don't really gain anything, except being able to classify their objects as Employees, from extending it. Would changing the Employee class to an abstract class change this at all in your opinion?
The second approach to this problem, having said that, would show mastery of Inheritance I would think. Each other class gains a lot from extending this form of Employee.
The second approach probably loses out on polymorphism, due to the pay() method being inherited by the other classes.
The second approach still show mastery of Encapsulation, however, with each class still having its own data members that it needs, and any methods, assuming they might need other methods, they need.
I prefer the second method of programming. It seems "slicker" in my opinion. There is probably a chance to gain all three aspects with the second option, and it makes the idea of inheritance much clearer for students.
by Richard Jones - Friday, 28 March 2008, 10:39 AM
Hi Woz
thanks for your comments which I think are on track, the second method is definitely more like mastery in code. Of course mastery also assumes that the candidate has a discussion or description in B1 that makes it clear they have given some thought to the design of the data structures.
I don't believe that making a class abstract affects the issue one way or the other, I'm thinking we are only really interested in the concrete implementations the student uses.
Examples of merging files
MergingFilesEva MS Word document - link on the File list page
Merging Files and parsing
by Thomas Wozniak - Thursday, 27 March 2008, 06:29 PM
Here's how I see the merge of two files working. If this is too simple, let me know.
Create FileReader for file A and B
Create FileWriter for file C, the merged file
read record a from file A
read record b from file B
while(a != null && b != null)
{
if(a is less then b)
{ write a to C; read a from A; }
else
{ write b to C; read b from B; }
}
if(a != null)
while(a != null)
{ write a to C; read a from A; }
if(B != null)
while(B != null)
{ write B to C; read B from B; }
close readers and writer
*******************************************
public void parse(String parseThis)
{
for(int i=0; i less than parseThis.length(); i++)
{
if(parseThis.charAt(i) == ' ')
System.out.println();
else
System.out.print(parseThis.charAt(i);
}
}
Monkeys and Ostriches with trunks??
by Peter Napthine - Saturday, 29 March 2008, 08:33 PM
04_ZOO.zip
OOP is new to me so I am going through the Encapsulation, Inheritance & polymorphism activities carefully and enjoying them.
However, I hit a problem with the Zoo activity - as I couldn't access the getTrunkLength accessor that I added to the Elephant sub-class, Zoo would give a compile error, unless I also added the trunk characteristics to the Animal class, which seemed silly, as there would be need no need for Monkeys and Ostriches to inherit trunk characteristics. mixed
even then it output 0 for the length instead of the correct value.
I reckon there is something blatantly obvious that I have missed, can anyone help? attached are the files and a word document explaining the problem in a little more detail...
thanks Peter
by Richard Jones - Sunday, 30 March 2008, 02:45 AM
04_ZOO_2.zip Hi Peter
Please see attached zip with altered code and comments which I hope prove useful to you.
Cheers Richard
by Peter Napthine - Monday, 31 March 2008, 11:01 PM=
Thanks Richard for your comments, seem to make sense...
however, using these files, it still seems to output the trunk length of 0.0 metres, and I am not sure what you have changed in either animal, elephant or zoo classes,.. the data members of the animal class are not private (except keeper), so I am wondering if these are the corrected files...?
Cheers Peter
by Richard Jones - Tuesday, 1 April 2008, 08:20 AM
zoo.zip Hi Peter
I must have inadvertently have zipped up the wrong project directory. Sorry about that, try this one.
Richard
by Peter Napthine - Wednesday, 2 April 2008, 06:36 PM
Thanks Richard,
yes this zip file had your corrections and worked fine. Thanks for all the comments and explanations in the code - really helpful. I think that grappling with this little challenge and the resulting problem, and then the working through the solution to it has helped me a lot with understanding these concepts.
Peter
Examples of parsing
CharParserEva MS Word document - link on the File list page
Programming Question
by Thomas Wozniak - Wednesday, 26 March 2008, 12:10 AM
Is there a reason that this...
protected void setKeeper(String name){ this.keeper = name; } would be preferable over this...
protected void setKeeper(String k){ keeper = k; } //where keeper is a private instance field in the class
Is either one really better?
by Jennifer Janesko - Wednesday, 26 March 2008, 05:24 AM
I thought that this would be an answer for Richard to answer, but my logic may be incorrect, so here it goes.
Suppose you had just written a long and involved program that was going to solve world strife. And, before you had properly regression tested it after your last changes based on user feedback, you were abducted by aliens.
NOW, suppose I was the lucky coder who got to fix those last bugs...
If you had provided code that used a variable such as:
void setKeeper(String name){ this.keeper = name;}
I would know that the string being passed to the method is some sort of name. Now, is it the first, the last, the name of the keeper's professional organization? I don't know... but, at least I have a place to start.
If you had provided code that looked like this:
setKeeper(String k){ keeper = k; }
The only thing that I would know is that k represents some sort of string. Hmmm... That could be any variety of things, actually. The world will have to wait a little longer to be saved...
I know that seems a little extreme. After all, yours is just a simple example. But, I have inherited too many programs where the former coder did not document nor use meaningful variable names, and I spent too much time determining what things meant in the programs. In some cases, I simply ended up rewriting programs to replace outdated code that was too cryptic to fix.
I tend to lean towards the extreme side, though: I force my students to give meaningful names to all of their variable names. If they always do this, then they do not have to decide which things are significant and which are trivial. One of the dossier criteria marks programming style. One of the descriptors includes having "having identifiers with meaningful names" (page 58). I like to point back to that when they get "lazy".
Also, if you look at Sun's guidelines for good programming style, they recommend: "Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters." http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367 It could be argued that your variable might be a throw away variable, but I'm not 100% sure there.
I also encourage my students to use "this" when it is appropriate. Partially because "this" becomes a running joke in the classroom, and partially because I can see when they are misusing it and make corrections.
by Richard Jones - Wednesday, 26 March 2008, 11:16 AM
Jenn has done a great job of explanation so there is not much more to say except perhaps this :-)
protected void setKeeper(String keeper){ this.keeper = keeper; } The this keyword will identify the Class data member and distinguish it from the local identifier supplied by the parameter of the same name.

