Computer Science 225
Advanced Programming
Spring 2017, Siena College
ColorPoint BlueJ Project
Click here to download a BlueJ project for ColorPoint.
ColorPoint Source Code
The Java source code for ColorPoint is below. Click on a file name to download it.
/* * Example ColorPoint: extend the Point class to have a Color field * * Jim Teresco, Siena College, Computer Science 225, Spring 2017 * */ import java.awt.Color; // Note the extends keyword to indicate inheritance public class ColorPoint extends Point { /** Additional field: the point's Color */ protected Color color; /** * construct a ColorPoint at the origin * * @param c the Color to associate with this point */ public ColorPoint(Color c) { // call our superclass constructor super(); color = c; } /** * construct a ColorPoint at the given coordinates * * @param x the x-coordinate of the point * @param y the y-coordinate of the pont * @param c the Color to associate with this point */ public ColorPoint(double x, double y, Color c) { // call our superclass constructor super(x, y); color = c; } /** * override toString method of Point * * @return string representation of our ColorPoint */ public String toString() { return "(" + x + "," + y + ") " + color; } public static void main(String[] args) { ColorPoint a = new ColorPoint(Color.red); Point b = new Point(3, 4); ColorPoint c = new ColorPoint(9, 12, Color.blue); Point d = new Point(b); System.out.println("a: " + a); System.out.println("b: " + b); System.out.println("c: " + c); System.out.println("d: " + d); System.out.println("Distance from b to c: " + b.distance(c)); System.out.println("Distance from d to origin: " + d.distance()); System.out.println("Distance from c to (1, 1): " + c.distance(1,1)); } }
/* * Example PointOverload: a class demonstrating ad hoc polymorphism * by overloading constructors and the distance method * * Jim Teresco, Siena College, Computer Science 225, Spring 2017 * */ public class Point { /** the coordinates */ protected double x, y; /** * constructor for the default point (0,0) */ public Point() { // x and y are already 0.0 } /** * constructor for a point at given x, y coordinates * * @param x the x coordinate for the point * @param y the y coordinate for the point */ public Point(double x, double y) { this.x = x; this.y = y; } /** * constructor for a point to be initialized using another point * * @param p the point whose coordinates are to be used for this one's */ public Point(Point p) { x = p.x; y = p.y; } /** * compute the distance from this point to another * * @param p the other point * @return the distance from this point to p */ public double distance(Point p) { double dx = x - p.x; double dy = y - p.y; return Math.sqrt(dx * dx + dy * dy); } /** * compute the distance from this point to a given coordinate pair * * @param x the x-coordinate of the other point * @param y the y-coordinate of the other point * @return the distance from this point to (x,y) */ public double distance(double x, double y) { double dx = x - this.x; double dy = y - this.y; return Math.sqrt(dx * dx + dy * dy); } /** * compute the distance from this point to the origin * * @return the distance from this point to the origin */ public double distance() { return Math.sqrt(x * x + y * y); } /** * @return string representation of this Point */ public String toString() { return "(" + x + "," + y + ")"; } public static void main(String[] args) { Point a = new Point(); Point b = new Point(3, 4); Point c = new Point(9, 12); Point d = new Point(b); System.out.println("a: " + a); System.out.println("b: " + b); System.out.println("c: " + c); System.out.println("d: " + d); System.out.println("Distance from b to c: " + b.distance(c)); System.out.println("Distance from d to origin: " + d.distance()); System.out.println("Distance from c to (1, 1): " + c.distance(1,1)); } }