Adding Comments in Java

October 3rd, 2008

A comment is a text that’s outside the normal flow. The computer doesn’t act on this comment. There are no instructions for the computer to perform inside this comment. Instead, the comment tells other programmers something about your code.

Comments are for your own benefits, too. Imagine that you set aside your code for a while and work on something else. When you return later to work on the code again, the comments help you remember what you were doing.

Java programming language has three kinds of comments:

1. Traditional Comments: This kind of comment begins with /* and ends with */. Everything between the opening /* and the closing */ is for human eyes only. Nothing between the /* and */ get translated by the compiler. Below is an example.

/*Your comment here
Another comment here
Another comment here*/

2. End-of-line Comments: An end-of-line comment starts with two slashes, and goes to the end of a line of type. Below is an example.

//This is an example of an end-of-line comment

3. Javadoc comments: A special javadoc comment is any traditional comment that begins with an extra asterisk. Below is an example.

/**
*This is a javadoc comment
*/

Adding Comments in PHP

September 25th, 2008

Comments in PHP, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs


Here are two types of comments in PHP:

Use // to make a single-line comment
Use /* and */ to make a one or more line comment. Comments should be place between /* and */.

Below is an example:

<html>
<body>

<?php
//This is a single-line comment

/*
One or
more line
comments are placed here
*/
?>

</body>
</html>

This will have no output because they are all comments.

Basic PHP Syntax

September 25th, 2008
Basic PHP Syntax, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs

All PHP codes should start with <?php and end with ?>. PHP codes can be placed anywhere in the document.

You can also start with <? and end with ?> but for maximum compatibility, i recommend using the <?php and ?>.

Below is an example:

<html>
<body>

<?php
echo “Hello World”;
?>

</body>
</html>

Output: Hello World

Each php statement should end with a semicolon used as a separator to distinguish one set of instructions from another.

Declaring a Method with a Parameter

April 29th, 2008

Class declaration with one method that has a parameter.

// Class declaration with a method that has a parameter.

public class GradeBook
{
// display a welcome message to the GradeBook user
public void displayMessage( String courseName )
{
System.out.printf( “Welcome to the grade book for\n%s!\n”,
courseName );
} // end method displayMessage

} // end class GradeBook

Creating a GradeBook object and passing a String to its displayMessage method

// Create GradeBook object and pass a String to
// its displayMessage method.
import java.util.Scanner; // program uses Scanner

public class GradeBookTest
{
// main method begins program execution
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();

// prompt for and input course name
System.out.println( “Please enter the course name:” );
String nameOfCourse = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

// call myGradeBook’s displayMessage method
// and pass nameOfCourse as an argument
myGradeBook.displayMessage( nameOfCourse );
} // end main

} // end class GradeBookTest

OUTPUT:

Please enter the course name:CS101 Introduction to Java Programming
Welcome to the grade book forCS101 Introduction to Java Programming!

Declaring a Class with a Method and Instantiating an Object of a Class

April 28th, 2008

Class declaration with one method

// Class declaration with one method.
public class GradeBook
{
// display a welcome message to the GradeBook user
public void displayMessage()
{
System.out.println( “Welcome to Java Tutorials!” );
} // end method displayMessage

} // end class GradeBook

Creating an object of class GradeBook and calling its displayMessage method.

// Create a GradeBook object and call its displayMessage method.
public class GradeBookTest
{
// main method begins program execution
public static void main( String args[] )
{
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();

// call myGradeBook’s displayMessage method
myGradeBook.displayMessage();
} // end main

} // end class GradeBookTest

OUTPUT:
Welcome to Java Tutorials!

Displaying Text in Java

April 28th, 2008

We display text in java using the System.out.print and System.out.println. System.out.print displays a text without adding a new line after the text while the System.out.println adds a new line after the text.

Here is an example:

//text printing program

public class displayText
{
//main method begins execution of Java application
public static void main (String[]args)
{
System.out.print(”Welcome to Java Tutorials.”);
} //end method main

} // end class displayText

Output of the program:
Welcome to Java Tutorials.

PHP Counting Words in a String

April 26th, 2008
PHP Counting Words in a String, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs, php hacks, java hacks

Use a pattern to identify the individual words in the string, and then count how many
times that pattern recurs:

<?php
// define string
$text = “Fans of the 1980 group will have little trouble recognizing ?
the group’s distinctive synthesized sounds and hypnotic dance beats,?
since these two elements are present in almost every song on the ?
album; however, the lack of diversity and range is troubling, and I’m ?
hoping we see some new influences in the next album. More
intelligent lyrics might also help.”;

// decompose the string into an array of “words”
$words = preg_split(’/[^0-9A-Za-z\']+/’, $text, -1, ?
PREG_SPLIT_NO_EMPTY);

// count number of words (elements) in array
// result: “59 words”
echo count($words) . ” words”;
?>

PHP Splitting Strings into Smalller Chunks

April 26th, 2008
PHP Splitting Strings into Smalller Chunks, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs, php hacks, java hacks

Use the str_split() function to break the string into fixed-length “chunks”:
<?php
// define string
$str = “The mice jumped over the cat, giggling madly ?
as the moon exploded into green and purple confetti”;

// define chunk size
$chunkSize = 11;

// split string into chunks
// result: [0] = The mice ju [1] = mped over t [2] = he cat, gig
// [3] = gling madly …
$chunkedArr = str_split($str, $chunkSize);
print_r($chunkedArr);
?>

PHP Converting Between ASCII Characters and Codes

April 26th, 2008
PHP Converting Between ASCII Characters and Codes, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs, php hacks, java hacks

Use the ord() function to get the ASCII code for a character:

<?php
// define character
$char = “\r”;

// retrieve ASCII code
// result: 13
$asc = ord($char);
echo $asc;
?>

Use the chr() function to get the character corresponding to an ASCII code:

<?php
// define ASCII code
$asc = 65;

// retrieve character
// result: “A”
$char = chr($asc);
echo $char;
?>

PHP Removing Whitespace from Strings

April 26th, 2008

PHP Removing Whitespace from Strings, programming tutorials, php tutorials, php guides, php tips and tricks, programming tips and tricks, php mysql tutorials, php mysql guides, php mysql tips and tricks, java guide, java programming, java tutorials, php sample programs, java sample programs, php mysql sample programs, php hacks, java hacks

<?php
// define string
$str = “   this is a string with  lots of    emb e dd    ed whitespace    ”;

// remove all whitespace from the string
// result: “thisisastringwithlotsofembeddedwhitespace”
$newStr = ereg_replace(’[[:space:]]+’, ”, trim($str));
echo $newStr;
?>
To convert more then one whitespace to single whitespace

<?php
// define string
$str = “   this is a string with  lots of    emb e dd   ed  whitespace    ”;

// trim the whitespace at the ends of the string
// compress the whitespace in the middle of the string
// result: “this is a string with lots of emb e dd ed whitespace”
$newStr = ereg_replace(’[[:space:]]+’, ‘ ‘, trim($str));
echo $newStr;
?>