【发布时间】:2014-12-03 09:41:55
【问题描述】:
我需要从文件中获取信息并将它们创建为对象并将它们放入数组中,以便我可以比较对象的区域并在数组中列出哪个对象具有最大的区域及其在数组中的位置。
我很困惑如何从文件中获取信息并将每个信息创建为一个对象(圆形或矩形),然后在创建该对象后将该对象分配到一个数组中。我认为我的其他课程还不错,我只是坚持完成驱动程序。
通常,我会做类似 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);
}
}
}
几何对象:
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 从您的数组映射到 Java 对象。我知道这不是您要问的,但它可能会使您的代码更简单。
-
作业/作业提及在哪里?
标签: java arrays object geometry area