【问题标题】:Java - Calculate area, if area is > 1000 and shape = green, print list of shapesJava - 计算面积,如果面积 > 1000 并且形状 = 绿色,则打印形状列表
【发布时间】:2019-09-10 18:06:43
【问题描述】:

我的程序读取一个形状列表,如果面积 > 1000 并且颜色字符串匹配绿色,则打印形状。

下面的示例数据:

矩形,宽度,高度,颜色 -

圆、半径、颜色。

矩形 68.01 77.63 橙色

主要课程 - 初步尝试。


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;


public class Main {
    private static String SHAPE_DATA = "shapes.txt";

    public static boolean main(String[] args) throws Exception{

        List<Shape> shapes = ShapeParser.parseFile(SHAPE_DATA);
        for(int = 0 ; i < shapes.isValid() i++);
                System.out.print(shapes);
                //System.out.println("%s") shapes;
                private static boolean isValid (shapes) ; {
                    return shapes.getArea() > 1000 && shapes.getColour().equals("green");


                }
    }


}



【问题讨论】:

  • 不确定为什么需要模式匹配。你是不是一时兴起添加标签?
  • 能否添加shapes.txt的示例数据
  • 您似乎对parse(shape_data) 函数感到困惑。此函数一次应采用一行文本并返回一个形状。你似乎让它生成了一个新的 Rectangle,然后是一些 Circle 形状,然后返回创建的第一个 Rectangle 对象。

标签: java parsing math


【解决方案1】:

你永远不会进入你的 while 循环,因为 endof 永远不会是假的。

在您的 parse(shape_data) 中,您需要根据parts[0] 决定创建哪种类型的形状。

例如,对于输入“矩形 68.01 77.63 橙色”

public static Shape parse(String shape_data) {
    Shape shape;

    // TODO: complete this method
    String[] parts = shape_data.split(" ");
    switch(parts[0])
    {
        case "rectangle":
        shape = new Rectangle(parts[1], parts[2], parts[3]);
        break;
       etc...
    }
    return shape;
}

请注意,这不会进行输入验证,以查看 shape_data 字符串是否实际上包含足够的参数。

【讨论】:

    【解决方案2】:

    带颜色的形状接口:

    public interface ColoredShape {
        String getColor();
        double getArea();
    }
    

    基础彩色形状类:

    public abstract class BaseColoredShape implements ColoredShape {
        protected final String color;
    
        public BaseColoredShape(String color) {
            this.color = color;
        }
        @Override
        public String getColor() {
            return this.color;
        }
        @Override
        public String toString() {
            return this.color;
        }
    }
    

    矩形实现:

    public class Rectangle extends BaseColoredShape {
        private final double width;
        private final double height;
    
        public Rectangle(String color, double width, double height) {
            super(color);
            this.width = width;
            this.height = height;
        }
    
        @Override
        public double getArea() {
            return width * height;
        }
    
        @Override
        public String toString() {
            return String.format("rectangle %s %s %s", this.width, this.height, this.color);
        }
    }
    

    Cercle 实现:

    public class Circle extends BaseColoredShape {
        private final double radius;
    
        public Circle(String colour, double radius) {
            super(colour);
            this.radius = radius;
        }
    
        @Override
        public double getArea() {
            return Math.PI * radius * radius;
        }
    
        @Override
        public String toString() {
            return String.format("circle %s %s", this.radius, this.color);
        }
    }
    

    输入数据解析器(txt文件)实现:

    public class ShapeDataParser {
        private final String filepath;
        private final ColoredShapeFactory factory;
    
        public ShapeDataParser(String filepath) {
            this.filepath = filepath;
            this.factory = new ColoredShapeFactory();
        }
    
        public Stream<ColoredShape> parse() throws IOException, URISyntaxException {
            Path path = Paths.get(getClass().getClassLoader().getResource(filepath).toURI());
            return Files.lines(path).map(this::stringToColoredShape);
        }
    
        private ColoredShape stringToColoredShape(String shapeStr) {
            String[] shapeParts = shapeStr.split(" ");
            String shapeType = shapeParts[0];
            if("circle".equals(shapeType)) {
                return factory.createCircle(shapeParts);
            }
            return factory.createRectangle(shapeParts);
        }
    }
    

    形状工厂:

    public class ColoredShapeFactory {
    
        public ColoredShape createCircle(String[] shapeParts) {
            //TODO implement validation of shapeParts array
            double radius = Double.valueOf(shapeParts[1]);
            String color = shapeParts[2];
            return new Circle(color, radius);
        }
    
        public ColoredShape createRectangle(String[] shapeParts) {
            //TODO implement validation of shapeParts array
            double width = Double.valueOf(shapeParts[1]);
            double height = Double.valueOf(shapeParts[2]);
            String color = shapeParts[3];
            return new Rectangle(color, width, height);
        }
    }
    

    最后入口点类:

    import java.io.IOException;
    import java.net.URISyntaxException;
    
    public class ShapeFilter {
        public static void main(String[] args) throws IOException, URISyntaxException {
            ShapeDataParser parser = new ShapeDataParser("shape_data.txt");
            parser.parse()
                    .filter(ShapeFilter::isValid)
                    .forEach(System.out::println);
        }
    
        private static boolean isValid(ColoredShape shape) {
            return shape.getArea() > 1000 && shape.getColor().equals("green");
        }
    }
    

    输入数据shape_data.txt:

    rectangle 68.01 77.63 orange
    circle 88.06 green
    circle 18.29 green
    circle 71.71 red
    rectangle 17.91 8.75 orange
    circle 2.16 white
    rectangle 83.12 98.71 green
    rectangle 37.27 35.93 green
    rectangle 45.13 74.55 green
    circle 36.62 white
    circle 72.59 yellow    
    

    输出:

    circle 88.06 green
    circle 18.29 green
    rectangle 83.12 98.71 green
    rectangle 37.27 35.93 green
    rectangle 45.13 74.55 green
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2018-10-06
      • 2013-08-12
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      相关资源
      最近更新 更多