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

1. Write a class called Rectangle that maintains two attributes to represent the

ID: 3731863 • Letter: 1

Question

1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get, set, and toString methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width.

2. Write a java program (a driver application) that tests the above class by providing the users with the following menu options:

1 - to set the length
2 - to set the width
3 - to get the length
4 - to get the width
5 - to get the perimeter
6 - to get the area
7 - to print the object as string
0 - to quit

Your program should create one Rectangle object at the beginning using the default constructor, and then repeatedly call the appropriate method for that object depending on the user selection from the above menu. Use JOptionPane for all input/output.

Explanation / Answer

Following are the two classes:

Rectangle.java

public class Rectangle {
  
    int length, width; //instance Variables

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    @Override
    public String toString() {
        return "Rectangle{" + "length=" + length + ", width=" + width + '}';
    }
  
    public int perimeter(int l, int w)
    {
        return 2*(l + w);
    }
  
    public int area(int l, int w)
    {
        return (l * w);
    }
  
    public Rectangle()
    {
        length = 0;
        width = 0;
    }
  
    public Rectangle(int l, int w)
    {
        length = l;
        width = w;
    }
  
}

/* the above class is the base of all operation. all getters and setters are implemented */

Driver.java

/* this class is the driver class for the rectangle class written above. it uses a JOptionFrame for all Input and Output operations.*/

import javax.swing.JOptionPane;

public class Driver {
  
    public static void main(String[] args){
      
        String s = ""; // to take input within the switch case construct
        Rectangle r = new Rectangle(); // rectangle object created
        int g = -1; // sentinel for while loop
        while(g < 0)
        {
            String in = JOptionPane.showInputDialog( "1 - to set the length " +
            "2 - to set the width " +
            "3 - to get the length " +
            "4 - to get the width " +
            "5 - to get the perimeter " +
            "6 - to get the area " +
            "7 - to print the object as string " +
            "0 - to quit " ); // taking choices from menu
            if(in.length() > 0)
            {
                int i = Integer.parseInt(in); // input for switch construct
                switch( i )
                {
                    case 1:
                        s = JOptionPane.showInputDialog("Enter the Length"); // Taking length
                        r.setLength(Integer.parseInt(s));
                        break;
                    case 2:
                        s = JOptionPane.showInputDialog("Enter the Width"); // Taking width
                        r.setWidth(Integer.parseInt(s));
                        break;
                    case 3:
                        JOptionPane.showMessageDialog(null, r.getLength(),"Length", JOptionPane.PLAIN_MESSAGE); // Showing length
                        break;
                    case 4:
                        JOptionPane.showMessageDialog(null, r.getWidth(),"Width", JOptionPane.PLAIN_MESSAGE); // Showing Width
                        break;
                    case 5:
                        JOptionPane.showMessageDialog(null, r.perimeter(r.getLength(), r.getWidth()), "Perimeter", JOptionPane.PLAIN_MESSAGE); // showing Perimeter
                        break;
                    case 6:
                        JOptionPane.showMessageDialog(null, r.area(r.getLength(), r.getWidth()),"Area", JOptionPane.PLAIN_MESSAGE); // Showing Area
                        break;
                    case 7:
                        JOptionPane.showMessageDialog(null, r.toString()); // Showing Object specifications
                        break;
                    case 0:
                        g = 0; // updating sentinel g to exit the while loop.
                        break;
                }
            }
            else
                System.out.println("Enter your Choice");
        } // While loop ends here
      
    } // main() ends here
  
} // Class ends here

/* PLEASE KEEP BOTH THE CLASSES IN THE SAME PACKAGE OR ELSE IT WILL END UP CAUSING REFRENCING ISSUES*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote