【发布时间】:2015-12-25 01:47:10
【问题描述】:
我正在尝试打印出我的数组的内容,该数组具有从文本文件中分配给它的值。但是我遇到了两个错误,任何帮助将不胜感激。 我的代码是:
import java.util.*;
import java.io.*;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
GeometricObject g = null;
File diskFile = new File("src/file.txt");
Scanner diskScanner = new Scanner(diskFile);
while (diskScanner.hasNext()) {
String list = diskScanner.nextLine();
g = recreateObject(list);
}
diskScanner.close();
}
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(color, filled, radius);
}
if (geoObject.equals("Rectangle")) {
String color = list[1];
boolean filled = Boolean.valueOf(list[2]);
double length = Double.valueOf(list[3]);
double width = Double.valueOf(list[4]);
return new Rectangle(color, filled, length, width);
}
return null;
}
}
但是,我必须创建三个新类来消除错误。分配要求该方法完全像这样编写 - private static GeometricObject recreateObject(String data) 。我不确定为什么需要创建新类来消除这些错误。
public class Circle extends GeometricObject
{
}
public class GeometricObject
{
}
public class Rectangle extends GeometricObject
{
}
我仍然有两个错误:
- 构造函数 Circle(String, boolean, double) 未定义。
- 构造函数 Rectangle(String, boolean, double, double) 未定义。
任何帮助都将不胜感激,因为我已经尝试了大约 4 个小时,但无济于事。
我的 file.txt 包含
Circle,red,false,4.0
Circle,blue,true,2.0
Circle,blue,true,10.0
Rectangle,yellow,true,10.0,6.0
Rectangle,green,true,5.0,11.0
Rectangle,red,true,20.0,15.0
这是实际的任务
The goal is to read persistent data from disk, use the data to recreate a collection of geometric objects, and
apply a series of operations on them as indicated below.
1. Read each disk record, create the corresponding object (Circle or Rectangle), add the object to a
dynamic array called list.
2. Nicely print the contents of the list (indicate position and content).
3. Write a method to find the position on the list holding the largest element (biggest area of them all).
4. Print the position and data (including area) of the selected object (main method).
5. Write a method to find the position on the list holding the largest element of a given color (try “RED”).
6. Print the position and data (including area) of the selected object – if any!
The following are signatures of methods you need to write
private static GeometricObject recreateObject(String data)
[Needed in step 1]
private static void showObjects(ArrayList<GeometricObject> list)
[Needed in step 2]
private static int findPositionLargestObject(ArrayList<GeometricObject> list)
[Step 3]
private static int findPositionBiggestColor(ArrayList<GeometricObject> list,
String searchColor) [Step 5]
private static int findPositionSmallestCircle(ArrayList<GeometricObject> list)
[Step 7]
【问题讨论】:
-
请发布实际的作业要求,因为您可能对它们的解释有误。实际上你可能只需要一个类,
GeometricObject。 -
我添加了整个作业。但是,我只真正关心在尝试任何其他部分之前弄清楚第一部分。谢谢。
-
请参阅编辑回答。
标签: java