如何从文件中的数据创建对象并将其分配给数组?

时间:2014-10-08 16:27:19

标签: java arrays object geometry area

我需要从文件中获取信息并将它们创建为对象并将它们放入数组中,以便我可以比较对象的区域和数组中的列表,哪个对象具有最大的区域及其在数组中的位置。 / p>

我很困惑我如何从文件中获取信息并将每个信息创建为一个对象(圆形或矩形),然后在创建后将该对象分配到一个数组中。我认为我的其他课程都很好,我只是坚持完成驱动程序。

通常,我会做像Circle c1 = new Circle();创建一个新对象,但如何从具有预定义信息的文件中执行此操作并将其分配给数组?

数据:

“CIRCLE”, 1, “blue”, true
“RECTANGLE”, 1, 2, “blue”, true
“RECTANGLE”, 10, 2, “red”, true
“CIRCLE”, 2, “green”
“RECTANGLE”
“CIRCLE”

驱动:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Driver {
public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("C:/Users/Charles/Desktop/GeometricObjectsData.txt"));

    ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();

    while (input.hasNext()) {
        String line = input.nextLine();
        System.out.println(line);
    }
  }
}

GeometricObject:

public abstract class GeometricObject {
    //class variables
    private String color;
    private boolean filled;

    //constructors
    public GeometricObject() {
        super();
        color = "white";
        filled = false;
    }

    public GeometricObject(String color, boolean filled) {
        super();
        this.color = color;
        this.filled = filled;
    }

    //mutators
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    //user-defined methods
    public abstract double getArea();

    public abstract double getPerimeter();

    @Override
    public String toString() {
        return super.toString() + " \tColor=" + this.getColor() + " \tFilled=" + this.isFilled();
    }

}

界:

 public class Circle extends GeometricObject {
//class variables
private double radius;

//constructors
public Circle() {
    super();
    radius = 1;
}

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

//mutators
public double getRadius() {
    return radius;
}

public void setRadius(double radius) {
    this.radius = radius;
}

//user-defined methods
@Override
public double getArea() {
    //area of a circle
    return (radius * radius * Math.PI);
}

@Override
public double getPerimeter() {
    //perimeter of a circle
    return (2 * radius * Math.PI);
}

@Override
public String toString() {
    return super.toString() + "\nCircle: Radius=" + this.getRadius();
    }

}

矩形:

public class Rectangle extends GeometricObject {
    //class variables
    private double height;
    private double width;

    //constructors
    public Rectangle() {
        super();
        height = 1;
        width = 1;
    }
    public Rectangle(double height, double width, String color, boolean filled) {
        super(color,filled);
        this.height = height;
        this.width = width;
    }

    //mutators
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }

    //user-defined methods
    @Override
    public String toString() {
        return super.toString() + "\nRectangle: Height=" + this.height + "\tWidth=" + this.width;
    }
    @Override
    public double getArea() {
        return (height * width);
    }

    @Override
    public double getPerimeter() {
        return (2 * height + 2 * width);
    }

}

1 个答案:

答案 0 :(得分:0)

  • 在您的文字文件中,您的形状项目周围会有特殊引号。这将使您的生活更加困难,所以如果可能的话,您应该改变它
  • 如何制作对象的示例(来自您的main方法):

while (input.hasNext()) { String line = input.nextLine(); System.out.println(line); String[] parts = line.split(","); if (parts[0].indexOf("Circle") != -1) { Circle c = new Circle(); // ... parse the rest of the attributes to set up your circle } else if ... // fill in the other shape cases }