Java applet – hello world

A java applet is a web object that allows you to do some funky things within a web page, you could even have a game on the screen with a java applet. But as normal you have to start from the start as such, the most used starts are the classic “hello world” example.

In a java applet, it is basically a class called Applet that you extend from and this has all of base code inside it so you only need to implement the virtual functions

  1. init
  2. start
  3. paint
  4. destory
  5. stop

And these are the basic functions that allow you to call other classes of your choice. The init – (initialize), setups up the environment of your choice, start is where the application (GUI interface) will run within, paint is what is painted to the applet screen, destory is when you destory the applet you may need to clean up some code, stop is when the application/applet is stopped.

The main basic class will extend from a applet (java.applet.Applet) so you will need to import that class library

import java.applet.Applet;
 
// basic class extends the applet
public class HelloWorld extends java.applet.Applet {

and then implement the functions start,init,stop,destory and paint if required. Below is more code that will resize the basic applet object and display (paint) the words Hello World using the Graphics class object drawString method.

import java.awt.Graphics;		// need this for the Graphics part of the paint function
import java.applet.Applet;		// to extend from.
 
public class HelloWorld extends java.applet.Applet {
 
    // resize the screen 
    public void init()
    {
	resize(150,25);
    }
 
    public void start()
    {
    }
 
    // draw with the Graphics class object a string on the canvas, point 50, 25
    public void paint(Graphics graphicsObj)
    {
	graphicsObj.drawString("Hello World!", 50,25);
    }
 
    public void stop()
    {
    }
 
    public void destroy()
    {
    }
}

Is you save that as “HelloWorld.java” and then you need to compile it into a class file (which is a common object file that java can read to run within the virtual machine).

javac HelloWord.java

which will output the HelloWorld.class file, which you will need to include into the html code applet tag, below is the HTML code

<HTML>
<head>
<title>A hello world</title>
</head>
<body>
Test output of the program
<applet code="HelloWorld.class" width=150 height=25>
</applet>
</body>
</HTML>

The output would be

Test output of the program

Note: If you look at the source code of this page, it will say

<applet code="HelloWorld.class" codebase="/Appletsexamples/"  archive="HelloWorld.jar" width="150" height="25"></applet>

I had to add in the codebase and archive because of the hosting, to create a jar file you just need to do

jar cf <tarname> <file(s) name>
 
jar cf HelloWorld.jar HelloWorld.class

The jar file is basically a zipped archive.

const – constants in functions

Const is a constant type, it makes what is talking to a constant value. The value cannot be altered in anyway, a good reason for this could be for a constant string for error codes or if the value of a variable needs to be checked but not altered in anyway.

Pointers can be constants too and also there are many places that you can put the const type. Here are some examples

// constant integer value
const int constValue = 3;
// normal value.
int Value = 10;
 
//pointers can be constants too
const int * pConst = &constValue;
//the pConst is a pointer to a const int value, the value pointed to cannot be altered, but the pConst memory location can to where it is pointing to.
int * const pConst = &Value;
// here the pConst value pointed to can be altered, but the memory location for the pConst for where it is pointing to cannot be altered (always pointing to the same place).
// of course you can have both.
const int * const pConstConst = &constValue;
// here it is a const int value that has a const pointer that is not allow to change.

You can also place const around the function definition as well, there are three places where you can place the const type.

const int returnValue(const int value1) const;

In the order of the const on the function definition list

