Using JDK Point. Modify PointMain

SoccerBoss
Category:
Programming
Price: $5 USD

Question description

7. Using JDK Point. Modify PointMain, pp. 547-548 by dropping the calls to distanceFromOrigin, and adding the "import java.awt.*" to allow use of JDK Points. Write code to put the two Points in an array of 2 Points named pair.  Do the translates using array references, such as pair[0] and pair[1] instead of p1 and p2.  Print the points out using a loop over the array.  Also try Arrays.toString on the array. If you do not modfy the code at the link and just copy that you will get zero points. Also, note that you are using JDK Point objects. You cannot access x and y directly, but you do have access to all the methods for the Point class of the JDK.

// A program that deals with 2D points.

// Fourth version, to accompany encapsulated Point class.

public class PointMain {

public static void main(String[] args) {


// create two Point objects

Point p1 = new Point(7, 2);

Point p2 = new Point(4, 3);


// print each point and its distance from origin

System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); 

System.out.println("distance from origin = " + p1.distanceFromOrigin()); 

System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")"); 

System.out.println("distance from origin = " + p2.distanceFromOrigin());


// translate each point to a new location

p1.translate(11, 6);

p2.translate(1, 7);

// print the points again

System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")");

 System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")");

}

}


Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1819 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors