Sunday, 11 September 2011

class and object in java

/* A PROGRAM THAT USES Box CLASS.
CALL THIS PROGRAM AS BoxDemo.java */


class Box {
double width;
double height;
double depth;
}

// THIS CLASS DECLARES AN OBJECT OF TYPE Box.

class BoxDemo {

public static void main (String args[]) {

Box mybox = new Box();
double volume;

// assign values to the mybox's instance variables

mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;

// compute volume of box

volume= mybox.width * mybox.height * mybox.depth;

System.out.println("Volume of the Box is:" +volume);
 }
}

return in java

class Return
{
public static void main(String args[])
{
boolean t=true;

           System.out.println("Before return Statement");  
           if(t) return; // return is caller
       System.out.println("This line won't execute");
   }
}


Using break as a Goto satement in JAVA

/* Using break as a Goto satement:

Java doesnot have a goto statement, so we use break inted of Goto statement

*/

class Goto
{
public static void main(String args[])
{
boolean t=true;

first: {
   second: {
       third: {
           System.out.println("Before Break Statement");        
        if(t) break second; // break out of the second block
           System.out.println("This line won't execute");
              }
             System.out.println("This line won't execute");
           }
      System.out.println("This is after second block");
    }
  }
}