Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:20 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 3 posts ] 
Author Question
 Question subject: Need Help with Java Source code.
PostPosted: Tue Oct 26, 2010 1:35 am 
Offline
Newbie
User avatar

Joined: Tue Oct 26, 2010 1:32 am
Posts: 2
Has thanked: 0 time
Have thanks: 0 time

Okay so Im trying to make a method that fits this criteria.

A public method named distanceTo that accepts a parameter of type Point and returns a double value
representing the distance between the two points (i.e., between the Point object on which the method
was called and the other Point object that was passed as a parameter to the method). Recall that to
compute the distance between two points in 2D space you must:
{ Find the di erence between the two x-coordinates
{ Compute the square of that di erence
{ Find the di erence between the two y-coordinates
{ Compute the square of that di erence
{ Add those two squares together
{ Take the square root of the result


and.... so far I have this

/**
* Changes the distance between the two points
*/
public void distanceTo(double xPosition, double yPosition)
{

}


Im at a lost and don't know where to start any help would be great.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Need Help with Java Source code.
PostPosted: Tue Oct 26, 2010 11:16 pm 
Offline
Newbie
User avatar

Joined: Tue Oct 26, 2010 1:32 am
Posts: 2
Has thanked: 0 time
Have thanks: 0 time
Bump....anyone that can help would be really appreciated.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Need Help with Java Source code.
PostPosted: Wed Nov 03, 2010 3:57 pm 
Offline
Newbie
User avatar

Joined: Wed Nov 03, 2010 3:48 pm
Posts: 4
Has thanked: 0 time
Have thanks: 1 time
Hi Wizkid390,

one possible solution would be:

Code:
import java.lang.Math;

public class xPoint {

   /**
    * Makes the class executable
    */
   public static void main(String[] args) {
      xPoint p1 = new xPoint(5, 7);
      xPoint p2 = new xPoint(8, 4);
      System.out.println("Distance = " + p1.distanceTo(p2));
   }

   /**
    * Private member containing the x coordinate
    */
   private double x;

   /**
    * Private member containing the y coordinate
    */
   private double y;
   
   /**
    * Method to compute the distance to another xPoint instance
    * @param xPosition
    * @param yPosition
    */
   public xPoint(double xPosition, double yPosition) {
      x = xPosition;
      y = yPosition;
   }

   /**
    * Method to compute the distance to another xPoint instance.
    * @param other Instance to compute distance to.
    * @return Distance between the current instance of xPoint and other.
    */
   public double distanceTo(xPoint other) {
      return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
   }
}


Executing the program results in this output:

Code:
Distance = 4.242640687119285


Best regards
apeter


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 3 posts ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Code to open multiple links.
 I need help with checkers code
 Java Chat
 Steganography in java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team