gpt4 book ai didi

java - 构造函数 Circle(String, boolean, double) 未定义

转载 作者:行者123 更新时间:2023-12-02 04:20:37 25 4
gpt4 key购买 nike

我正在尝试打印数组的内容,该数组已从文本文件分配了值。然而我遇到了两个错误,任何帮助将不胜感激。我的代码是:

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
{

}

我仍然有两个错误:

  1. 构造函数 Circle(String, boolean, double) 未定义。
  2. 构造函数 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]

最佳答案

哎呀,你的 Circle 类没有没有构造函数,这意味着它只有默认的无参数构造函数:

public Circle() {

}

因此,错误消息确切告诉您出了什么问题——您正在尝试使用不存在的构造函数。如果您想为需要 3 个参数的类调用构造函数,则必须为该类提供 1 个参数。

public class Circle extends GeometricObject {
// you'll need fields here

public Circle(String color, boolean somethingNotSureWhat, double radius) {
// call the super class's constructor if it takes parameters
// here set your fields that are not part of the super class
}
}
<小时/>

最重要的是——无需猜测,因为有大量资源可以向您展示如何使用和创建构造函数,包括:

<小时/>

编辑

  1. Read each disk record, create the corresponding object (Circle or Rectangle), add the object to a dynamic array called list.

您的说明告诉您,您将需要创建并填写 ArrayList<GeometricObject> 。你的ArrayList在哪里?

  1. Nicely print the contents of the list (indicate position and content).

这告诉您,您的所有类(class)都需要一个像样的 toString()方法允许简单、干净的输出。

  1. Write a method to find the position on the list holding the largest element (biggest area of them all).

抽象的 super GeometricObject 类将需要 abstract public double getArea()方法,每个子类都需要实现该方法,以便它返回一个计算区域。然后,您需要创建一个静态方法来迭代 ArrayList,找到列表中最大的区域。

  1. Print the position and data (including area) of the selected object (main method).

上面的方法应该返回列表中最大的 GeometricObject,然后您需要输出它的属性。

  1. Write a method to find the position on the list holding the largest element of a given color (try “RED”).

另一种方法,与上面的方法类似,但它同时查看getColor()返回的颜色字符串。和 getArea() 返回的 double .

  1. Print the position and data (including area) of the selected object – if any!

不言自明

The following are signatures of methods you need to write

以下内容都是不言自明的

所以,是的,您将需要三个类,一个具有 abstract public double getArea(); 的抽象 GeometricObject。方法签名,它具有名称字符串(实际上可能不是必需的)、颜色字符串、 boolean 字段以及接受名称、颜色和 boolean 值的构造函数(尽管同样,名称可能不是必需的,因为该字符串告诉您使用哪个子类)。然后,您将需要两个子类,每个子类都有自己的字段,圆形的双半径字段,矩形的双倍 sideA 和 sideB,以及接受此信息以及父类(super class)构造函数所需信息的构造函数。

关于java - 构造函数 Circle(String, boolean, double) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32812475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com