【问题标题】:How would I remove a shape from this array using the shape's area?如何使用形状的区域从该数组中删除形状?
【发布时间】:2017-11-08 18:51:43
【问题描述】:

我有这个程序,它应该使用它的类型(矩形、三角形、圆形)和它的尺寸(长度/宽度、底边/高度、半径)将形状添加到数组中。它还应该能够从数组中删除一个形状。目前,我可以向数组添加一个形状,但是当我尝试删除它时(基于形状类型和面积,而不是形状类型和尺寸),它会打印出找不到形状。

示例:我添加了一个长度为 3 和宽度为 2 的矩形。它的面积是 6。当我尝试删除一个面积为 6 的矩形时,它不会从数组中删除这个矩形,因为它应该不能找到它。

为了澄清,主要问题出在前端的 removeAShapeDialogue 部分和代码的 Collection 部分的 removeShape 方法。

注意事项:

  • 形状是一个界面
  • 我无法按照指令创建任何其他类、方法或接口,因此必须有使用此处的任何内容的解决方案。
  • 我认为问题不在于 Rectangle、Triangle 或 Circle 类
  • 我也遇到了 ShapeCollection 类中的选择排序问题,因此我们将不胜感激。

前端

import java.util.Scanner;
public class ShapeFrontEnd {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Welcome to the Shapes Collection.");
    ShapeCollection shapes = new ShapeCollection();//New instance of ShapeCollection

    boolean quit = false;
    while(!quit)//Runs until quits
    {
        printOptions();
        int pick = keyboard.nextInt();
        keyboard.nextLine();
        switch(pick)
        {
        case 1://Add shape
            shapes.addShape(makeAShapeDialogue());
            break;
        case 2://Remove shape
            shapes.removeShape(removeAShapeDialogue());
            break;
        case 9://Quit
            quit = true;
            System.out.println("Goodbye");
            System.exit(0);
            break;
        default:
            System.out.println("Invalid input.");
        }
        System.out.println("Current Shapes:");
        shapes.printShapes(shapes);
    }
}
    //Helper Methods
    public static void printOptions()//Prints user's input options
    {
        System.out.println("Enter 1: Add a shape\nEnter 2: Remove a shape\nEnter 9: Quit");
    }
    public static Shape makeAShapeDialogue()//Prints the dialogue after user enters "1"
    {
        Scanner keyboard = new Scanner(System.in);
        Shape newShape;
        System.out.println("What type of shape? Rectangle, Triangle, or Circle?");
        String shapeType = keyboard.nextLine();

        if(shapeType.equalsIgnoreCase("rectangle"))
        {
            System.out.println("Enter length.");
            double length = keyboard.nextDouble();
            System.out.println("Enter height.");
            double height = keyboard.nextDouble();
            keyboard.nextLine();
            newShape = new Rectangle(length, height);
        }
        else if(shapeType.equalsIgnoreCase("triangle"))
        {
            System.out.println("Enter base.");
            double base = keyboard.nextDouble();
            System.out.println("Enter height.");
            double height = keyboard.nextDouble();
            keyboard.nextLine();
            newShape = new Triangle(base, height);
        }
        else if(shapeType.equalsIgnoreCase("circle"))
        {
            System.out.println("Enter radius.");
            double radius = keyboard.nextDouble();
            keyboard.nextLine();
            newShape = new Circle(radius);
        }
        else
        {
            newShape = null;
        }
        return newShape;
    }
    public static Shape removeAShapeDialogue()
    {
        ShapeCollection shapes = new ShapeCollection();
        Scanner keyboard = new Scanner(System.in);
        Shape newShape;

        System.out.println("What type of shape? Rectangle, Triangle, or Circle?");
        String shapeType = keyboard.nextLine();
        System.out.println("Enter area.");
        double area = keyboard.nextDouble();
        keyboard.nextLine();

        if(shapeType.equalsIgnoreCase("rectangle"))
        {
            newShape = new Rectangle();
        }
        if(shapeType.equalsIgnoreCase("triangle"))
        {
            newShape = new Triangle();
        }
        if(shapeType.equalsIgnoreCase("circle"))
        {
            newShape = new Circle();
        }
        else
        {
            newShape = null;
        }
        return newShape;
    }

}

集合/数组类

