Tuesday, November 26, 2013
Abstract classes and methods
"Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/1gcH6gn )
Tuesday, November 12, 2013
Wednesday, November 6, 2013
Reflection: Instantiation
Class<?> cl = Class.forName("javax.swing.JLabel");
Constructor<?> cons = cl.getConstructor(String.class);
Object o = cons.newInstance("JLabel");
Tuesday, October 15, 2013
Exiting a Java program
The following statement causes a Java program to exit and close:
System.exit(0)
Thursday, October 10, 2013
Convert a Java project to a Maven project
This post has been moved to ec2pants
http://ec2pants.blogspot.com.au/2013/10/convert-java-project-to-maven-project.html
http://ec2pants.blogspot.com.au/2013/10/convert-java-project-to-maven-project.html
What is Maven?
This post has been moved to ec2pants
http://ec2pants.blogspot.com.au/2013/10/what-is-maven.html
http://ec2pants.blogspot.com.au/2013/10/what-is-maven.html
Making a dump file - MySQL
This post has been moved to ec2pants
http://ec2pants.blogspot.com.au/2013/06/backing-up-mysql-with-mysqldump.html
http://ec2pants.blogspot.com.au/2013/06/backing-up-mysql-with-mysqldump.html
Wednesday, October 9, 2013
Setting up Windows/Apache Tomcat development environment for Eclipse
This post has been moved to ec2pants
An Eclipse plugin selection
How to install plugins in Eclipse:
Android ADT plugin for Eclipse
- From the main menu > Help > Install new software > Add new
- enter the following:
- name: name of plugin (a description is fine)
- url: url of the update site
Android ADT plugin for Eclipse
Update site: https://dl-ssl.google.com/android/eclipse/
From the list of available software, select the following:
From the list of available software, select the following:
- Developer Tools
Install and then restart Eclipse.
Subclipse Subversion plugin for Eclipse
Update site: http://subclipse.tigris.org/update_1.0.x
From the list of available software, select the following:
From the list of available software, select the following:
- Subclipse plugin
Install and then restart Eclipse.
Maven plugin for Eclipse
Update site: http://download.eclipse.org/technology/m2e/releases
From the list of available software, select the following:
From the list of available software, select the following:
- Maven Integration for Eclipse
Install and then restart Eclipse.
Eclipse Software repository:
Update site: http://download.eclipse.org/releases/kepler/
From the list of available software, select the following:
From the list of available software, select the following:
- Web, XML, Java EE and OSGi Enterprise Development
Eclipse Web Tools
Update site: http://download.eclipse.org/webtools/repository/kepler/
From the list of available software, select the following:
From the list of available software, select the following:
- Web Tools Platform (WTP) 3.5.0
More information on installing Eclipse plugins:
Using GoogleCode repository with Eclipse
There are free code repositories such as GoogleCode for storing a project’s codebase. Github also has a free usage tier. The catch is that all hosted code must be open source. As such anything you store in the repository is public, and can be downloaded. You are, however, in control of who can check code in to the project.
In this tute, we are going to use GoogleCode with the Subclipse Subversion plugin for Eclipse.
Setting up a project at Google code
- Go to Google Code https://code.google.com/
- Click Create a new project
- Enter all fields as required -
- for version control system, enter Subversion
- for source code licence - enter Apache Licence 2.0
- Click Create Project
- You will then be presented with your project’s home page.
- Click on the source tab
- There are two svn checkout commands:
- The first one is the one you will use to check your projects in and out with - note that it includes a URI, and your username at the end of the command.
- The second one may be used by anyone to check out your project
- Bookmark this page.
Install the Subversion plugin into Eclipse
- From the main menu > Help > Install new software > Add new
- enter the following:
- name: Subclipse
- Click OK
- Allow the list of available software to populate
- Select/Accept/Download/Install the subclipse plugin
- You will be asked to restart. Do so.
Connect existing Eclipse projects to your Google code repo
- Create a new project or select an existing project (any type of project)
- Right click the project > Team > Share
- Select the Create a new repository location option > Next
- Enter the URL component of the SVN check in command (from your Google code repo)
- Click Next
- Leave the next lot of options as default > Next
Click Finish
- Enter your Google Code username and password:
- username: your email addresspassword: your Google code generated password (return to your Google code project source page and click on the googlecode.com password link to generate a password.
- Click OK
- Click OK on the next screen.
Notice that your project now has an *asterisk on it’s name. This is to indicate that the current version of the project has not yet been checked in to the repo. There is also repo location information next to the project name.
Your project and Eclipse is now ready to use the repo (it is not checked in yet).
Commit the project to the repo
- Right click the project >Team > Commit
- Click Select All
- You can enter a comment with the commit if you like.
- Click OK
- The local copy of the project is now committed to the repo.
- Your project icon now looks like this:
- Return to Google code and click Source > Browse
In the svn tree under directories, expand the trunk node.
You can click and view the java files in the src directory too!
Connect Eclipse to existing projects in the Google code repo
- Find the project you wish to check out on Google Code and select browse > source (you can use my code repo to test on)
- Copy the check out URL
- In Eclipse, from the main menu select Import > Other > SVN > Checkout projects from SVN > Next
- Select Create a new repository location > next
- Copy in/enter the project’s check out url for example
- https://my-diploma-workspace.googlecode.com/svn
- > Next
- The list should populate with all of the project folders available in the repository. Select the one/s you require and select Next > Next > Finish.
Here's a cat.
Tuesday, October 1, 2013
Google-gson User Guide
From https://code.google.com/p/google-gson/
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
Friday, September 20, 2013
Accumulator variables: Incrementing and decrementing
There is no actual such thing as an accumulator variable but it is a concept or rather a technique for using a normal variable to accumulate a value.
The following statement declares an int variable and assigns it a value of 0:
int num = 0;
if we then write another statement:
num = 10;
...what we have done is overwrite the value of num from 0 to 10.
But often, we want to increase the value of num with another number, rather than overwrite it's value.
In this case, we can use the add and (+=) operator, rather than the assignment operator (=):
int num = 0;
num += 10;
num += 3;
The value of num has accumulated a value of 13.
Consider the following example, which uses a for loop to accumulate a value:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num += 10;
}
The loop runs 10 times and on each pass of the loop, num accumulates another value of 10. The end result is that num has a value of 100.
In the next example, we use the Java increment operator (++) to increase the value of num by 1:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num ++;
}
You should also note that the loop counter itself makes use of the increment operator to add 1 to itself on each pass of the loop.
What about decrementing?
int num = 10;
num -= 5;
num -= 1;
Using the minus and (-=) operator, num now equals 4
and in a loop we might:
int num =10;
for (int x = 0 ; x < 10 ; x ++ ) {
num --;
}
Using the decrement operator (--), num equals 0.
What about Strings?
Use the add and (+=) operator to append 1string to another:
String name = "Sabrina";
name += "Albright";
The following statement declares an int variable and assigns it a value of 0:
int num = 0;
if we then write another statement:
num = 10;
...what we have done is overwrite the value of num from 0 to 10.
But often, we want to increase the value of num with another number, rather than overwrite it's value.
In this case, we can use the add and (+=) operator, rather than the assignment operator (=):
int num = 0;
num += 10;
num += 3;
The value of num has accumulated a value of 13.
Consider the following example, which uses a for loop to accumulate a value:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num += 10;
}
The loop runs 10 times and on each pass of the loop, num accumulates another value of 10. The end result is that num has a value of 100.
In the next example, we use the Java increment operator (++) to increase the value of num by 1:
int num = 0;
for (int x = 0 ; x < 10 ; x ++ ) {
num ++;
}
You should also note that the loop counter itself makes use of the increment operator to add 1 to itself on each pass of the loop.
What about decrementing?
int num = 10;
num -= 5;
num -= 1;
Using the minus and (-=) operator, num now equals 4
and in a loop we might:
int num =10;
for (int x = 0 ; x < 10 ; x ++ ) {
num --;
}
Using the decrement operator (--), num equals 0.
What about Strings?
Use the add and (+=) operator to append 1string to another:
String name = "Sabrina";
name += "Albright";
Simple Frames and Panels
CODE DOWNLOAD - FramesAndPanelsTute.zip
A JFrame is a Window that can contain other GUI components such as JPanel. We can override the JPanel's paint method to force the JPanel to draw the way we would like it to. In the demo code, the final result looks like the following:
A JFrame is a Window that can contain other GUI components such as JPanel. We can override the JPanel's paint method to force the JPanel to draw the way we would like it to. In the demo code, the final result looks like the following:
In this example, inheritance has been used to extend both the JFrame and the JPanel class. Both of these classes come from the Java Swing library.
Labels:
Demo Code Project,
extends,
Inheritance,
JFrame,
Jpanel,
paint,
Swing
Wednesday, September 18, 2013
Overriding Methods
A child class can override concrete methods from the parent. This is done by precisely duplicating the parent's method signature, in the child.
Let's override the toString() method which our class inherits from the Object class:
Let's override the toString() method which our class inherits from the Object class:
Here's the call:
Here's the output:
When overriding, ensure the method signature is reproduced perfectly.
Here's a cat.
Friday, August 30, 2013
HashMap
A HashMap is just like TreeMap except that a HashMap is not ordered by its keys.
See long post on TreeMap for methods and iteration technique. Go!
Tuesday, July 30, 2013
Monday, June 3, 2013
Anatomy of a simple Java file
When we write java code, we will save our code files with the .java file extension. These .java files are written using the syntax, rules and conventions of the Java programming language and are human readable.
Java ‘code’ must be transformed into a form of code which is readable or executable by the machine. This is achieved by compiling the Java code in the .java file to transform it into bytecode which is stored in .class files. Bytecode is the language which is understood by machines that have the Java Virtual Machine (Java) installed.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HelloWorld.java
The code above shows a simple example of a .java file. It contains the definition for a class called HelloWorld, a single method called main, and within that, a single statement which prints the words “Hello World!” to the console.
Java is case sensitive
Java is case sensitive! This means that main is different to Main.
All Java code is contained within a class
All Java code is contained within a class. The class begins with a class declaration. The .java file must have the same name as the class. The class declaration includes a set of curly braces, which contain all other code in the file.
public class HelloWorld {
}
You cannot omit either of the curly braces and you cannot place code outside of them either.
All Java apps contain exactly one main method
All Java applications contain exactly one main method. Think of the main method as the front door of the application. From the main method, program execution begins.
public static void main(String[] args) {
}
Additionally, the main method is able to accept a String array parameter. A String is any collection of alphanumeric characters (such as a file path) and an array is like a list of items.
(String[] args)
In this case we could say that the main method is able to accept a list of Strings. More on this later.
All methods are enclosed in a set of curly braces
All methods are enclosed in a set of curly braces (such as the main method above). There will be many sets of curly braces in your .java files. It is important that sets remain sets (don’t accidently delete a brace).
All method names are followed by parenthesis ()
All method names are followed by parenthesis - open and close round brackets. The parentheses contain parameters used by the method. Parameters are sometimes also called arguments. Not all methods use parameters but some methods may use one or more.
The example below shows an empty method (no code within the curly braces) called logout. The parenthesis following the world logout are empty. This method does not use any parameters.
public static void logout() {
}
The example below shows an empty method called login. The parenthesis following the world login contain two parameters; each is separated by a comma. This method uses the parameters to complete it’s task.
public static void login(String username, String password) {
}
All statements are terminated using the semi-colon ;
All statements are terminated using the semi-colon character. A statement is an instruction such as...
System.out.println("Hello World!");
Methods often contain many statements. The semi-colon ensures that all statements are separate.
// is used to comment the code
The double slash // is used to comment the code. Code commenting is useful as it provides internal documentation which allows yourself and others to read a description of the purpose of the code.
public class HelloWorld {
// This is the main method.
public static void main(String[] args) {
// Prints Hello World! to the console.
System.out.println("Hello World!");
} // Closes the main method.
} // Closes the class.
Comments are ignored by the compiler.
Subscribe to:
Posts (Atom)

