2013-09-24 47 views
0

我在爲項目實現代碼中的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; 
    } 
} 

主要目標是擴展由給定標先前製造的線段,我似乎與掙扎。

在此先感謝您的幫助!

+0

什麼是newLineSegment?你不要在任何地方聲明它,或者在方法中使用它。 – Jems

+0

糟糕,在我開始嘗試一切之前,這本來就是「標量」,但即使我把標量放在那裏也不起作用 – user2785079

回答

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編輯源代碼,它有一個非常友好的錯誤提示