We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
Exercise 2: Superclass Point2D and its Subclasses
The Superclass Point2D.java
/** * The Point2D class models a 2D point at (x, y). */ public class Point2D { // Private instance variables private int x, y; // Constructors /** Construct a Point2D instance at (0,0) */ public Point2D() { // default constructor this.x = 0; this.y = 0; } /** Constructs a Point2D instance at the given (x,y) */ public Point2D(int x, int y) { this.x = x; this.y = y; } // Getters and Setters public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } /** Returns a self-descriptive string in the form of "(x,y)" */ @Override public String toString() { return "(" + this.x + "," + this.y + ")"; } }
The Subclass Point3D.java
1/** 2 * The Point3D class models a 3D point at (x, y,z), 3 * which is a subclass of Point2D. 4 */ 5public class Point3D extends Point2D { 6 // Private instance variables 7 private int z; 8 // Constructors 9 /** Constructs a Point3D instance at (0,0,0) */ 1 public Point3D() { // default constructor 0 super(); // x = y = 0 1 this.z = 0; 1 } 1 /** Constructs a Point3D instance at (x,y,z) */ 2 public Point3D(int x, int y, int z) { 1 super(x, y); 3 this.z = z; 1 } 4 // Getters and Setters 1 public int getZ() { 5 return this.z; 1 } 6 public void setZ(int z) { 1 this.z = z; 7 } 1 /** Returns a self-descriptive string in the form of "(x,y,z)" */ 8 @Override 1 public String toString() { 9 return "(" + super.getX() + "," + super.getY() + "," + this.z + 2")"; 0 } 2 1 2 2 2 3 2 4 2 5 2 6} 2 7 2 8 2 9 3 0 3 1
A Test Driver for Point2D and Point3D Classes
(TestPoint2DPoint3D.java) 1/** 2 * A test driver for the Point2D and Point3D classes 3 */ 4public class TestPoint2DPoint3D { 5 public static void main(String[] args) { 6 /* Test Point2D */ 7 // Test constructors and toString() 8 Point2D p2a = new Point2D(1, 2); 9 System.out.println(p2a); // toString() 10 Point2D p2b = new Point2D(); // default constructor 11 System.out.println(p2b); 12 // Test Setters and Getters 13 p2a.setX(3); // Test setters 14 p2a.setY(4); 15 System.out.println(p2a); // toString() 16 System.out.println("x is: " + p2a.getX()); 17 System.out.println("x is: " + p2a.getY()); 18 19 /* Test Point3D */ 20 // Test constructors and toString() 21 Point3D p3a = new Point3D(11, 12, 13); 22 System.out.println(p3a); // toString() 23 Point2D p3b = new Point3D(); // default constructor 24 System.out.println(p3b); 25 // Test Setters and Getters 26 p3a.setX(21); // in superclass 27 p3a.setY(22); // in superclass 28 p3a.setZ(23); // in this class 29 System.out.println(p3a); // toString() 30 System.out.println("x is: " + p3a.getX()); // in superclass 31 System.out.println("y is: " + p3a.getY()); // in superclass 32 System.out.println("z is: " + p3a.getZ()); // in this class 33 } 34}