• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem understanding classes and objects.  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I am having a hard time understanding this concepts and it seems I am stuck. I really need help, and sorry if I am asking for too much of your time.
I completed the chapter for classes and started with the exercises.
The first one is :
Add the following method to the Point class:
public int manhattanDistance(Point other)
Returns the “Manhattan distance” between the current Point object and the given other Point object. The
Manhattan distance refers to the distance between two places if one can travel between them only by moving horizontally
or vertically, as though driving on the streets of Manhattan. In our case, the Manhattan distance is the sum
of the absolute values of the differences in their coordinates; in other words, the difference in x plus the difference in
y between the points.


Okay, splendid. I think I did something but I don't understand what. I don't understand what is this "other" thing, where does it come from and what is it's purpose. Also, there is no client code so I can actually run this. It's just his and I have no idea what is going on here.
there are four more exercises like this which I also did so they compile.
2. Add the following method to the Point class:
public boolean isVertical(Point other)
Returns true if the given Point lines up vertically with this Point, that is, if their x-coordinates are the same.
3. Add the following method to the Point class:
public double slope(Point other)
Returns the slope of the line drawn between this Point and the given other Point. Use the formula (y2 – y1) / (x2 – x1)
to determine the slope between two points (x1, y1) and (x2, y2). Note that this formula fails for points with identical
x-coordinates, so throw an IllegalArgumentException in this case.
4. Add the following method to the Point class:
public boolean isCollinear(Point p1, Point p2)
Returns whether this Point is collinear with the given two other Points. Points are collinear if a straight line can be
drawn that connects them. Two basic examples are three points that have the same x- or y-coordinate. The more general
case can be determined by calculating the slope of the line between each pair of points and checking whether this
slope is the same for all pairs of points. Use the formula (y2 – y1) / (x2 – x1) to determine the slope between two points
(x1, y1) and (x2, y2). (Note that this formula fails for points with identical x-coordinates so this will have to be a special
case in your code.) Since Java’s double type is imprecise, round all slope values to a reasonable accuracy such as
four digits past the decimal point before you compare them.


And what I did.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're doing well. You need a way to set the coordinates in your Point class. Have you learned about constructors? If so, write a constructor that sets the two coordinate.

Have you learned about the "main" method? This is how I would test your Point class for now (there are other ways later).

File name: TestPoint.java


You can also put the main method in your class, but the above is a better technique. Try it out and post what you get.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Werner Holt wrote:
Add the following method to the Point class:
public int manhattanDistance(Point other)
Returns the “Manhattan distance” between the current Point object and the given other Point object.
I don't understand what is this "other" thing, where does it come from and what is it's purpose.


It (other) is a parameter of type Point that is being passed into the manhattanDistance method. It's a poor choice of variable name. I would probably have called it secondPoint or nextPoint or destinationPoint or something that better describes what it is. Just calling it "other" is vague and confusing.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Werner Holt wrote:
Add the following method to the Point class:
public int manhattanDistance(Point other)
Returns the “Manhattan distance” between the current Point object and the given other Point object. The Manhattan distance refers to the distance between two places if one can travel between them only by moving horizontally or vertically, as though driving on the streets of Manhattan. In our case, the Manhattan distance is the sum of the absolute values of the differences in their coordinates; in other words, the difference in x plus the difference in y between the points.



As a side note:

Anyone who ever drove on the streets of Manhattan knows that the majority of the streets are one way streets. Furthermore, for the case of two way streets, many of them don't allow left turns now. And let's not even start talking about lower Manhattan or Central park. The distance is definitely not as described ...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!