ava Programming History Part-5

Java programming topics
Post Reply
User avatar
shchamon
Sergeant
Sergeant
Posts: 20
Joined: Sat Apr 14, 2012 8:48 pm
Location: gobindogonj, bangladesh.

ava Programming History Part-5

Post by shchamon » Fri Apr 20, 2012 11:14 pm

A more comprehensive example:

Code: Select all

// OddEven.java
import javax.swing.JOptionPane;
 
public class OddEven {
    /**
     * "input" is the number that the user gives to the computer
     */
    private int input; // a whole number("int" means integer)
 
    /**
     * This is the constructor method. It gets called when an object of the OddEven type
     * is being created.
     */
    public OddEven() {
    /*
     * In most Java programs constructors can initialize objects with default values, or create
     * other objects that this object might use to perform its functions. In some Java programs, the
     * constructor may simply be an empty function if nothing needs to be initialized prior to the
     * functioning of the object.  In this program's case, an empty constructor would suffice, even if
     * it is empty. A constructor must exist, however if the user doesn't put one in then the compiler
     * will create an empty one.
     */
    }
 
    /**
     * This is the main method. It gets called when this class is run through a Java interpreter.
     * @param args command line arguments (unused)
     */
    public static void main(final String[] args) {
        /*
         * This line of code creates a new instance of this class called "number" (also known as an
         * Object) and initializes it by calling the constructor.  The next line of code calls
         * the "showDialog()" method, which brings up a prompt to ask you for a number
         */
        OddEven number = new OddEven();
        number.showDialog();
    }
 
    public void showDialog() {
        /*
         * "try" makes sure nothing goes wrong. If something does,
         * the interpreter skips to "catch" to see what it should do.
         */
        try {
            /*
             * The code below brings up a JOptionPane, which is a dialog box
             * The String returned by the "showInputDialog()" method is converted into
             * an integer, making the program treat it as a number instead of a word.
             * After that, this method calls a second method, calculate() that will
             * display either "Even" or "Odd."
             */
            this.input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));
            this.calculate();
        } catch (final NumberFormatException e) {
            /*
             * Getting in the catch block means that there was a problem with the format of
             * the number. Probably some letters were typed in instead of a number.
             */
            System.err.println("ERROR: Invalid input. Please type in a numerical value.");
        }
    }
 
    /**
     * When this gets called, it sends a message to the interpreter.
     * The interpreter usually shows it on the command prompt (For Windows users)
     * or the terminal (For *nix users).(Assuming it's open)
     */
    private void calculate() {
        if ((this.input % 2) == 0) {
            JOptionPane.showMessageDialog(null, "Even");
        } else {
            JOptionPane.showMessageDialog(null, "Odd");
        }
    }
} 
The import statement imports the JOptionPane class from the javax.swing package.
The Odd Even class declares a single private field of type int named input. Every instance of the OddEven class has its own copy of the input field. The private declaration means that no other class can access (read or write) the input field.
OddEven() is a public constructor. Constructors have the same name as the enclosing class they are declared in, and unlike a method, have no return type. A constructor is used to initialize an object that is a newly created instance of the class.
The calculate() method is declared without the static keyword. This means that the method is invoked using a specific instance of the OddEven class. (The reference used to invoke the method is passed as an undeclared parameter of type OddEven named this.) The method tests the expression input % 2 == 0 using the if keyword to see if the remainder of dividing the input field belonging to the instance of the class by two is zero. If this expression is true, then it prints Even; if this expression is false it prints Odd. (The input field can be equivalently accessed as this.input, which explicitly uses the undeclared this parameter.)
OddEven number = new OddEven(); declares a local object reference variable in the main method named number. This variable can hold a reference to an object of type OddEven. The declaration initializes number by first creating an instance of the OddEven class, using the new keyword and the OddEven() constructor, and then assigning this instance to the variable.
The statement number.showDialog (); calls the calculate method. The instance of OddEven object referenced by the number local variable is used to invoke the method and passed as the undeclared this parameter to the calculate method.
input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number")); is a statement that converts the type of String to the primitive data type int by using a utility function in the primitive wrapper class Integer.

Special classes

Applet
Java applets are programs that are embedded in other applications, typically in a Web page displayed in a Web browser.

Code: Select all

// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
 
public class Hello extends JApplet {
    public void paintComponent(final Graphics g) {
        g.drawString("Hello, world!", 65, 95);
    }
The import statements direct the Java compiler to include the javax.swing.JApplet and java.awt.Graphics classes in the compilation. The import statement allows these classes to be referenced in the source code using the simple class name (i.e. JApplet) instead of the fully qualified class name (i.e. javax.swing.JApplet).

The Hello class extends (subclasses) the JApplet (Java Applet) class; the JApplet class provides the framework for the host application to display and control the lifecycle of the applet. The JApplet class is a JComponent (Java Graphical Component) which provides the applet with the capability to display a graphical user interface (GUI) and respond to user events.

The Hello class overrides the paint Component (Graphics) method (additionally indicated with the annotation, supported as of JDK 1.5, Override) inherited from the Container super class to provide the code to display the applet. The paint Component () method is passed a Graphics object that contains the graphic context used to display the applet. The paint Component () method calls the graphic context drawstring(String, int, int) method to display the "Hello, world!" string at a pixel offset of (65, 95) from the upper-left corner in the applet's display.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!-- Hello.html -->
<html>
  <head>
    <title>Hello World Applet</title>
  </head>
  <body>
    <applet code="Hello.class" width="200" height="200">
    </applet>
  </body>
</html> 
An applet is placed in an HTML document using the <applet> HTML element. The applet tag has three attributes set: code="Hello" specifies the name of the JApplet class and width="200" height="200" sets the pixel width and height of the applet. Applets may also be embedded in HTML using either the object or embed element, although support for these elements by Web browsers is inconsistent. However, the applet tag is deprecated, so the object tag is preferred where supported.

The host application, typically a Web browser, instantiates the Hello applet and creates an Applet Context for the applet. Once the applet has initialized itself, it is added to the AWT display hierarchy. The paint Component () method is called by the AWT event dispatching thread whenever the display needs the applet to draw itself.


N:B:-Come back again :arrow:
Post Reply

Return to “Java Programming”