java - 按给定因子缩放线段

我在为项目的代码中实现scaleByFactor()方法时遇到麻烦。我不断收到错误消息,说return语句是错误的,但我根本不知道代码应该返回什么。

public class LineSegment {

    /**
     * The start and end Points of this LineSegment.
     */
    private final Point start;
    private final Point end;
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private double slopeX;
    private double slopeY;
    private double slope;
    private double yIntercept;
    private double length;
    private double scalar;
    private double scalarX;
    private double scalarY;

    /**
     * Construct a LineSegment having the given Point parameters as start and end.
     * 
     * @param start the Point (X1, Y1) at the start of this LineSegment.
     * @param end the Point (X2, Y2) at the end of this LineSegment.
     */
    public LineSegment(Point start, Point end) {
      this.start = start;
      this.end =end;
    }


    /**
     * @return the slope of this LineSegment, calculated using the formula: 
     *         (Y2 - Y1)/(X2 - X1). 
     */
    public double slope() {
      start.getX();
      end.getY();
      slopeX = x2 - x1;
      slopeY = y2 - y1;
      slope = (slopeY / slopeX);
        return slope;

    }



    /**
     * @return the Y intercept of this LineSegment, calculated using the formula:
     *         Y1 - (M * X1)
     *         where M is the slope of this LineSegment.
     */
    public double yIntercept() {
      yIntercept = y1 - (slope * x1);  
      yIntercept = y2 - (slope * x2);  
      System.out.print(yIntercept);  
        return yIntercept;
    }


    /**
     * @return the start Point of this LineSegment. 
     *         This corresponds to the Point (X1, Y1).
     */
    public Point getStart() {
        return start;
    }


    /**
     * @return the end Point of this LineSegment.
     *         This corresponds to the Point (X2, Y2).
     */
    public Point getEnd() {
        return end;
    }


    /**
     * @return the length of this LineSegment, calculated using the formula: 
     *         sqrt((X2 - X1)^2 + (Y2 - Y1)^2).
     */
    public double length() {
      Math.sqrt((x2 - x1)^2 + (y2 - y1)^2);
      return length;
    }


    /**
     * @param scalar the double value to be used to
     *        scale this LineSegment. Scaling is done 
     *        with the following formula:
     *        (fill in the formula you will use here!).
     * @return a new LineSegment whose start Point is the
     *         same start Point of this LineSegment and
     *         the end Point has been scaled by the given
     *         scalar value.        
     */
    public LineSegment scaleByFactor(double scalar) {
      scalarX = x2 + (x2 - x1) * length;
      scalarY = y2 + (y2 - x1) * length;
      return scalar;
    }


    /**
     * @return the following String message: 
     *         "y = M x + B" 
     *         where M is the slope of this LineSegment, 
     *         and B is the Y intercept of this LineSegment. 
     *         For example: if the slope were 5 and the Y
     *         intercept were 7, this method would return:
     *         "y = 5 x + 7".
     */
    @Override
    public String toString() {
        return "y = "+slope+"x + "+yIntercept;
    }
}

基本上,目标是通过给定的标量缩放以前制作的线段,而这个标量我似乎很努力。

先谢谢您的帮助!

最佳答案

1,函数的返回类型和定义的返回值必须一致

//modify public LineSegment scaleByFactor(double scalar) {
public double scaleByFactor(double scalar) {
  scalarX = x2 + (x2 - x1) * length;
  scalarY = y2 + (y2 - x1) * length;
  return scalar;
}

2,建议您使用eclipse编辑源代码,它有一个非常不错的错误提示

https://stackoverflow.com/questions/18971952/

相关文章:

java - 编写一个程序,询问产品代码,产品数量并汇总所有价格

java - 在其他类中访问主类中的公共(public)变量

compiler-errors - Java类路径问题

c# - 编译内容项目时出错

c++ - 发布版本中的 Visual C++ 6 编译错误

ios - drawRect :中的EXC_BAD_ACCESS

java - 在PHP中获取Java编译错误

c++ - C++中的运行时错误字符串声明

c++ - Count.h :10:5: error: ISO C++ forbids declar

java - 错误 :class interface or enum expected