Tuesday, December 10, 2013

Method in JAVA : Day 3

a) How to access the instance variable using Method?


b) Write a program to calculate the Volume of a Box using Method.

8 comments:

  1. b))) ***Write a program to calculate the Volume of a Box using Method.

    => class Box()
    {
    double width,height,depth;
    void volume()
    {
    System.out.println(width*height*depth);
    }
    }
    class BoxDemo
    {
    public static void main()
    {
    Box mybox1=newbox();
    mybox1.width=20;
    mybox1.heigth=15;
    mybox1.depth=20;
    mybox1.volume();
    }
    }
    double volume()
    {
    return width*height*depth;
    }
    {
    vol=mybox1.volume();
    System.out.println(vol);
    }

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. b) Write a program to calculate the Volume of a Box using Method.


    public class Methode {

    double width;
    double height;
    double depth;

    void claculatevolum()
    {

    double vol;

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

    vol = width*height*depth;

    System.out.println("The Volume is : " +vol);
    }

    }



    public class Mybox {


    public static void main(String[] args) {

    Methode m = new Methode();
    m.claculatevolum();

    }

    }

    ReplyDelete
  4. ******* How to access the instance variable using Method?*******

    Solution:

    source code:

    Instance variables using method:

    public class Rectangle
    {
    double length;
    double breadth;
    }

    //This class declares an object of type Rectangle.

    class RectangleDemo
    {
    public static void main(String args[])
    {
    Rectangle myrect1 = new Rectangle();
    Rectangle myrect2 = new Rectangle();
    double area1,area2;

    //assign values to myrect1 instance variables

    myrect1.length = 15;
    myrect1.breadth = 25;

    //compute area of Rectangle

    area1 = myrect1.length * myrect1.breadth;
    System.out.println("Area of Rectangle 1 : " + area1);


    //assign values to myrect2 instance variables
    myrect2.length = 15;
    myrect2.breadth = 25;

    //compute area of Rectangle
    area2 = myrect2.length * myrect2.breadth;
    System.out.println("Area of Rectangle 2 : " + area2);
    }
    }

    ReplyDelete
  5. Q:write a program Java program using method :
    ohammad Ashraful Hasan Sobuj
    My Batch- 46th
    My Department :CSE
    My ID 201420656

    package Box;
    class sobuj
    {
    double width;
    double height;
    double depth;
    double volume()
    {

    return(width*height*depth);
    }
    }
    public class Box {
    public static void main(String[] args) {

    sobuj mybox1 = new sobuj();
    sobuj mybox2 = new sobuj();
    double vol;

    mybox1.width=100;
    mybox1.height=200;
    mybox1.depth=150;

    mybox2.width=200;
    mybox2.height=250;
    mybox2.depth=110;

    vol = mybox1.volume();
    System.out.println("Volume of box1 is: " +vol);

    vol = mybox2.volume();
    System.out.println("\nVolume of box2 is: "+vol);
    System.out.println();
    }
    }

    ReplyDelete
  6. Q:write a program Java program using method :
    Sharmin Akter
    My Batch- 46th
    My Department :CSE
    My ID 201420769


    package Box;
    class Sharmin
    {
    double width;
    double height;
    double depth;
    double volume()
    {

    return(width*height*depth);
    }
    }
    public class Box {
    public static void main(String[] args) {

    Sharmin myboxa = new Sharmin();
    Sharmin myboxb = new Sharmin();
    double vol;

    myboxa.width=100;
    myboxa.height=200;
    myboxa.depth=150;

    myboxb.width=200;
    myboxb.height=250;
    myboxb.depth=110;

    vol = myboxa.volume();
    System.out.println("Volume of boxa is: " +vol);

    vol = myboxb.volume();
    System.out.println("\nVolume of boxb is: "+vol);
    System.out.println();
    }
    }

    ReplyDelete
  7. ID-201420406
    Batch-46th

    class Box
    {
    int l,b,h;
    int volume( )
    {
    return(l*b*h);
    }
    }
    class Abc
    {
    public static void main(String args[ ])
    {
    Box b1= new Box( );
    b1.l=4;
    b1.b=6;
    b1.h=10;
    System.out.println(“Length of box is = 4″);
    System.out.println(“Breadth of box is = 6″);
    System.out.println(“Height of box is = 10″);
    System.out.println(“Volume of box is =”+b1.volume( ));
    }
    }

    ReplyDelete
  8. ID-201421068
    Batch-46th

    package ex4;
    import java.util.Scanner;
    class reverse {
    int sum = 0;
    int getvalue()
    {
    return sum;
    }
    }
    public class Ex4 {
    public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    reverse nm = new reverse();
    int m,rv=0,nber,sum=0;
    System.out.println("Plz enter the int number");

    m = s.nextInt();
    nber = m;

    while (nber!=0) {
    rv = rv*10;
    rv = rv+nber%10;
    nber = nber/10;
    sum+=rv;
    }
    System.out.println("Reverses " +rv);
    double result = nm.getvalue();
    System.out.println("These Reversed number is " +sum);
    }
    }

    ReplyDelete