• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Working Wth Objects

 
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
  }
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic