Monday, March 19, 2012

Java naming conventions

Because...
  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

...we follow a set of naming conventions when defining names for:
  • packages
  • classes
  • interfaces
  • methods
  • variables
  • constants

Oracle docs http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367


Variables and Data Types

We use variables to store values in a program. Values can have be of different types - for example a value may be a String (any collection of keyboard characters) or a number such as an int.

Why would you want to store values? What kind of values?

Things like:

  • username
  • email address
  • age
  • URL
  • game stats
  • price


The format for declaring a variable is as follows:

Type identifier;

The format for assigning a value to a variable is as follows:

identifier = value;

The format for declaring a variable & assigning a value to it in one statement, is as follows:

Type identifier = value;

Here are some concrete examples:



http://www.functionx.com/java/Lesson02.htm

http://www.tutorialspoint.com/java/java_basic_datatypes.htm

Java reserved keywords

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Java syntax cheat cheat

"Java Syntax Cheat Sheet" ( http://bit.ly/y7RduJ )

The Static keyword

The static keyword is a Java modifier which associates a class member or method with a class rather than an instance of a class.

For instance, the main method of a class is always static as there must be exactly one instance of the class containing main in order for the compiler to find it.

A class containing static members or methods does not require instantiation in order to use the static methods of the class.

Sunday, March 18, 2012

Scanner class - Part 1

DOWNLOAD THE CODE: Scanner.zip

The Scanner class has methods for reading a number of different types of data such as String, int, Double & Boolean.

By default, the Scanner class uses whitespace to delineate tokens (lines, words, phrases, values, etc).

We can use the Scanner class to read input from keyboard input or from txt files.

Use Scanner to read keyboard input
The example below uses the Scanner class to read keyboard input. Scanner contains several methods for reading different types of data. In this example, the next() & nextInt() methods are used to read the input String and the input int, respectively.


The output:




Using Scanner to read from a text file
The following shoppinglist.txt file contains a number of shopping items. Each item is followed by a number representing the quantity required.


Declare an instance of File and then declare an instance of the Scanner class. Pass the file instance to the scanner instance.

The Scanner class contains a number of Boolean functions to determine whether there is another token of a specified type. The example below uses a while loop in conjunction with the Scanner's hasNext() method of Scanner to determine if another token of any type exists.  In effect, the loop has been primed to 'continue looping while there are more tokens to read'. When there are no more toekn to read, the loop will stop.




The output: 



The final token in this example is the character 1.

By default, the Scanner's next() method is using the whitespace separating the characters in the file, to delineate is string. As such, any string separated by whitespace, is being read in each pass of the loop. The statement input.next() reads the next token in the file.

Let's change the code a little to get these values onto the same line.

The while loop body now looks like this:


The first call to next() reads in the name of the shopping item and is printed to screen via the print() method. The second call to next() is used to read in the quantity is appended to the next line via the println() method. 

The output now looks like this:


Read on to learn about using a nominated deliminator character with the Scanner class.



"Scanner (Java Platform SE 7 )" ( http://bit.ly/ABSuGl )


Simple tutorial using the Scanner class: "Java For Complete Beginners - user input" ( http://bit.ly/Lkr7uN )

"Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)" ( http://bit.ly/LkD03X )

"File (Java Platform SE 7 )" ( http://bit.ly/Jxdc9T )

"The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/LkGjIm )

System.in.read()

The System.in.read() function returns an integer (whole number) representing the next character's unicode code. As such, it is suitable in situations where you only need to read the first byte of input.


Returns the 7-bit ASCII code of the next character.