Breaking

Search

Monday, June 22, 2020

classes and objects in java with realtime examples

Class object and Method

Class object and Method


The java program to dismonstrate the concept of Class,object and Method


  • Step 1: Define a class by name Rectangle.
  • Step 2: Declare Two Instance Variable Length and Width.
  • Step 3: Declare and define Two Method Set Data and Call Area get Value and
  •              to return area of Rectangle.
  • Step 4: Define a Class Rectangle Demo.
  • Step 5: Define a Main() and Create Object to rectangle Class.
  • Step 5: The using Println()Show the Area of rectengle.

CLASS OBJECT AND METHOD


Program

class Rectangle
{
int length,width;
void setdata(int l, int w)
{
length=l;
width=w;
}
int calArea()
{
int area;
area=length * width;
return area;
}
}
class RectangleDemo
{
public staic void main (String args[])
{
Rectangle r1=new Rectangle();
r1.length=15;
r1.width=4;
System.out.println("Area of First Rectangle is ="+r1.length*r1.width);
Rectangle r2=new Rectangle();
r2.setData(5,3);
System.out.println("Area of Second Rectangle is ="+r2.calArea());
}
}

Output:

C:\class\java>javac RectangleDemo.java
C:\class\java>java RectangleDemo

Area of First Rectangle is=60
Area of Second Rectangle is =15


RESULT:
    Thus the program Succesfully Exected.

No comments:

Post a Comment