Java OOP program to repeat a number of “Hello World” or other user defined string.

class helloOBJ
{
    Integer count;
    String Greeting;
    
    helloOBJ() {
        count = 10;
        Greeting = "Hello World.";
    }
    
    void assignGreeting(String s) {
        if(s!="") Greeting = s;
    }
    
    void assignCount(Integer i) {
        if(i>0) count = i;
    }
    
    public void run() {
        for(Integer i=1; i<=count; i++)
        {
            System.out.printf("[%04d] %s\n", i, Greeting);
        }
    }
}

class helloAPP
{
    public static void main(String[] args)
    {

        Integer pramCount = args.length;
        helloOBJ myObj = new helloOBJ();

        try {
            if(pramCount>=1) {
                Integer j = Integer.parseInt( args[0] );
                myObj.assignCount( j );
            }
            if(pramCount>=2)
                myObj.assignGreeting( args[1] );
            myObj.run();
        } catch (Exception e) {
            System.out.println("An error occured, check arguments.");
        }

    }
}

Syntax

helloAPP [count [custom string]]

Linkable Object C++ Hello World

// thello.hpp
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class THello
{
	string TGreeting;
	unsigned long TRepeatCount;
public:
	THello();
	THello(string UserGreeting);
	THello(unsigned long Count);
	THello(string UserGreeting, unsigned long Count);
	void AssignGreeting(string UserGreeting);
	void AssignRepeat(unsigned long Count);
	void operator<<(string UserGreeting);
	void run();
};

//thello.cpp
#include "thello.hpp"

void THello::AssignGreeting(string UserGreeting)
{
	TGreeting = UserGreeting;
}

void THello::AssignRepeat(unsigned long Count)
{
	if(Count>0) TRepeatCount = Count;
}

THello::THello()
{
	AssignGreeting("Hello World.");
	AssignRepeat(10);
}

THello::THello(string UserGreeting)
{
	AssignGreeting(UserGreeting);
	AssignRepeat(10);
}

THello::THello(unsigned long Count)
{
	AssignGreeting("Hello World.");
	AssignRepeat(Count);
}

THello::THello(string UserGreeting, unsigned long Count)
{
	AssignGreeting(UserGreeting);
	AssignRepeat(Count);
}

void THello::operator<<(string UserGreeting)
{
	AssignGreeting(UserGreeting);
}

void THello::run()
{
	unsigned int i = 1;
	while(i<=TRepeatCount)
	{
		cout << "[" << setfill('0') << setw(5) << i << "] ";
		cout << TGreeting << endl;
		i++;
	}
}

This creates an object that can be linked to a main program containing all hello world services to a parent program.

#include "hellolib\thello.hpp"
#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	switch(argc)
	{
		case 1:
		{	THello World;
			World.run();
			break;
		}
		case 2:
		{
			long i = atol(argv[1]);
			THello World( i );
			World.run();
			break;
		}
		default:
		{
			long i = atol(argv[1]);
			string s = argv[2];
			THello World( s , i );
			World.run();
		}
	}
	return 0;
}

A sample program using that object.

C++ Hello World using OOP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

class hello
{
	unsigned int limit;
	exec();
	exec(int i);
	string greet;
public:
	hello();
	operator=(unsigned int i);
	run();
	run(int i);
};

hello::exec()
{
	cout << greet << endl;
}

hello::exec(int i)
{
	cout << "[" << setfill('0') << setw(4) << i << "] ";
	exec();
}

hello::hello()
{
	limit=10;
	greet = "Hello World.";
}

hello::operator=(unsigned int i)
{
	if(i>0) limit=i;
}

hello::run()
{
	run(1);
}

hello::run(int i)
{
	if(i<=limit)
	{
		exec(i);
		i++;
		run(i);
	}
}

int main(int argc, char *argv[])
{
	hello world;
	if(argc>1)
	{
		stringstream ss(argv[1]);
		int i;
		ss >> i;
		world = i;
	}
	world.run();
	return 0;
}

This Hello World program, illustrates a number of object oriented programming features of C++; including use of classes, method and operator overloading and recursive functions.

Hello World C++ with Objects

Ok… this is a “Hello World” program in C++ using objects. It illustrates a number of aspects of object oriented programming in C++ allowing you to get a quickstart into C++ programming. First there is the #include directives which tells C++ that the program uses two libraries, iostream and string (actually they specify the header files that define the libraries to be used). “iostream” provides input/output to streams, in this case we are sending a stream to the console. “string” is a string type library that provide a string datatype that is not native to the base C++, this allows us to manipulate strings in an easy manner.

