Article:
  Rethinking the Java Curriculum: Goodbye, HelloWorld!
Subject:   The first program needs to show it all
Date:   2002-08-28 21:13:43
From:   porterskog
"Hello World" is just the sound check, and has no pedagogical value.


We need to look at enriched environments in which to teach programming. The sparse environments bore the students. Enriched environments in which the students modify interesting programs to learn the various structures and techniques is much more interesting to students. I believe someone has already mentioned Robocode.


If you are using an IDE with a good step though debugger, I would avoid "cout" all together.


The following is a one file example that is a starting point for teaching that allows modification to learn all the control structures and allows introduction of objects, inheritance, and polymorphism.


A multi file example can place the arrayOfObjects in another file and "extern" them into the program, to show that you may not have control over the objects that are used in your porgram.




#include <iostream>


using std::cout;
using std::endl;


class CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CBaseClass. If the Sub classes "
<< "defines a function with this signiture/n"
<< "You should never see this!" << endl;
}


void nonpolymorphic()
{
cout << "I am CBaseClass. You should always see this!" << endl;
}


// For our example this prevents an object of type
// CBaseClass from being instantiaed
private:
virtual void purevirtual() = 0;
};


class CSubClass1 : public CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CSubClass1!" << endl;
}


void nonpolymorphic()
{
cout << "This is the nonpolymorphic all from "
<< "CSubClass1" << endl;
}


private:
virtual void purevirtual()
{
// It is now defined. Are you happy?
}
};


class CSubClass2 : public CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CSubClass2!" << endl;
}


void nonpolymorphic()
{
cout << "This is the nonpolymorphic all from "
<< "CSubClass2" << endl;
}


private:
virtual void purevirtual()
{
// It is now defined. Are you happy?
}
};


int main(int argc, char* argv)
{
int numberOfObjects = 2;


CBaseClass* arrayOfObjects[2];
arrayOfObjects[0] = new CSubClass1;
arrayOfObjects[1] = new CSubClass2;


for (int i = 0; i < numberOfObjects; ++i)
{
cout << "The value of the variable \"i\" is "
<< i << endl << endl
<< "The next statement is: arrayOf"
<< "Objects[i]->whatAmI()" << endl;
arrayOfObjects[i]->whatAmI();


cout << endl << "The next statement is: arrayOf"
<< "Objects[i]->nonpolymorphic()" << endl;
arrayOfObjects[i]->nonpolymorphic();


cout << endl << "End of this cycle through the loop" << endl << endl;
}


cout << "I am out of the loop" << endl;
return 0;
}



</code>

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • The first program needs to show it all
    2002-08-28 21:21:17  porterskog [View]

    I know that the code is C++, but break it out into a few files for the classes and get rid of the cout and pointers and your there. OOP is the same in both languages, just the libraries are different.

    My fingers just took over and banged out the C++ stuff, sorry.