首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于每个循环打印对象

用于每个循环打印对象
EN

Stack Overflow用户
提问于 2015-10-13 04:39:32
回答 2查看 1.6K关注 0票数 0

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

代码语言:javascript
运行
复制
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方法中,我有两个圆圈或矩形的返回签名),那么它将在循环类中使用printCircle方法,但没有输出。我的第二个逻辑路径是,如果对象列表中的任何地方都包含“矩形”一词,当然这意味着它是一个矩形对象,并且将访问矩形类中的printRectangle方法,但同样没有。但是,我会注意到,如果我只使用((Circle) o).printCircle(); --例如,它将对我的所有循环对象调用printCircle方法,并在它到达我的第一个矩形对象时抛出一个异常(我想我已经接近了!)

代码语言:javascript
运行
复制
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();
         }
    }

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

代码语言:javascript
运行
复制
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 (我试图重新创建的对象)conatin

代码语言:javascript
运行
复制
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

任何正确方向的帮助或暗示都将不胜感激。谢谢你一吨!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-13 04:56:49

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

您需要在代码中适当地处理.equals()方法,或者在代码中使用。

代码语言:javascript
运行
复制
 if ( o instanceof Circle) 

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

票数 1
EN

Stack Overflow用户

发布于 2015-10-13 04:45:45

我需要使用一种方法来打印我的矩形和圆圈对象的内容。

代码语言:javascript
运行
复制
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]

任何正确方向的帮助或暗示都将不胜感激。

下面是Circle类的一个提示。使用以下方法打印圆圈:

代码语言:javascript
运行
复制
public void printCircle() {
        System.out.println("Circle [" +
                "radius=" + radius +
                "  Area=" + getArea() +
                "  Perimeter=" + getPerimeter() +
                ' ]');
    }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33094249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档