Convert the following ADT into a Java interface and implement it with a Java class:
Adt:point
Amplitude (): Real
Distanceto (point): Real
Equals (point): Boolean
Magnitude (): Real
ToString (): String
Xcoordinate (): Real
Ycoordinate (): Real
Adt:line
Contains (point): Boolean
Equals (line): Boolean
IsHorizontal (): Boolean
Isvertical (): Boolean
Slope (): Real
ToString (): String
Xintercept (): Real
Yintercept (): Real
Adt:circle
Area (): Real
Center ():P Oint
Circumference (): Real
Contains (point): Boolean
Equals (Circle): Boolean
Radius (): Real
ToString (): String
Adt:polynomial
Derivative ():P olynomial
Equals (polynomial): Boolean
sum (polynomial):P olynomial
ToString (): String
Valueat (real): Real
Java interface:
Public interface Point {public double amplitude ();p ublic double Distanceto (point point);p ublic boolean equals (Object Obje CT);p ublic double magnitude ();p ublic String toString ();p ublic double xcoordinate ();p ublic double ycoordinate ();}
Public interface Line {public ' Boolean contains (point point);p ublic Boolean equals (Object object);p ublic boolean Ishorizon Tal ();p ublic boolean isvertical ();p ublic double slope ();p ublic String toString ();p ublic double xintercept ();p ublic Double yintercept ();}
Public interface Circle {public double area ();p ublic Point Center ();p ublic double circumference ();p ublic Boolean contains (Point point);p ublic Boolean equals (Object object);p ublic double radius ();p ublic String toString ();
public interface polynomial {public int degree ();p ublic polynomial derivative ();p ublic Boolean equals (Object object); Public polynomial sum (polynomial polynomial);p ublic String toString ();p ublic double valueat (double x);}
Java class:
public class MyPoint implements point {private Double X, y;public static point ORIGIN = new MyPoint ();p rivate mypoint () {} Public MyPoint (double x, double y) {this.x = X;this.y = y;} public double amplitude () {return Math.atan (y/x);} Public double Distanceto (point point) {if (Point.Equals (this)) {return 0.0;} else if (!) Point instanceof MyPoint) {throw new IllegalArgumentException (' Use a MyPoint object ');} else {mypoint that = (mypoint) PO int;double dx = that.x-this.x;double dy = That.y-this.y;return math.sqrt (dx * dx + dy * dy);}} public Boolean equals (Object object) {if (object = = this) {return true;} else if (!) Object instanceof MyPoint) {return false;} MyPoint that = (mypoint) object;return (that.x = = This.x && That.y = = This.y);} Public double magnitude () {return math.sqrt (x * x + y * y);} Public String toString () {return String.Format ("(%.2f,%.2f)", X, y);} Public double xcoordinate () {return x;} Public double ycoordinate () {return y;}}
public class Myline implements line {Private double m, b;//slope,interceptpublic static Line X_axis = new Myline ();p Riva Te Myline () {}public myline (double m, double b) {this.m = m;this.b = b;} Public Boolean contains (point point) {double x = point.xcoordinate ();d ouble y = point.ycoordinate (); return y = = m * x + B; }public Boolean equals (Object object) {if (object = = this) {return true;} else if (!) Object instanceof Myline) {return false;} Myline that = (myline) object;return (that.m = = THIS.M && that.b = = this.b);} public Boolean ishorizontal () {return m = = 0;} public Boolean isvertical () {return m = = Double.positive_infinity | | m = = double.negative_infinity;} public double slope () {return m;} Public String toString () {return String.Format ("y=%.2fx+%.2f", M, b);} Public double xintercept () {if (IsHorizontal ()) {throw new RuntimeException ("This line is horizontal");} Return-b/M;} Public double yintercept () {if (isvertical ()) {throw new RuntimeException ("This line is vertical");} RetuRN b;}}
public class Mycircle implements Circle {private point C;//Centerprivate double R; Radiuspublic mycircle () {}public mycircle (point C, double r) {this.c = C;THIS.R = r;} Public double area () {return Math.PI * R * r;} Public Point Center () {return C;} public double circumference () {return 2 * Math.PI * r;} Public Boolean contains (point point) {double x = point.xcoordinate ();d ouble y = point.ycoordinate (); return (X-c.xcoordina TE ()) * (X-c.xcoordinate ()) + (Y-c.ycoordinate ()) * (Y-c.ycoordinate ()) < R * R;} public Boolean equals (Object object) {if (object = = this) {return true;} else if (!) Object instanceof Mycircle) {return false;} Mycircle that = (mycircle) object;return (that.c = = THIS.C && THAT.R = = THIS.R);} Public double radius () {return r;} Public String toString () {return String.Format ("[center:%s; RADIUS:%.2F] ", C, R);}}
public class Mypolynomial implements polynomial {private double[] C;//Coefficientspublic mypolynomial (double[] a) {//A [I]=coefficient of X^iint n = a.length;c = new Double[n]; System.arraycopy (A, 0, c, 0, n);} public int degree () {return c.length-1;} Public polynomial derivative () {double[] da = new Double[c.length-1];for (int i = 0; i < da.length; i++) {Da[i] = (i + 1) * c[i + 1];} return new mypolynomial (DA);} public Boolean equals (Object object) {if (object = = this) {return true;} else if (!) Object instanceof Mypolynomial) {return false;} Mypolynomial that = (mypolynomial) Object;return java.util.Arrays.equals (that.c, THIS.C);} Public polynomial sum (polynomial p) {if (!) ( P instanceof mypolynomial) {throw new IllegalArgumentException ("Use a Mypolynomial object");} Mypolynomial that = (mypolynomial) p;double[] pc = that.c;int n = Math.max (c.length, pc.length); mypolynomial q = new mypolynomial (new Double[n]); for (int i = 0; i < n; i++) {q.c[i] = C[i] + pc[i];} return q;} Public StRing toString () {StringBuilder buf = new StringBuilder (); int n = c.length;if (n > 0 && c[0]! = 0.0) {Buf.append (C[0]);} if (n > 1 && c[1]! = 0.0) {buf.append (String.Format ("+%.2fx", c[1]));} for (int i = 2; i < n; i++) {if (c[i]! = 0.0) {buf.append (String.Format ("+%.2fx^%d", C[i], i));}} return buf.tostring ();} Public double valueat (double x) {Double y = 0.0;for (int i = 0; i < c.length; i++) {y + = c[i] * MATH.POW (x, i);} return y;}}
Learning notes--Data structure Learning guide and problem solving