Advertisement
QwarkDev

prc 3 / java

Sep 3rd, 2020
1,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1.  
  2. // Models.java:
  3.  
  4. package com.company;
  5.  
  6. abstract class Shape
  7. {
  8.     String color;
  9.     Boolean filled;
  10.  
  11.     public Shape(String color, Boolean filled)
  12.     {
  13.         this.color = color;
  14.         this.filled = filled;
  15.     }
  16.  
  17.     public Shape() { }
  18.  
  19.     String getColor()
  20.     {
  21.         return color;
  22.     }
  23.  
  24.     void setColor(String color)
  25.     {
  26.         this.color = color;
  27.     }
  28.  
  29.     Boolean isFilled()
  30.     {
  31.         return filled;
  32.     }
  33.  
  34.     void setFilled(Boolean isFilled)
  35.     {
  36.         filled = isFilled;
  37.     }
  38.  
  39.     abstract double getArea();
  40.     abstract double getPerimeter();
  41. }
  42.  
  43. class Circle extends Shape
  44. {
  45.     double radius;
  46.  
  47.     public Circle() { }
  48.  
  49.     public Circle(double radius)
  50.     {
  51.         this.radius = radius;
  52.     }
  53.  
  54.     public Circle(double radius, String color, Boolean isFilled)
  55.     {
  56.         this.radius = radius;
  57.         super.color = color;
  58.         super.filled = isFilled;
  59.     }
  60.  
  61.     @Override
  62.     double getArea()
  63.     {
  64.         return Math.PI * radius * radius;
  65.     }
  66.  
  67.     @Override
  68.     double getPerimeter()
  69.     {
  70.         return 2 * Math.PI * radius;
  71.     }
  72.  
  73.     @Override
  74.     public String toString()
  75.     {
  76.         return String.format("Circle:\nS=%f\nP=%f\nColor=%s\nIsFilled?=%s",
  77.                 getArea(), getPerimeter(), getColor(), isFilled());
  78.     }
  79. }
  80.  
  81. class Rectangle extends Shape
  82. {
  83.     double width;
  84.     double length;
  85.  
  86.     public Rectangle() { }
  87.  
  88.     public Rectangle(double width, double length)
  89.     {
  90.         this.width = width;
  91.         this.length = length;
  92.     }
  93.  
  94.     public Rectangle(double width, double length, String color, Boolean isFilled)
  95.     {
  96.         this.width = width;
  97.         this.length = length;
  98.         super.color = color;
  99.         super.filled = isFilled;
  100.     }
  101.  
  102.     public double getWidth() {
  103.         return width;
  104.     }
  105.  
  106.     public void setWidth(double width) {
  107.         this.width = width;
  108.     }
  109.  
  110.     public double getLength() {
  111.         return length;
  112.     }
  113.  
  114.     public void setLength(double length) {
  115.         this.length = length;
  116.     }
  117.  
  118.     @Override
  119.     double getArea() {
  120.         return width * length;
  121.     }
  122.  
  123.     @Override
  124.     double getPerimeter() {
  125.         return 2 * width + 2 * length;
  126.     }
  127.  
  128.     @Override
  129.     public String toString()
  130.     {
  131.         return String.format("Rectangle:\nS=%f\nP=%f\nColor=%s\nIsFilled?=%s",
  132.                 getArea(), getPerimeter(), getColor(), isFilled());
  133.     }
  134. }
  135.  
  136. // Main.java:
  137.  
  138. package com.company;
  139.  
  140. class Main
  141. {
  142.     public static void main(String[] args)
  143.     {
  144.         var shapes = new Shape[10];
  145.  
  146.         for (int i = 0; i < 10; i++)
  147.         {
  148.             if (i % 2 == 0)
  149.             {
  150.                 shapes[i] = new Circle(Math.random() * 100, "Black", i % 3 == 0);
  151.             }
  152.             else
  153.             {
  154.                 shapes[i] = new Rectangle(Math.random() * 100, Math.random() * 100, "Black",  i % 3 == 0);
  155.             }
  156.         }
  157.  
  158.         for (var current : shapes)
  159.         {
  160.             System.out.println(current.toString());
  161.             System.out.println();
  162.         }
  163.     }
  164. }
  165.  
  166. // Output:
  167.  
  168. Circle:
  169. S=21150,609484
  170. P=515,544758
  171. Color=Black
  172. IsFilled?=true
  173.  
  174. Rectangle:
  175. S=593,755608
  176. P=100,585965
  177. Color=Black
  178. IsFilled?=false
  179.  
  180. Circle:
  181. S=24750,825957
  182. P=557,698890
  183. Color=Black
  184. IsFilled?=false
  185.  
  186. Rectangle:
  187. S=1559,316118
  188. P=166,709883
  189. Color=Black
  190. IsFilled?=true
  191.  
  192. Circle:
  193. S=3810,251887
  194. P=218,817361
  195. Color=Black
  196. IsFilled?=false
  197.  
  198. Rectangle:
  199. S=7992,962780
  200. P=357,641006
  201. Color=Black
  202. IsFilled?=false
  203.  
  204. Circle:
  205. S=10894,135395
  206. P=369,999653
  207. Color=Black
  208. IsFilled?=true
  209.  
  210. Rectangle:
  211. S=664,407538
  212. P=212,718715
  213. Color=Black
  214. IsFilled?=false
  215.  
  216. Circle:
  217. S=8592,151006
  218. P=328,591165
  219. Color=Black
  220. IsFilled?=false
  221.  
  222. Rectangle:
  223. S=516,969441
  224. P=139,738379
  225. Color=Black
  226. IsFilled?=true
  227.  
  228. Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement