Wednesday, March 26, 2014

Installing plugins in Eclipse

How to install plugins in 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 plugin's update site
  • click Add

Once the list of available software has populated, select the components you wish to install and click Next. Accept any licence agreements and click OK. Eclipse will require a restart to complete the plugin installation.

Monday, March 24, 2014

Variable scope and lifetime


The scope of a variable refers to how visible it is to different parts of the program. A variable which is declared inside of a method is said to have local scope - this means that it is visible only within that method. A variable which is declared at the top of a class is said to have class-level scope.

In the example below, a variable called username has been declared twice - each has a different scope. The one at the top has class-level scope and the one in the main method has local scope. The compiler will first attempt to reference the variable locally and if one cannot be found, it will look for one at class-level.

As a result, when the main method references the variable called username, it will be looking at the local declaration and therefore print "KiddThunda". When the variable called username is referenced from methodOne(), "Straven" is printed because it is referencing the variable at class-level.





In the next example, width and height variables are declared and assigned values in the initialiseValues() method. 



Because the width and height variables are local to the initialiseValues() method, they cannot be seen from the calculateArea() method - or any other method.

If you require access to a variable(s) from many methods, you can increase the scope of the variable to class-level, as below:


(In this case, because the variables are being called from static methods, the variables are declared with the static keyword - but this is just incidental and not particular to the discussion on scope.)

The scope of a variable has a direct affect on where it can be seen in the program. Additionally, the scope of a variable affects the lifetime of the variable.

Lifetime refers to the period of time that a variable stores a value before having it reset.

Consider the following method which is using the variable numberOfClicks to keep count of the number of clicks. Syntactically correct, however, the logic is incorrect. Each time the method is called, the numberOfClicks variable is being re-declared and set to zero before the variable is incremented. This results in the variable always having a value of 1. We can say that the numberOfClicks variable has a lifetime of 1 method call.



The correct logic is below:


The scope of numberOfClicks has been increased to class-level, therefore extending it's lifetime. Now, the variable is declared and initialised once. When the countClicks() method is called, numberOfClicks is actually incremented. The numberOfClicks variable now has the same lifetime as the object in which it is contained (Scope3Demo).

It is a common scenario that you declare variables locally and then realise that the scope and lifetime of some of them will need to be increased, as your project grows.

Variables may also have block scope - meaning, the are used within the block of a loop or conditional (if) block.

The loop counter in a for loop is a good example of this:


Notice that the loop counter (x) can be accessed within the loop block but not outside of it. We can say that x has a lifetime of the length of the loop.

In the example below, the error is occurring because the variable called message has block scope - that is, the variable has been declared within the conditional block and cannot be seen outside of that block.




We can increase the scope of message if we need to use it beyond the conditional block:


Why the main method is static

In Java, there must be a main method in at least one class. This is the front door to the application. The runtime system begins with a call to main and in turn, the main method calls other functions of the program.

The main method is a special case and is always declared with the static keyword - this means that the method belongs to the class rather than to an instance of the class - there is only one.

All internal members (attributes) and methods which are called by main, must have the static modifier.

If you attempt to call a non-static internal method from a static method, you will get a compile error, as in below:


Notice that the call to the doSomething() method does not have errors. This is because it has been defined with the static keyword. The call to doSomethingElse(), however, has errors because it is a non-static method.

Similarly, attempting to call a non-static member from the static method will produce compile errors:



Notice that the call to the username member does not have errors. This is because it has been defined with the static keyword. The call to password, however, has errors because it is a non-static member.

This is different to an instance (a regular object) where there may be many instances. In this case, members and methods belong to each instance and are not static.

Sunday, March 9, 2014

Convert a negative number to a positive number


Code sample on pastebin

"Math.abs (Java Platform SE 7 )" ( http://bit.ly/1hWJjy9 )

Consider the following example where we want to calculate the distance between 2 axis coordinates.

int x1 = 200;
int x2 = 300;
int distance = x1 - x2;

The result is distance =  -100.

If we are simply interested in the distance and not whether the distance is a negative, we can use Math.abs to get the absolute (positive) value of distance, like so:

int distance = Math.abs(x1 - x2);

The result is distance =  100.

Literal



A literal is any number, text, or other information that directly represents a value.


Literal format of String

eg.
String s1 = "This is the literal format of String";

"This is the literal format of String" is the String literal.

This is as opposed to:

String s2 = s1;

where s2's value is a reference to another variable.


Literal format of int

eg.
int num1 = 500;

500 is the int literal.

This is as opposed to:

int num2 = num1;

where int2's value is a reference to another variable.


For a listing of literals for all Java types also see:
"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )

Friday, March 7, 2014

Using the Random class to generate a random int

Code sample at pastebin


The Random class

java.util.Random
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Importing java.util.Random

You will need the following imports statement at the top of your code file:
import java.util.Random;

Generate any random number

Random r = new Random();
r.nextInt();
generates a random number in the int range (-2,147,483,648 to 2,147,484,647).

Generate a random within a maximum range

Random r = new Random();
r.nextInt(max);

generates a random number in the positive int range, inclusive of 0 but exclusive of max.
eg.
r.nextInt(500);
generates a number from 0 to 499. If you want to include 500 in the range, add + 1.

r.nextInt(500 + 1);

Generate a random within a minimum and maximum range

Random r = new Random();
r.nextInt(max - min) + min;

generates a random number in the positive int range, inclusive of min but exclusive of (max - min) and then adds the minimum to correct the range.

eg.
r.nextInt(500 - 300) + 300;

Generates a number from 0 to 299. You can then add your minimum so now the number has the range of 299 to 599. Remember, the actual number will be exclusive of the max so if you want to include the max add + 1.

r.nextInt((500 - 300) + 1) + 300;



Here's a cat.