【问题标题】:The constructor Circle(String, boolean, double) is undefined构造函数 Circle(String, boolean, double) 未定义
【发布时间】: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 
{

}

我仍然有两个错误:

  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]

【问题讨论】:

  • 请发布实际的作业要求,因为您可能对它们的解释有误。实际上你可能只需要一个类,GeometricObject
  • 我添加了整个作业。但是,我只真正关心在尝试任何其他部分之前弄清楚第一部分。谢谢。
  • 请参阅编辑回答。

标签: java


【解决方案1】:

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

public Circle() {

}

因此,错误消息准确地告诉您出了什么问题——您正在尝试使用不存在的构造函数。如果你想为需要 3 个参数的类调用构造函数,你将不得不给类一个。

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. 读取每个磁盘记录,创建相应的对象(圆形或矩形),将对象添加到名为列表的动态数组中。

您的说明告诉您,您需要创建并填写一个ArrayList&lt;GeometricObject&gt;。你的 ArrayList 在哪里?

  1. 很好地打印列表的内容(指示位置和内容)。

这告诉你,你所有的类都需要一个像样的toString() 方法来实现简单而干净的输出。

  1. 编写一个方法来查找列表中包含最大元素(所有元素中最大的区域)的位置。

抽象的超 GeometricObject 类将需要一个abstract public double getArea() 方法,并且每个子类都需要实现该方法,以便它返回一个计算面积。然后,您需要创建一个遍历 ArrayList 的静态方法,查找列表中最大的区域。

  1. 打印选中对象的位置和数据(包括面积)(main方法)。

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

  1. 编写一个方法来查找列表中包含给定颜色的最大元素的位置(尝试“RED”)。

另一种方法,类似于上面的方法,但同时查看getColor() 返回的颜色字符串和getArea() 返回的双精度。

  1. 打印所选对象的位置和数据(包括区域)——如果有的话!

不言自明

以下是你需要编写的方法的签名

以下内容一目了然

所以是的,您将需要三个类,一个具有abstract public double getArea(); 方法签名的抽象 GeometricObject,具有名称字符串(实际上可能没有必要),一个颜色字符串,一个布尔值字段,以及一个接受名称、颜色和布尔值的构造函数(尽管同样,名称可能不是必需的,因为该字符串会告诉您要使用哪个子类)。然后您将需要两个子类,每个子类都有自己的字段,Circle 的 double radius 字段,Rectangle 的 doubleA 和 sideB,以及接受此信息的构造函数以及超类构造函数所需的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多