Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description Write a Java program to compute the perimeter and the area of two di

ID: 3853913 • Letter: D

Question

Description

Write a Java program to compute the perimeter and the area of two different rectangles. The user will input the length and the width of the two rectangles and the program will display the perimeter and area of each.

Write a class Rectangle with private instance variables for length and width. Provide a public constructor for initializing length and width. Also provide public methods for computing perimeter and area. Additionally provide public accessor (get) methods for accessing length and width. (For example, provide getLength( ) and getWidth ( ) methods).

Write a class TestRectangle containing the method main. From the method main, prompt the user to input the length and the width of the first rectangle. Create an object of type Rectangle and initialize its values via the constructor with the length and the width supplied by the user. Prompt the user to input the length and the width of the second rectangle. Create an object of type Rectangle and initialize its values via the constructor with the length and the width supplied by the user for the second rectangle.

Then call the object’s methods of the first object to compute its perimeter and area. Use the object’s accessor (get) methods to retrieve length and width values.

Then call the object’s methods of the second object to compute its perimeter and area. Use the object’s accessor (get) methods to retrieve length and width values of this object.

Then display values of length, width, perimeter and area of the first rectangle followed by those of the second rectangle.

Instructions

Use JOptionPane.showInputDialog ( ) for entering input values and JOptionPane.showMessageDialog ( ) for displaying the result. Import javax.swing.* package for using these classes.

Test Data

Test the program:

Using the following length and width values for the first object:

30, 20

Using the following length and width values for the first object:

60, 40

Test Output:

The output should look as below:

Values for the first object:

Length: 30

Width: 20

Perimeter: 100

Area: 600

Values for the second object:

Length: 60

Width: 40

Perimeter: 200

Area: 2400

Explanation / Answer

Rectangle.java :

package rect;

public class Rectangle

{       

            //instance variables

            private double length;

            private double width;

            //Constructor

            public Rectangle (double l, double w)

            {

                        //initialize instance variables above using the parameters received

                        //in the constructor

                        length = l;

                        width = w;

            }

            //instance methods

            public double getLength ( )

            {

                        return length;

            }

            public double getWidth ( )

            {

                        return width;

            }

            public double compArea (double l,double w )

            {

                        double a;

                        //Calculating area
                        a = l*w;

                        //end code

                        return a;

            }

            public double compPerimeter (double l,double w )

            {

                        //local variables

                        double p;

                        //calculating perimeter
                         p = 2*(l + w);

                        //end code

                        return p;

            }

         

}

##############################################################################

TestRectangle.java :

package rect;
import rect.*;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;

public class TestRectangle

{

            public static void main (String [] args)

            {
               double l = 0,w=0;
               System.out.println("Values for the first object:");

               JFrame frame1 = new JFrame("Please enter length");

                // prompt the user to enter the length
                String length = JOptionPane.showInputDialog(frame1, "Enter length of object1");

          
                JFrame frame2 = new JFrame("Please enter width");

                // prompt the user to enter the length
                String width = JOptionPane.showInputDialog(frame2, "Enter width of object1");

              
              
                   l = Double.parseDouble(length);
                    w = Double.parseDouble(width);
                   System.out.println("length: " + l);
                   System.out.println("width: " + w);
                

                        //input length and width values from the user for first object

                       //create the Rectangle object
                
                        Rectangle r1 = new Rectangle (l, w);
                       double area1 = r1.compArea(l, w);
                       double per1 = r1.compPerimeter(l, w);
                       System.out.println("Perimeter : "+per1);
                       System.out.println("Area : "+area1);
                       JFrame frame3 = new JFrame("Area and perimeter results");
                       JOptionPane.showMessageDialog(frame3, per1, "the results of lenth.", JOptionPane.PLAIN_MESSAGE);
                       JOptionPane.showMessageDialog(frame3, area1, "The result of perimeter.", JOptionPane.PLAIN_MESSAGE);
                     

                        //input length and width values from the user for second object                

                       //create the second Rectangle object
                       System.out.println("Values for the second object:");
                       JFrame frame4 = new JFrame("Please enter length");

                       // prompt the user to enter the length
                       String length1 = JOptionPane.showInputDialog(frame4, "Enter length of object2");

                 
                       JFrame frame5 = new JFrame("Please enter width");

                       // prompt the user to enter the length
                       String width1 = JOptionPane.showInputDialog(frame2, "Enter width of object2");

                      
                          l = Double.parseDouble(length1);
                           w = Double.parseDouble(width1);
                          System.out.println("length: " + l);
                          System.out.println("width: " + w);


                        Rectangle r2 = new Rectangle (l, w);
                      
                        double area2 = r2.compArea(l, w);
                        double per2 = r2.compPerimeter(l, w);
                        //Call methods of Rectangle objects to find values of  
                      

                        //display the results   
                        System.out.println("Perimeter : "+per2);
                        System.out.println("Area : "+area2);
                        JFrame frame7 = new JFrame("Area and perimeter results");
                        JOptionPane.showMessageDialog(frame7, per2, "the results of lenth.", JOptionPane.PLAIN_MESSAGE);
                        JOptionPane.showMessageDialog(frame7, area2, "The result of perimeter.", JOptionPane.PLAIN_MESSAGE);

            }
}

########################################