Advertisement
agoncharova

As-4

Jun 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. /* As-4 */
  2.  
  3. import javax.swing.*;
  4.  
  5.  
  6. public class RectangleObj {
  7.     public static class Rectangle {
  8.         private double length;
  9.         private double width;
  10.  
  11.         public Rectangle(double l, double w) {
  12.             length = l;
  13.             width = w;
  14.         }
  15.  
  16.         public double getLength() {
  17.             return length;
  18.         }
  19.  
  20.         public double getWidth() {
  21.             return width;
  22.         }
  23.  
  24.         public double compArea() {
  25.             return length * width;
  26.         }
  27.  
  28.         public double compPerim() {
  29.             return 2 * (length + width);
  30.         }
  31.  
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.  
  36.         String in = JOptionPane.showInputDialog("Enter the length of the first rectangle: ");
  37.         double l = Double.parseDouble(in);
  38.  
  39.         in = JOptionPane.showInputDialog("Enter the width of the first rectangle: ");
  40.         double w = Double.parseDouble(in);
  41.  
  42.         Rectangle r1 = new Rectangle(l, w);
  43.  
  44.         in = JOptionPane.showInputDialog("Enter the length of the second rectangle: ");
  45.         l = Double.parseDouble(in);
  46.  
  47.         in = JOptionPane.showInputDialog("Enter the width of the second rectangle: ");
  48.         w = Double.parseDouble(in);
  49.  
  50.         Rectangle r2 = new Rectangle(l, w);
  51.  
  52.         double area1 = r1.compArea();
  53.         double area2 = r2.compArea();
  54.         double perim1 = r1.compPerim();
  55.         double perim2 = r2.compPerim();
  56.  
  57.  
  58.         String out = "Values for the first object: "
  59.                 + "\nLength: " + r1.getLength() + "\n Width: " + r1.getWidth() + "\n Perimeter: " + perim1 + "\n Area: " + area1;
  60.  
  61.         JOptionPane.showMessageDialog(null, out);
  62.         out = "Values for the second object: "
  63.                 + "\nLength: " + r2.getLength() + "\n Width: " + r2.getWidth() + "\n Perimeter: " + perim2 + "\n Area: " + area2;
  64.         JOptionPane.showMessageDialog(null, out);
  65.         System.exit(0);
  66.  
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement