概述
13.8
package dishisanzhang;
import java.util.ArrayList;
public class MyStack implements Cloneable{
private ArrayList<Object> list = new ArrayList<>();
public boolean isEmpty(){
return list.isEmpty();
}
public int getSize(){
return list.size();
}
public Object peek(){
return list.get(list.size()-1);
}
public Object pop(){
Object o = list.get(getSize()-1);
list.remove(getSize()-1);
return o;
}
public void push(Object o){
list.add(o);
}
@Override
public String toString(){
return "stack: "+list.toString();
}
@SuppressWarnings("unchecked") //告诉编译器忽略 unchecked 警告信息
@Override
public Object clone() throws CloneNotSupportedException{
MyStack stack = (MyStack)super.clone();
stack.list = (ArrayList<Object>)list.clone();
return stack;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
MyStack stack = new MyStack();
for (int i = 0; i < 10; i++)
stack.push(i);
MyStack stack1 = (MyStack) stack.clone();
for (int i = 0; i < 10; i++)
stack1.pop();
System.out.println(stack.getSize());
System.out.println(stack1.getSize());
}
}
13.9
package dishisanzhang;
import java.util.Date;
public abstract class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
protected GeometricObject() {
dateCreated = new Date();
}
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
dateCreated = new Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "Create on " + dateCreated + "nColor: " + color + "n and Filled" + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package dishisanzhang;
public class Circle extends GeometricObject implements Comparable< Circle > {
private double radius;
public Circle() {
}
public Circle(double radius) {
this(radius, "white", false);
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double getArea() {
return radius * radius * Math.PI;
}
@Override
public double getPerimeter() {
return 2 * radius * Math.PI;
}
@Override
public String toString() {
return "nCircle Radius : " + getRadius();
}
@Override
public int compareTo(Circle c) {
if (getArea() < c.getArea())
return -1;
else if (getArea() > c.getArea())
return 1;
else
return 0;
}
@Override
public boolean equals(Object o) {
if (((Circle) o).getRadius() == radius)
return true;
else
return false;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Circle c1 = new Circle(2);
Circle c2 = new Circle(3);
Circle c3 = new Circle(2);
System.out.println("c1 equals c2 ? " + c1.equals(c2));
System.out.println("c1 equals c3 ? " + c1.equals(c3));
}
}
13.10
package dishisanzhang;
import java.util.Date;
public abstract class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
protected GeometricObject() {
dateCreated = new Date();
}
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
dateCreated = new Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "Create on " + dateCreated + "nColor: " + color + "n and Filled" + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package dishisanzhang;
public class Rectangle extends GeometricObject implements Comparable< Rectangle > {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this(width, height, "white", false);
}
public Rectangle(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public int compareTo(Rectangle r) {
if (getArea() > r.getArea())
return 1;
else if (getArea() < r.getArea())
return -1;
else
return 0;
}
@Override
public boolean equals(Object o) {
if (compareTo((Rectangle) o) == 0)
return true;
else
return false;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Rectangle r1 = new Rectangle(1,1);
Rectangle r2 = new Rectangle(2,3);
Rectangle r3 = new Rectangle(3,2);
System.out.println("r1 equals r2 ? " + r1.equals(r2));
System.out.println("r2 equals r3 ? " + r2.equals(r3));
}
}
13.11
package dishisanzhang;
import java.util.Date;
public abstract class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
protected GeometricObject() {
dateCreated = new Date();
}
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
dateCreated = new Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "Create on " + dateCreated + "nColor: " + color + "n and Filled" + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package dishisanzhang;
public class Octagon extends GeometricObject implements Comparable< Octagon >, Cloneable {
private double side;
public Octagon() {
}
public Octagon(double side) {
this(side, "White", false);
}
public Octagon(double side, String color, boolean filled) {
super(color, filled);
this.side = side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double getArea() {
return (2 + 4 * Math.sqrt(2)) * side * side;
}
@Override
public double getPerimeter() {
return 8 * side;
}
@Override
public String toString() {
return super.toString() + "nside : " + side;
}
@Override
public int compareTo(Octagon octagon) {
if (getArea() > octagon.getArea())
return 1;
else if (getArea() < octagon.getArea())
return -1;
else
return 0;
}
@Override
public Object clone() {
try {
Octagon octagon = (Octagon) super.clone();
return octagon;
} catch (CloneNotSupportedException e) {
return null;
}
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Octagon o1 = new Octagon(2);
Octagon o2 = (Octagon)o1.clone();
System.out.println(o1.getArea());
if (o1.compareTo(o2)==0)
System.out.println("True! equal!");
else
System.out.println("False! not equal!");
}
}
13.12
使用上面的Circle,Rectangle ,Octagon类来测试sumArea()方法。
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Circle c1 = new Circle(2);
Circle c2 = new Circle(3);
Rectangle r1 = new Rectangle(2, 3);
Rectangle r2 = new Rectangle(3, 2);
Octagon o1 = new Octagon(2);
GeometricObject[] figures = { c1, c2, r1, r1, o1 };
double sum = sumArea(figures);
System.out.println(sum);
}
public static double sumArea(GeometricObject[] a) {
double sum = 0.0;
for (GeometricObject o : a)
sum += o.getArea();
return sum;
}
}
13.13
package dishisanzhang;
public class Course implements Cloneable {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
@Override
public Object clone() {
Course course = null;
try {
course = (Course) super.clone();
course.courseName = courseName;
course.numberOfStudents = numberOfStudents;
course.students = students.clone();
} catch (Exception e) {
// TODO: handle exception
}
return course;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Course c1 = new Course("");
for (int i = 0; i < 10; ++i)
c1.addStudent(i + "");
System.out.println(c1.getNumberOfStudents());
Course c2 = (Course) c1.clone();
c2.addStudent("sad");
System.out.println(c2.getNumberOfStudents());
String[] s = c2.getStudents();
System.out.print("c2: ");
for (int i = 0; i < 11; i++)
System.out.print(s[i] + " ");
}
}
13.14
package dishisanzhang;
class Rational extends Number implements Comparable< Rational > {
private long[] r = new long[2];
public Rational() {
this(1, 0);
}
public Rational(long numerator, long denominator) {
long l = gcd(numerator, denominator);
this.r[0] = ((denominator > 0) ? 1 : -1) * numerator / l;
this.r[1] = Math.abs(denominator) / l;
}
public static long gcd(long numerator, long denominator) {
long l1 = Math.abs(numerator);
long l2 = Math.abs(denominator);
int gcd = 1;
for (int k = 1; k <= l1 && k <= l2; k++) {
if (l1 % k == 0 && l2 % k == 0) {
gcd = k;
}
}
return gcd;
}
public long getNumerator() {
return r[0];
}
public long getDenominator() {
return r[1];
}
public Rational add(Rational secondRational) {
long n = r[0] * secondRational.getDenominator() + r[1] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
long n = r[0] * secondRational.getDenominator() - r[1] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational multiply(Rational secondRational) {
long n = r[0] * secondRational.getNumerator();
long d = r[1] * secondRational.getDenominator();
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
long n = r[0] * secondRational.getDenominator();
long d = r[1] * secondRational.getNumerator();
return new Rational(n, d);
}
@Override
public String toString() {
if (r[1] == 1 || r[0] == 0) {
return r[0] + "";
} else
return r[0] + "/" + r[1];
}
@Override
public boolean equals(Object other) {
if ((this.subtract((Rational) (other))).getNumerator() == 0) {
return true;
} else
return false;
}
@Override
public int intValue() {
return (int) doubleValue();
}
@Override
public float floatValue() {
return (float) doubleValue();
}
@Override
public double doubleValue() {
return r[0] * 1.0 / r[1];
}
@Override
public long longValue() {
return (long) doubleValue();
}
@Override
public int compareTo(Rational o) {
if (this.subtract(o).getNumerator() > 0) {
return 1;
} else if (this.subtract(o).getNumerator() < 0) {
return -1;
} else
return 0;
}
}
package dishisanzhang;
public class dishisanzhang {
public static void main(String[] args) throws CloneNotSupportedException {
Rational r1 = new Rational(1, 123456789);
Rational r2 = new Rational(1, 123456789);
Rational r3 = new Rational(1, 123456789);
System.out.println("r1*r2*r3 is " + r1.multiply(r2).multiply(r3));
}
}
最后
以上就是狂野八宝粥为你收集整理的Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(二)13.813.913.1013.1113.1213.1313.14的全部内容,希望文章能够帮你解决Java语言程序设计与数据结构(基础篇)课后练习题 第十三章(二)13.813.913.1013.1113.1213.1313.14所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复