• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working Wth Objects  RSS feed

 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this.x needs to be different than x so this.x-x isn't 0. I do not understand why this code keep giving me result of 0?


Class


Main
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the line 6, points[i] print one pair of x,y and points[i + 1] print another pair. All I need to do is to send those two pairs to the class, perform a math calculation and calculate the area.
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This method completely ignores the input parameter. Looks like you copied it from the other distance() method but then forgot to make the necessary changes to use the x and y values from the MyPoint object passed into the method.
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly where I am stuck! I do not know how I can deal with the objects that are sent into the methods. How do I work with x-y values of the object sent in?
 
Recaip Sanli
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it, thank you for the hint!!

   public double distance(MyPoint Object){
      MyPoint o = (MyPoint) Object;
      double newX = Math.pow(this.x - o.x, 2);
      double newY = Math.pow(this.y - o.y, 2);
      return Math.sqrt(newX + newY);
   }
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Math.pow(x, 2); use x * x.
Don't mess around with square roots or similar when there is a method in the Math class which will do all that work for you.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do this.

It's confusing and unnecessary.

In the first line, Object is taken as the parameter name. That's confusing because Object is also a standard class name in Java. Avoid using standard class names for any of your own identifiers.
In the second line, you cast the parameter which is already declared as a reference to a MyPoint to MyPoint. There's no point to doing that, it's totally unnecessary.

This does the same thing:

Better yet, think of better names for newX and newY. Those names don't reflect what they represent, which are the distances between the current point and the other point on the X and Y axes, respectively. A better way to write it would be:

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