Your Own Point Object Class

posted by: Mr. Bergquist 15 February 2012 No Comment

Write your Point (Object) Class and use a PointMain (Client) classe to test it.  There’s code on the Object & Contructor slides from yesterday but try to build them from memory as much as possible!  Make sure you update your PointMain class to test your additions as you go.

  1. public Point(int x, int y)

    Until we have have covered the Constructor presentation, just use the simple creation of your point and assignments. Then later create the Constructor (slides) method to make a point with coordinates (x, y).

  2. public void translate(int dx, int dy)

    Move the point by a given the amounts dx (added to the x value) and  dy (added to the y value).

  3. public void draw(Graphics g)

    Displays the point as a circle of diameter 3 at (x, y). Label with the coordinates (just like in the slides). NOTE: you will be updating it later…

  4. public boolean isVertical(Point other)

    Returns true if the given Point lines up vertically with the other Point; that is, if their x-coordinates are the same (equals).

  5. public void setColor(Color myColor)

    Sets the color of this point.  You will first need to add a new field named “point_color” of type Color; that’s the easy part. You also have to update the draw method: when the point is drawn, it should appear in that set color. After it is drawn though, the pen should then be set back to black (or, to be fancier, to the color it was before drawing the point. How would you save it? Take a look at the Graphics reference.)

  6. public double distance(Point other)

    Returns the distance between this Point and the given other Point. Look up the distance formula if you forget!

When you are done, upload this code to demonstrate your works correctly for Mr. Bergquist.

When you are done, do this one – we will do it later with guidance and some help on Arrays of Object (slides) :

  • Write a separate main to complete the earthquake simulation described on Second slide of the Object presentation . Use this file cities.txt. You will need to build an array of points using the number of cities given at the top of the data file. You will prompt the user for epicenters over and over again until the user quits.
1 Star2 Stars3 Stars4 Stars5 Stars (23 votes, average: 3.39 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>