Advertisement
atanasovetr

Exercise2

Dec 14th, 2020
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. //class Rectangle
  2. public class Rectangle {
  3.     private double width;
  4.     private double height;
  5.  
  6.     public Rectangle() {
  7.         this.width = 0;
  8.         this.height = 0;
  9.     }
  10.  
  11.     public Rectangle(double width, double height) {
  12.         this.width = width;
  13.         this.height = height;
  14.     }
  15.     public double perimeter(){
  16.         double peri = 2 * this.width + 2 * this.height;
  17.         return peri;
  18.     }
  19.     public double area(){
  20.         double area = this.width * this.height;
  21.         return area;
  22.     }
  23.  
  24.     @Override
  25.     public String toString() {
  26.         return "Recrangle: " +
  27.                 "Width= " + width +
  28.                 ", Height= " + height +
  29.                 ", Perimeter= " + perimeter() +
  30.                 ", Area= " + area();
  31.     }
  32. }
  33.  
  34.  
  35. //class RectangleMain
  36. import java.util.ArrayList;
  37. import java.util.Random;
  38.  
  39. public class RectangleMain {
  40.     public static void main(String[] args) {
  41.         Random rand = new Random();
  42.         ArrayList<Rectangle> rectangles = new ArrayList<>();
  43.  
  44.         for(int i = 0; i < 10; i++){
  45.             double rand_width = rand.nextDouble()*100;
  46.             double rand_height = rand.nextDouble()*100;
  47.             rectangles.add(new Rectangle(rand_width, rand_height));
  48.         }
  49.  
  50.         for(Rectangle r: rectangles){
  51.             System.out.println(r);
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement