0

In a project for my CS class, I am supposed to use a double value to scale a LineSegment and return a new LineSegment whose start Point is the same start Point of the old LineSegment, but with a new end point from being scaled. I am unsure of how to do this. I attempted to multiply the line segment by scalar, but that did not work and gave me an incompatible typing error. Here is my code.

public class LineSegment {
private final Point start;
private final Point end;
public LineSegment(Point start, Point end) {
    this.start = start;
    this.end = end;
}
public double slope() {
    return ((end.getY()-start.getY())/(end.getX()-start.getX()));
}
public double yIntercept() {
    return (start.getY()-(this.slope()*start.getX()));
}
public Point getStart() {
    return this.start;
}
public Point getEnd() {
    return this.end;
}
public double length() {
    return (Math.sqrt(Math.pow((end.getX()-start.getX()),2) + Math.pow((end.getY()-start.getY()),2)));
}

public LineSegment scaleByFactor(double scalar) {
    return null;
}
@Override
public String toString() {
    return ("y = " + this.slope() + "x +" + this.yIntercept());
}
}

1 Answers1

1

This won't work:

public LineSegment scaleByFactor(double scalar) {
    return (this.length*scalar);
}

Note that the this.length field does not exist.

But even if you called the length method, length(), you'd still have a serious problem, since your method states that it will return a LineSegment Object and you'd be returning a number. I suggest that you use the calculation to create a new LineSegment object (hint -- call the constructor with new and with parameters that use your calculations) and then return it.

in pseudocode:

public LineSegment scaleByFactor(double scalar) {
    // use scalar, start and end to calculate a new end parameter value
    // create new LineSegement object with the old start and new end parameters
    // return this newly created object
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Sorry, misguided edit. Anyway, the original problem is that `length` is a method, not a variable. This is what would explain the original error. – Jan Dörrenhaus Sep 24 '13 at 01:06
  • 1
    @JanDoerrenhaus: OK thanks, I've edited my code to reflect this. – Hovercraft Full Of Eels Sep 24 '13 at 01:07
  • That's much clearer. The project only wants me to calculate a new end parameter value, but your pseudo-code is still a good help. Only problem is I don't exactly know how to calculate the new end parameter. I'll edit my code with my newest crack at the code. – Kevin Roberts Sep 24 '13 at 01:18
  • @KevinRoberts: Please don't. If this were my project, I'd work on calculating that end Point for the next hour or two before throwing in the towel. You should too. It's the struggle that teaches you how to code. – Hovercraft Full Of Eels Sep 24 '13 at 01:22
  • @KevinRoberts: Sigh... Please try to think this through. I know you can solve this if you get out a bit of paper and a pencil and work it out. Think -- what does is the Point class comprised of? Does it make sense to multiple Point by a number? It would be the same as trying to multiple a String by a number -- won't work. So use Point's fields to create a new Point. Again, think this through, don't rush it. -1 to your question for rushing your attempt at a solution. I will remove the down-vote and re-add my up-vote if you get rid of your edit. – Hovercraft Full Of Eels Sep 24 '13 at 01:26
  • I've given this some thought, and I am thinking this might have something to do with the Pythagorean theorem. So, if I make side a = x2-x1, b = y2-y1, and c = the length I previously calculated and multiply all the sides by scalar, will it work? – Kevin Roberts Sep 24 '13 at 02:17