按给定因子缩放线段

时间:2013-09-24 02:18:52

标签: java compiler-errors project scalar

我在项目代码中实现scaleByFactor()方法时遇到问题。我一直收到错误,说返回语句错了,但我根本不知道代码应该返回什么。

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 个答案:

答案 0 :(得分:0)

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编辑源代码,它有一个非常好的错误提示