Monday, February 25, 2013

Unicode

"Unicode (The Java™ Tutorials > Internationalization > Working with Text)" ( http://bit.ly/13gWvIM )

"Unicode Chart" ( http://bit.ly/13gX0m1 )

Character wrapper class for char

"Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/LUgCyw )

Methods and method parameters

Consider a method (also called a behaviour) for logging on to a network. When a user logs on, we could reasonably assume that the user must provide a username and password. A method for logging on could look like this:


This method signature can be broken down as follows:

public - The visibility of the method. A method that is public (as opposed to private) can be seen by other classes.

void - This indicates that the method will not return a value. Some methods return values (as in the case of the Scanner’s nextInt() function).

login - This is the name of method. Method names are named in the same way as variables in that they should start with a lower case letter. If the method name contains serveral words, each subsequent word starts with an uppercase letter. Method names must be unique - except where method overloading is used.

(String userName, String password) - This is the parameter list for method. When a method requires some data to perform its task, a list of that data is specified. This parameter list contains 2 parameters (although a parameter list may contain more, less or no parameters). 

Each parameter has a name, a type and is separated by a comma.

Getting input

DOWNLOAD THE CODE: InputFromScannerPkg

The Scanner class
A class is a template for an object. A class contains methods for performing particular tasks. From one class we can create many object instances.

The Scanner class belongs to the Java library of classes. It contains methods for reading many different types of data, from different types of input.

System.out
You have already seen System.out in action when we print to the console. This is the default stream for output.

This statement causes the words “Print this’ to be printed to the console in Eclipse.

System.out.println(“Print this”);

What you cannot see, however, are the special line ending characters which this method places at the end of the line. When subsequent lines are printed using println(), they each begin on a new line.

Here’s a different way to print to the console:

System.out.print(“Print this”);

Notice that we are now using the print() method. This method does not include special line ending characters. Each subsequent line of characters that is printed using print() will begin immediately where the last line of characters left off.




and the output...


System.in
The following code example uses Scanner’s nextInt() method to read an int input from System.in (default input from console). The input is assigned to the int variable called aNumber.

Scanner myScanner = new Scanner(System.in);
int aNumber = myScanner.nextInt();


Scanner can be told to read different types of data - or tokens. By default, Scanner uses whitespace characters as delimiters of tokens (the characters that separate tokens).

In the case of the nextInt() function, Scanner is using whitespace to delimit tokens, and return the token as an int. (seen in the figure below).

Other Scanner functions of note include:

next() - Return the next token, of any type.
next(String pattern) - the next token is delimited by the pattern supplied as a parameter.
nextLine() - reads the current line and returns it.



and the output...

In the figures above, the user is prompted to enter information. Methods of the scanner class are used to read the input typed into the console, and assign it to variables. The variable values are then printed to the console.

You can read the entire Scanner class specification here:
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Java I/O and Import statements
When we wish to use Scanner:
At the top of the code file we import a reference to the Scanner class by using the line:

import java.util.Scanner;

This line says that we intend to use the class called Scanner from the package called java.util.

In Java, all input and output (I/O) is performed by importing methods from the java.io or java.util packages.

Any source of input or destination of output is called a stream: keyboard, screen, network connection, a file, etc., are all streams.

There are three predefined Streams already open and ready to use in every program:
  •     System.in
  •     System.out
  •     System.err



Object Instantiation
To use the Scanner class, we must first declare an instance of Scanner and give it a name. Similar to declaring a variable in that we declare a type and an identifier. In this case, the type is Scanner.

As well as being declared, an object also needs to be instantiated. The use of the new keyword, instantiates the object called myScanner from the Scanner class.

Scanner myScanner = new Scanner(System.in);

In the next statement, we declare an int variable and assign it the value of the next int that is returned myScanner’s nextInt() function.


int aNumber = myScanner.nextInt();

You will notice that when we instantiated a Scanner object, we have also included System.in, inside of the parenthesis (round brackets). This is an example of passing data to a method. Often, a method needs data passed to it so that it can perform its task.

System.in is the input stream from the keyboard, via the Java console, so, we are telling the Scanner object to use this as its source.

Variable Syntax

Every variable has a (data) type and an identifier.

Variable declaration syntax (single line):

Type identifier = value;

      or (assignment statement in 2nd line):

Type identifier;
identifier = value;

Naming variables
There are rules and conventions around how Java variables are named.

A Java variable name:
  • should begin with a lowercase letter
  • can contain numbers
  • can contain an underscore
  • must be less than 255 characters long
  • must not contain spaces
  • must not begin with a number
  • if the name consists of more than word, capitalize the first letter of each word

Case sensitive
All names in Java are case sensitive. This means that a variable named numberOfClicks is different to a variable named numberofclicks.


Variable Declaration

When we create a variable in our program, we are said to be ‘Declaring a variable’. Variables are declared in methods. Methods are named blocks of code which perform a particular task. Variables may also be declared in the body of a class - that is, within the class declaration, but not within the body of a method.




When we declare a variable, we are telling the program to remember a value. Of course, there are many different types of data so we need to select the most appropriate variable type to store that value in.


The statement below is a variable declaration for some String data called myPassword. The value it is storing is “secret’.


String myPassword = “secret”;





Assigning values - The assignment operator

When we assign a value to a variable, we use the assignment operator - the equals (=) character. 

The assignment operator takes the value on the right and assigns it to the operand on the left side to form an assignment expression.


In the example below, the value “GateKeeper99” has been assigned to the String variable called myUserName.


String myUsername = “GateKeeper99”;


The assignment operator is also used to change the value being stored by the variable, as follows:


int numberOfUsers = 100;
numberOfUsers = 101;


We are also able to declare a variable in one statement and assign a value to it, in another statement.

As follows:


int xCoordinate;
xCoordinate = 500;


This technique is often used where we need to increase the scope of a variable.


Variable scope
A variable may only be referenced from within the region where it is declared. This means that if a variable is declared within a method, it is only accessible from within that method. Also, you cannot reference a variable before it is declared. If a variable is declared in the class body, this variable can be accessed from all methods in the class.




Variable lifetime

Variable lifetime refers to the length of time that a variable stores a value. Depending on the scope, a variable may have a lifetime that lasts for the time it takes a single block of code to execute, or for the entirety of program execution (more on that later).

What is a variable?


Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.

Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.

There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.

For instance, in a Java program, we could do the following:

String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;


The words String and int are types of Java variables - or data types.

Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.

Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.

String whatTheDogSaid = “Right on partner number 2!”;

A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.

Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.

int numOfStudentsOnBus = 22; 
double dogWeight = 12.2;

The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.

A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.

There are many data types in the Java language!

Here's a cat:


Importing packages into Eclipse


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