2014-10-08 14 views
0

我需要从文件中获取信息并将它们创建到对象中并将它们放入数组中,以便我可以比较数组中对象和列表的区域,该对象具有最大面积和其在数组中的位置。如何从文件中的数据创建对象并将其分配给数组?

我在我如何利用从该文件的信息,并创建每一个到一个对象(圆或矩形),然后将该对象分配到一个数组它已经被创建之后困惑。我认为我的其他课程很好,我只是在完成驾驶员的工作。

通常情况下,我会做类似圆形C1 =新圈();创建一个新的对象,但是如何从一个预定义信息的文件中做到这一点,并将其分配给一个数组?

数据:

“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); 
    } 

} 
+0

您可以尝试使用orika从地图你的数组到Java对象。我知道这不是你问的,但它可能会让你的代码更简单。 – nbz 2014-10-08 17:04:25

+0

[如何从文件中读取数据并创建对象并将其分配给数组?](http://stackoverflow.com/questions/26241322/how-to-read-data-from-a-file -and-create-an-object-and-assign-it-to-an-array) – Spektre 2014-10-09 05:14:01

+0

分配/作业提及在哪里? – Spektre 2014-10-09 05:15:24

回答

0
  • 在文本文件中,您对您的形状物品的特殊报价。这会让你的生活更加困难,所以你应该改变这种状况,如果可能的话,如何使物体(从你的主要方法)
  • 例子:

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 }

相关问题