#include <iostream.h>
#include <string.h>
using namespace std;

class oHello
{
    string hstr;
public:
    oHello();
    ~oHello();
    void print();
};

oHello::oHello()
{
    hstr = "Hello World\n";
}

oHello::~oHello()
{
}

void oHello::print()
{
    cout << hstr;
}

>int main()
{
    oHello myObject;
    myObject.print();
    return 0;
}

Class oHello is defined as having three methods, oHello method is the constructor that is executed when the object is created, ~oHello is the destructor that is executed when the program destroys the object (in this case, when the program terminates), lastly there is print which sends hstr to the output stream. These are declared as public so that they can be accessed from other modules in the program that are not members of the oHello object class. “hstr” is private to the class and only the class methods can access this data. It is of type string, the object string that was defined in the string class library.

The oHello method does one thing, assign hstr the value of “Hello World\n”. “\n” means add a carriage return to the end of the string. Now, when any member methods access hstr it would contain the value “Hello World\n”.

The print method contains the instruction cout << hstr, this calls the hstr object using the << method which directs it to send the value it contains to the cout stream class, which as stated earlier sends the data to the output stream.

Now that all the objects are defined… we can use them in the main() function which is automatically executed when the program is run. First we create an instance of the oHello object, called myObject. Next we call the print method of the myObject object, which prints “Hello World\n” to the output stream (in this case the console). Lastly since main() is a function it must return something before terminating, so we return the value zero.

Hello World ooREXX

Ok, as my previous post showed how to write a routine oriented hello world program in gforth, I now explain a hello world program using objects in object oriented rexx (ooRexx).

#!/usr/bin/rexx
/*
    Object Oriented Hello World in REXX
*/

myobject = .hello~new
myobject~print
exit

::class hello
 ::method init
 expose hellostring
    hellostring = "Hello World"
 return
 ::method print
 expose hellostring
    say hellostring
 return;

Like all rexx programs, the main() like section is the beginning lines of the program. The object definitions begin after the main instruction sequence. In this case we have one object called “hello”

::class hello

It contains two methods, a constructor and general purpose method named “print”. Both objects share a variable common to the object called “hellostring”, which contains the string we want the object to print. It is initially set by the constructor to the value “Hello World”. The print method does only one thing, to print the value of the variable “hellostring” to the console.

So, here we have an illustration of using objects, methods, constructors and internal data passing between objects in a rexx program. (Note, I am not going into the details of what is object oriented programming, as if you reading this you should have already studied what is object oriented programming).

The beginning part of the program does two things, create an object named “myobject” of the type “hello” and initialize it. The second instruction calls the “print” method.

Pretty simple.

Hello World Gforth (better example)

One of the issues of most of the information out there with respect to the forth language (and this includes books) is the lack of a clear illustration of how to write a routine and call it in the program in a easy to understand manner. They show how to write a routine and call it from another routine but no one tells you where your main program should be to call all the functions… do you create an main() like routine, do you put the instructions at the beginning and the routines after, or do you define the routines and call them after…. most of the forth manuals seem to skip this part in clear english. I am not a anything like an experienced forth programmer, but like all experimental programmers, I am interested in all programming languages, including forth… while most of what I might do is in other languages, I would like to say that I know something of forth, but most of the information out there leaves a lot to be desired when you do not “know” forth.

Anyway this is my illustration of routine calls in a forth program. Ye old “Hello World” program. I believe all first “Hello World” program in any programming language should illustrate how routines (subroutines or functions) call called.

: hello
." Hello World" cr ;

hello
bye

So what does this program do? First it defines a routine called “hello” that does only one thing, print the text string “Hello World” with a carriage return.

With respect to forth, the routine must be defined before being used, so it comes before any instruction that calls the routine. This is why the rest of the program follows the routine. BTW the routine definition is terminated by the semi-colon. (Note all colons and semi-colons are spaced away from any other element, as forth needs a space when parsing to determine it sytnax).

There is no main() like construct in forth, so the instructions are listed right after all routines are defined. We therefore have two instructions: “hello” and “bye”. “hello” calls the hello routine and executes it before passing it off to the next instruction “bye” which in gforth tells the forth environment to terminate, exiting the program.

There you have it, a gforth “hello world” program that illustrates the use of routines in the source program.