Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Write a program for calculating area and perimeter of a rectangle

 
 

Share on Google+Share on Google+

Write a program for calculating area and perimeter of a rectangle

Advertisement
If you are a newbie in Java programming then our tutorials and examples will be helpful in understanding Java programming in the most simplest way.

Write a program for calculating area and perimeter of a rectangle

     

If you are a newbie in Java programming then our tutorials and examples will be helpful in understanding Java programming in the most simplest way. Here after reading this lesson, you will be able to write program for calculating the area and perimeter of a rectangle. 

First of all create a class named RecArea under Java.io package. Now define two integer variable 'l' and 'w'. As the program will be based on keyboard numerical input, it is important for every programmer to use correct data without any mistake. In this case the exception methods like try/catch mechanism helps in detecting user input errors. So before starting the functional code, enclosed it with try clause so that any error in the statement causes the execution of the catch clauses. 

Now create an abstract buffer class which is the super class of all classes and represents a stream of input bytes.  The InputSreamReader reads the character stream and stores it in the buffer class. Now use parseInt for both length and width of the rectangle. This is an instance of class method and is used to convert a string to an integer. Define the area as l*w and perimeter as 2*(l+w) and in the end use the catch exception. 

Now compile and run the program and input the value as you see the message and get the ultimate result. If you find any kind of error, then check the whole program again. 

Here is the code of the program:

import java.io.*;
class RecArea 
{
  public static void main(String[] args) 
  {

  int l=0;
  int w=0;

  try{
  
  BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter length of rectangle : ");
  l = Integer.parseInt(br1.readLine());
  System.out.println("Enter width of rectangle : ");
  w = Integer.parseInt(br1.readLine());
  int area = l*w;
  System.out.println("Area of Rectangle : "+area);
  int perimiter = 2*(l+w);
  System.out.println("Perimeter: " + perimiter);

  }catch(Exception e){System.out.println("Error : "+e);}

  }
  }

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Related Tutorials

Discuss: Write a program for calculating area and perimeter of a rectangle   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
javaheadache
September 29, 2011
Rectangle Class

I have tried to get this java program to run without success using NetBean IDE public class Rectangle2 { /** Main method */ /** The area of the rectangle */ double area; /** The number of objects created */ static int numberOfObjects = 0; /** Construct a rectangle with area 1 */ Rectangle2() { area = 1.0; numberOfObjects++; } /** Construct a rectangle with a specified area */ Rectangle(double newArea) { area = newArea; numberOfObjects++; } /** Return numberOfObjects */ static int getNumberOfObjects() { return numberOfObjects; } /** Return the area of this rectangle */ double getArea() { return area * area * Math.PI; } } Instruction: Design a class named Rectangle to represent a rectangle. The class contains: two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. A method named getPerimeter() that returns the perimeter. I also ran this program; however it is not setup according to instruction. import java.io.*; public class Rectangle { public static void main(String[] args) { int l=1; int w=1; try{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter length of rectangle (e.g. 1, 2,etc) : "); l = Integer.parseInt(br1.readLine()); System.out.println("Enter width of rectangle (e.g. 1, 2, etc) : "); w = Integer.parseInt(br1.readLine()); int area = l*w; System.out.println("Area of Rectangle : "+area); int perimiter = 1*(l+w); System.out.println("Perimeter of Total: " + perimiter); }catch(Exception e){System.out.println("Error : "+e);} } }
ekta gupta
November 15, 2011
programming principle & algorithm

hello sir can you help me?
ekta gupta
November 15, 2011
c programming

hello sir ji
Chandrashekhar Singh
February 26, 2013
java

java Question and programe discuss.
Shara Carter
February 23, 2012
Question on how to write a program code

Write a program that prompts user to input the length and width of a rectangle and then prints the rectangle's area dn perimeter. (Assume that the length and width are decimal numbers).
carol kangina
April 2, 2012
congrats

thanx for the gud job u r doing.
joseph tuguinay
September 2, 2012
computer

good
rajesh
September 14, 2012
core java

what is buffer reader
rajesh
September 14, 2012
ask me question

try,catch,why r u use in programe
DMCA.com