Posts

Showing posts with the label Java

Using fullpage.js to implement HTML page scrolling

Image
Some instructions on how to use fullpage.js to scroll between sections in your web page. I am not a web developer, so this post will contain a minimum of javascripting, css styling and so on. I just wanted to see how fullpage.js can be incorporated so that individual web pages can be scrolled horizontally in an animated way. Step 1: Download fullpage.js Fullpage.js javascript can be downloaded from here: https://alvarotrigo.com/fullPage/ This will download a file called fullPage.js-master.zip. Once the zip file has been downloaded, extract it somewhere. Step 2: Create folder structures for your web page(s) I have created a folder called 'FullPage' containing the subfolders 'html' and 'resources' in which the HTML file(s) and style sheets/javascripts will be contained respectively: Step 3: Install the necessary styling and resources. In the 'resources' folder create a further two subfolders 'css' and 'javascript'...

Applying the 2-opt algorithm to travelling salesman problems in Java

Image
This post tackles the problem of applying the 2-opt algorithm to travelling salesman problems in Java. The results of applying the 2-opt heuristic and applying it to a number standard traveling salesman test problems. are shown For a more in-depth description of the 2-opt heuristic, please refer to the following Wiki page: http://en.wikipedia.org/wiki/2-opt The actual 2-opt heuristic can be summarised by the following pseudocode steps, repeating for all feasible combinations of I and k: [code language="xml"] 1. take route[1] to route[i-1] and add them in order to new_route 2. take route[i] to route[k] and add them in reverse order to new_route 3. take route[k+1] to end and add them in order to new_route 4. return the new_route; [/code] A nearest neighbour search algorithm is included in the Java implementation. A comparison is made of the kind of results we get from the 2-opt algorithms, with and without improving the initial tour using the nearest n...

Displaying changing graphics in Java

Some hints on how to display changing graphics in Java using a simple example I have borrowed from the following site: http://www.dreamincode.net/forums/topic/30222-working-with-graphics-in-java/ I use the same approach to draw a number of concentric circles of differing sizes, but with a small delay in between each draw, refreshing the display at each iteration to give the illustration of the changing graphics. Full Java code follows: [code language="java"] import java.awt.*; import javax.swing.*; public class MainApp extends JPanel { private static final long serialVersionUID = 1L; static JFrame frame = new JFrame("BullsEye"); static int endpoint = 220; static int x, y; public static void main(String args[]) { MainApp eye = new MainApp(); frame.add(eye); frame.setSize(300,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (x = 20; x <= 110; x += 10) ...

Using Eclipse to create a model view presenter project in Java

Image
Some instructions on how to create a very simple Model View Presenter example in Java in the Eclipse development environment. A reference I found useful, from which I re-use all the code in this post: http://www.dreamincode.net/forums/topic/353210-swing-passive-model-view-presenter-in-5-minutes/ Step 1: Create a new Eclipse project In Eclipse select File > New > Java Project. Give your project a name and select the JRE execution environment. For this example I am using JRE-8: Step 2: Create the Model class Right-click the src folder in your Eclipse project. Select New > Class and name it 'Model': Model.java [code language="java"] public class Model { private String password; public Model() { password = "password"; } public void setPassword(String pass) { password = pass; } public String getPassword() { return password; } } [/code] Step 3:...

Creating a Windows application in Java using Eclipse

Image
Step 1: Create a new Java Application In Eclipse select File > New > Java Project: Step 2: Create a new Frame In Eclipse, right click the 'src' folder and select New > Other. In the wizard dialog that appears select WindowBuilder > Swing Designer > JFrame: Click Next. Give your JFrame a name: Click Finish. Note how the following code for gui.java gets generated: [code language="java"] import java.awt.BorderLayout; public class gui extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { gui frame = new gui(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public gui() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); conten...

Getting started with Java in Eclipse

Image
1. Download Eclipse Obtain the installer from the following site: https://eclipse.org/downloads/ and complete the installation: 2. Create a new Eclipse project Open Eclipse and select File > New > Java Project. Give the project a name ('HelloWorld'): Click Next. And then click Finish. 3. Add your Java class Select File > New > Class: Set the Name field to 'HelloWorld' and check the box labelled 'public static void main(String[] args)': Click Finish. So that your project looks like this: 4. Write your code In this example, the proverbial "Hello World" example: [code language="java"] public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } [/code] 5. Build and run your Java project: Select the down arrow next to the Run icon and select Run As > Java Application: Giving the desired "Hello World" console output: ...

Converting between binary and decimal representations of IEEE 754 floating-point numbers in C++, Java and Python

Image
This post implements a previous post that explains how to convert 32-bit floating point numbers to binary numbers in the IEEE 754 format. What we have is some C++ / Java / Python routines that will allows us to convert a floating point value into it's equivalent binary counterpart, using the standard IEEE 754 representation consisting of the sign bit, exponent and mantissa (fractional part).

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: