首页 > 代码库 > 学习笔记——数据结构学习指导与习题解答

学习笔记——数据结构学习指导与习题解答

将下面的ADT转换成Java接口并用一个Java类实现:

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():Point
circumference():Real
contains(Point):Boolean
equals(Circle):Boolean
radius():Real
toString():String

ADT:Polynomial
derivative():Polynomial
equals(Polynomial):Boolean
sum(Polynomial):Polynomial
toString():String
valueAt(Real):Real

Java接口:

public interface Point {	public double amplitude();	public double distanceTo(Point point);	public boolean equals(Object object);	public double magnitude();	public String toString();	public double xCoordinate();	public double yCoordinate();}
public interface Line {	public boolean contains(Point point);	public boolean equals(Object object);	public boolean isHorizontal();	public boolean isVertical();	public double slope();	public String toString();	public double xIntercept();	public double yIntercept();	}
public interface Circle {	public double area();	public Point center();	public double circumference();	public boolean contains(Point point);	public boolean equals(Object object);	public double radius();	public String toString();}
public interface Polynomial {	public int degree();	public Polynomial derivative();	public boolean equals(Object object);	public Polynomial sum(Polynomial polynomial);	public String toString();	public double valueAt(double x);}

Java类:

public class MyPoint implements Point {	private double x, y;	public static Point ORIGIN = new MyPoint();	private 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) point;			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,intercept	public static Line X_AXIS = new MyLine();	private MyLine() {	}	public MyLine(double m, double b) {		this.m = m;		this.b = b;	}	public boolean contains(Point point) {		double x = point.xCoordinate();		double 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; // center	private double r; // radius	public 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();		double y = point.yCoordinate();		return (x-c.xCoordinate()) * (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; // coefficients	public MyPolynomial(double[] a) { // a[i]=coefficient of x^i		int 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;	}}

  

学习笔记——数据结构学习指导与习题解答