FunLokam.Com  
      FAQ   Search   Memberlist   Usergroups   Register   Profile   Private messages   Watched Topics   Log in     
Free SMS to India
Orkut Heart Scrap Generator


 
Post new topic   Reply to topic    Thank Post    ..FunLokam.Com.. Forum Index Internet & E-Books
Board Alert Board Message
There are 9 posts to view in this topic if you are logged in.
Please Login or Register for checking all posts!!
Back to top  Login here and be redirected to this Topic Register
View previous topic :: View next topic  
Author Message
Salilrane
LKG
LKG
India

Gender: Gender:Male
Joined: 23 Oct 2006
Posts: 31

Bank: 0 FunDollar

Current Location: India
User Country: India
User's local time:
2012 May 23 - 10:51 PM
Reputation: 4.4Reputation: 4.4Reputation: 4.4Reputation: 4.4

65.1 FunDollar

Medals: None
Items


Salilrane is offline 

PostPosted: 23 October 2006, 3:00 pm    Post subject:
java tutorial
Reply with quote

Lesson One - Introduction


I like to keep our lessons informal. There is only one way that you can learn to program in a new
language and that is by doing it. You can read as many books as you like, watch any number of
presentations but in the end you still need to type the code.

You may be disappointed to hear that Java is not a GUI language. Sure we can use Java to make
some neat Web pages but essentially it is a character based language.

Java is unusual in that we use a compiler to compile our code and then use a interpreter to run it, we
will explain in greater detail later why this is so.

No first lesson in a new language could be complete without writing a "Hello World" program,
something simple to wet our appetites with.

You may like to create a new directory if you haven’t done, to keep all your code handy in the one
spot. What we need to create our code is a text editor. The Windows notepad editor is just fine, you
may use your favorite word processor as long as you remember to save you files as text.

WARNING - Do not use the DOS editor as it only allows you to save files as 8.3 format. The Java
source code must be saved with the .java extension.

Lets start.


Copy 'n paste or type this program into your editor.

/* Lesson 1 */
/** This is our Hello World application */

class Helloworld
{
public static void main (String[] arguments)
{
System.out.println("Hello World.");
// More comments
}
}


Hint - When learning something new I always find it better if I do the typing. Feel free to replace
"Hello World" with any other text you like.

Now what does all that mean ?.

The ‘Class’ statement transferred to English means, ‘this programs name is HelloWorld’.

The ‘main’ statement is the start of our Java application.

The curly brackets are used to group logical pieces of code together.

Hint - indent the layers of the brackets, it makes your code more readable and maintainable by other
programmers. The above piece of code is logically equivalent to the following code, which one would
you like to maintain.

/* Lesson 1 */
/** This is our Hello World application */
class Helloworld
{
public static void main (String[] arguments)
{
System.out.println("Hello world ");
// More comments
}
}

What are these things /* */, /** */ and //.

All of the above are comments, anything between the /* */ are ignored by the Java compiler. The /**
*/ is a documentation comment, the Java compiler ignores these also. The javadoc tool uses these
comments when preparing automatically generated documentation. // Anything after these characters
on a line are ignored by the Java compiler.

Save the program as text with the extension .java, the file name must be the same as the class name
including the capitalization.

All java programs must be compiled, go to the directory where the java code resides and type the line

javac Helloworld.java

from the DOS prompt if you are using Win 9x or the command prompt if you are using Win NT.

If the program compiles without any errors (the java compiler only complains if there are errors) a
new file is created called Helloworld.class.

If you have any errors check


Your spelling
Your capitalization
Your brackets
any semicolons.


Once your program has compiled without any errors then we are going to run it.

Type the line

java Helloworld

from the DOS prompt if you are using Win 9x or the command prompt if you are using Win Nt.

You should see the output

‘Hello World’.

Congratulations you have completed your first Java program. In our next lesson we will extend our
Helloworld program and convert it to an applet that we can run from the Web.
Back to top
View user's profile Send private message Yahoo Messenger
Salilrane
LKG
LKG
India

Gender: Gender:Male
Joined: 23 Oct 2006
Posts: 31

Bank: 0 FunDollar

Current Location: India
User Country: India
User's local time:
2012 May 23 - 10:51 PM
Reputation: 4.4Reputation: 4.4Reputation: 4.4Reputation: 4.4

65.1 FunDollar

Medals: None
Items


Salilrane is offline 

PostPosted: 23 October 2006, 3:00 pm    Post subject:
Reply with quote

Lesson Two - Our First Web Applet


Following on from our first lesson where we created our ‘Hello World’ application we will see what
else we can do with a simple java application, then we will convert our ‘Hello World’ application to
an applet and deploy it on a Web Page.

Lets start.

Cut 'n paste or type this program into your editor.

/* Lesson 2 */
/** This is our weight application */
class weight
{
public static void main(String[] arguments)
{
int lbs = 90;
System.out.println("Ally McBeals weight is now " +
lbs +
" lbs.");
}
}

Now what is new in this program?

We have declared a variable called lbs and it is an integer (a number to the laymen) and our output
consists of concatenated strings and the lbs variable.

Compile the application typing

javac weight.java

from the DOS prompt if you are using Win 9x or the Command Prompt from Win NT.

When you have no compilation errors then run it using

java weight

also from the DOS prompt if you are using Win 9x or the Command Prompt from Win NT.

You should see the output

‘Ally McBeals weight is 90 lbs.’.

You may disagree with what I think she may weigh so what we are now going to do is pass some
arguments to the application with what you think she may weigh.

Cut 'n paste or type this program into your editor.

/* Lesson 2 */
/** This is our weight2 application */
class weight2
{
public static void main(String[] arguments)
{
int lbs = 0;
if (arguments.length > 0)
lbs = Integer.parseInt (arguments[0]);
System.out.println("Ally McBeals weight " +
"is now " +
lbs + " lbs.");
}
}


Now what is new in this program.

We check if we have passed any arguments to the application by using the line ‘if (arguments.length >
0)’ and when then use ‘lbs = Integer.parseInt (arguments[0])’ to set the value of the variable lbs to
the parameter that we used on the command line.

Compile the application using

javac weight2.java.

When you have no compilation errors then run it using

java weight2 77.

You should see the output

‘Ally McBeals weight is 77 lbs.’.

Try it for yourself.

Now lets convert our weight application to a java applet.

/* Java 101 - Lesson 2 */
/** This is our weightapplet applet */
public class weightapplet extends java.applet.Applet
{
int lbs;
public void init()
{
lbs = 90;
}
public void paint(java.awt.Graphics g)
{
g.drawString("Ally McBeals weight is "
+ lbs + " lbs.",5,50);
}
}


Compile the applet using

javac weightapplet.java.

Applets cannot be tested using the java interpreter tool. You have to put them on a web page and view
them in one of two ways.


Use a web browsers such as the current versions of Netscape Navigator or MS Internet
Explorer

Use the appletviewer tool that comes with the Java Developers Kit.


<applet>
</applet>


Save the file as weightapplet.htm

Type the following line

appletviewer weightapplet.htm

and our weight estimate will appear in the viewer. We will discuss applets in more detail later in our
course.
Back to top
View user's profile Send private message Yahoo Messenger
Salilrane
LKG
LKG
India

Gender: Gender:Male
Joined: 23 Oct 2006
Posts: 31

Bank: 0 FunDollar

Current Location: India
User Country: India
User's local time:
2012 May 23 - 10:51 PM
Reputation: 4.4Reputation: 4.4Reputation: 4.4Reputation: 4.4

65.1 FunDollar

Medals: None
Items


Salilrane is offline 

PostPosted: 23 October 2006, 3:01 pm    Post subject:
Reply with quote

Lesson Three - Variables and Strings


Today we will cover what a variable is, how to declare it, Operators and using string variables.

Our lesson will cover five types of variables, not that this is all but with these five you will still be
able to do almost everything with Java, secondly we don’t want to overloaded you with variable types
you don’t need at this stage.

/* Lesson 3 */
class lesson3a
{
public static void main (String[] arguments)
{
// More Variables to follow
int lotsadigits;
float pi;
}
}

The piece of code above has declared two different types of variables.

int lotsadigits;

has defined a variable called lotsadigits that is an integer (ie a whole number) that can contain a
number in the range -2.14 billion to 2.14 billion.

float pi;

has declared a variable pi (remember back to High School when you had to calculate the area of a
circle) that is not a whole number e.g. 3.1428571428571428571428571428571.

If Java could only store numbers it would never have got out of the development lab.

/* Lesson 3 */
class lesson3b
{
public static void main (String[] arguments)
{
// More Variables to follow
int lotsadigits;
float pi;
char initial = 'E';
String surname = "Mcpherson";
}
}

When using char type variables you must use single quotes and when using Strings you must use
double quotes.

Cut 'n paste or type this program into your editor.

