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:
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial