Showing posts with label implement. Show all posts
Showing posts with label implement. Show all posts

Transforming an ADT into java interface

Problem:

Implement the ADT below:
ADT: Polynomial
degree(): int
derivative(): Polynomial
equals(Point): Boolean
sum(Polynomial): Polynomial
toString(): String
valueAt(Real): Real

Output:

Not applicable.

Solution:

public class MyPolynomial implements Polynomial {
 private double[] c; // coefficients
 public MyPolynomial(double[] a) { // a[i] = coeffficient of x^i
 int n = c.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(" + %.2f", c[1]));
 }
for (int i=2; i<n; i++) {
 if (c[i] != 0.0) {
 buf.append(String.format(" + %.2f^%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;
 }
}
Read More

How to implement an ADT in Java

Problem:

Implement the ADT below:
ADT: Line
contains(Point): Boolean
equals(Line): Boolean
isHorizontal(): Boolean
isVertical(): Boolean
slope(): Real
toString(): String
xIntercept(): Real
yIntercept(): Real

Output:

Not applicable.

Solution:

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.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;
 }
}
Read More

Implement the ADT with a Java Example

Problem:

Implement the ADT below:
ADT: Point
amplitude(): Real
distanceTo(Point): Real
equals(Point): Boolean
magnitude(): Real
toString(): String
xCoordinate(): Real
yCoordinate(): Real

Output:

Not applicable.

Solution:

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;
 }
}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact