파일에서 정보를 가져 와서 객체로 만들고이를 배열에 넣을 필요가 있습니다. 그래서 객체의 영역을 비교할 수 있고 어떤 객체가 배열에서 가장 큰 영역과 위치를 갖는 배열에 나열 할 수 있습니다.파일의 데이터에서 객체를 만들고 배열에 할당하는 방법은 무엇입니까?
파일에서 정보를 가져 와서 객체 (원 또는 직사각형)에 각각을 작성한 다음 해당 객체를 작성한 후 배열에 할당하는 방법에 대해 혼란 스럽습니다. 나는 나의 다른 클래스 학생들이 멋지다라고 생각한다, 나는 운전자를 끝내는 것에 단지 붙이게된다.
일반적으로 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);
}
}
할 수 있습니다에서 매핑 orika 사용해보십시오 자바 객체에 대한 배열. 나는 그것이 당신이 요구하는 것이 아니라 당신의 코드를 더 간단하게 만들 수 있음을 압니다. – nbz
가능한 복제본 [파일에서 데이터를 읽고 개체를 만들어 배열에 할당하는 방법] (http://stackoverflow.com/questions/26241322/how-to-read-data-from-a-file - 및 - 객체 -와 - 할당 - 그것 - 배열을 생성) – Spektre
어디 assigment/숙제 언급은 무엇입니까? – Spektre