Ryan Harrison My blog, portfolio and technology related ramblings

Java - A better Hello World program?

For years now this has been the Java version of the famous ‘Hello World’ program that is often the very first program that new developers write:

public class HelloWorld {  
    public static void main(String[] args) {  
        System.out.println("Hello World!");  

But is this a really the kind of code beginners should be seeing when they begin the Java adventures - putting all the logic in the main method and making no use of object oriented practices? Surely it would be better to drive in the fundamental OOP principles from the very start? You see all too much people using loads of static methods and calling them all in their main method which by now spans a good few hundred lines of code. Maybe they get this idea from this ‘Hello World’ program?

If we are really following object oriented programming practices (which Java tries so hard to enforce), the ‘Hello World’ program should really look more like this:

public class HelloWorld {  
    public static void main(String[] args) {  
        new HelloWorld().printHelloWorld();  

    public void printHelloWorld() {  
        System.out.println("Hello World!");  

Now we are suddenly using classes and methods! Ok it’s a little more complicated for complete beginners to learn, but at least it gives the teachers more to talk about rather than trying to avoid the inevitable questions about the static keyword and that String[].

Traditionally the ‘Hello World’ program is normally the shortest amount of code possible to generate the necessary output. That’s fair enough and the first example does this fine, but for teaching practices the second program is so much better. Classes/methods are amongst the hardest concepts to teach new programmers so why not begin that journey from the very beginning? A lot of new Java developers try to force procedural code into a purely object oriented language. Most of the time it doesn’t end well at all. Maybe this could be avoided by skipping the whole concept entirely (the static keyword is the main culprit). Everything should be inside methods of a class - of which objects should be created and those methods called with them. Hopefully the main method would then never grow into a hundred line mess of logic.

Just a suggestion to any Java teachers out there. Although a little harder to teach, in my experiences I reckon this could be a pretty helpful change in the long run.

Read More

Java - Math Engine

This project is something that I have been working on practically since I started programming in Java. It’s been through a load of changes through the years, and in some cases complete rewrites. It started out as a simple stack based expression parser and evolved to use regular expressions and again into its current state which uses expression trees and a recursive descent parser. The expression parser has also been joined with many other packages to create the full mathematical library as it is today.

The full source code for the library is available in the GitHub repository.


A mathematical library complete with an advanced expression parser with custom functions and support for Vectors and Matrices. Includes packages for symbolic differentiation, numeric integration, equation solving, unit conversions and much more.


The Function class provides a common interface into many packages of this library. A Function models an equation of one variable that maps to a resulting Y value. To create a basic Function object use the constructor that takes a single String argument representing the desired equation:

Function function = new Function("x^2 + 8*x + 12");

To create more customised Functions, use the overloaded constructors where you can define the variable and the angle units that will be used when calculating any trigonometric values in the function:

Function function = new Function("x^2 + 8*x + 12", "x", AngleUnit.Radians);

By default the Function will have a variable of x and will use Radians if they are not explicitly specified in the constructor.

Once and instance of the Function class is created, you also have the ability to evaluate the function at a specified point (any instances of the variable will become the specified value during evaluation). For example:

Function function = new Function("x^2 + 8*x + 12");
double result = function.evaluateAt(12.5);


You can also evaluate the equation at another expression. This is better explained in an example:

Function function = new Function("x^2 + 8*x + 12");
double result = function.evaluateAt("6 + 6.5");



The differential package includes classes to both symbolically and numerically differentiate Function instances. Numeric differentiation will only allow you to differentiate at a certain point (giving the value of the derivative at this point). Conversely symbolic differentiation will return a new Function object representing the exact derivative to the Function. This resulting Function can then be evaluated as above to get the a value at a certain point (as in numeric differentiation).


Function function = new Function("x^2 + 8*x + 12");
DividedDifferenceMethod method = new DividedDifferenceMethod(function, DifferencesDirection.Central);

In this example a Function is first created by which the deriviatives values will be calculated. Then a new instance of a DividedDifferenceMethod differentiator is created that uses the the Finite Difference method with central equations. A result of the first derivative at the point 3.5 can be obtained by:

double derivative = method.deriveFirst();


Values can change depending on the supplied value of h (the difference). By default the value is 0.01. Other classes are available that can yield more accurate results in certain circumstances are also available which include the ExtendedCentralDifferenceMethod (more accurate than standard differences as above) and RichardsonExtrapolationMethod (which again can yield more accurate results).

The numerical methods only give estimates of the derivatives and in some cases may be wildly inaccurate. However for most well formed, continuous functions, and a sensible value of h, a reasonably accurate answer can be obtained. For exact results consider the symbolic package.


The symbolic differentiation package offers support to obtain exact derivatives of Function objects. Instead of returning a double value of the derivative at a certain point (as in the numeric package), the symbolic package returns a new Function object representing the exact derivative function. This can then be evaluated to get the derivative at a certain point:

Function function = new Function("x^2 + 8*x + 12");
Differentiator differentiator = new Differentiator();
Function derivative = differentiator.differentiate(function, true);

f(x) = 2*x+8

As you can see the symbolic differentiator returns a Function representing the equation of the derivative, which in this case is 2*x + 8. This function is then evaluated at the some point 3.5 to get an answer of 15. Note how this result is exact and without the small error retrieved from the numeric method of differentiation.

Read More

PHP - Stocks Portfolio Main Page

I have uploaded a website that I completed for coursework for a web development module. The website mimics a stocks portfolio of a user with an underlying database, and various pages to view, add, and query the stocks currently in the portfolio.

The main page offers a simple view of all the stocks currently in the portfolio. The values used do not currently update from any official source (as realtime stock pricing solutions can get extremely expensive). This page does a simple query of the underlying portfolio database, which holds the physical data about each stock, and displays them in a user friendly manner. The rows of the table are highlighted green or red depending on the price change of the stock (again this is not realtime, but was set explicitly when the stock was added). Here is a screenshot of my portfolio main page:

Stocks Portfolio Mainpage

As you can see the main page displays all the relevant data about each stock in the portfolio, including the sector, price, yearly range, and the market capitalisation. The name of the stock on each row is also a link to the relevant page on Yahoo! Finance where a user can find even more detail about that particular stock.

The main page of the system displays all of the stocks currently in the portfolio, however does not give any kind of logical order (they are in fact always in the order that they were added). It would therefore be helpful to have some way of viewing the best and worst performing stocks in the current portfolio.This is the job of the top and worst stocks pages. Visually they are very similar to the main page of the system, however their queries to the database are modified to only include the best/worst stocks correspondingly, along with providing a more logical ordering.

The top stocks page, which only displays the stocks in the portfolio whose price change is positive (i.e they are making money). As such they are all coloured green. The list if also ordered depending on how large the price change is. The stocks with the highest positive price change appear at the top of the table, and the lowest towards the bottom. Here is a screenshot from my portfolio:

Stocks Portfolio Top Stocks

In comparison the worst stocks page only displays the stocks in the portfolio whose price change is negative (i.e they are losing money). As such they are all coloured red. The list if also ordered depending on how large the negative price change is. The stocks with the highest negative price change appear at the top of the table, and the lowest towards the bottom. Here is a screenshot from my portfolio:

Stocks Portfolio Top Stocks

The site also features a login system which allows users to add a new stock to the current portfolio. This is perhaps the best part of the system and includes some cool features to aid users.

You have to be logged in to the system to be able to add a new stock to the portfolio. This is a very helpful feature for future versions where multiple portfolios could co-exist, however at this moment the fact that users have to login to add new stocks to the portfolio simply prevents the unwanted hassle from a load of bots endlessly submitting the page and adding new rows to the underlying database (which would in itself ruin the rest of the site). Here is a screenshot of the login page which is what the redirect brings you to:

Stocks Portfolio Login Page

The login page is quite simple - just a username and password field. The system currently only has one registered user with a username of ‘admin’ and a password of ‘pass’. Entering in this two values and then pressing the ‘Submit’ button should successfully log you in to the system. The page now includes one link that takes you back to the main portfolio page.

The add new stocks page can be accessed from the link at the top of the main portfolio page. Now when following this link, you shouldn’t be redirected to the login page as you have already successfully logged in. Instead the ‘Add a new Stock to the Portfolio’ should load.

This page allows the user to enter information about a new stock they wish to add to the current portfolio. In doing so the stock will appear in the main page along with either the top or worst stocks pages where relevant.

The site uses a few Javascript functions to improve the overall usability. Such an example is a script that makes sure that all the compulsory fields are filled in with numeric data (where necessary), along with a script that automatically calculates the price change given a current price and yearly low/high. The formula used not exact by anyway means. It just calculates the difference between the current price and the yearly average:

change = price - ((yearlow + yearhigh) / 2)

The change is re-calculated every time any relevant field is updated to improve responsiveness.

However the best feature of this page is how the fields can be all automatically filled given a stock ticker from the user. For example starting with a blank form, type YHOO into the ticker field, and click on the ‘Find information from ticker’ button. A piece of Javascript then takes this ticker and downloads all the relevant information from Yahoo! Finance. This includes the current financial data and company information. When done, all the fields will now be filled out with the correct information:

Stocks Portfolio Add Stock

When all of the fields are filled in, the ‘Submit’ button can be pressed to add the new stock to the underlying portfolio database table. Doing so will then make the new stock appear in all other pages.

Read More

WordPress Custom Text Editor Buttons

As a follow up to my previous post on adding a new custom inline code shortcode to the WordPress editor, I figured it what also prove helpful to add a custom button to the text editor itself to surround the current text selection inside the inline code shortcode tags.

Again, in WordPress this is pretty easy to do and can be accomplished in a couple lines of code. I found on my searches that there were many tutorials online about adding custom buttons to the WordPress Visual editor, however not so many about custom buttons in the Text editor - which is what I use most of the time when making posts. I did however find a short article which includes a quick and simple answer. The following code is taken from this post.

To add a new button to the text editor, simply open up functions.php, which should be located in your theme folder, and add in the following code (this example adds the inline code tags, but can easily be modified to insert any HTML or shortcode tags that you like). These button in the text editor are known as Quicktag buttons. In the code below replace the your_shortcode text with your custom shortcode.


function add_button() {

The parameters to the addButton method are:

  • Button HTML ID (required)
  • Button display, value=”” attribute (required)
  • Opening Tag (required)
  • Closing Tag (required)
  • Access key, accesskey=”” attribute for the button (optional)
  • Title, title=”” attribute (optional)
  • Priority/position on bar, 1-9 = first, 11-19 = second, 21-29 = third, etc. (optional)

The new button should then show up in the WordPress text editor. Clicking on it will add your chosen tags to the editor.

Wordpress custom button


Read More

Inline code in WordPress

When posting recently it’s become quite annoying that there is no default way of presenting inline code in posts. I’ve resorted to making the text italic, which does provide a small amount of differentiation, yet does not offer the quality provided in most programming forums where the text inside inline code is in a monospaced font and sometimes has a border.

Luckily though customising WordPress is very easy, and it requires a very small amount of code to achieve the desired effect for inline code.

First of all a custom CSS class needed to be added to the styles.css file of the WordPress theme. This is the class I used:

.inlinecode {  
    padding: 2px;  
    background: #FFF;  
    border: 1px dotted #000;  
    color: #000;  
    font-family: monospace!important;  

Which simply forces the text into a black monospace font and adds a small border around the outside. The only thing to do now is hook up WordPress to use this new style. WordPress uses shortcodes to accomplish this. Essentially you provide a code inside square brackets and then put the content in between an ending brace.

For example migrated over to Jekyll

For my site I use the il shortcode. We then need to tell WordPress what to do when it sees this new shortcode. In this case all that needs to happen is a span tag with the new CSS style is added to encapsulate the inline code. This is done through a simple PHP function:

function inlinecode( $atts, $content = null ) {  
    return '<span class="inlinecode">'.$content.'</span>';

Finally one more line is needed to hook this new function into wordpress with the chosen shortcode:

add_shortcode("il", "inlinecode");  

And that’s it. This is one of the benefits of using WordPress - it’s so easy to customise and there are a load of guides and tutorials online to help you do it. Here is the resulting inline code (although it has been used throughout this post already):

Some example inline code (in Jekyll)

I will also hopefully be gradually adding the use of this new inline code into previous posts in order to improve the overall presentation and readability.

Read More