Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.File;
- import java.lang.*;
- import java.util.Arrays;
- /**
- * This program will perform the following:
- * -Shapes are created with pre-defined criteria.
- * -Shapes are sorted into an array called 'MySolids"
- * -Shapes have their area and volume calculated individually.
- * -Shapes have their total area and total volume calculated.
- * -Shapes are sorted by area, and then the shape with the max area is printed.
- * -Shapes are sorted by volume and the shape with the minimum volume is printed.
- * -Shapes are sorted one last time, from smallest to largest based on area and then printed.
- *
- * @author Carlos
- */
- public class ShapesAbstract {
- public static void main(String[] args) throws Exception {
- PrintWriter outpt;
- //now equate the internal name to an external file through the PrintWriter
- outpt = new PrintWriter(new File("JavaAbsOut.txt"));
- int i;
- //All shapes pre-defined before being placed in the "MySolids array"
- Cube Cube1 = new Cube(4, 4, 4, outpt);
- Cube Cube2 = new Cube(8, 8, 8, outpt);
- Sphere Sphere1 = new Sphere(6, 6, 6, outpt, 6);
- Sphere Sphere2 = new Sphere(3, 3, 3, outpt, 3);
- Cone Cone1 = new Cone(6, 6, 6, outpt, 5);
- Cone Cone2 = new Cone(3, 12, 12, outpt, 3);
- Brick Brick1 = new Brick(3, 6, 9, outpt);
- Brick Brick2 = new Brick(2, 4, 6, outpt);
- TCone TCone1 = new TCone(6, 6, 6, outpt, 5, 3);
- TCone TCone2 = new TCone(4, 4, 4, outpt, 8, 6);
- //The shapes having their areas and volumes calculated.
- System.out.println("\nAll Shapes Without Sorting:\n");
- outpt.println("\n All Shapes Without Sorting:\n");
- Brick1.calcArea();
- Brick1.calcVolume();
- Brick2.calcArea();
- Brick2.calcVolume();
- Cone1.calcArea();
- Cone1.calcVolume();
- Cone2.calcArea();
- Cone2.calcVolume();
- Cube1.calcArea();
- Cube1.calcVolume();
- Cube2.calcArea();
- Cube2.calcVolume();
- Sphere1.calcArea();
- Sphere1.calcVolume();
- Sphere2.calcArea();
- Sphere2.calcVolume();
- TCone1.calcArea();
- TCone1.calcVolume();
- TCone2.calcArea();
- TCone2.calcVolume();
- Brick1.print(outpt);
- Brick2.print(outpt);
- Cone1.print(outpt);
- Cone2.print(outpt);
- Cube1.print(outpt);
- Cube2.print(outpt);
- Sphere1.print(outpt);
- Sphere2.print(outpt);
- TCone1.print(outpt);
- TCone2.print(outpt);
- //An Array so that the solids can be referred to and sorted by Area.
- Solid[] mySolids = new Solid[10];
- mySolids[0] = Brick1;
- mySolids[1] = Brick2;
- mySolids[2] = Cone1;
- mySolids[3] = Cone2;
- mySolids[4] = Cube1;
- mySolids[5] = Cube2;
- mySolids[6] = Sphere1;
- mySolids[7] = Sphere2;
- mySolids[8] = TCone1;
- mySolids[9] = TCone2;
- SortSmallByArea(mySolids, 9);
- //Now We sort the shapes by Area
- Solids[] things = new Solids[10];
- for (i = 0; i <= 9; i++) things[i] = mySolids[i];
- SortSmallByArea(things, 9);
- //A new array so that the shapes can be referred to and sorted based on Volume
- Solid[] mySolidsVolume = new Solid[10];
- mySolidsVolume[0] = Brick1;
- mySolidsVolume[1] = Brick2;
- mySolidsVolume[2] = Cone1;
- mySolidsVolume[3] = Cone2;
- mySolidsVolume[4] = Cube1;
- mySolidsVolume[5] = Cube2;
- mySolidsVolume[6] = Sphere1;
- mySolidsVolume[7] = Sphere2;
- mySolidsVolume[8] = TCone1;
- mySolidsVolume[9] = TCone2;
- SortSmallByVolume(mySolidsVolume, 9);
- //Now lets sort the shapes by Volume so we can find the minimum
- Solids[] thingsVolume = new Solids[10];
- for (i = 0; i <= 9; i++) thingsVolume[i] = mySolidsVolume[i];
- SortSmallByVolume(thingsVolume, 9);
- //Printing out the sorted shapes
- System.out.println("All Shapes Sorted From Smaller To Larger By Area:\n");
- outpt.println("All Shapes Sorted From Smaller To Larger By Area\n");
- for (i = 0; i <= 9; i++) things[i].print(outpt);
- for (i = 0; i <= 9; i++) things[i].getVolume();
- //now we print the total area of all solids
- int fullArea = TotalArea(mySolids);
- System.out.println("\nTotal Area of All Shapes: ");
- System.out.println(fullArea);
- //now we print the total volume of all solids
- int fullVolume = TotalVolume(mySolids);
- System.out.println("\nTotal Volume of All Shapes: ");
- System.out.println(fullVolume);
- //now we print out the minimum Volume and maximum Area
- System.out.println("\nShape With the Minimum Volume: ");
- outpt.println("Minimum Volume: ");
- mySolidsVolume[0].print(outpt);
- System.out.println("\nShape with the Maximum Area: ");
- outpt.println("Maximum Area: ");
- mySolids[9].print(outpt);
- outpt.close();
- }
- /**
- * This method calculates the total area of the array using an enhanced for loop
- * @param x
- * @return
- */
- public static int TotalArea (Solids[] x){
- int totalArea = 0;
- for (Solids s: x) {
- totalArea += s.getArea();
- }
- return totalArea;
- }
- /**
- * This method calculates the total volume of the array using an enhanced for loop
- * @param x
- * @return
- */
- public static int TotalVolume(Solids[] x){
- int totalVolume=0;
- for (Solids s: x) {
- totalVolume += s.getVolume();
- }
- return totalVolume;
- }
- /**
- * This method sorts the array of solids based on their area,using a switch statement.
- *
- * @param x
- * @param xlast
- */
- public static void SortSmallByArea(Solids[] x, int xlast) {
- //this method will sort an array of Solids objects based on their CompareTo function
- Solids xsave;
- int isw = 1;
- while (isw == 1) {
- isw = 0;
- for (int i = 0; i <= xlast - 1; i++) {
- switch (x[i].compareTo(x[i + 1])) {
- case 1://the objects in array x are in the right order
- break;
- case -1://objects out of order, they must be changed.
- // This is where to the place holder is used to swap values
- xsave = x[i];
- x[i] = x[i + 1];
- x[i + 1] = xsave;
- isw = 1;
- break;
- default://objects are equal no change
- }
- }
- }
- }
- /**
- * This will sort the array of solids based on volume using a switch statment
- *
- * @param x
- * @param xlast
- */
- public static void SortSmallByVolume(Solids[] x, int xlast) {
- //this method will sort an array of Solids objects based on their CompareTo function
- Solids xsave;
- int isw = 1;
- while (isw == 1) {
- isw = 0;
- for (int i = 0; i <= xlast - 1; i++) {
- switch (x[i].compareToByVolume(x[i + 1])) {
- case 1://the objects in array x are in the right order
- break;
- case -1://objects out of order, they must be changed.
- // This is where to the place holder is used to swap values
- xsave = x[i];
- x[i] = x[i + 1];
- x[i + 1] = xsave;
- isw = 1;
- break;
- default://objects are equal no change
- }
- }
- }
- }
- }
- /**
- * The shapes class
- */
- abstract class Shape {
- //now define the abstract methods
- //No CONSTRUCTOR SINCE ABSTRACT CLASS CANNOT BE INSTANTIATED
- public abstract double getArea();
- public abstract String getName();
- public abstract double getVolume();
- }
- /**
- * The Solids class, implements CompareTo so that the objects can be compared via area.
- */
- class Solids extends Shape implements Comparable {
- protected int width;
- protected int length;
- protected int height;
- //Create the constructor for Point
- public Solids(int len, int wid, int hig, PrintWriter outf) {
- width = wid;
- length = len;
- height = hig;
- }
- //CompareTo for Area
- public int compareTo(Object o) {
- if (getArea() < ((Solids) o).getArea())
- return 1;
- else if (getArea() > ((Solids) o).getArea())
- return -1;
- else
- return 0;
- }
- //CompareTo for Volume
- public int compareToByVolume(Object o) {
- if (getVolume() < ((Solids) o).getVolume())
- return 1;
- else if (getVolume() > ((Solids) o).getVolume())
- return -1;
- else
- return 0;
- }
- public void setWidth(int width) {
- return;
- }
- public void setHeight(int height) {
- return;
- }
- public void setLength(int length) {
- return;
- }
- public int getWidth() {
- return width;
- }
- public int getLength() {
- return length;
- }
- public int getHeight() {
- return height;
- }
- public String getName() {
- return "Point";
- }
- public double getArea() {
- return 0.0;
- } //Got to have this for the compareTo
- public double getVolume() {
- return 0.0;
- }
- public void print(PrintWriter outf) {
- outf.println("Length " + length + " Width " + width + " Height " + height);
- System.out.println("Length: " + length + " Width: " + width + " Height: " + height);
- return;
- }
- }
- class Solid extends Solids {
- /* Solid is the parent of Cone, TCone, Brick, Cube, & Sphere. These derived classes are compared based on their areas and can
- only be compared at the Solid level.*/
- protected double area;
- protected double volume;
- public Solid(int length, int width, int height, PrintWriter outf) {
- super(length, width, height, outf);
- }
- public double getArea() {
- return area;
- }//we need this because we will use a getArea from the lower group
- public void calcArea() {
- return;
- }//we need this so we can calculate the area of there it is a place holder
- public void print(PrintWriter outf) {
- return;
- }//we need to print others
- public String getName() {
- return "Solid";
- }
- public double getVolume() {
- return volume;
- }
- public void calcVolume() {
- return;
- }//we need this for calculation of others
- }//end of Solid class
- class Cube extends Solid {
- //now the constructor for Cube
- public Cube(int length, int width, int height, PrintWriter outf) {
- super(length, width, height, outf);
- }
- public void calcArea() {
- area = 6 * width * width;
- }
- public void calcVolume() {
- volume = width * width * width;
- }
- public String getName() {
- return " Cube ";
- }
- public void print(PrintWriter outf) {
- outf.println("Cube with Width " + width + " Length " + length + " Height " + height);
- System.out.println("Cube with a Width of: " + width + " Length of: " + length + " Height of: " + height);
- outf.println(" Area of:" + area);
- System.out.println("Area: " + area);
- outf.println(" Volume " + volume + "\n");
- System.out.println("Volume of: " + volume + "\n");
- }
- }//End of Cube
- class Sphere extends Solid {
- protected int radius;
- //now the constructor for sphere
- public Sphere(int length, int width, int height, PrintWriter outf, int radius) {
- super(length, width, height, outf);
- this.radius = radius;
- }
- public void calcArea() {
- area = 3.14 * 2 * radius * 2 * radius;
- }
- public void calcVolume() {
- volume = 4.0 / 3.0 * 3.14 * radius * radius * radius;
- }
- public String getName() {
- return " Sphere ";
- }
- public void print(PrintWriter outf) {
- outf.print("Sphere Length " + length + " Width " + width + " height " + height + " Radius " + radius);
- System.out.println("Sphere with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Radius of: " + radius);
- outf.println("\n Area " + area);
- System.out.println("Area of: " + area);
- outf.println(" Volume " + volume + "\n");
- System.out.println("Volume of: " + volume + "\n");
- }
- }//end of sphere
- class Brick extends Solid {
- //now the constructor for Brick
- public Brick(int length, int width, int height, PrintWriter outf) {
- super(length, width, height, outf);
- }
- public void calcArea() {
- area = 2 * length * width + 2 * width * height + 2 * height * length;
- }
- public void calcVolume() {
- volume = length * width * height;
- }
- public String getName() {
- return " Brick ";
- }
- public void print(PrintWriter outf) {
- outf.println("Brick with Length " + length + " Width " + width + " Height " + height);
- System.out.println("Brick with a Length of: " + length + " Width of: " + width + " Height of: " + height);
- outf.println(" Area " + area);
- System.out.println("Area of: " + area);
- outf.println(" Volume " + volume + "\n");
- System.out.println("Volume of: " + volume + "\n");
- }
- }//end Brick
- class Cone extends Solid {
- protected int radius;
- //now the constructor for sphere
- public Cone(int length, int width, int height, PrintWriter outf, int radius) {
- super(length, width, height, outf);
- this.radius = radius;
- }
- public void calcArea() {
- area = 3.14 * radius * (radius + Math.sqrt((height * height) + (radius * radius)));
- }
- public void calcVolume() {
- volume = 3.14 * ((radius * radius) * (height / 3));
- }
- public String getName() {
- return " Cone ";
- }
- public void print(PrintWriter outf) {
- outf.print("Cone with Length " + length + " Width " + width + " Height " + height + " Radius " + radius);
- System.out.println("Cone with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Radius of: " + radius);
- outf.println("\n Area " + area);
- System.out.println("Area of: " + area);
- outf.println(" Volume " + volume + "\n");
- System.out.println("Volume of: " + volume + "\n");
- }
- }//end of Cone
- class TCone extends Solid {
- protected int radius;
- protected int radius2;
- //now the constructor for sphere
- public TCone(int width, int length, int height, PrintWriter outf, int radius, int radius2) {
- super(width, length, height, outf);
- this.radius = radius;
- this.radius2 = radius2;
- }
- public void calcArea() {
- area = 3.14 * ((radius2 + radius) * Math.sqrt((height * height) + ((radius - radius2) * (radius - radius2))) + (radius2 * radius2) + (radius * radius));
- }
- public void calcVolume() {
- volume = 3.14 * height * ((radius * radius) + (radius2 * radius2) + radius * radius2) / 3;
- }
- public String getName() {
- return " TCone ";
- }
- public void print(PrintWriter outf) {
- outf.print("Truncated Cone Length " + length + " Width " + width + " Height " + height + " Base Radius " + radius + " Top Radius " + radius2);
- System.out.println("Truncated Cone with a Length of: " + length + " Width of: " + width + " Height of: " + height + " Base Radius of: " + radius + " Top Radius of: " + radius2);
- outf.println("\n Area " + area);
- System.out.println("Area of: " + area);
- outf.println(" Volume " + volume + "\n");
- System.out.println("Volume of: " + volume + "\n");
- }
- }//end of TCone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement