FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

Java Tutorial: Java Basic Programs Part I

gravikumar
2/11/2005 4:56:30 PM, Views: 4001
The following Java basic programs will be very helpful for the beginners. The java basic programs is an easy to understand copy paste code! Enjoy learning java.

Java Sample Program: 1

hello1.java

/* this is a `comment. you can tell because the lines start with `//. these lines are all ignored by the java compiler, so you can write whatever you like. at the very least you should start with a comment saying what the program does

note that this program has far more comments than real program statements; this would probably not be the case in most real programs.

The first real (i.e., non-comment) line of the program is `import this tells the compiler what classes to use apart from any defined in this program. in this case i tell the compiler to consider using any of the classes that are part of the `package java.awt.
and the class `appleti have to do that because the program uses the class `applet to do most of the work, and this class is defined in the java.applet package and the java.awt package. most java programs will start with an `import statement */

import java.applet.applet;
import java.awt.*;

/* so now down to work. define a class. all java programs have at least one class. in this case we are writing an applet, so the new class is a type of applet. `extends means, essentially, `is a type of. note that java rules stipulate that a `public class must be defined in a file of the same name, i.e., hello1 must be defined in `hello1.java */

public class hello1 extends applet

/** the open brace below denotes that all the statements that follow are part of the class hello1, until the matching closing brace at the end of the program **/

{

/**now we define an operation called `paint. providing this operation ensures that when the program starts up, something useful will happen. if we do not provide a `paint operation, then the program will compile and run correctly, but it wont display   anything at all. the concept of an `operation will be covered in more detail later in the course**/

public void paint (graphics g)
   {

** now the program text is `indented, this is, all the lines start a few spaces from the left margin. doing this makes it easy for the (human) reader to identify all the lines that are part of `paint. the computer does not care about this, but people will find the program easier to understand

`drawstring is an operation in the class called graphics. its job is simply to display text in an area of the screen. in this case the text is positioned 20 pixels across, and 20 pixels down**/


   g.drawstring ("welcome to int4120", 20, 20);
   }


/** this final brace denotes the end of the class `hello1 and, in this case, the end of the program **/

}


Java Sample Program: 2

add1.java
/*A program that adds 2 and 2 like most programs that use applets, we need the following two lines to tell the compiler what an applet is and how it works. */

import java.applet.applet;
import java.awt.*;

/* as in all java programs we have to define a class. i am calling this class `add1, so this program must be in a file called `add1.java */

public class add1 extends applet
{
public void paint (graphics g)
   {

   // ``int is short for `integer, meaning a whole number.
   // so this line defines a thing called ``result which stores
   // the result of adding 2 and 2.

   int result = 2 + 2;
   
   // the next line displays the text ``2+2= on the screen

   g.drawstring ("2+2=", 20, 20);

   // this line displays the result of the addition on the
   //   next line.

   g.drawstring (integer.tostring(result), 20, 40);
   }
}


Java Sample Program: 3

add1_1.java
// A program that adds 2 and 2

import java.applet.applet;
import java.awt.*;

public class add1_1 extends applet
{
public void paint (graphics g)
   {
   int fred = 2 + 2;
   
   g.drawstring ("2+2=", 20, 20);

   g.drawstring (integer.tostring(fred), 20, 40);
   }
}
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Avg Visitor Rating: Average Visitor Rating is 4 out of 5
Number of Ratings : 4 Votes
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
Java RSS Feed
gravikumar
Most Popular Tutorial
Most Popular Solution
Top Rated
Top Rankers
Overall
1. jawahar (250)
2. gravikumar (200)
Yearly -2008
No Rankings!
Expertsforge Sponsors
bnrtop