public class ShapeCollection {

private Shape[] shapes;
public static final int MAX_SHAPES = 5;

//Constructor
public ShapeCollection()
{
    shapes = new Shape[MAX_SHAPES];
}
//Method to get all the Shapes
public Shape[] getShapes()
{
    return this.shapes;
}

//Add Shape
public void addShape(Shape aShape)
{
    for(int i=0;i<shapes.length;i++)
    {
        if(shapes[i] == null)
        {
            shapes[i] = aShape;
            return;
        }
    }
    System.out.println("You cannot fit any more shapes.");
}
//Remove Shape
public void removeShape(Shape aShape)
//public void removeShape(String aShapeType, double anArea)
{
    for(int i=0;i<shapes.length;i++)
    {
        System.out.println(shapes[i]);
        if(shapes[i] != null && shapes[i].equals(aShape))
        //if(shapes[i] != null && shapes[i].getType().equals(aShapeType) && shapes[i].getArea() == (anArea))
        {
            shapes[i] = null;
            return;
        }
    }
    System.out.println("Cannot find that shape.");
}

//Sort Shapes
private void sortShapes()
{
    for(int i=0;i<shapes.length-1;i++)
    {
      int smallestIndex = i;
      for(int j=i+1; j<shapes.length;j++)
      {
        if(shapes[j].getArea() < shapes[smallestIndex].getArea())
        {
          smallestIndex = j;
        }
        /*if(smallestIndex !=i)
        {
          Shape number = shapes[i];
          shapes[i] = shapes[smallestIndex];
          shapes[smallestIndex] = number;
        }*/
      }
      Shape temp = shapes[smallestIndex];
      shapes[smallestIndex] = shapes[i];
      shapes[i] = temp;
    }    
}
//Prints Shapes
public void printShapes(ShapeCollection shapeC)
{
    //sortShapes();
    for(Shape s : shapeC.getShapes())
    {
        if(s == null)
            continue;
                System.out.println(s);
                System.out.println();   
    }
}

}

形状界面

public interface Shape {
public double getArea();
public String toString();
public String getType();
}

矩形类

public class Rectangle implements Shape{

//Attributes
private double length;
private double width;

//Constructors
public Rectangle()//Default
{
    this.length = 0.0;
    this.width = 0.0;
}
public Rectangle(double aLength, double aWidth)//Parameterized
{
    this.setLength(aLength);
    this.setWidth(aWidth);
}

//Accessors
public double getLength()
{
    return this.length;
}
public double getWidth()
{
    return this.width;
}

//Mutators
public void setLength(double aLength)
{
    if(aLength>0)
    {
        this.length = aLength;
    }
    else
    {
        System.out.println("Invalid length.");
    }
}
public void setWidth(double aWidth)
{
    if(aWidth>0)
    {
        this.width = aWidth;
    }
    else
    {
        System.out.println("Invalid width.");
    }
}

//Other Methods
public double getArea()
{
    return this.length*this.width;
}
public String getType()
{
    return "RECTANGLE";
}
public String toString()
{
    return getType() + " | Length: " + this.length + " | Width: " + this.width + " | Area: " + getArea();
}
}

三角形类

public class Triangle implements Shape{

//Attributes
private double base;
private double height;

//Constructors
public Triangle()
{
    this.base = 0.0;
    this.height = 0.0;
}
public Triangle(double aBase, double aHeight)
{
    this.setBase(aBase);
    this.setHeight(aHeight);
}

//Accessors
public double getBase()
{
    return this.base;
}
public double getHeight()
{
    return this.height;
}

//Mutators
public void setBase(double aBase)
{
    if(aBase>0)
    {
        this.base = aBase;
    }
    else
    {
        System.out.println("Invalid base.");
    }
}
public void setHeight(double aHeight)
{
    if(aHeight>0)
    {
        this.height = aHeight;
    }
    else
    {
        System.out.println("Invalid height.");
    }
}

//Other Methods
public double getArea()
{
    return (this.base*this.height)/2;
}
public String getType()
{
    return "TRIANGLE";
}
public String toString()
{
    return getType() + " | Base: " + this.base + " | Height: " + this.height + " | Area: " + getArea();
}
}

圆类

public class Circle implements Shape{

//Attributes
private double radius;

//Constructors
public Circle()
{
    this.radius = 0.0;
}
public Circle(double aRadius)
{
    this.setRadius(aRadius);
}

//Accessors
public double getRadius()
{
    return this.radius;
}

//Mutators
public void setRadius(double aRadius)
{
    if(aRadius>0)
    {
        this.radius = aRadius;
    }
    else
    {
        System.out.println("Invalid radius.");
    }
}

//Other Methods
public double getArea()
{
    return Math.PI*(radius*radius);
}
public String getType()
{
    return "CIRCLE";
}
public String toString()
{
    return getType() + " | Radius: " + this.radius + " | Area: " + getArea();
}
}

【问题讨论】:

    标签: java arrays object


    【解决方案1】:

    您正在使用他们的 equals 方法比较形状:shapes[i].equals(aShape) 但您还没有实现它,所以您实际上是在使用默认的 Object.equals() 方法进行比较,它不知道 Shape 是什么,而是比较对象的引用。

    【讨论】:

    • 所以我只需要在 Rectangle、Triangle 和 Circle 类中创建一个 equals 方法,对吗?
    • 创建它并根据您想要比较这些形状的逻辑来实现它。
    • @DanielZippel 作为旁注,您的问题清晰易懂,但有助于发布更少(不相关)的代码。没有人愿意阅读所有这些。只需发布相关的点点滴滴:stackoverflow.com/help/mcve
    • 好的,谢谢。通常我发布的代码不够多,并且已经坚持了好几天,所以我决定全部发布。不太确定需要什么来帮助人们理解问题。
    • 这是可以理解的。不过请查看该链接 (stackoverflow.com/help/mcve),阅读时间为 30 秒,它将帮助您进行调试和提问。
    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多