  1. constant return type, cannot alter the returned value (unless you place it in a another variable)
  2. constant parameter passed, you cannot alter the value1 in this case
  3. constant “this”, this is the class that it is placed in and you cannot alter any values within that class

I have placed below some code that will hopefully explain more for the different types and also the error codes that come if you try and compile up if you are not obeying const rules and I placed the code below for what caused the error.

#include 

using namespace std;

// there are 3 different places you can put a const (constant)
// restriction on a function definition line.
// const int returnConstInt(const int value1) const
// {
// }
// in order of placement in the function definition line
// 1: cannot alter the returning value
// 2: cannot alter the passing value
// 3: cannot alter the values of "this" as being the class this

class constTest {
private :
int value;
public:
constTest() { value = 0;};

int returnIntConst(int passingValue) const;
};

// cannot alter any value for the class variables e.g. value in this case.
// of course you could do a const_cast.. which takes off the constant (const) type
int constTest::returnIntConst(int passingValue) const
{
// cannot alter any value inside the function
// assignment of data-member

Pointers and references – part 2

As a follow on from the part 1 – pointers and references – this tutorial is all about how to pass pointers and references, with usage in functions.

As shown in the previous tutorial, a pointer is just a point to another memory location where the data is actually stored and a reference/address is the address (memory) location of a variable. e.g.

int *pInt= NULL;
int intValue = 10;
pInt = &intValue;

Here the pointer (*pInt) is first set to NULL, to point to no where in memory because it may have a rogue data in there and you would not want that. Then the intValue variables is set to 10 and then pInt value is set to the memory (address/reference) location of intValue.

When passing a parameter to a normal function you are just passing a copy of the value and not the actual variable being passed, so that if you make any alternations to that parameter variable is it not reflected back into the variable that was actual passed. I have included code at the bottom with the output that displays the memory locations of the local (function variables parameters) and the parameters passed and you can see that the memory location is different in the standard copy-value function (normalReturnMethod).

So for a coding example

int normalReturnMethod(int value)
{
    value = value +2;
    return (value);
}
 
int intValue = 10;
 
int newValue = normalReturnMethod(intValue);
cout << "new Value :  " << newValue << " intValue  : " << intValue;

and the output would be

new Value = 12 int Value : 10

Because even though in the function we are incrementing the variable value, it has not direction relationship with the intValue that was passed to it.

But if you did a pointer (*) in the parameters of the function, then the actual variable memory location is passed and not just a copy of the variable, in which case you can alter the variable.

For example in code

void pointerReturnMethod(int *pValue)
{
    // increment the value pointed to in the pValue parameter
    *pValue +=2;
}
int intValue = 10;
pointerReturnMethod(&intValue);
cout << "intValue = " << intValue;

and the output would be

intValue = 12

Because the function is talking to the parameter variable passed, instead of just a copy and you can do anything on that parameter as you would normally.

The other method is by passing a reference to the function instead of a address (memory) location or copy-of-value, this one is slightly more fun, because since you do not alter the way that you knowing are passing the variable e.g. passing in the memory location with the &, then you could think it is just the copy-of-value function and it may alter the passing variable as well, which really is not a good thing.

Code example

void referenceReturnMethod(int &fValue)
{
    fValue +=4;
}
 
int intValue = 10;
referenceReturnMethod(intValue);
cout << "intValue = " << intValue;

and the output would be

intValue = 14;

which if you was not wanting to pass the reference to the variable to be “worked on” as such, then you may hit some problems with debugging and start to wonder why the variable was alter when passed to a function.

Here is some more code that you can compile up and see what is happening. have fun.!!

#include <iostream>
 
using namespace std;
 
int normalReturnMethod(int value)
{
    cout << "normal Return Method memory location " << &value << endl;
    return (value + 2);
}
 
// pass in the pointer to a variable, the pointer (*)value is the same place 
// instead of a copy as normalReturnMethod parameter
void pointerReturnMethod(int *pValue)
{
    // increment the value pointed to in the pValue parameter
    *pValue +=2;
    cout << "pointer Return Method memory location  " << pValue << endl;
}
 
// pass in the reference to the method in the parameter, the reference is 
// a reference point to the passed in variable e.g. same place.
void referenceReturnMethod(int &fValue)
{
    fValue +=4;
    cout << "reference Return Method memory location " << &fValue << endl;
}
 
int main()
{
    // the standard way to update a value is to return a value
    // this is called the copy-variable way - it copies the value from the passing parameter to the function parameter
    int returnValue = normalReturnMethod(10);
    cout << "Hex value of memory place of returnValue " << &returnValue << endl;
    cout << "The new interger value " << returnValue << endl;
 
    // but if you pass the reference of variable &, this will alter the actually variable instead of needing to 
    // pass the variable back in the return type. - it passes a memory location reference
    pointerReturnMethod(&returnValue);
    // the returnValue has been altered.
    cout << "The new interger value " << returnValue << endl;
 
    referenceReturnMethod(returnValue);
    cout << "The returnvalue updated " << returnValue << endl;
    cout << "The memory address of the reference/pointer functions are the same as the returnValue hex value"<< endl;  
    cout << endl  << endl;
 
    int *valuePointer = &returnValue;
    // the find out the difference * and & here are the values
    cout << "The Hex value of memory place for returnValue and the VALUE of the memory place for the pointer are the same"<< endl;
    cout << "The value of the value pointer " << valuePointer << endl;
    cout << "Hex value of memory place of returnValue " << &returnValue << endl;
    cout << "The actually memory place of the valuepointer " << &valuePointer << " and the POINTED value " << *valuePointer << endl;
    cout << "And of course the valuepointer is the same, because it is pointing to the same place " << *valuePointer << endl;
 
 
    // the pointer (*) is the pointed to memory location. e.g. int value=10 could be a memory location that is pointed to
    // the reference (&) is the memory address of the variable
    // so for a pointer you need to set the memory location of a variable to its value, which in turn its 
    // pointed value is the variable, but its address/reference (&) is always the same becaues that is the place where
    // the pointer variable is setup, but the value (the memory location) is what alters.
    return 0;
}

and my test outputs would be, of course your memory locations will be different.

normal Return Method memory location 0x7fff34d8d1bc
Hex value of memory place of returnValue 0x7fff34d8d1dc
The new interger value 12
pointer Return Method memory location  0x7fff34d8d1dc
The new interger value 14
reference Return Method memory location 0x7fff34d8d1dc
The returnvalue updated 18
The memory address of the reference/pointer functions are the same as the returnValue hex value
 
 
The Hex value of memory place for returnValue and the VALUE of the memory place for the pointer are the same
The value of the value pointer 0x7fff34d8d1dc
Hex value of memory place of returnValue 0x7fff34d8d1dc
The actually memory place of the valuepointer 0x7fff34d8d1d0 and the POINTED value 18
And of course the valuepointer is the same, because it is pointing to the same place 18

Box model

Introduction

The box model within HTML is how the content and the area around it is defined. There is the content itself, paddings, border and margin’s. Each one can be altered and also each part of the top,bottom,left and right.

Image of the box model

box model in HTML
box model in HTML

Green : content
Blue : padding
red : border
yellow : margin

How to alter the each aspect of the model

To alter the margin for example you just need to define the actual HTML object that you want to “talk” to, so lets define that first.

<div id="alterhere">
hi there this is the content
</div>

and then to talk to the HTML object you just select it ( if was a class then you use “.” or if it was a id then use “#”, the way that I remember is that “.” is like class method call in c++/java/c# etc and “#” is what you could call a variable).

#alterhere 
{
margin : 20px;
}

means to have a full margin of 20px (pixels) but to pull out just the left part to be bigger then

#alterhere
{
margin-left : 50px;
}

so the margin left will now be 50px instead of 20px. You can do the same for padding as well, padding-left, padding-right, padding-top, padding-bottom.

Conclusion

The box model is very nice and also allows you to fully control the content and the box around it.

Function arguments – c++

There are sometimes that you want to pass more than a defined number of parameters to a function (arguments). Of course you can pass a array of parameters or just use a inbuilt stdarg.h library. This allows you to pass allot of arguments into a function, but without defining them at compile time.

For example if you want to add up a list of numbers and run time, but do not know how many to pass at the compile time then you define the function method as

int sumvalues(int numbersToBePassed, ...);

The important part is the “…”, you have to always pass in the first parameter at least, because at run time this parameter will tell the function how many additional parts to the function there will be.

Then to setup the argument list, you use the c++ library stdarg.h and this will include the function/methods that will allow this to happen.

To start with you will need to setup the argument list,

va_list args

This will setup the variable args to contain the list of additional arguments passed, then to setup where to start to pull arguments from, e.g. where the last parameter named at compile time will know, if the parameter list is like

int sumedvalues(int value1, int value2, int additionalValues, ...)

then the additionalValues is the last parameter that the compile time will know about, so to setup the pointer to the next parameter for the args variable

// for the above example
va_start(args, additionValues);
//for the code example
va_start(args, numbersToBePassed);

to pull out a argument you just need to use the va_arg function, its parameters are

va_arg(va_list, type);

where the va_list is the args variable and the type is the type that you want back, e.g. int or float.

To complete the argument list you need to end the argument pull by

va_end(va_list)

where again the va_list would be args in this example.

Here is some code that will demo the above as well.

#include <iostream>
#include <stdarg.h>
 
using namespace std;
 
int sumvalues(int numbersToBePassed, ...)
{
  int sumd =0;
  va_list args;
  va_start(args,numbersToBePassed);
 
  for (int i =0; i < numbersToBePassed; i++)
  {
    sumd +=va_arg(args,int);
  }
  va_end(args);
  return sumd;
}
 
int main()
{
  cout << "Add the values 5 4 3 5 together = " << sumvalues(4, 5,4,3,5) << endl;
  return 0;
}

With the output being

Add the values 5 4 3 5 together = 17

Regular expression – PHP

Regular expression is when you have a match condition and a string to search through. Here is a cheat sheet to download to view regular expressions syntax.

The regular matching method in php that I use is preg_match_all

int preg_match_all ( string pattern, string subject, array &matches [, int flags [, int offset]] )

which basically means that it will return a integer value of how many matches it has found within the subject text. The flags and offset are optional values.

Here is a example to pull out HTML A links within a text, A links start with <a and have some optional parameters e.g. href, title >text to display on the screen</a> (the closing tag. So in xml speak as such

<a[optional attributes]>value</a>

So the basics of searching for HTML A links within a string is to look for “” which then has the value and then the closing tag . So to put that in regular expression talk, to break it down we first search for the start <a followed by x amount of characters \s.* with a ending tag of <\/a> the “\” is there because the / is a condition statement for regular expression. And with this together it would look like

<a\s.*<\/a>

But because <\/a> happens twice in the string and either one can be the end point of the search since we are searching string values “\s.*” 0-x amount of them. \s means white space character “.” means any character that is not \n and “*” means 0 – more times of the previous check. So there is potential for a problem here we are not telling the regular expression to stop at the first < character and to this we use the [] to match some characters, the ^ in this context means any character not in this list about to come and "<" is the stopping character. So the full regular expression will be.

<a\s.[^<]*<\/a>

Here is the php code that will display all of the HTML A links from a string.

<?php
// here is a string with 2 a href links inside it and sounding text.
$matchAStr = "coding friends is my site :) <a href=\"http://www.codingfriends.com\">codingfriends.com</a> hi there.. " . 
	    " mountains are nice. <a href=\"http://www.norfolkhospice.org.uk/\">norfolkhospice.org.uk</a>" . 
	    " support the tapping house in Norfolk";
 
// the preg_match_all uses regular expression to match and pull out strings, $matches is the matches found.
 
$matched = preg_match_all("/<a\s.[^<]*<\/a>/",$matchAStr,$matches);
echo "There was $matched matche(s)\n";
 
echo "<ol>\n";
foreach($matches[0] as $url)
{
  echo "<li>".$url."</li>\n";
}
echo "</ol>\n";
?>
There was 2 matche(s)
<ol>
<li><a href="http://www.codingfriends.com">codingfriends.com</a></li>
<li><a href="http://www.norfolkhospice.org.uk/">norfolkhospice.org.uk</a></li>
</ol>