Question

I am having trouble implementing a scaleByFactor() method in my code for a project. I keep getting errors that say the return statement is wrong but I simply don't know what the code is supposed to be returning.

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;
    }
}

basically the goal is to scale a previously made line segment by a given scalar which I seem to be struggling with.

Thanks in advance for the help!

Was it helpful?

Solution

1、The return value of the function's return type and definitions should be consistent

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

2、I suggest you use eclipse to edit the source code, it has a very nice error prompt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top