Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. package com.company.shape;
  2.  
  3. abstract class Shape {
  4. protected String color = "red";
  5. protected boolean filled = true;
  6.  
  7. public Shape(String color, boolean filled) {
  8. this.color = color;
  9. this.filled = filled;
  10. }
  11.  
  12. public Shape() {
  13.  
  14. }
  15.  
  16. public String getColor() {
  17. return color;
  18. }
  19.  
  20. public void setColor(String color) {
  21. this.color = color;
  22. }
  23.  
  24. public boolean isFilled() {
  25. return filled;
  26. }
  27.  
  28. public void setFilled(boolean filled) {
  29. this.filled = filled;
  30. }
  31.  
  32. @Override
  33. public String toString() {
  34. return "Shape{" +
  35. "color='" + color + '\'' +
  36. ", filled=" + filled +
  37. '}';
  38. }
  39. }
  40.  
  41. class Circle extends Shape {
  42.  
  43. protected double radius = 1.0;
  44.  
  45. public Circle() {
  46.  
  47. }
  48.  
  49. public Circle(double radius) {
  50. this.radius = radius;
  51. }
  52.  
  53. public Circle(String color, boolean filled, double radius) {
  54. this.radius = radius;
  55. setColor(color);
  56. setFilled(filled);
  57.  
  58. }
  59.  
  60. public double getRadius() {
  61. return this.radius;
  62. }
  63.  
  64. public void setRadius(double p) {
  65. this.radius = p;
  66. }
  67.  
  68. public double getArea() {
  69. return Math.PI * radius * radius;
  70. }
  71.  
  72. public double getPerimeter() {
  73. return 2 * Math.PI * radius;
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. return "Circle{" +
  79. "Shape{color='" + getColor() + '\'' +
  80. ", filled=" + isFilled() +
  81. "} radius=" + radius +
  82. '}';
  83. }
  84. }
  85.  
  86. class Rectangle extends Shape {
  87.  
  88. protected double width = 1;
  89. protected double length = 1;
  90.  
  91.  
  92. public Rectangle() {
  93.  
  94. }
  95.  
  96. public Rectangle(double width, double lenght) {
  97. this.width = width;
  98. this.length = lenght;
  99. }
  100.  
  101. public Rectangle(double width, double lenght, String color, boolean filled) {
  102. this.width = width;
  103. this.length = lenght;
  104. setColor(color);
  105. setFilled(filled);
  106. }
  107.  
  108. public double getWidth() {
  109. return width;
  110. }
  111.  
  112. public void setWidth(double width) {
  113. this.width = width;
  114. }
  115.  
  116. public double getLength() {
  117. return length;
  118. }
  119.  
  120. public void setLenght(double lenght) {
  121. this.length = lenght;
  122. }
  123.  
  124. public double getArea() {
  125. return width * length;
  126. }
  127.  
  128. public double getPerimeter() {
  129. return 2 * width + 2 * length;
  130. }
  131.  
  132. @Override
  133. public String toString() {
  134. return "Rectangle{" +
  135. "Shape{color='" + getColor() + '\'' +
  136. ", filled=" + isFilled() +
  137. "} width=" + width +
  138. ", length=" + length +
  139. '}';
  140. }
  141.  
  142. }
  143.  
  144. class Square extends Rectangle {
  145. public Square() {
  146.  
  147. }
  148.  
  149. public Square(double side) {
  150. setLenght(side);
  151. setWidth(side);
  152. }
  153.  
  154. public Square(double side, String color, boolean filled) {
  155. setLenght(side);
  156. setWidth(side);
  157. setColor(color);
  158. setFilled(filled);
  159. }
  160.  
  161. public double getSide() {
  162. return getWidth();
  163. }
  164.  
  165. public void setSide(double side) {
  166. setLenght(side);
  167. setWidth(side);
  168. }
  169.  
  170. @Override
  171. public String toString() {
  172. return "Square" +
  173. "{Rectangle{" +
  174. "Shape{color='" + getColor() + '\'' +
  175. ", filled=" + isFilled() +
  176. "} width=" + getWidth() +
  177. ", length=" + getLength() +
  178. '}' + '}';
  179. }
  180. }
  181.  
  182. class Main {
  183. public static void main(String[] args) {
  184. Shape s1 = new Circle("RED", false, 5.5);
  185. System.out.println(s1);
  186. // get Area nie zadziała ponieważ bierzemy klasę absta
  187. // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
  188. // System.out.println(s1.getArea());
  189. //System.out.println(s1.getPerimeter());
  190. System.out.println(s1.getColor());
  191. System.out.println(s1.isFilled());
  192. //System.out.println(s1.getRadius());
  193.  
  194. Circle c1 = (Circle) s1;
  195. System.out.println(c1);
  196. System.out.println(c1.getArea());
  197. System.out.println(c1.getPerimeter());
  198. System.out.println(c1.getColor());
  199. System.out.println(c1.isFilled());
  200. System.out.println(c1.getRadius());
  201.  
  202. // Klasa abstrakcyjna nie mozę być zaincjowana
  203. // Shape s2 = new Shape();
  204.  
  205. Shape s3 = new Rectangle(1.0, 2.0, "RED", false);
  206. System.out.println(s3);
  207. // get Area nie zadziała ponieważ bierzemy klasę absta
  208. // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
  209. // System.out.println(s3.getArea());
  210. /// System.out.println(s3.getPerimeter());
  211. System.out.println(s3.getColor());
  212. // System.out.println(s3.getLength());
  213.  
  214. Rectangle r1 = (Rectangle) s3;
  215. System.out.println(r1);
  216. System.out.println(r1.getArea());
  217. System.out.println(r1.getColor());
  218. System.out.println(r1.getLength());
  219.  
  220. // get Area nie zadziała ponieważ bierzemy klasę absta
  221. // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
  222. Shape s4 = new Square(6.6);
  223. System.out.println(s4);
  224. // System.out.println(s4.getArea());
  225. System.out.println(s4.getColor());
  226. // System.out.println(s4.getSide());
  227.  
  228.  
  229. // get Area nie zadziała ponieważ bierzemy klasę absta
  230. // nie mozna wywoalac tych metod ponieważ Shape owych nie zawiera
  231. Rectangle r2 = (Rectangle) s4;
  232. System.out.println(r2);
  233. System.out.println(r2.getArea());
  234. System.out.println(r2.getColor());
  235. // System.out.println(r2.getSide());
  236. System.out.println(r2.getLength());
  237.  
  238.  
  239. Square sq1 = (Square) r2;
  240. System.out.println(sq1);
  241. System.out.println(sq1.getArea());
  242. System.out.println(sq1.getColor());
  243. System.out.println(sq1.getSide());
  244. System.out.println(sq1.getLength());
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement