Java语言程序设计与数据结构(基础篇)课后练习题 第十章(二)

}

public int dequeue(){

int key = elements[0];

for (int i = 0;i < size;i++){

elements[i] = elements[i+1];

}

size–;

return key;

}

public boolean empty(){

return size == 0;

}

public int getSize(){

return size;

}

}

10.11

==================================================================

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Circle2D c1 = new Circle2D(2, 2, 5.5);

System.out.println("The c1’s area is " + c1.getArea());

System.out.println("The c1’s perimeter is " + c1.getPerimeter());

System.out.println("The points contains is " + c1.contains(3, 3));

System.out.println("The circle contains is " + c1.contains(new Circle2D(4, 5, 10.5)));

System.out.println("The circle contains is " + c1.contains(new Circle2D(3, 5, 2.3)));

}

}

class Circle2D {

private double x;

private double y;

private double radius;

public Circle2D() {

this.x = 0;

this.y = 0;

this.radius = 1;

}

public Circle2D(double x, double y, double radius) {

this.x = x;

this.y = y;

this.radius = radius;

}

public double getX() {

return x;

}

public double getY() {

return y;

}

public double getRadius() {

return radius;

}

public double getArea() {

return Math.PI * radius * radius;

}

public double getPerimeter() {

return 2 * Math.PI * radius;

}

public boolean contains(double x, double y) {

return Math.pow((x - this.x), 2) + Math.pow((y - this.y), 2) < Math.pow(this.radius, 2);

}

public boolean contains(Circle2D circle) {

return Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) <= Math

.abs(circle.radius - this.radius);

}

public boolean overlaps(Circle2D circle) {

return Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) > Math

.abs(circle.radius - this.radius)

&& Math.pow((circle.x - this.x), 2) + Math.pow((circle.y - this.y), 2) < circle.radius + this.radius;

}

}

10.12

==================================================================

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Triangle2D triangle1 = new Triangle2D();

System.out.println(triangle1.getArea());

System.out.println(triangle1.getPerimeter());

System.out.println(triangle1.contains(new MyPoint2D(1, 1)));

System.out.println(triangle1.contains(new Triangle2D(new MyPoint2D(2.5, 2), new MyPoint2D(4.2, 3), new MyPoint2D(5, 3.5))));

}

}

class MyPoint2D {

private double x, y;

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

public MyPoint2D(double x, double y) {

this.x = x;

this.y = y;

}

public double distance(MyPoint2D p) {

return Math.sqrt(Math.pow(x - p.getX(), 2) + Math.pow(y - p.getY(), 2));

}

}

class Triangle2D {

private MyPoint2D p1, p2, p3;

public Triangle2D() {

this(new MyPoint2D(0, 0), new MyPoint2D(1, 1), new MyPoint2D(2, 5));

}

public Triangle2D(MyPoint2D p1, MyPoint2D p2, MyPoint2D p3) {

this.p1 = p1;

this.p2 = p2;

this.p3 = p3;

}

public double getArea() {

double d1 = p1.distance(p2);

double d2 = p2.distance(p3);

double d3 = p2.distance(p3);

double p = (d1 + d2 + d3) / 2;

return Math.sqrt(p * (p - d1) * (p - d2) * (p - d3));

}

public double getPerimeter() {

return p1.distance(p2) + p2.distance(p3) + p3.distance(p1);

}

public boolean contains(MyPoint2D p) {

double areaS = this.getArea();

double pA = (p.distance(p1) + p.distance(p2) + p1.distance(p2)) / 2;

double pB = (p.distance(p1) + p.distance(p3) + p1.distance(p3)) / 2;

double pC = (p.distance(p3) + p.distance(p2) + p3.distance(p2)) / 2;

double areaA = Math.sqrt(pA * (pA - p.distance(p1)) * (pA - p.distance(p2)) * (pA - p1.distance(p2)));// 海伦公式

double areaB = Math.sqrt(pB * (pA - p.distance(p1)) * (pB - p.distance(p3)) * (pB - p1.distance(p3)));

double areaC = Math.sqrt(pC * (pA - p.distance(p3)) * (pC - p.distance(p2)) * (pC - p3.distance(p2)));

if (areaS == areaA + areaB + areaC)

return true;

else

return false;

}

public boolean contains(Triangle2D t) {

if (contains(t.p1) && contains(t.p2) && contains(t.p3))

return true;

else

return false;

}

public MyPoint2D getP1() {

return p1;

}

public void setP1(MyPoint2D p1) {

this.p1 = p1;

}

public MyPoint2D getP2() {

return p2;

}

public void setP2(MyPoint2D p2) {

this.p2 = p2;

}

public MyPoint2D getP3() {

return p3;

}

public void setP3(MyPoint2D p3) {

this.p3 = p3;

}

}

10.13

==================================================================

public class dishizhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

MyRectangle2D r1 = new MyRectangle2D(2, 2, 5.5, 4.9);

System.out.println("The r1’s area is " + r1.getArea());

System.out.println("The r1’s perimeter is " + r1.getPerimeter());

System.out.println("The point contain is " + r1.contains(3, 3));

System.out.println("The new myRectangle contain is " + r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)));

System.out.println("The new myRectangle overLaps is " + r1.overLaps(new MyRectangle2D(3, 5, 2.3, 5.4)));

}

}

class MyRectangle2D {

private double x;

private double y;

private double width;

private double height;

public MyRectangle2D() {

this.x = 0;

this.y = 0;

this.height = 1;

this.width = 1;

}

public MyRectangle2D(double x, double y, double width, double height) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

}

public double getArea() {

return height * width;

}

public double getPerimeter() {

return (height + width) * 2;

}

public boolean contains(double x, double y) {

return Math.abs(x - this.x) < width / 2 && Math.abs(y - this.y) < height / 2;

}

public boolean contains(MyRectangle2D r) {

return Math.abs(this.x - r.getX()) <= Math.abs((this.width - r.width) / 2)

&& Math.abs(this.y - r.y) <= Math.abs((this.height - r.height) / 2);

}

public boolean overLaps(MyRectangle2D r) {

boolean overlaps1 = Math.abs(this.x - r.getX()) > Math.abs((this.width - r.width) / 2)

&& Math.abs(this.y - r.y) > Math.abs((this.height - r.height) / 2);

boolean overlaps2 = Math.abs(this.x - r.getX()) < Math.abs((this.width + r.width) / 2)

&& Math.abs(this.y - r.y) < Math.abs((this.height + r.height) / 2);

return overlaps1 && overlaps2;

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

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;

}

}

10.14

==================================================================

import java.util.GregorianCalendar;

public class dishizhang {

// 34355555133101L

public static void main(String[] args) {

MyDate t1 = new MyDate();

System.out.println("t1: " + t1.getYear() + “/” + t1.getMonth() + “/” + t1.getDay());

MyDate t2 = new MyDate(34355555133101L);

System.out.println("t2: " + t2.getYear() + “/” + t2.getMonth() + “/” + t2.getDay());

MyDate t3 = new MyDate();

t3.setDate(561555550000L);

System.out.println("t3: " + t3.getYear() + “/” + t3.getMonth() + “/” + t3.getDay());

}

}

class MyDate {

int year;

int month;

int day;

public MyDate() {

GregorianCalendar g = new GregorianCalendar();

this.year = g.get(GregorianCalendar.YEAR);

this.month = g.get(GregorianCalendar.MONTH);

this.day = g.get(GregorianCalendar.DAY_OF_MONTH);

}

public MyDate(long t) {

GregorianCalendar g = new GregorianCalendar();

g.setTimeInMillis(t);

this.year = g.get(GregorianCalendar.YEAR);

this.month = g.get(GregorianCalendar.MONTH);

this.day = g.get(GregorianCalendar.DAY_OF_MONTH);

}

public MyDate(int year, int month, int day) {

this.year = year;

this.month = month;

this.day = day;

}

public int getYear() {

return year;

}

public int getMonth() {

  • 24
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值