java - 如何使用形状的面积从该数组中删除形状?

标签 java arrays object

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

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

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

注释:

  • 形状是一个界面
  • 我无法按照说明创建任何其他类、方法或接口(interface),因此必须有一个使用此处提供的解决方案。
  • 我认为问题不在于 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();
}
}

最佳答案

您正在使用其 equals 方法来比较形状:shapes[i].equals(aShape) 但您尚未实现它,因此您实际上是在比较使用默认的 Object.equals() 方法,该方法不知道 Shape 是什么,而是比较对象的引用。

关于java - 如何使用形状的面积从该数组中删除形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187477/

相关文章:

java.sql.SQLIntegrityConstraintViolationException : Column 'library_idlibrary' cannot be null

arrays - 将列组合成嵌套数组

java - 有没有办法将结构字段作为数组访问?

java - 我如何在这里删除 System.out.println()

object - 我如何摆脱这个 'warning: the expression is unused' 警告?

java - 在 JAVA (Android) 中更改 HTML 值

java - 如果构造函数参数不是通用的,自动类型推断如何工作

java - 如何在 android 中的 Tab 旁边放置一个按钮? (与UI相关)

javascript - 如何通过 JavaScript 中的属性获取对象的索引?

java - 从 jtable 获取对象的正确方法