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.

C++ Prime Numbers

#include <stdio.h>
#include <time.h>

using namespace std;

bool isPrime(long n)
{
	long i, m;
	m = n / 2;
	if(n % 2==0)
		return false;
	else for(i=3; i<m; i+=2)
		if(n % i==0)
			return false;
	return true;
}

int main()
{
	long j;
	clock_t tStart = clock();
	for(j=1;j<1000000;j++)
		if(isPrime(j))
			printf("%ld\n", j);
	printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
	return 0;
}

 

Chloe

' Chloe Program
data "C", "h", "l", "o", "e", " ", "G", "r", "a", "c", "e"
data " ", "M", "o", "r", "e", "t", "z", ",", " ", "I", " "
data "l", "o", "v", "e", " ", "y", "o", "u", "!", chr$(13), "I"
data " ", "w", "a", "n", "t", " ", "t", "o", " ", "m", "a"
data "k", "e", " ", "l", "o", "v", "e", " ", "t", "o", " "
data "y", "o", "u", "!", chr$(13), chr$(26)

dim a as integer
dim c as string * 1

const newline=chr$(13)
const endline=chr$(26)

do while c<>endline
	read c
	select case c
	case newline
		print
	case endline
	case else
		print c;
	end select
loop
end

 

Fractal Tree C++ Logo Output

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>

usingnamespace std;

class ifstree
{
public:
    float minfactor;
    float maxfactor;
    float bend;
    float angle;
    float initlen;
    int fdim;
    ifstree();
    void turnleft(float ang)
    {
        printf("lt %4.2f\n", ang);
    };
    void turnright(float ang)
    {
        printf("rt %4.2f\n", ang);
    };
    void movefw(float value)
    {
        printf("fw %4.2f\n", value);
    };
    void movebw(float value)
    {
        printf("bw %4.2f\n", value);
    };
    float randfactor();
    void draw();
    void draw(float linelen,int depth);
};

ifstree::ifstree()
{
    minfactor =(float)1/3;
    maxfactor =(float)2/3;
    bend =8.0;
    angle =40.0;
    fdim =8;
    srand( time(NULL));
    initlen =100.0;
}

void ifstree::draw()
{
    draw(initlen, fdim);
}

float ifstree::randfactor()
{
    float rnd =(float) rand()/RAND_MAX;
    return(float) rnd;
}

void ifstree::draw(float linelen,int depth)
{
    if(depth>0)
    {
       float minlength = linelen * minfactor;
       float maxlength = linelen * maxfactor;
       float difflength = maxlength - minlength;
       movefw(linelen);
       turnleft(angle+bend);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnright(angle);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnright(angle);
       draw(minlength+(randfactor()*difflength), depth-1);
       turnleft(angle-bend);
       movebw(linelen);
    }
}

int main(int argc,char*argv[])
{
    ifstree T;
    if(argc>1)
    {
        string svalue(argv[1]);
        float fv =(float)atof(svalue.c_str());
        T.initlen = fv;
    }
    T.draw();
    return0;
}

 

Javascript

I am planning to do practice some javascript programming at the console rather than embedded in a web page. My main system runs windows so it does support jscript in the windows scripting host. However, I want something better than what windows provides. One of the possibilities was Google’s V8 interpreter, the fastest javascript interpreter today. However, it only available in source code and requires compiling. The problem is it not easy to compile using MinGW or Digital Mars, as the source code is generally targeted for production on a linux environment. I tried searching for compiled binaries but no luck finding any and I have no desire to run two platforms to achieve my ends. However when I am on linux, there no such problem. But when I am on windows I want to have the same benefits of when I am on linux. The second choice was what is available from Mozilla, two products, Rhino and Spidermonkey. Rhino was pretty easy to setup but it is a java application, it will run on any system with a java runtime. However, I wanted something to run natively. Spidermonkey like V8 runs natively, however I was beginning to face the same level of complexity with V8. However, there were a few resources online that detailed all the steps in developing a windows binary, including installing some Mozilla specific support files etc. So it was looking like I was going to have to go down that path to get a javascript interpreter. That is, until, I found a website, which did have a compiled version of spidermonkey. A sigh of relief and I clicked the download. Opened the archive, which included three files, a text file listing command line options, the binary and a support DLL. I copied them into my windows directory so they be available to me wherever directory my command line is in.

Mission accomplished.

Mistake: I didn’t bookmark the website I found it…. lol.

 

ROT13 C++

The following program (written in C++) creates a filter program for converting ascii into ROT13 encoding. A very simple cypher where each character in the alphabet is replaced with the 13th letter from its position. To download a compiled windows compatible binary, click here.

#include <cstring>
#include <cstdio>
#include <cctype>

using namespace std;

char translate(char c)
{
    int cflag = 0;
    char oc = c;
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    int alphalen = strlen(alphabet);
    if(isalpha(c)) {
        if(isupper(c)) {
            cflag = 1;
            c = tolower(c);
        }
        for(int i=0;i<alphalen; ++i) {
            if(c==alphabet[i]) {
                oc = alphabet[(i+13)%alphalen];
            }
        }
        if(cflag==1) {
            oc = toupper(oc);
        }
    }
    return oc;
}

int main() {
    char c;
    while((c=(char)getchar())!=EOF) {
        putchar(translate(c));
    }
    return 0;
}