Advertisement
Luninariel

Shapes - WIP

Jan 28th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.File;
  3. import java.lang.*;
  4. import java.util.Arrays;
  5.  
  6. /**
  7. * This program will perform the following:
  8. * -Shapes are created with pre-defined criteria.
  9. * -Shapes are sorted into an array called 'MySolids"
  10. * -Shapes have their area and volume calculated individually.
  11. * -Shapes have their total area and total volume calculated.
  12. * -Shapes are sorted by area, and then the shape with the max area is printed.
  13. * -Shapes are sorted by volume and the shape with the minimum volume is printed.
  14. * -Shapes are sorted one last time, from smallest to largest based on area and then printed.
  15. *
  16. * @author Carlos
  17. */
  18.  
  19. public class ShapesAbstract {
  20.  
  21. public static void main(String[] args) throws Exception {
  22.  
  23.  
  24. PrintWriter outpt;
  25. //now equate the internal name to an external file through the PrintWriter
  26.  
  27. outpt = new PrintWriter(new File("JavaAbsOut.txt"));
  28. int i;
  29.  
  30. //All shapes pre-defined before being placed in the "MySolids array"
  31. Cube Cube1 = new Cube(4, 4, 4, outpt);
  32. Cube Cube2 = new Cube(8, 8, 8, outpt);
  33. Sphere Sphere1 = new Sphere(6, 6, 6, outpt, 6);
  34. Sphere Sphere2 = new Sphere(3, 3, 3, outpt, 3);
  35. Cone Cone1 = new Cone(6, 6, 6, outpt, 5);
  36. Cone Cone2 = new Cone(3, 12, 12, outpt, 3);
  37. Brick Brick1 = new Brick(3, 6, 9, outpt);
  38. Brick Brick2 = new Brick(2, 4, 6, outpt);
  39. TCone TCone1 = new TCone(6, 6, 6, outpt, 5, 3);
  40. TCone TCone2 = new TCone(4, 4, 4, outpt, 8, 6);
  41.  
  42. //The shapes having their areas and volumes calculated.
  43. System.out.println("\nAll Shapes Without Sorting:\n");
  44. outpt.println("\n All Shapes Without Sorting:\n");
  45. Brick1.calcArea();
  46. Brick1.calcVolume();
  47. Brick2.calcArea();
  48. Brick2.calcVolume();
  49. Cone1.calcArea();
  50. Cone1.calcVolume();
  51. Cone2.calcArea();
  52. Cone2.calcVolume();
  53. Cube1.calcArea();
  54. Cube1.calcVolume();
  55. Cube2.calcArea();
  56. Cube2.calcVolume();
  57. Sphere1.calcArea();
  58. Sphere1.calcVolume();
  59. Sphere2.calcArea();
  60. Sphere2.calcVolume();
  61. TCone1.calcArea();
  62. TCone1.calcVolume();
  63. TCone2.calcArea();
  64. TCone2.calcVolume();
  65. Brick1.print(outpt);
  66. Brick2.print(outpt);
  67. Cone1.print(outpt);
  68. Cone2.print(outpt);
  69. Cube1.print(outpt);
  70. Cube2.print(outpt);
  71. Sphere1.print(outpt);
  72. Sphere2.print(outpt);
  73. TCone1.print(outpt);
  74. TCone2.print(outpt);
  75.  
  76. //An Array so that the solids can be referred to and sorted by Area.
  77. Solid[] mySolids = new Solid[10];
  78. mySolids[0] = Brick1;
  79. mySolids[1] = Brick2;
  80. mySolids[2] = Cone1;
  81. mySolids[3] = Cone2;
  82. mySolids[4] = Cube1;
  83. mySolids[5] = Cube2;
  84. mySolids[6] = Sphere1;
  85. mySolids[7] = Sphere2;
  86. mySolids[8] = TCone1;
  87. mySolids[9] = TCone2;
  88. SortSmallByArea(mySolids, 9);
  89. //Now We sort the shapes by Area
  90. Solids[] things = new Solids[10];
  91. for (i = 0; i <= 9; i++) things[i] = mySolids[i];
  92. SortSmallByArea(things, 9);
  93. //A new array so that the shapes can be referred to and sorted based on Volume
  94.  
  95. Solid[] mySolidsVolume = new Solid[10];
  96. mySolidsVolume[0] = Brick1;
  97. mySolidsVolume[1] = Brick2;
  98. mySolidsVolume[2] = Cone1;
  99. mySolidsVolume[3] = Cone2;
  100. mySolidsVolume[4] = Cube1;
  101. mySolidsVolume[5] = Cube2;
  102. mySolidsVolume[6] = Sphere1;
  103. mySolidsVolume[7] = Sphere2;
  104. mySolidsVolume[8] = TCone1;
  105. mySolidsVolume[9] = TCone2;
  106. SortSmallByVolume(mySolidsVolume, 9);
  107. //Now lets sort the shapes by Volume so we can find the minimum
  108. Solids[] thingsVolume = new Solids[10];
  109. for (i = 0; i <= 9; i++) thingsVolume[i] = mySolidsVolume[i];
  110. SortSmallByVolume(thingsVolume, 9);
  111.  
  112. //Printing out the sorted shapes
  113. System.out.println("All Shapes Sorted From Smaller To Larger By Area:\n");
  114. outpt.println("All Shapes Sorted From Smaller To Larger By Area\n");
  115. for (i = 0; i <= 9; i++) things[i].print(outpt);
  116. for (i = 0; i <= 9; i++) things[i].getVolume();
  117.  
  118.  
  119. //now we print the total area of all solids
  120. int fullArea = TotalArea(mySolids);
  121. System.out.println("\nTotal Area of All Shapes: ");
  122. System.out.println(fullArea);
  123.  
  124. //now we print the total volume of all solids
  125. int fullVolume = TotalVolume(mySolids);
  126. System.out.println("\nTotal Volume of All Shapes: ");
  127. System.out.println(fullVolume);
  128.  
  129. //now we print out the minimum Volume and maximum Area
  130. System.out.println("\nShape With the Minimum Volume: ");
  131. outpt.println("Minimum Volume: ");
  132. mySolidsVolume[0].print(outpt);
  133. System.out.println("\nShape with the Maximum Area: ");
  134. outpt.println("Maximum Area: ");
  135. mySolids[9].print(outpt);
  136. outpt.close();
  137.  
  138. }
  139.  
  140. /**
  141. * This method calculates the total area of the array using an enhanced for loop
  142. * @param x
  143. * @return
  144. */
  145. public static int TotalArea (Solids[] x){
  146. int totalArea = 0;
  147.  
  148. for (Solids s: x) {
  149. totalArea += s.getArea();
  150.  
  151. }
  152.  
  153.  
  154. return totalArea;
  155. }
  156.  
  157. /**
  158. * This method calculates the total volume of the array using an enhanced for loop
  159. * @param x
  160. * @return
  161. */
  162. public static int TotalVolume(Solids[] x){
  163. int totalVolume=0;
  164.  
  165. for (Solids s: x) {
  166. totalVolume += s.getVolume();
  167.  
  168. }
  169.  
  170. return totalVolume;
  171. }
  172. /**
  173. * This method sorts the array of solids based on their area,using a switch statement.
  174. *
  175. * @param x
  176. * @param xlast
  177. */
  178. public static void SortSmallByArea(Solids[] x, int xlast) {
  179. //this method will sort an array of Solids objects based on their CompareTo function
  180. Solids xsave;
  181. int isw = 1;
  182. while (isw == 1) {
  183. isw = 0;
  184. for (int i = 0; i <= xlast - 1; i++) {
  185. switch (x[i].compareTo(x[i + 1])) {
  186. case 1://the objects in array x are in the right order
  187. break;
  188. case -1://objects out of order, they must be changed.
  189. // This is where to the place holder is used to swap values
  190. xsave = x[i];
  191. x[i] = x[i + 1];
  192. x[i + 1] = xsave;
  193. isw = 1;
  194. break;
  195. default://objects are equal no change
  196. }
  197. }
  198. }
  199. }
  200.  
  201. /**
  202. * This will sort the array of solids based on volume using a switch statment
  203. *
  204. * @param x
  205. * @param xlast
  206. */
  207. public static void SortSmallByVolume(Solids[] x, int xlast) {
  208. //this method will sort an array of Solids objects based on their CompareTo function
  209. Solids xsave;
  210. int isw = 1;
  211. while (isw == 1) {
  212. isw = 0;
  213. for (int i = 0; i <= xlast - 1; i++) {
  214. switch (x[i].compareToByVolume(x[i + 1])) {
  215. case 1://the objects in array x are in the right order
  216. break;
  217. case -1://objects out of order, they must be changed.
  218. // This is where to the place holder is used to swap values
  219. xsave = x[i];
  220. x[i] = x[i + 1];
  221. x[i + 1] = xsave;
  222. isw = 1;
  223. break;
  224. default://objects are equal no change
  225. }
  226. }
  227. }
  228. }
  229. }
  230.  
  231. /**
  232. * The shapes class
  233. */
  234. abstract class Shape {
  235. //now define the abstract methods
  236. //No CONSTRUCTOR SINCE ABSTRACT CLASS CANNOT BE INSTANTIATED
  237. public abstract double getArea();
  238.  
  239. public abstract String getName();
  240.  
  241. public abstract double getVolume();
  242.  
  243. }
  244.  
  245. /**
  246. * The Solids class, implements CompareTo so that the objects can be compared via area.
  247. */
  248. class Solids extends Shape implements Comparable {
  249. protected int width;
  250. protected int length;
  251. protected int height;
  252.  
  253. //Create the constructor for Point
  254. public Solids(int len, int wid, int hig, PrintWriter outf) {
  255. width = wid;
  256. length = len;
  257. height = hig;
  258. }
  259.  
  260. //CompareTo for Area
  261. public int compareTo(Object o) {
  262. if (getArea() < ((Solids) o).getArea())
  263. return 1;
  264. else if (getArea() > ((Solids) o).getArea())
  265. return -1;
  266. else
  267. return 0;
  268. }
  269.  
  270. //CompareTo for Volume
  271. public int compareToByVolume(Object o) {
  272. if (getVolume() < ((Solids) o).getVolume())
  273. return 1;
  274. else if (getVolume() > ((Solids) o).getVolume())
  275. return -1;
  276. else
  277. return 0;
  278. }
  279.  
  280. public void setWidth(int width) {
  281. return;
  282. }
  283.  
  284. public void setHeight(int height) {
  285. return;
  286. }
  287.  
  288. public void setLength(int length) {
  289. return;
  290. }
  291.  
  292. public int getWidth() {
  293. return width;
  294. }
  295.  
  296. public int getLength() {
  297. return length;
  298. }
  299.  
  300. public int getHeight() {
  301. return height;
  302. }
  303.  
  304. public String getName() {
  305. return "Point";
  306. }
  307.  
  308. public double getArea() {
  309. return 0.0;
  310. } //Got to have this for the compareTo
  311.  
  312. public double getVolume() {
  313. return 0.0;
  314. }
  315.  
  316. public void print(PrintWriter outf) {
  317. outf.println("Length " + length + " Width " + width + " Height " + height);
  318. System.out.println("Length: " + length + " Width: " + width + " Height: " + height);
  319. return;
  320.  
  321. }
  322. }
  323.  
  324. class Solid extends Solids {
  325. /* Solid is the parent of Cone, TCone, Brick, Cube, & Sphere. These derived classes are compared based on their areas and can
  326. only be compared at the Solid level.*/
  327. protected double area;
  328. protected double volume;
  329.  
  330. public Solid(int length, int width, int height, PrintWriter outf) {
  331. super(length, width, height, outf);
  332. }
  333.  
  334. public double getArea() {
  335. return area;
  336. }//we need this because we will use a getArea from the lower group
  337.  
  338. public void calcArea() {
  339. return;
  340. }//we need this so we can calculate the area of there it is a place holder
  341.  
  342. public void print(PrintWriter outf) {
  343. return;
  344. }//we need to print others
  345.  
  346. public String getName() {
  347. return "Solid";
  348. }
  349.  
  350. public double getVolume() {
  351. return volume;
  352. }
  353.  
  354. public void calcVolume() {
  355. return;
  356. }//we need this for calculation of others
  357. }//end of Solid class
  358.  
  359. class Cube extends Solid {
  360. //now the constructor for Cube
  361. public Cube(int length, int width, int height, PrintWriter outf) {
  362. super(length, width, height, outf);
  363. }
  364.  
  365. public void calcArea() {
  366. area = 6 * width * width;
  367. }
  368.  
  369. public void calcVolume() {
  370. volume = width * width * width;
  371. }
  372.  
  373. public String getName() {
  374. return " Cube ";
  375. }
  376.  
  377. public void print(PrintWriter outf) {
  378. outf.println("Cube with Width " + width + " Length " + length + " Height " + height);
  379. System.out.println("Cube with a Width of: " + width + " Length of: " + length + " Height of: " + height);
  380. outf.println(" Area of:" + area);
  381. System.out.println("Area: " + area);
  382. outf.println(" Volume " + volume + "\n");
  383. System.out.println("Volume of: " + volume + "\n");
  384. }
  385. }//End of Cube
  386.  
  387. class Sphere extends Solid {
  388. protected int radius;
  389.  
  390. //now the constructor for sphere
  391. public Sphere(int length, int width, int height, PrintWriter outf, int radius) {
  392. super(length, width, height, outf);
  393. this.radius = radius;
  394. }
  395.  
  396. public void calcArea() {
  397. area = 3.14 * 2 * radius * 2 * radius;
  398. }
  399.  
  400. public void calcVolume() {
  401. volume = 4.0 / 3.0 * 3.14 * radius * radius * radius;
  402. }
  403.  
  404. public String getName() {
  405. return " Sphere ";
  406. }
  407.  
  408. public void print(PrintWriter outf) {
  409. outf.print("Sphere Length " + length + " Width " + width + " height " + height + " Radius " + radius);
  410. System.out.println("Sphere with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Radius of: " + radius);
  411. outf.println("\n Area " + area);
  412. System.out.println("Area of: " + area);
  413. outf.println(" Volume " + volume + "\n");
  414. System.out.println("Volume of: " + volume + "\n");
  415. }
  416. }//end of sphere
  417.  
  418. class Brick extends Solid {
  419. //now the constructor for Brick
  420. public Brick(int length, int width, int height, PrintWriter outf) {
  421. super(length, width, height, outf);
  422. }
  423.  
  424. public void calcArea() {
  425. area = 2 * length * width + 2 * width * height + 2 * height * length;
  426. }
  427.  
  428. public void calcVolume() {
  429. volume = length * width * height;
  430. }
  431.  
  432. public String getName() {
  433. return " Brick ";
  434. }
  435.  
  436. public void print(PrintWriter outf) {
  437. outf.println("Brick with Length " + length + " Width " + width + " Height " + height);
  438. System.out.println("Brick with a Length of: " + length + " Width of: " + width + " Height of: " + height);
  439. outf.println(" Area " + area);
  440. System.out.println("Area of: " + area);
  441. outf.println(" Volume " + volume + "\n");
  442. System.out.println("Volume of: " + volume + "\n");
  443.  
  444. }
  445. }//end Brick
  446.  
  447. class Cone extends Solid {
  448. protected int radius;
  449.  
  450. //now the constructor for sphere
  451. public Cone(int length, int width, int height, PrintWriter outf, int radius) {
  452. super(length, width, height, outf);
  453. this.radius = radius;
  454. }
  455.  
  456. public void calcArea() {
  457. area = 3.14 * radius * (radius + Math.sqrt((height * height) + (radius * radius)));
  458. }
  459.  
  460. public void calcVolume() {
  461. volume = 3.14 * ((radius * radius) * (height / 3));
  462. }
  463.  
  464. public String getName() {
  465. return " Cone ";
  466. }
  467.  
  468. public void print(PrintWriter outf) {
  469. outf.print("Cone with Length " + length + " Width " + width + " Height " + height + " Radius " + radius);
  470. System.out.println("Cone with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Radius of: " + radius);
  471. outf.println("\n Area " + area);
  472. System.out.println("Area of: " + area);
  473. outf.println(" Volume " + volume + "\n");
  474. System.out.println("Volume of: " + volume + "\n");
  475. }
  476. }//end of Cone
  477.  
  478. class TCone extends Solid {
  479. protected int radius;
  480. protected int radius2;
  481.  
  482. //now the constructor for sphere
  483. public TCone(int width, int length, int height, PrintWriter outf, int radius, int radius2) {
  484. super(width, length, height, outf);
  485. this.radius = radius;
  486. this.radius2 = radius2;
  487. }
  488.  
  489. public void calcArea() {
  490. area = 3.14 * ((radius2 + radius) * Math.sqrt((height * height) + ((radius - radius2) * (radius - radius2))) + (radius2 * radius2) + (radius * radius));
  491. }
  492.  
  493. public void calcVolume() {
  494. volume = 3.14 * height * ((radius * radius) + (radius2 * radius2) + radius * radius2) / 3;
  495. }
  496.  
  497. public String getName() {
  498. return " TCone ";
  499. }
  500.  
  501. public void print(PrintWriter outf) {
  502. outf.print("Truncated Cone Length " + length + " Width " + width + " Height " + height + " Base Radius " + radius + " Top Radius " + radius2);
  503. System.out.println("Truncated Cone with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Base Radius of: " + radius + " Top Radius of: " + radius2);
  504. outf.println("\n Area " + area);
  505. System.out.println("Area of: " + area);
  506. outf.println(" Volume " + volume + "\n");
  507. System.out.println("Volume of: " + volume + "\n");
  508. }
  509. }//end of TCone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement