Monday, May 28, 2012

PrintWriter


DOWNLOAD THE CODE: PrintToFile.java

The PrintWriter class contains 2 very useful methods:
  • print(String)
  • println(String)
These methods write a String to a file.
The println method adds the end-of-line character after the string.

The constructor of the PrintWriter class accepts a file to write to. As such, a File object has been declared. The constructor of the File object accepts a String file path of the file's location. It is interesting to note that if the file has not already been created, the PrintWriter will create it automatically.

The example below contains 2 loops. The first loop writes the numbers 1-10 (using the loop counter) to data.txt using the print() method. As such, each number is written immediately after the last. The second loop also writes the numbers 1-10 to data.txt but uses the println() method so that each number is written on a new line.


Here is the output:




Throws FileNotFoundException. If you do not implement exception handling for this exception, the compiler will force you to rethrow it.

And if you want to read text file from a file, here's a post about the Scanner class.

"PrintWriter (Java Platform SE 7 )" ( http://bit.ly/LsOVki )

Here's a cat:


Sunday, May 27, 2012

While loop

DOWNLOAD THE CODE: usingWhileLoops.java.zip

While is a type of loop. Statements inside of the loop will continue to repeat while the loop condition is true.

The while loop is primed with a boolean expression/condition. When the condition becomes false, the loop will stop.

while (condition) {
    // statements to loop.
}


The example below uses a while loop to increment the value of the int variable num. On each pass of the loop, num has 100 added to it. The loop will continue to execute while the value of num is less than or equal to the value of the int variable maxnum.


The output:


The loop condition can be any expression that return a boolean value.


"Java For Complete Beginners - while loops" ( http://bit.ly/JGnAII )

What is a Sequence Diagram?

A sequence diagram is a UML (Unified Modelling Language) diagram. It is used to show the interactions between objects in the sequence they occur.


Here is a class diagram of the objects in the system...

The sequence diagram below, models the interactions required to get the 'Message' from the SpecialPower object.



Where a class diagram shows the details of each class & the relationships between them; a sequence diagram shows the sequential interactions between these objects.


Lifelines
Each object has a lifeline. This is a dashed line. The solid areas denote when an object is active in a process.




Numbering

Notice that each interaction in the sequence diagram is numbered according to the sequential flow of execution.



Void


In design documentation, where no value is returned from an interaction, the word 'void' is often used. A 'void' return is often seen as the result of instantiating an object. This is because a constructor cannot return a value. As well as that, you could expect to see 'void' as the return from a sub procedure.



"UML basics: The sequence diagram" ( http://ibm.co/Lk8yqN )

err....here's a cat:


Sequence

"MyVBBlog: Sequence" ( http://bit.ly/JmnK6J )