Posts

Showing posts from November, 2011

Polymorphism in Java

An example: [code language="cpp"] import java.util.*; public class JavaPolymorph { public void Print() { System.out.println( "JavaPolymorph"); } public static void main(String[] args) { JavaPolymorph j1 = new JavaPolymorph(); JavaPolymorph j2 = new SubJavaPolymorph(); j1.Print(); j2.Print(); } } public class SubJavaPolymorph extends JavaPolymorph { public void Print() { System.out.println( "SubJavaPolymorph"); } } [/code] Giving the output: JavaPolymorph SubJavaPolymorph

Java Threads: The Basics

Method 1: Write a class that implements the Runnable interface (i) Put the thread code in the run() method. (ii) Create a thread object by passing a Runnable object as an argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method. Like this: (new Thread(new MyThread())).start(); Simple code sample as follows: [code language="java"] package javathread1; class MyThread implements Runnable { public void run() { //Display info about thread System.out.println(Thread.currentThread()); } } public class JavaThread1 { public static void main(String[] args) { // Create the thread Thread thread1 = new Thread(new MyThread(), "thread 1"); // Start the thread thread1.start(); } } [/code] Method 2: Declare the class to be a Subclass of the Thread class (i) Override the run() method from the Thread class to define the ...

Mathematical Expression Parsers in Java and C++

Image
Basic Expression Parsing Click here for advanced expression parsing When writing your own calculator it is necessary to build a converter that can transform an input mathematical expression such as ( 1 + 8 ) – ( ( 3 * 4 ) / 2 ) , into a format that is more suited for evaluation by computers. When evaluating expressions such as the one above (known as “ infix notation "), that which appears simple and intuitive to us humans, is usually not so straightforward to implement in a programming language. The shunting-yard algorithm is a method for parsing mathematical expressions written in infix notation to Reverse Polish Notation (RPN) . The RPN notation is different to infix notation in that every operator (+, -, * etc) comes after the operands (numbers) and there are no parentheses (brackets). So ( 3 * 4 ) for example becomes 3 4 * . When given an input string in Reverse Polish Notation, it is then possible to employ a simple algorithm based around the use o...

Reading Text Files into String Arrays in Java

Programming Tip: Now you can load your essential programming tools such as emulators and IDE`s into the cloud with high performance citrix vdi from CloudDesktopOnline and access it remotely at your convenience on your preferred device(PC/Mac/android/iOS). If you prefer a gpu dedicated server, Try dedicated gpu hosting from Apps4Rent with 24*7*365 days top-notch tech-support and migration assistance. Some example Java code to read the contents of text file into a string array, line-by-line. Here is the Java class which is used to output the string array after the file location has been passed to it: [code language="java"] // ReadFile.java package javareadtextfile; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ReadFile { public String[] readLines(String filename) throws IOException { FileReader fileReader = n...

Java Collections: The Basics

One long section of code outlining how to use the Java Collections. As per the Java Strings post, this consists of one long code snippet outlining commonly used Java collections such as Hash Maps, Linked Lists etc. As this is ongoing, expect to see newer stuff added as time progresses.

Java Strings: The Basics

One long section of code outlining how to achieve some basic string handling objectives in Java. Currently trying to get to grips with this language after spending far too many years concentrating on C++. Each technique is demonstrated with a code snippet. As with a lot of my other stuff, any new stuff I find useful will get added at a later date:

Getting Started with Java in NetBeans

Image
1. Create a new project In the File menu, select New Project:

Getting Started with Java in Eclipse

Image
1. Open Eclipse and create a new project Select File -> New -> Java Project. Give your project a name and change the default locxation of the folder location, if desired. Click the Finish button:

Implementing Dijkstra’s Algorithm using Sedgewick's C++ Code

Image
Introduction Dijkstra's algorithm solves the shortest path problem for a graph with nonnegative edge weights, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms, the k-shortest paths algorithm, for example.

Priority Queues and Min Priority Queues in STL / C++

Image
Programming Tip: Now you can load your essential programming tools such as emulators and IDE`s into the cloud with high performance citrix vdi from CloudDesktopOnline and access it remotely at your convenience on your preferred device(PC/Mac/android/iOS). If you prefer a gpu dedicated server, Try dedicated gpu hosting from Apps4Rent with 24*7*365 days top-notch tech-support and migration assistance. Priority Queues A priority queue is just like a normal queue data structure except that each element inserted is associated with a ”priority”. It supports the usual push() , pop() , top() etc operations, but is specifically designed so that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. STL Usage In STL, priority queues take three template parameters: [code language="cpp"] template < class T, class Container = vector<T>, class Compare = less<typename...