Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Friday 2 September 2016

Write a Program to calculate the area of rectangle

In this tutorial , we are learning that how to calculate area of the rectangle in java.

The programmer asks users for the length and width values and then output will be calculated and display on console. DataInputStream is used for read primitive java data types from an underlying input stream.

Integer.parseInt() is used to convert data into Integer format.

File Name : RecArea.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
class area
{
 int length,breadth,area;

 public void getRectangleData() throws Exception
 {
  System.out.println("Enter Length :");
  DataInputStream in = new DataInputStream(System.in);
  length = Integer.parseInt(in.readLine());
  System.out.println("Enter Breadth :");
  breadth = Integer.parseInt(in.readLine());
  
 }

 public void calculateArea() throws Exception
 {
  area = length * breadth;
   
 }

 public void displayRectangleData() throws Exception
 {
  System.out.println("Length of the rectangle :"+length);
  System.out.println("Breadth of the rectangle :"+breadth);
  System.out.println("Area of the rectangle :"+area);
 }

}

class RecArea
{
 public static void main(String args[]) throws Exception
 {
  area a1 = new area();
  a1.getRectangleData(); 
  a1.calculateArea();
  a1.displayRectangleData();

 }
}

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.