Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* As-4 */
- import javax.swing.*;
- public class RectangleObj {
- public static class Rectangle {
- private double length;
- private double width;
- public Rectangle(double l, double w) {
- length = l;
- width = w;
- }
- public double getLength() {
- return length;
- }
- public double getWidth() {
- return width;
- }
- public double compArea() {
- return length * width;
- }
- public double compPerim() {
- return 2 * (length + width);
- }
- }
- public static void main(String[] args) {
- String in = JOptionPane.showInputDialog("Enter the length of the first rectangle: ");
- double l = Double.parseDouble(in);
- in = JOptionPane.showInputDialog("Enter the width of the first rectangle: ");
- double w = Double.parseDouble(in);
- Rectangle r1 = new Rectangle(l, w);
- in = JOptionPane.showInputDialog("Enter the length of the second rectangle: ");
- l = Double.parseDouble(in);
- in = JOptionPane.showInputDialog("Enter the width of the second rectangle: ");
- w = Double.parseDouble(in);
- Rectangle r2 = new Rectangle(l, w);
- double area1 = r1.compArea();
- double area2 = r2.compArea();
- double perim1 = r1.compPerim();
- double perim2 = r2.compPerim();
- String out = "Values for the first object: "
- + "\nLength: " + r1.getLength() + "\n Width: " + r1.getWidth() + "\n Perimeter: " + perim1 + "\n Area: " + area1;
- JOptionPane.showMessageDialog(null, out);
- out = "Values for the second object: "
- + "\nLength: " + r2.getLength() + "\n Width: " + r2.getWidth() + "\n Perimeter: " + perim2 + "\n Area: " + area2;
- JOptionPane.showMessageDialog(null, out);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement