Saturday, September 4, 2010

What Are Packages?

What Are Packages?

A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all be to do with a specific application or perform a specific set of tasks. For example, the Java API is full of packages. One of them is the javax.xml package. It and its subpackages contain all the classes in the Java API to do with handling XML.
Defining a Package

To group classes into a package each class must have a package statement defined at the top of its .java file. It lets the compiler know which package the class belongs to and must be the first line of code. For example, imagine you're making a simple Battleships game. It makes sense to put all the classes needed in a package called battleships:

package battleships

class GameBoard
{

}

Every class with the above package statement at the top will now be part of the Battleships package.

Typically packages are stored in a corresponding directory on the filesystem but it is possible to store them in a database. The directory on the filesystem must have the same name as the package. It's where all the classes belonging to that package are stored. For example, if the battleships package contains the classes GameBoard, Ship, ClientGUI then there will be files called GameBoard.java, Ship.java and ClientGUI.java stored in a directory call battleships.

Saturday, May 15, 2010

String Function in Vb.net


Some String manipulation functions with example

  1. Getting the length of a string or variable
            Dim StrText As String
            Dim r As Integer    
            StrText = "How long is this text?"
            r = StrText.Length
            MsgBox(r)
 
  1. Getting the substring from a string or variable
            Dim r As String = "Welcome to the world"
            r = r.Substring(8)             
            MsgBox(r)
 
            Dim r As String = "Welcome to the world"
            r = r.SubString(8, 6)
            MsgBox(r)

  1. If you wish to search the text for a particular word, then you will use the IndexOf(Find word, StartPosition) function.

Dim r As String = "The weather is warm and sunny"

r = r.IndexOf("warm")

MsgBox(r)

This code starts at the beginning of 'The weather today is reasonably warm and sunny', because we didn't give a start position, and searches for the word 'warm' in it. If it does not find the word warm in the string, then it will return the value as 0, and you get a message saying '0'. However, if it finds the word, then it returns a number saying where it found the start of the word. In this case, you would see a message box saying '16' because the 'w' of warm is 16 characters into the string.

  1. Find and replace in a string

.Replace(search for text, replace with text) is used to search through a string, and replace certain words or characters with other ones. The Replace function returns the text that it has replaced.

            Dim i As String = "Only a fool goes outside in the rain"
            i = i.Replace("fool", "brave bloke")    
            MsgBox(i)