Tuesday, December 17, 2013

Keyword and Constructor in JAVA : Day 6


a) What is this Keyword?

b) Write any program using Parameter with Constructor.

7 comments:

  1. b))) ***Write any program using Parameterized Constructor.

    =>

    /*Here Box uses a parameterized constructor
    to initialize the dimension of a box */

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

    //This is the constructor for the box.

    Box(double w,double h,double d)
    {
    width=w;
    height=h;
    depth=d;
    }

    //compute and return volume

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

    //declare, allocate and initialize Box objects

    Box.mybox1=new Box(20,10,5);
    Box.mybox2=new Box(4,8,9);
    double vol;

    //get volume of first box.

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

    //get volume of second box.

    vol=mybox2.volume();
    System.out.println("Volume is" + vol);
    }
    }

    ReplyDelete
  2. a))) *** What is this keyword ?

    =>

    this keyword is a reference variable in java that refers to the current object. It refers current class instance variable. this() can be used to invoke current class constructor. this keyword invokes current class method (implicitly). It passed as an argument in the method and constructor call. It also returns the current class instance.

    //example of this keyword

    class Student
    {
    int id;
    String name;

    student(int id,String name)
    {

    /* here this keyword is used to distinguish between local and instance variable because without this parameter(formal arguments) and instance variables are same */

    this.id = id;
    this.name = name;
    }
    void display()
    {
    System.out.println(id+" "+name);
    }
    public static void main(String args[]){
    Student s1 = new Student(111,"Karan");
    Student s2 = new Student(222,"Aryan");
    s1.display();
    s2.display();
    }
    }

    ReplyDelete
  3. *******write any program using parameterized constructor.**********

    Solution:

    package rectangle1;
    public class Rectangle1
    {
    double length;
    double breadth;

    Rectangle1(double len,double bre)
    {
    length=len;
    breadth=bre;
    }
    double volume()
    {
    return length*breadth;
    }
    }

    class Rectangle1Demo
    {
    public static void main(String args[])
    {
    Rectangle1 r1 = new Rectangle1(25.5,15.5);
    Rectangle1 r2 = new Rectangle1(20.5,10.5);
    double vol;
    vol = r1.volume();
    System.out.println("Volume is " + vol);
    vol = r2.volume();
    System.out.println("Volume is " + vol);
    }
    }

    ReplyDelete
  4. **********write any program using parameterized constructor.**********(Another one)

    Solution:

    package Rectangle;
    public class Rectangle
    {
    int length;
    int breadth;

    Rectangle(int len, int bre)
    {

    length=len;
    breadth=bre;

    }

    }

    class RectangleDemo
    {
    public static void main(String args[])
    {
    Rectangle r1 = new Rectangle(20,10);


    System.out.println("Length of a Rectangle : " + r1.length);

    System.out.println("Breadth of a Rectangle : " + r1.breadth);
    }
    }

    ReplyDelete
  5. **********What is this keyword?**********

    Solution:

    Definitions: There can be a lot of usage of this keyword. In java, this is a reference variable that refers to the current object. We can use this keyword in any method or constructor. Use this keyword from any method or constructor to refer to the current object that calls a method or invokes constructor.

    Syntax : this keyword

    this.demo


    Example of this keyword:(Source Code)

    package Square;
    public class Square
    {
    double length;
    double width;

    Square(double len,double wid)
    {
    this.length=len;
    this.width=wid;
    }
    double area()
    {
    return length*width;
    }
    }

    class SquareDemo
    {
    public static void main(String args[])
    {
    Square s1 = new Square(25,25);
    Square s2 = new Square(20.5,20.5);
    double A;
    A = s1.area();
    System.out.println("Area is " + A);
    A = s2.area();
    System.out.println("Area is " + A);
    }
    }

    ReplyDelete
  6. package javaparameter;

    class Rectangle
    {
    double lenth;
    double breadth;
    double hight;

    Rectangle (double a, double b, double c)
    {
    lenth=a;
    breadth=b;
    hight=c;
    }
    double result()
    {
    return lenth*breadth*hight;
    }
    }
    public class Javaparameter {

    public static void main(String[] args) {

    Rectangle m1=new Rectangle(10, 5,2);
    Rectangle n1=new Rectangle (20,5,2);
    System.out.println(" Result is = " + m1.result() );
    System.out.println(" Result is = " + n1.result() );
    }
    }

    ReplyDelete