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

将下面的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;
	}
}

  

时间: 2024-11-08 19:46:07

学习笔记——数据结构学习指导与习题解答的相关文章

[学习笔记]数据结构与算法

1.排序简单排序:?冒泡排序:将n个数从上往下排列,从第0个数开始依次对前n个.前n-1个.前n-2个数进行比较,保持小数在前大数在后,不符合就交换.在这个过程中,最后一个数始终是最大数.?选择排序:对所有n个.后n-1个.后n-2个依次比较,用一个变量存最小数,一趟比较完成之后,将最小数与所比较数据的第一个数进行交换.在这个过程中,第一个数始终是最小数.?插入排序:从第1个数开始向前扫描比较,小则插入.对于未排序数据,在已排序序列中向前扫描,并找到相应的位置插入.在这个过程中,整个序列局部有序

JAVA学习笔记 -- 数据结构

一.数据结构的接口 在Java中所有类的鼻祖是Object类,但是所有有关数据结构处理的鼻祖就是Collection和Iterator接口,也就是集合与遍历. 1.Collection接口 Collection c = new Xx(); // c可以称为Collection接口回调对象,虽然它被声明为Collection类型,但是实例化时实现的是接口的实现类Xx.它的方法也是用来操作实现类的对象. <span style="white-space:pre"> </s

contiki-main.c 中的process系列函数学习笔记 &lt;contiki学习笔记之六&gt;

说明:本文依然依赖于 contiki/platform/native/contiki-main.c 文件. ------------------------------------------------------------------------------------------------------------------------------------- 根据上一个笔记里面添加的printf()语句的打印信息提示,hello world 打印是在执行了 1 autostart_

[学习笔记]iphone学习小技巧

1. 版本控制 -- 是否响应某个方法 .查看当前系统版本. eg: [self respondsToSelector:@Selector(presentModalViewController:animated:)]//Yes:表示响应这个方法 [[UIDevice currentDevice].systemVersion floatValue] < 7.0 //判断当前系统是否小于7.0 2. 模态视图动画设置 eg: ModalViewController *modalVC = [[Moda

java JDK8 学习笔记——助教学习博客汇总

java JDK8 学习笔记——助教学习博客汇总 1-6章 (by肖昱) Java学习笔记第一章——Java平台概论 Java学习笔记第二章——从JDK到IDEJava学习笔记第三章——基础语法Java学习笔记第四章——认识对象 Java学习笔记第五章——对象封装 Java学习笔记第六章——继承与多态 7-10.12.14章 (by吴子怡) Java学习笔记JDK8>学习总结 11.13.15-18章 (by宋宸宁) 第11章 第13章第15章第16章第17章第18章

C++primer(第五版)第二章的学习笔记(也有对部分习题的解答和指出c++11特性)

算术类型分为两类:整型(字符和bool在内)和浮点型. C/C++算术类型 类型 含义 最小尺寸 bool 布尔类型 未定义 char 字符 8位 wchar_t 宽字符 16位 char16_t Unicode字符 16位 char32_t Unicode字符 32位 short 短整型 16位 int  整型 16位 long 长整型 32位 long long 长整型 64位 unsigned long 无符号长整型 32位 double 双精度浮点数 10位有效数字 long doubl

Python学习笔记——数据结构和算法(一)

1.解压序列赋值给多个变量 任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多个变量. 唯一的前提就是变量的数量必须跟序列元素的数量是一样的. >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ]>>> name, shares, price, date = data >>> name, shares, price, (year, mon, day) = data >>>

R语言学习笔记——数据结构

参考书籍:R语言实战 数据结构: 1. 向量 : 用于存储数值型.字符型或逻辑型数据的一维数组 1.1 创建 : a <- c(1, 2, 3, 4) 1.2 访问 : a[1] : 1 a[c(2, 4)] : 2 4 (向量a中的第二个和第四个元素) a[1:4] : 1 2 3 4 (向量a中的第一个直到第四个元素) 1.3 注意 : 1)  单个向量中的数据必须拥有相同的类型或模式(数值型.字符型或逻辑型) 2) 标量是只含一个元素的向量,例如f <- 3 . g <- &quo

【 python 学习笔记 -- 数据结构与算法 】哈希表 Implementation of a Hash Table

Python内建的字典就是用 hash table实现的.这里我们只是通过实现自己的hash table来加深对hash table 和hash functions的理解. [ 概念1: Mapping (映射)] 字典通过键(Key)来索引.一个key对应一个存储的value.任意不可变的数据类型均可作为key. [ 概念2:Hash Table (哈希表)] Hash Table根据key直接访问在内存存储位置的数据结构,因而加快了查找速度 (O(1)). 下图是一个size为11的空的Ha