Create a new Java Class Rectangle – This Python Tutorial will explain the creation of a new Java class named Rectangle with the following class specification:
How To Create a New Rectangle Class in Java
- Write a Java program using Objects and Classes concepts in Object Oriented Programming. Define the Rectangle class that will have:
- Two member variables width and height ,
- A no-arg constructor that will create the default rectangle with width = 1 and height =1.
- A parameterized constructor that will create a rectangle with the specified x, y, height and width.
- A method getArea() that will return the area of the rectangle.
Note: Formula for area of rectangle is (width x height)
Formula for perimeter of rectangle is: 2 x (width+height)
A method getPerimeter() that will return the perimeter of the rectangle.
Write a test program that creates two rectangle objects. It creates the first object with default values and the second object with user specified values. Test all the methods of the class for both the objects.
How to Calculate Area and Perimeter in Rectangle Class
Formula for Area of a Rectangle
To calculate area of triangle, we need the number of square units to cover the surface of a figure.
Hence, the formula is: Area = Length x Width
Formula for perimeter of Rectangle
The perimeter may be defined as the distance around a figure.
Hence formula is:
Perimeter of Rectangle = 2 x Length + 2 x Width
Perimeter of Rectangle = 2 (Length + Width)
The Source Code of Java Rectangle Class Program
/* * Write a Java program to create a new class called 'Rectangle' with length and height fields and a no argument constructor and parameterized constructor. Moreover add two methods to calculate and return area and perimeter of rectangle. Write a test class to use this Rectangle class. Create different objects initialized by constructors. Call methods for these objects and show results. */ package testrectangle; /** * @author www.EasyCodebook.com */ class Rectangle{ // define two fields double length, width; // define no arg constructor Rectangle() { length = 1; width = 1; } // define parameterized constructor Rectangle(double length, double width) { this.length = length; this.width = width; } // define a method to return area double getArea() { return (length * width); } // define a method to return perimeter double getPerimeter() { return (2 * (length + width)); } } public class TestRectangle { public static void main(String[] args) { // create first object //and initialize with no arg constructor Rectangle rect1 = new Rectangle(); // create first object //and initialize with no arg constructor Rectangle rect2= new Rectangle(15.0,8.0); System.out.println("Area of first object="+rect1.getArea()); System.out.println("Perimeter of first object="+rect1.getPerimeter()); System.out.println("Area of second object="+rect2.getArea()); System.out.println("Perimeter of second object="+rect2.getPerimeter()); } }
The output of a sample run of the Java Program
Area of first object=1.0 Perimeter of first object=4.0 Area of second object=120.0 Perimeter of second object=46.0
Pingback: Define Circle Class in Java with Radius and Area | EasyCodeBook.com
Keep up the good work, thanks!