Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

This site has a good tutorial. i am beginner in java.
i have learn very good tips in this site. i am a php developer.

Posted by Govind on Friday, 08.31.07 @ 18:36pm | #24621

this code snippet is awesome
i can get the clear idea of user input functions
but it could be better if you would show different method of implementing the same

than

Posted by ramesh on Saturday, 06.2.07 @ 13:44pm | #18011

Hi,
Dear Karthik!
I got your problem [BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in))]. You visit the following url for better understanding to this.
You can also take data from the keyboard through command line.

For example:

int a = Integer.parseInt(args[0]), int b = Integer.parseInt(args[1]) and so on.]

Read more: [url = http://www.roseindia.net/java/example/java/io/ReadStandardIO.shtml] BufferedReader class[/url]

vinod kumar

Posted by vinod kumar on Tuesday, 03.6.07 @ 19:08pm | #10862

I am lil bit confused at the usage of
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in))

is there any other possibility with out using this?

Posted by karthik on Monday, 02.26.07 @ 14:11pm | #9650

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.