示例#1
0
  public static void main(String[] args) {

    ConsoleIO con = new ConsoleIO();

    Shape tri = new Triangle();
    tri.name = "Triangle";
    tri.setColor = "Yellow";

    tri.getArea();
    tri.getPerimeter();

    Shape sq = new Square();
    System.out.println("square");
    sq.setColor = "blue";

    sq.getArea();
    sq.getPerimeter();

    Shape rec = new Rectangle();
    System.out.println("rectangle");
    rec.setColor = "Green";

    rec.getArea();
    rec.getPerimeter();

    Shape circ = new Circle();
    System.out.println("circle");
    circ.setColor = "Red";

    circ.getArea();
    circ.getPerimeter();
  }
示例#2
0
文件: Shape.java 项目: thu/DemoJavaSE
  public static void main(String[] args) {
    // square
    Square square = new Square(5.6);
    System.out.println("area: " + square.getArea());
    System.out.println("perimeter: " + square.getPerimeter());

    // rectangle
    Rectangle rectangle = new Rectangle(1.2, 3.4);
    System.out.println("area: " + rectangle.getArea());
    System.out.println("perimeter: " + rectangle.getPerimeter());

    // circle
    Circle circle = new Circle(1.2);
    System.out.println("area: " + circle.getArea());
    System.out.println("perimeter: " + circle.getPerimeter());

    // triangle
    Triangle triangle = new Triangle(1.2, 1.2, 1.2);
    System.out.println("area: " + triangle.getArea());
    System.out.println("perimeter: " + triangle.getPerimeter());

    // shape
    Shape shape = new Circle(1);
    System.out.println("area: " + shape.getArea());
    System.out.println("perimeter: " + shape.getPerimeter());
  }
  @Override
  public double getArea(SpatialContext ctx) {
    double MAX_AREA = bbox.getArea(ctx);
    double sum = 0;
    for (Shape geom : shapes) {
      sum += geom.getArea(ctx);
      if (sum >= MAX_AREA) return MAX_AREA;
    }

    return sum;
  }
示例#4
0
  public static void main(String[] args) {
    List<Shape> shapes = new ArrayList<>();
    // Static Factory Method
    Shape square = Shape.createSquareWithSidesOfLength(5);
    shapes.add(square);

    // Abstract Factory
    Shape circle = new CircleFactory().create(5);
    shapes.add(circle);

    for (Shape shape : shapes) {
      System.out.println(
          shape.getName()
              + " has an area of: "
              + shape.getArea()
              + " and a perimeter of: "
              + shape.getPerimeter());
    }
  }
示例#5
0
  public static void main(String[] args) {
    File file = new File("./shapesInput.txt");
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
      fin = new FileInputStream(file);
      while ((ch = fin.read()) != -1) {
        strContent.append((char) ch);
      }
      fin.close();
    } catch (FileNotFoundException e) {
      System.out.println("File" + file.getAbsolutePath() + " could not be found on filesystem");
    } catch (IOException ioe) {
      System.out.println("Exception while reading the file" + ioe);
    }
    String fileOutput = strContent.toString();
    String[] commands = fileOutput.split(";");
    List<Shape> shapes = new ArrayList<Shape>();
    List<Double> areas = new ArrayList<Double>();
    List<Double> areasD = new ArrayList<Double>();
    for (int i = 0; i <= commands.length - 1; i++) {
      String codeLine = commands[i];
      String shapeType = codeLine.substring(17, 20);
      switch (shapeType) {
        case "Cir":
          String rad = codeLine.substring(23, codeLine.length());
          rad = rad.replace("(", "");
          rad = rad.replace(")", "");
          int radius;
          radius = Integer.parseInt(rad);
          shapes.add(new Circle(radius));
          break;
        case "Rec":
          String len = codeLine.substring(26, codeLine.length());
          len = len.replace("(", "");
          len = len.replace(")", "");
          String[] params = len.split(",");
          String h = params[0];
          String l = params[1];
          int height = Integer.parseInt(h);
          int length = Integer.parseInt(l);
          shapes.add(new Rectangle(height, length));
          break;
        case "Rho":
          String stuff = codeLine.substring(24, codeLine.length());
          stuff = stuff.replace("(", "");
          stuff = stuff.replace(")", "");
          String[] parame = stuff.split(",");
          String first = parame[0];
          String second = parame[1];
          int fir = Integer.parseInt(first);
          int sec = Integer.parseInt(second);
          shapes.add(new Rhombus(fir, sec));
          break;
        case "Tra":
          String total = codeLine.substring(26, codeLine.length());
          total = total.replace("(", "");
          total = total.replace(")", "");
          String[] seperated = total.split(",");
          String onep = seperated[0];
          String twop = seperated[1];
          String threep = seperated[2];
          int op = Integer.parseInt(onep);
          int tp = Integer.parseInt(twop);
          int thp = Integer.parseInt(threep);
          shapes.add(new Trapezoid(op, tp, thp));
          break;
        case "Tri":
          String measures = codeLine.substring(25, codeLine.length());
          measures = measures.replace("(", "");
          measures = measures.replace(")", "");
          String[] listp = measures.split(",");
          String aaa = listp[0];
          String bbb = listp[1];
          int aa = Integer.parseInt(aaa);
          int bb = Integer.parseInt(bbb);
          shapes.add(new Triangle(aa, bb));
          break;
      }
    }
    for (int q = 0; q <= shapes.size(); q++) {
      for (int i = 0; i <= shapes.size() - 2; i++) {
        if (shapes.get(i).getArea() > shapes.get(i + 1).getArea()) {
          Shape temp;
          temp = shapes.get(i);
          shapes.set(i, shapes.get(i + 1));
          shapes.set(i + 1, temp);
        } else {

        }
      }
    }
    for (Shape s : shapes) {
      System.out.println("\nCalculating " + s.getShapeName() + " area:");
      System.out.println("Area = " + s.getArea());
      System.out.println("Printing " + s.getShapeName() + " description:");
      s.printDescription();
    }
  }
示例#6
0
 public void printAreas() {
   System.out.println("Calculating matrices");
   for (Shape shape : shapes) System.out.println("Shape size is: " + shape.getArea());
 }