用于每个循环打印对象

时间:2015-10-13 04:39:32

标签: java object arraylist foreach

所以我需要使用一种方法来打印出矩形和圆形对象的内容。我希望他们格式化这样的

GeometricObject [color=red, filled=false, dateOfCreation=Wed Feb 11 12:21:51 EST
2015]
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]

我获得了方法签名所以我必须使用它,我尝试了两种不同的方法来测试对象是矩形还是圆形然后采取正确的操作但是我不能让它们打印任何东西(我相信我可能会遗漏我的公共类hw2redo中的一些内容,更具体地说是我的recreateObject方法)。我的目标是,如果对象等于一个圆(在我的recreateObject方法中,我有两个圆形或矩形的返回签名),那么它将使用Circle类中的printCircle方法,但没有输出。我的第二个逻辑路径是,如果对象列表包含单词" Rectangle"在它的任何地方,当然这意味着它是一个矩形对象,并将访问Rectangle类中的printRectangle方法,但同样没有。但是,如果我使用,我会注意到 ((Circle) o).printCircle(); 例如,它将为我的所有圆形对象调用printCircle方法,并在它到达我的第一个矩形对象时抛出异常(我想我已经关闭了!)。

private static void showObjects(ArrayList<GeometricObject> list) {

         for(GeometricObject o : list) {

             if ((o.equals("Circle")))
             ((Circle) o).printCircle();
             if (o.equals(list.contains("Rectangle")))
             ((Rectangle) o).printRectangle();
         }
    }

如果您有兴趣,这是我的全部代码。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;


public class hw2redo 
{
     public static void main(String[] args) throws FileNotFoundException {

          GeometricObject g = null;
          File diskFile = new File("file.txt");
          Scanner diskScanner = new Scanner(diskFile);
          ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
          while(diskScanner.hasNext()){
              String geolist = diskScanner.nextLine();
              g = recreateObject(geolist);

              list.add(g);

          }
          diskScanner.close();
         /* while (diskScanner.hasNext()) {
              String list = diskScanner.nextLine();
              g = recreateObject(list);
          }
          diskScanner.close();*/
          showObjects(list);
       }





    private static GeometricObject recreateObject(String data) {

          String[] list = data.split(",");
          String geoObject = list[0];

          if (geoObject.equals("Circle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]); 
             double radius = Double.valueOf(list[3]);
             return new Circle(radius, color, filled);
          }

          if (geoObject.equals("Rectangle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]);
             double height = Double.valueOf(list[3]);
             double width = Double.valueOf(list[4]);
             return new Rectangle(width, height, color, filled);
          }
        return null;


       }

    private static void showObjects(ArrayList<GeometricObject> list) {

         for(GeometricObject o : list) {

             if ((o.equals("Circle")))
             ((Circle) o).printCircle();
             if (o.equals(list.contains("Rectangle")))
             ((Rectangle) o).printRectangle();
         }
    }
}

abstract class GeometricObject {
       private String color = "white";
       private boolean filled;
       private java.util.Date dateCreated;
       private String data;

      /** Construct a default geometric object */
      public GeometricObject() {
          super();
     dateCreated = new java.util.Date();
     //this.data = data;
      }


    /** Construct a geometric object with the specified color
     * and filled value */
     public GeometricObject(String color, boolean filled) {
     super();
     dateCreated = new java.util.Date();
     this.color = color;
     this.filled = filled;

     }

     /** Return color */
     public String getColor() {
     return color;
     }

     /** Set a new color */
     public void setColor(String color) {
     this.color = color;
     }

     /** Return filled. Since filled is boolean,
     its getter method is named isFilled */
     public boolean isFilled() {
     return filled;
     }

     /** Set a new filled */
     public void setFilled(boolean filled) {
     this.filled = filled;
     }

     /** Get dateCreated */
     public java.util.Date getDateCreated() {
     return dateCreated;
     }

     /** Return a string representation of this object */
     public String toString() {
     return  "GeometricObject [color=" + color +", filled="+ filled + ", dateOfCreation=" + dateCreated +
             "]\n";

     }
     }
class Circle extends GeometricObject
{
private double radius;

public Circle() {
    super();
    radius = 1;

}


public Circle(double radius,
String color, boolean filled) {
    super(color, filled);
this.radius = radius;
setColor(color);
setFilled(filled);
}

/** Return radius */
public double getRadius() {
return radius;
}

/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}

/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
return 2 * radius;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}

/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}


class Rectangle extends GeometricObject {
private double width;
private double height;

public Rectangle() {
    super();
}



public Rectangle(
double width, double height, String color, boolean filled) {
    super(color, filled);
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

/** Return width */
public double getWidth() {
return width;
}

/** Set a new width */
public void setWidth(double width) {
this.width = width;
}

/** Return height */
public double getHeight() {
return height;
}

/** Set a new height */
public void setHeight(double height) {
this.height = height;
}

/** Return area */
public double getArea() {
return width * height;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
public void printRectangle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + width);
}
}

我的file.txt(我试图重新创建的对象)conatins

Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0

非常感谢任何正确方向的帮助或提示。万分感谢!

2 个答案:

答案 0 :(得分:1)

我没有在您的代码中看到.equals()方法被覆盖,以便您可以使用     if ((o.equals("Circle")))方法中的showObjects()

您需要在代码中适当地 处理.equals()方法

 if ( o instanceof Circle) 

而不是if ((o.equals("Circle")))方法中的showObjects()。您可能需要对其他对象(例如上面提到的Rectangle)执行相同的操作。

答案 1 :(得分:0)

  

我需要使用方法打印出内容   我的矩形和圆形物体的。

Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]
  

非常感谢任何正确方向的帮助或提示。

以下是Circle课程的提示。使用以下方法打印圆圈:

public void printCircle() {
        System.out.println("Circle [" +
                "radius=" + radius +
                "  Area=" + getArea() +
                "  Perimeter=" + getPerimeter() +
                ' ]');
    }

我没有检查你的逻辑,因为你需要自己学习它:)。