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

10.8

咳,这个代码太多,不推荐做了,想做的老铁照着以前的练习题8.12,按照题目改改就行,很简单。

10.9

import java.util.ArrayList;
import java.util.List;

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Course course1 = new Course("Date Structures");
	course1.addStudents("Peter Jones");
	course1.addStudents("Kim Smith");
	course1.addStudents("Anne Kennedy");
	List<String> students = course1.getStudents();
	System.out.println("Number of students in course1: " + students.size());
	System.out.print(students.get(0));
	for (int i = 1; i < students.size(); i++) {
		System.out.print(", " + students.get(i));
	}
	course1.dropStudents("Peter Jones");
	System.out.println("\nNumber of students in course1: " + students.size());
	System.out.print(students.get(0));
	for (int i = 1; i < students.size(); i++) {
		System.out.print(", " + students.get(i));
	}
}

}

class Course {

	private String courseName;
	private ArrayList<String> students = new ArrayList<String>();
	private int numOfStudents;
	public Course(String courseName) {
		this.courseName = courseName;
	}

	public String getCourseName() {
		return courseName;
	}

	public void addStudents(String student) {
		students.add(student);
	}

	public ArrayList<String> getStudents() {
		return students;
	}

	public int getNumberOfStudents() {
		return students.size();
	}

	public void dropStudents(String student) {
		students.remove(student);
	}

	public void clear() {
		students.clear();
	}

}

10.10

public class dishizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Queue qu = new Queue();
    for (int i = 0;i < 20;i++)
        qu.enqueue(i);
    while (!qu.empty()){
        System.out.print(qu.dequeue() + " ");
    }
}

}

class Queue {

private  int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 8;
public Queue(){
    this(DEFAULT_CAPACITY);
}
public Queue(int capacity){
    elements = new int[capacity];
}
public void enqueue(int value){
    if (size >= elements.length){
        int[] temp = new int[elements.length * 2];
        System.arraycopy(elements,0,temp,0,elements.length);
        elements = temp;
    }
    elements[size] = value;
    size++;
}
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() {
	return month;
}

public int getDay() {
	return day;
}

public void setDate(long elapsedTime) {
	GregorianCalendar g = new GregorianCalendar();
	g.setTimeInMillis(elapsedTime);
	this.year = g.get(GregorianCalendar.YEAR);
	this.month = g.get(GregorianCalendar.MONTH);
	this.day = g.get(GregorianCalendar.DAY_OF_MONTH);
}

}

10.15

import java.util.Arrays;
import java.util.Scanner;

public class dishizhang {

public static void main(String[] args) {
	System.out.print("Enter five points:");
	Scanner input = new Scanner(System.in);
	double[][] points = new double[5][2];
	for (int i = 0; i < points.length; i++) {
		for (int k = 0; k < points[i].length; k++) {
			points[i][k] = input.nextDouble();
		}
	}
	MyRectangle2D r1 = getRectangle(points);
	System.out.print("The bounding rectangle's center ( " + r1.getX() + " , " + r1.getY() + " ), width "
			+ r1.getWidth() + ", height " + r1.getHeight());
	input.close();
}

public static MyRectangle2D getRectangle(double[][] points) {
	double[] x = new double[points.length];
	double[] y = new double[points.length];
	for (int i = 0; i < points.length; i++) {
		x[i] = points[i][0];
		y[i] = points[i][1];
	}
	Arrays.sort(x);
	Arrays.sort(y);
	MyRectangle2D r1 = new MyRectangle2D();
	r1.setX((x[4] + x[0]) / 2);
	r1.setY((y[4] + y[0]) / 2);
	r1.setWidth(x[4] - x[0]);
	r1.setHeight(y[4] - y[0]);
	return r1;
}

}

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

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值