Tuesday 6 September 2016

Area of Rectangle - Functions and classes in JAVA an introduction

Class - In JAVA class is a way to bind functions and objects together in a single unit.
unlike C it requires no header files.
Functions - Functions are a self contained block of statements, that perform a specific task.
import java.util.Scanner;

class Rectangle // it's a custom to write the first letter of class in Capitals.

{

int height,breadth; // Global variables

void getData(int x,int y)  // x and y are the formal parameters

{

height = x;

breadth= y;

}

int area()

{

return breadth*height;

}

}

class Rectangle2   // another class(Main class because main function is in this class)

{

public static void main(String args[])

{

Scanner in = new Scanner (System.in);

int z,h,g;

System.out.println("Enter the height and breadth");

h= in.nextInt();

g= in.nextInt();

 Rectangle e = new Rectangle();

 e.getData(h,g);

 z = e.area();

 System.out.println("Area is "+z);

 }

 }


1. Open terminal in case of linux
2. set the path
3. type javac  classname.java
4. java classname



No comments:

Post a Comment