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.

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 )

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)