Saturday, May 28, 2011

How do I get or set the state of JCheckBox?

This simple example shows you how to get or set the state of a JCheckBox. The method to set the state is JCheckBox.setSelected(boolean) and the method for getting the state is JCheckBox.isSelected() which return a boolean value.

package org.best.example.swing;

import javax.swing.*;
import java.awt.*;

public class CheckBoxState extends JFrame {
    public CheckBoxState() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        //
        // Creating checkbox with text label
        //
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        //
        // Get checkbox selection state
        //
        boolean selected = checkBox.isSelected();
        if (selected) {
            System.out.println("Check box state is selected.");
        } else {
            System.out.println("Check box state is not selected.");
        }

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CheckBoxState().setVisible(true);
            }
        });
    }
}

OOPS Concept-Inheritence

Inheritance

IS-A relation:
• Also known as Inheritance
• By using extends keyword we can implement IS-A relationship.
• Re usability is the benefit of IS-A relationship
Inheritance is accepted up to certain level but after reaching problems. Because for every child class object, internally all the parent objects will be created in the inheritance tree.
In the real time it is recommended to have inheritance up to 8 to 10 levels only. Beyond that it is not suggestible.
is a relation Example:
class Superclass
{
void display()
{
System.out.println("Hi");
}
}
class InheritanceExample extends Superclass
{
public static void main(String arg[])
{
InheritanceExample ie=new InheritanceExample();
ie.display();
}
}
HAS-A relation:
• Also known as composition or Aggregation.
• By using new operator we can implement HAS-A relationship.
• Reusability(CBM->Component Based Model)is the benefit of HAS–A relationship.
• The limitation of HAS-A relation ship is , we are increasing the dependency between the classes. As a result, the maintenance of the code becomes complex or costly.
has a relation Example:
class Car
{
Engine e= new Engine();
. …….
.......
}
class Engine
{
m1(){}
m2(){}
}

Car class has Engine reference. Hence Car allowed using all the features of engine. But without Engine object we can’t create Car object.

Friday, May 27, 2011

OOPS Concept-Encapsulation

Encapsulation
Encapsulation Advantages:
Security
Easy to enhance
Maintainability
Modularity
Example:
class Sample
{
public int i=10;
public int getId()
{
return i;
}
public void setId(int x)
{
this.i=x;
}
}

The major limitations od encapsulation is,it increases the code (because we have to getter and setter methods for the data variables )and hence slows down the execution.

Tightly encapsulated class:
A class is said to be tightly encapsulated if and only if data members as the private
Check whether the fallowing classes are tightly encapsulated or not.
a)
class A
{
private int x=10;
public void setX(int x)
{
this.x=x;
}
public int getX()
{
return x;
} ------------------//tightly encapsulated

b). class A{ private int y=10;} -----//tightly encapsulated

c). class A{ private int x=20;} ----// A is tightly encapsulated, B is not
class B extends A{ private int y=30;}

d). class A{ int y=20;}
class B extends A{ private int z=40;}
class C extends B { private int l=50; }

• If the parent class is not tightly encapsulated no child class is tightly encapsulated.

Thursday, May 26, 2011

How to Autorefersh the Servlet

This code contains the how 2 auto refresh the servlet and add display how many time u refresh this page

package org.best.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AutoRefresh extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession();
Long time = (Long) session.getAttribute("time");
if (time==null)
session.setAttribute("time",new Long(0));
long temp = 1;
if (time!=null)
temp = (time.longValue())+1;
if(temp<5)
res.addHeader("Refresh","15");

out.println("<html><head><title>Client Refresh</title></head><body>");
out.println("Viewed this page " +temp+"time.");
session.setAttribute("time",new Long(temp));
out.println("</body></html>");
}
}

OOPS Concept-Abstraction

 
Hiding internal implementation is called Abstraction.
Advantages:
Security
Enhancement easy
we can enhance the internal implementation with out effecting outside world.

Wednesday, May 25, 2011

OOPS Concept-Object

Object:

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variables to access objects, the terms object and variable are often used interchangeably. However, until memory is allocated, an object does not exist.

Any language present objects and this should not be confounded with the most powerful concept of object-orientation.

In procedural programming, an object may contain data or instructions, but not both. (Instructions may take the form of a procedure or function.) In object oriented programming, an object may be associated with both the data and the instructions that operate on that data.

How an object is created depends on the language. In aprototype-based language (e.g.,JavaScript) an object can be created from nothing, or can be based on an existing object. In a class-based language (e.g- ,Java), an object is created as an instance (or instantiation) of a class. The class forms a specification for the object.

To give a real world analogy, a house is constructed according to a specification. Here, the specification is a blueprint that represents a class, and the constructed house represents the object.
Object example:

class Sam
{
int i=10;
void mone()
{
        System.out.println("java");
}
public static void main(String arg[])
{
Sam s=new Sam(); //Here is the Object
}
}
}