Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. //I declare that this is my own work and that I have not passed the program
  5. //to anyone else and would be
  6. //willing to accept any penalty given to me if found to be doing so.
  7.  
  8. enum ShapeColor {
  9. Blue, Yellow, Red, Green, White
  10. }
  11.  
  12. interface Shape {
  13. public double area();
  14.  
  15. }
  16.  
  17. interface Resizable {
  18. public void resize();
  19. }
  20.  
  21. abstract class TwoD implements Shape {
  22.  
  23. protected ShapeColor sc;
  24. protected int a;
  25. protected int b;
  26. protected int c;
  27. protected int d;
  28.  
  29. public TwoD() {
  30.  
  31. }
  32.  
  33. public TwoD(ShapeColor sc, int a) {
  34. this.sc = sc;
  35. this.a = a;
  36. }
  37.  
  38. public TwoD(ShapeColor sc, int a, int b) {
  39. this.sc = sc;
  40. this.a = a;
  41. this.b = b;
  42.  
  43. }
  44.  
  45. public TwoD(ShapeColor sc, int a, int b, int c) {
  46. this.sc = sc;
  47. this.a = a;
  48. this.b = b;
  49. this.c = c;
  50. }
  51.  
  52. public TwoD(ShapeColor sc, int a, int b, int c, int d) {
  53. this.sc = sc;
  54. this.a = a;
  55. this.b = b;
  56. this.c = c;
  57. this.d = d;
  58.  
  59. }
  60.  
  61. public TwoD(TwoD td) {
  62. this.sc = td.sc;
  63. this.a = td.getA();
  64. this.b = td.getB();
  65. this.c = td.getC();
  66. this.d = td.getD();
  67.  
  68. }
  69.  
  70. public int getA() {
  71. return a;
  72. }
  73.  
  74. public int getB() {
  75. return b;
  76. }
  77.  
  78. public int getC() {
  79. return c;
  80. }
  81.  
  82. public int getD() {
  83. return d;
  84. }
  85.  
  86. public ShapeColor getShapeColor() {
  87. return sc;
  88. }
  89.  
  90. public void set(ShapeColor sc, int a, int b, int c, int d) {
  91. this.sc = sc;
  92. this.a = a;
  93. this.b = b;
  94. this.c = c;
  95. this.d = d;
  96. }
  97.  
  98. public String toString() {
  99. return String.format("(%s", sc, a, b, c, d);
  100. }
  101.  
  102. }
  103.  
  104. class Circle extends TwoD {
  105. public Circle() {
  106. super();
  107.  
  108. }
  109.  
  110. public Circle(ShapeColor sc, int radius) {
  111. this.sc = sc;
  112. this.a = radius;
  113.  
  114. }
  115.  
  116. public Circle(Circle c) {
  117. this.sc = c.getShapeColor();
  118. this.a = getA();
  119.  
  120. }
  121.  
  122. @Override
  123. public double area() {
  124. // TODO Auto-generated method stub
  125. double area = Math.pow(getRadius(), 2);
  126. area *= Math.PI;
  127. return area;
  128. }
  129.  
  130. public int getRadius() {
  131. return super.getA();
  132.  
  133. }
  134.  
  135. public void set(ShapeColor sc, int radius) {
  136. this.a = radius;
  137. this.sc = sc;
  138.  
  139. }
  140.  
  141. public String toString() {
  142. String superToString = super.toString();
  143. return String.format("Circle (2D %s,%s))\nArea = %.3f \nI am a circle shape", superToString, getRadius(),
  144. area());
  145.  
  146. }
  147.  
  148. }
  149.  
  150. class Rectangle extends TwoD {
  151.  
  152. public Rectangle() {
  153.  
  154. }
  155.  
  156. public Rectangle(ShapeColor sc, int length, int width) {
  157. this.sc = sc;
  158. this.a = length;
  159. this.b = width;
  160.  
  161. }
  162.  
  163. public Rectangle(Rectangle r) {
  164. this.sc = r.getShapeColor();
  165. this.a = r.getLength();
  166. this.b = r.getWidth();
  167. }
  168.  
  169. @Override
  170. public double area() {
  171. // TODO Auto-generated method stub
  172. return (getA() * getB());
  173. }
  174.  
  175. public int getLength() {
  176. return super.getB();
  177.  
  178. }
  179.  
  180. public int getWidth() {
  181. return super.getA();
  182.  
  183. }
  184.  
  185. public void set(ShapeColor sc, int length, int width) {
  186. this.sc = sc;
  187. this.a = length;
  188. this.b = width;
  189.  
  190. }
  191.  
  192. public String toString() {
  193. String superToString = super.toString();
  194. return String.format("Rectangle (2D %s,%s,%s))\nArea = %.3f \nI am a rectangle shape", superToString,
  195. getLength(), getWidth(), area());
  196.  
  197. }
  198.  
  199. }
  200.  
  201. class Triangle extends TwoD {
  202. public Triangle() {
  203.  
  204. }
  205.  
  206. public Triangle(ShapeColor sc, int a, int b, int c) {
  207. super(sc, a, b, c);
  208.  
  209. }
  210.  
  211. public Triangle(Triangle t) {
  212. this.a = t.getA();
  213. this.b = t.getB();
  214. this.c = t.getC();
  215. this.sc = t.getShapeColor();
  216.  
  217. }
  218.  
  219. @Override
  220. public double area() {
  221. // TODO Auto-generated method stub
  222. return .5 * getB() * getC();
  223. }
  224.  
  225. public int getA() {
  226. return super.getA();
  227.  
  228. }
  229.  
  230. public int getB() {
  231. return super.getB();
  232.  
  233. }
  234.  
  235. public int getC() {
  236. return super.getC();
  237. }
  238.  
  239. public void set(ShapeColor sc, int a, int b, int c) {
  240. this.a = a;
  241. this.b = b;
  242. this.c = c;
  243. this.sc = sc;
  244.  
  245. }
  246.  
  247. public String toString() {
  248. String superToString = super.toString();
  249. return String.format("Triangle (2D %s,%s,%s,%s))\nArea = %.3f \nI am a triangle shape", superToString, getA(),
  250. getB(), getC(), area());
  251. }
  252.  
  253. }
  254.  
  255. class Trapezoid extends TwoD {
  256. private int h;
  257.  
  258. public Trapezoid() {
  259.  
  260. }
  261.  
  262. public Trapezoid(ShapeColor sc, int a, int b, int c, int h) {
  263. this.h = h;
  264. this.a = a;
  265. this.b = b;
  266. this.c = c;
  267. this.sc = sc;
  268.  
  269. }
  270.  
  271. public int getA() {
  272. return super.getA();
  273.  
  274. }
  275.  
  276. public int getB() {
  277. return super.getB();
  278.  
  279. }
  280.  
  281. public int getC() {
  282. return super.getC();
  283.  
  284. }
  285.  
  286. public int getD() {
  287. return super.getD();
  288.  
  289. }
  290.  
  291. public int getHeight() {
  292. return h;
  293.  
  294. }
  295.  
  296. @Override
  297. public double area() {
  298. // TODO Auto-generated method stub
  299. return (getHeight() * (getA() + getB() / 2));
  300. }
  301.  
  302. public void set(ShapeColor sc, int a, int b, int c, int d) {
  303. this.a = a;
  304. this.b = b;
  305. this.c = c;
  306. this.d = d;
  307. this.sc = sc;
  308. this.h = ((a + b) / 2);
  309.  
  310. }
  311.  
  312. public String toString() {
  313. String superToString = super.toString();
  314. return String.format("Trapezoid (2D %s,%s,%s,%s,%s)%s)\nArea = %.3f \nI am a trapezoid shape", superToString,
  315. getA(), getB(), getC(), getD(), getHeight(), area());
  316. }
  317.  
  318. }
  319.  
  320. abstract class ThreeD implements Shape, Resizable {
  321.  
  322. protected ShapeColor sc;
  323. protected double a;
  324.  
  325. public ThreeD() {
  326.  
  327. }
  328.  
  329. public ThreeD(ShapeColor sc, double a) {
  330. this.sc = sc;
  331. this.a = a;
  332.  
  333. }
  334.  
  335. public ThreeD(ThreeD td) {
  336. this.sc = td.sc;
  337. this.a = td.getA();
  338.  
  339. }
  340.  
  341. public double getA() {
  342. return a;
  343. }
  344.  
  345. public void set(ShapeColor sc, double a) {
  346. this.sc = sc;
  347. this.a = a;
  348. }
  349.  
  350. public void resize() {
  351. this.a = (a * .9);
  352. }
  353.  
  354. protected double volume() {
  355. return Math.pow(a, 3);
  356.  
  357. }
  358.  
  359. public String toString() {
  360. return String.format("(%s (%s,%.0f) ", "3D", sc, a);
  361. }
  362.  
  363. }
  364.  
  365. class Sphere extends ThreeD {
  366. public Sphere() {
  367.  
  368. }
  369.  
  370. public Sphere(ShapeColor sc, double a) {
  371. super(sc, a);
  372.  
  373. }
  374.  
  375. public Sphere(Sphere s) {
  376. this.a = s.a;
  377. this.sc = s.sc;
  378.  
  379. }
  380.  
  381. @Override
  382. public double area() {
  383. // TODO Auto-generated method stub
  384. return (4 * Math.PI * Math.pow(a, 2));
  385. }
  386.  
  387. protected double volume() {
  388. return super.volume() * 4 / 3 * Math.PI;
  389.  
  390. }
  391.  
  392. public double getA() {
  393. return a;
  394. }
  395.  
  396. public void set(ShapeColor sc, double a) {
  397. this.a = a;
  398. this.sc = sc;
  399.  
  400. }
  401.  
  402. public String toString() {
  403. String returnvalue = "";
  404. returnvalue += String.format("Sphere %s \nSurface Area = %.3f\nVolume = %.3f", super.toString(), area(),
  405. volume());
  406. resize();
  407. returnvalue += String.format(
  408. "\nSize reduced by 10%% :Sphere %s \nUpdated Surface Area = %.3f\nUpdated Volume = %.3f",
  409. super.toString(), area(), volume());
  410. return returnvalue;
  411. }
  412.  
  413. }
  414.  
  415. class Cube extends ThreeD {
  416.  
  417. public Cube() {
  418.  
  419. }
  420.  
  421. public Cube(ShapeColor sc, double a) {
  422. super(sc, a);
  423.  
  424. }
  425.  
  426. public Cube(Cube c) {
  427. this.a = c.a;
  428. this.sc = c.sc;
  429.  
  430. }
  431.  
  432. @Override
  433. public double area() {
  434. // TODO Auto-generated method stub
  435. return (Math.pow(a, 2) * 6);
  436. }
  437.  
  438. protected double volume() {
  439. return super.volume();
  440.  
  441. }
  442.  
  443. public double getA() {
  444. return a;
  445. }
  446.  
  447. public void set(ShapeColor sc, double a) {
  448. this.a = a;
  449. this.sc = sc;
  450.  
  451. }
  452.  
  453. public String toString() {
  454. String returnvalue = String.format("Cube %s \nSurface Area = %.3f\nVolume = %.3f ", super.toString(), area(),
  455. volume());
  456. resize();
  457. returnvalue += String.format(
  458. "\nSize reduced by 10%% : Cube %s \nUpdated Surface Area = %.3f \nUpdated Volume = %.3f ",
  459. super.toString(), area(), volume());
  460. return returnvalue;
  461. }
  462.  
  463. }
  464.  
  465. class Tetrahedron extends ThreeD {
  466. public Tetrahedron() {
  467.  
  468. }
  469.  
  470. public Tetrahedron(ShapeColor sc, double a) {
  471. super(sc, a);
  472.  
  473. }
  474.  
  475. public Tetrahedron(Tetrahedron t) {
  476. this.a = t.a;
  477. this.sc = t.sc;
  478.  
  479. }
  480.  
  481. @Override
  482. public double area() {
  483. // TODO Auto-generated method stub
  484. return (Math.sqrt(3) * Math.pow(a, 2));
  485. }
  486.  
  487. protected double volume() {
  488. return super.volume() / (6 * Math.sqrt(2));
  489.  
  490. }
  491.  
  492. public double getA() {
  493. return a;
  494. }
  495.  
  496. public void set(ShapeColor sc, double a) {
  497. this.a = a;
  498. this.sc = sc;
  499.  
  500. }
  501.  
  502. public String toString() {
  503. String returnvalue = "";
  504. returnvalue += String.format(" Tetrahedron %s \nSurface Area = %.3f\nVolume = %.3f ", super.toString(), area(),
  505. volume());
  506. resize();
  507. returnvalue += String.format(
  508. "\nSize reduced by 10%% :Tetrahedron %s \nUpdated Surface Area = %.3f\nUpdated Volume = %.3f ",
  509. super.toString(), area(), volume());
  510. return returnvalue;
  511.  
  512. }
  513. }
  514.  
  515. public class test {
  516.  
  517. private static int getInt() {
  518. Random rand = new Random();
  519. return rand.nextInt(10);
  520. }
  521.  
  522. private static double getDouble() {
  523. Random rand = new Random();
  524. double maxRange = 10;
  525. double minRange = 1;
  526. double randomValue = minRange + (maxRange - minRange) * rand.nextDouble();
  527. return randomValue;
  528.  
  529. }
  530.  
  531. private static ShapeColor getColor() {
  532. Random rand = new Random();
  533. return ShapeColor.values()[rand.nextInt(ShapeColor.values().length)];
  534.  
  535. }
  536.  
  537. private static TwoD getTwoD() {
  538. ArrayList<TwoD> td = new ArrayList<TwoD>();
  539. Rectangle r = new Rectangle();
  540. Circle c = new Circle();
  541. Triangle t = new Triangle();
  542. Trapezoid tr = new Trapezoid();
  543. td.add(r);
  544. td.add(tr);
  545. td.add(c);
  546. td.add(t);
  547. Random rand = new Random();
  548. return td.get(rand.nextInt(td.size()));
  549.  
  550. }
  551.  
  552. private static ThreeD getThreeD() {
  553. ArrayList<ThreeD> td = new ArrayList<ThreeD>();
  554. Sphere s = new Sphere();
  555. Cube c = new Cube();
  556. Tetrahedron t = new Tetrahedron();
  557. td.add(s);
  558. td.add(c);
  559. td.add(t);
  560. Random rand = new Random();
  561. return td.get(rand.nextInt(td.size()));
  562.  
  563. }
  564.  
  565. private static void displayList(ArrayList<Shape> alist) {
  566. int i = 0;
  567. for (Shape s : alist) {
  568. i++;
  569. System.out.printf("\nShape %d: ", i);
  570. System.out.println(s.toString());
  571. String x = s.getClass().getName();
  572. System.out.print("------------------------------");
  573.  
  574. }
  575.  
  576. }
  577.  
  578. public static void main(String[] args) {
  579. // TODO Auto-generated method stub
  580. Random rand = new Random();
  581. int str = rand.nextInt(3);
  582. ArrayList<Shape> alist = new ArrayList<Shape>();
  583. while (str != 0) {
  584. switch (str) {
  585. case 1:
  586. TwoD get = getTwoD();
  587. if (get.getClass().getName().contains("Rectangle")) {
  588. Rectangle r = new Rectangle();
  589. r.set(getColor(), getInt(), getInt());
  590. alist.add(r);
  591. } else if (get.getClass().getName().contains("Circle")) {
  592. Circle c = new Circle();
  593. c.set(getColor(), getInt());
  594. alist.add(c);
  595. } else if (get.getClass().getName().contains("Triangle")) {
  596. Triangle t = new Triangle();
  597. t.set(getColor(), getInt(), getInt(), getInt());
  598. alist.add(t);
  599. } else {
  600. Trapezoid tr = new Trapezoid();
  601. tr.set(getColor(), getInt(), getInt(), getInt(), getInt());
  602. alist.add(tr);
  603. }
  604. break;
  605. case 2:
  606. ThreeD get2 = getThreeD();
  607. if (get2.getClass().getName().contains("Sphere")) {
  608. Sphere s = new Sphere();
  609. s.set(getColor(), getDouble());
  610. alist.add(s);
  611. } else if (get2.getClass().getName().contains("Cube")) {
  612. Cube c = new Cube();
  613. c.set(getColor(), getDouble());
  614. alist.add(c);
  615. } else {
  616. Tetrahedron t = new Tetrahedron();
  617. t.set(getColor(), getDouble());
  618. alist.add(t);
  619. }
  620. break;
  621. default:
  622. break;
  623. }
  624.  
  625. str = getInt();
  626. }
  627. displayList(alist);
  628.  
  629. }
  630.  
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement