2014-09-26 32 views
0

我有一個包含一堆代碼的細分類。我堅持使用兩種方法,isParallel和縮短方法。這是到目前爲止我的代碼(我有一個點類,以及其鏈接到這個類):如何在段類中使用isParallel並縮短方法?

public class Segment { 
 

 
\t //two Points that hold endpoints of the segment 
 
\t private Point p1, p2; 
 
\t 
 
\t 
 
\t //Default constructor that will set the endpoints to new 
 
\t //Points with values (0,0) and (4,4) 
 
\t public Segment(){ 
 
\t \t this(0, 0, 4, 4); 
 
\t } 
 
\t 
 
\t //Parameterized constructor that accepts (int x1, int y1, int x2, int y2) 
 
\t //and creates and sets the endpoints 
 
\t public Segment(int x1, int x2, int y1, int y2){ 
 
\t \t this.p1 = new Point(x1, y1); 
 
\t \t this.p2 = new Point(x2, y2); 
 
\t } 
 
\t 
 
\t //Parameterized constructor that accepts (Point p1, Point p2) and sets both 
 
\t //the endpoints to a deep copy of the Points that are passed in. 
 
\t public Segment(Point p1, Point p2){ 
 
\t \t this.p1 = new Point(p1.getX(), p1.getY()); 
 
\t \t this.p2 = new Point(p2.getX(), p2.getY()); 
 
\t } 
 
\t 
 
\t //Copy constructor that accepts a Segment and initializes the data (of the 
 
\t //new Segment being created) to be the same as the Segment that was received. 
 
\t public Segment(Segment other){ 
 
\t \t p1 = other.getP1(); 
 
\t \t p2 = other.getP2(); 
 
\t } 
 

 
\t public Point getP1(){ 
 
\t \t return p1; 
 
\t } 
 
\t 
 
\t public Point getP2(){ 
 
\t \t return p2; 
 
\t } 
 
\t 
 
\t //The length method returns the length of the Segment.In fact, this method is same as distanceTo method 
 
\t //So we can use distanceTo method which is already defined in Point class 
 
\t public double length(){ 
 
\t \t return (p1.distanceTo(p2)); 
 
\t } 
 
\t 
 
\t //The translate method returns nothing and should translate, or shift, \t 
 
\t //itself (the Segment) by the distances passed in 
 
\t public void translate(int xmove, int ymove) { 
 
\t   p1.translate(xmove,ymove); 
 
\t   p2.translate(xmove,ymove); 
 
\t } 
 
\t 
 
\t //The midpoint method calculates and returns the midpoint of the Segment as a new Point 
 
\t public Point midpoint(){ 
 
\t \t return (p1.halfWayTo(p2)); \t 
 
\t } 
 
\t 
 
\t //The slope method returns the slope of the Segment as a double. 
 
\t public double slope(){ 
 
\t \t \t return (double)(p2.getY() - p1.getY())/(p1.getX() - p2.getX()); 
 
\t } 
 

 

 
\t /** 
 
\t * The isParallel method returns true/false depending on whether the current Segment 
 
\t * is parallel to the Segment received. Think about how you can tell if two segments 
 
\t * are parallel. Note: Two overlapping segments ARE parallel. 
 
\t */ 
 
\t public boolean isParallel(Segment s1){ 
 

 
    
 
{ 
 
    
 
    /** 
 
\t * The shorten method changes its (the Segment's) endpoints so that they are both halfway 
 
\t * to the midpoint. Example: The segment (0,0)---(12,16) has midpoint (6,8). After 
 
\t * calling the shorten method, the segment should be (3,4)---(9,12). Each endpoint 
 
\t * has moved in toward the midpoint (which stayed the same). So (3,4) is halfway between 
 
\t * (0,0) and (6,8) and (9,12) is halfway between (12,16) and (6,8). 
 
\t */ 
 
    public void shorten();

有人可以給我如何在我的代碼中使用這兩種方法的想法。我非常感謝你的幫助。

謝謝!

+0

我的直覺是你不問你想問什麼。你想實現'isParallel'和'shorten'嗎? – 2014-09-26 23:07:56

+0

我只需要知道如何編寫這些方法!或者,也許是!我想知道如何實施它們! – Hr0419 2014-09-27 02:13:45

+0

事實上,你的問題的重寫是由於!至於手頭的事情,你所需要的只是對算法的一些數學支持。 – 2014-09-27 06:48:06

回答

0

我實際上工作在同一個確切的任務!對於isParallel方法,您需要檢查並查看當前分段的斜率是否與接收的分段的斜率相同。我還沒有研究過縮短的方法,所以我現在還無法幫助你。在我的課堂上(因爲我在做同樣的任務),我們不會使用getX()getY()方法,因爲它們失敗了學習封裝的全部目的。我們需要實際要求Point類來計算點的信息。例如,用坡度法而不是返回return (double)(p2.getY() - p1.getY())/(p1.getX() - p2.getX());,我在Point內做了一個方法來計算兩個點的斜率,稱爲calcSlope。在段內Slope方法內調用該方法:return p1.calcSlope(p2);

希望有幫助!