Sunday, April 29, 2012

Instantiation and object arrays

When we create an object from a class we are said to be 'instantiating an object from a class'.

Format:
ObjectType Identifier = new ObjectType(Parameters if any);

'new' is a keyword that calls the constructor of the class.

Consider the following class called NetworkUser:


To create an object from this class, we must declare a new NetworkUser. Each object we create from the class must have a unique identifier/name. The following code instantiates 3 NetworkUser objects, and calls a couple of their methods:


The following output is produced:



Object arrays
Alternatively, the NetworkUsers could be stored in an array and we could loop through each one to display their credentials:


The first statement instantiates an array of type NetworkUser. Only objects of type NetworkUser can be stored in the array. The array has a length of 3 which means that the first index (position) is 0 and the final is 2.

The next 3 statements instantiate a NetworkUser object directly into each index of the array. CurrentUsers[0] is a reference to the first index of the array called currentUsers. This is followed by the assignment operator (=) which is then followed by the object that will be stored in this index.

The for loop is primed to loop for as many times are there are indexes in the currentUsers array - the loop counter (x) starts at 0 (the same as the first index of the array) and is incremented on each pass of the loop by 1 until it reaches 2 (the final index of the array).

The print statements inside of the loop get the current NetworkUser in the array by using the statement currentUsers[x]. 

On the first pass of the loop, this will return the NetworkUser object at currentUsers[0].
On the second pass of the loop, this will return the NetworkUser object at currentUsers[1].
On the third pass of the loop, this will return the NetworkUser object at currentUsers[2].

All that's left to do is call the methods of NetworkUser. In this case, getUserName() and getPassword() are called.

Here's what the output looks like:


See more at pastebin.

Err, here's a cat (at animals[2]):











Convert String to int

You may convert a String to an int, provided the String contains number only.

The following example will work in cases where an int number is contained in the string called thisString:

String thisString = "100";
int x;
x = Integer.parseInt(thisString);

Java Basic Operators


Great resource for Java operators:

From http://www.tutorialspoint.com/java/java_basic_operators.htm:


Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:


Read more ...

Assignment Operators


Code file :: AssignmentOperators.java



From http://www.tutorialspoint.com/java/java_basic_operators.htm


Logical operators


Code file :: LogicalOperators.java



From http://www.tutorialspoint.com/java/java_basic_operators.htm


Relational operators


Code file :: RelationalOperators.java



From http://www.tutorialspoint.com/java/java_basic_operators.htm


Arithmetic operators

DOWNLOAD THE CODE: ArithmeticOpsPkg





From http://www.tutorialspoint.com/java/java_basic_operators.htm




Tuesday, April 24, 2012

Constructors and instantiation

"Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/I67fRB )

Every class has at least one Constructor. This is a special method that prepares the object for use. It has the same name as the class.


A class can have multiple constructors - this is known as method overloading.

The constructor is always the first method called on an object. Think of it as an 'on-switch'. When we instantiate an object of a class, we call it's constructor using the new keyword:

Syntax:
Type identifier = new Type(parameters, if any);

Example:
Telephone myTelephone = new Telephone();


In UML notation, the constructor is always referred to as new(). This is because UML is language independent and not Java specific. Different programming languages have different rules for naming a constructor.

Importing and Exporting Java Projects in Eclipse

"JIT Tutorial: Importing packages into Eclipse" ( http://bit.ly/Km6D5M )

"Importing and Exporting Java Projects in Eclipse" ( http://bit.ly/JxcNmZ )

Monday, April 23, 2012

JFrame

From Swing, JFrame is a top-level GUI container component.

"How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)" ( http://bit.ly/1eWsG71 )

http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html

JOptionPane

From Swing, a JOptionPane is a dialog window.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

The showInputDialog function returns a String:

String thisString = JOptionPane.showInputDialog("Please input a value");

If you need to use the return value as an int, you may use the following function:

int returnValue = Integer.parseInt(thisString);

(But if the string contains anything other than numbers !!! Your program will CRASH)

If the user presses the Cancel button, showInputDialog will return null.

The following snippet checks if the return is (not) equal to null:

if ((thisString != null) {
// Statements....
}

Swing

javax.swing

GUI Components

http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html

often used in conjunction with java.awt UI classes

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/package-summary.html

Tuesday, April 17, 2012

For-each Loop


"Java: For-each Loop" ( http://bit.ly/HThpAk )


For each Line in the array called myLine.

Friday, April 6, 2012

Updating a .jar file

"Updating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)" ( http://bit.ly/16Ut4zV )

eg.
jar uf /Users/username/Desktop/minecraft.jar /Users/username/Desktop/char.png