Skip to main content
ICT
Lesson A6 - Libraries and APIs
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

D. Point2D.Double page 6 of 12

  1. The Point2D class is useful for storing locations on a two dimensional space. It also contains several methods that can be used to do certain calculations. There are two sub-classes of Point2D, but you will generally only want to use Point2D.Double.

  1. Consider an application where you need to track the locations of two mice in a flat-bottomed box. You could use two Point2D.Double objects to keep track of their locations.

  2. What if we want to be able to tell how far apart the two mice are at any given time? We could use the distance formula from Geometry to calculate the distance. However, if we take a quick look at the Point2D API, we can find this method:
double distance(Point2D pt)
      Returns the distance from this Point2D to a specified Point2D.

A line of code as simple as

double distance = rat1.distance(rat2);

will give us the distance between the two rats. This is much simpler than trying to do all the calculations ourselves. Remember, whenever possible we want to avoid writing code that has already been written. By doing a slight bit of research, we have saved ourselves the hassle of writing and then debugging code.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.