/* Lesson 3 */
/** This is our weight application again*/
class lesson3c
{
public static void main(String[] arguments)
{
float lbs = 90;
lbs = lbs + 10;
lbs = lbs - 20;
lbs = lbs / 2;
lbs = lbs * 3;
System.out.println("Ally McBeals " +
"weight is now " +
lbs +
" lbs.");
}
}

Compile the application using javac lesson3c.java.

When you have no compilation errors then run it using java lesson3c.

You should see the output

‘Ally McBeals weight is 120 lbs.’.

The last variable type that we will discuss for the moment is the boolean type, a boolean variable has
one of two states, ‘TRUE’ and ‘FALSE’. Some examples of boolean variable are, ‘Is the account
Overdrawn’, ‘Has the user pressed a key’, these are just two examples of boolean variables, we will
use boolean variables later in our class.

How to display special characters in Strings.

When a string is created or displayed it is enclosed in double quotes, what do we do if we want to
display the quotes, then we need to protect them with a \ character.

Cut 'n paste or type this program into your editor.

/* Lesson 3 */
class lesson3d
{
public static void main(String[] arguments)
{
System.out.println("Calista Flockart is " +
"the star of " +
"\"Ally McBeal\".");
}
}

Compile the application using javac lesson3d.java.

When you have no compilation errors then run it using java lesson3d.

You should see the output ‘Calista Flockart is the star of "Ally McBeal".’

Incrementing and Decrementing a Variable.

Because this is done so often there is a simplified way of doing it in Java. To increment the variable
x by one just use the following statement:

x++;

Conversely to decrement the variable x by one use the following statement:

x--;

Other special characters.

\’ Single quotation mark.
\" Double quotation mark.
\\ Backslash.
\t Tab.
\b Backspace.
\r Carriage Return.
\f Formfeed.
\n Newline.

Comparing strings:

To compare strings in Java we use the equals() method.

Cut 'n paste or type this program into your editor.

/* Lesson 3 */
class lesson3e
{
public static void main(String[] arguments)
{
String firstName = "Ally";
String firstGuess = "Fred";
String secondGuess = "Ally";
System.out.println("Ms McBeals firstname is " +
firstGuess +
"?");
System.out.println("Answer: " +
firstName.equals(firstGuess));
System.out.println("Ms McBeals firstname " +
"is " +
secondGuess +
"?");
System.out.println("Answer: " +
firstName.equals(secondGuess));
}
}

Compile the application using javac lesson3e.java.

When you have no compilation errors then run it using java lesson3e.

You should see the output ‘Ms McBeals firstname is Fred?’
Answer: False
You should see the output ‘Ms McBeals firstname is Ally?’
Answer: True

"How Long is a String ?"

To determine the length of a string in Java we use the length() method for the string.

Cut 'n paste or type this program into your editor.

/* Lesson 3 */
class lesson3f
{
public static void main(String[] arguments)
{
String firstName = "Ally";
System.out.println("The length of " +
"\"Ally McBeals\" " +
" firstname is " +
firstName.length());
}
}

Compile the application using javac lesson3f.java.

When you have no compilation errors then run it using java lesson3f.

You should see the output ‘The length of "Ally McBeals fistname is 4’.

Changing a strings case.

To convert a string to upper or lower case we use the methods toLowerCase() or toUpperCase()
respectively.

Cut 'n paste or type this program into your editor.

/* Lesson 3 */
class lesson3g
{
public static void main(String[] arguments)
{
String firstName = "Ally";
System.out.println("Lets convert "
+ firstName
+ " to lower case "
+ firstName.toLowerCase());
System.out.println("Lets convert "
+ firstName
+ " to upper case "
+ firstName.toUpperCase());
}
}

Compile the application using javac lesson3g.java.

When you have no compilation errors then run it using java lesson3g.

You should see the output

‘Lets convert Ally to lower case ally.'
'Lets convert Ally to upper case ALLY'.
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    Thank Post    ..FunLokam.Com.. Forum Index Internet & E-Books All times are GMT + 5.5 Hours
Page 1 of 1
Board Alert Board Message
There are 9 posts to view in this topic if you are logged in.
Please Login or Register for checking all posts!!
Back to top  Login here and be redirected to this Topic Register

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

..FunLokam.Com.. topic RSS feed
Page created in 0.152 seconds with 39 SQL queries
     ..FunLokam.Com..  Funny Pictures, Malayalam tattukada Jokes, Free SMS to India!     
© 2006-2011 FunLokam.Com   Powered by phpBB © 2001, 2002 phpBB Group   c3s Theme © Zarron Media   phpBB SEO   IP Country Flag 2.9.2