日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第10章:*10.15(几何:边框)边框是指包围一个二维平面上点集的最小矩形,编写一个方法,为二维平面上一系列点返回一个边框

發(fā)布時間:2024/8/1 java 34 豆豆

*10.15編寫一個方法,為二維平面上一系列點返回一個邊框

  • 題目
    • 程序
  • 代碼
    • Test15.java
    • Test13_MyRectangle2D.java
    • 運行結(jié)果

題目


點擊這里跳轉(zhuǎn)編程練習(xí)題10.13

程序

Test15.java:測試程序
Test13_MyRectangle2D.java:編程練習(xí)題10.13構(gòu)造程序

代碼

Test15.java

import java.util.Scanner;public class Test15 {public static void main(String[] args) {// 獲取5個點的坐標(biāo)double[][] points = new double[5][2];System.out.print("Enter five points: ");Scanner input = new Scanner(System.in);for (int m = 0 ; m < 5 ; m++){for (int n = 0 ; n < 2 ; n++){points[m][n] = input.nextDouble();}}// 結(jié)束結(jié)果Test13_MyRectangle2D mt = getRectangle(points);System.out.print("The bounding rectangle's center (" + mt.getX() + ", " + mt.getY() +"), width " +mt.getWidth() + ", height " + mt.getHeight());}public static Test13_MyRectangle2D getRectangle(double[][] points){double xMax = points[0][0], yMax = points[0][1], xMin = points[0][0], yMin = points[0][1];for (int a = 0 ; a < points.length ; a++){if (points[a][0] > xMax)xMax = points[a][0];if (points[a][0] < xMin)xMin = points[a][0];if (points[a][1] > yMax)yMax = points[a][1];if (points[a][1] < yMin)yMin = points[a][1];}return new Test13_MyRectangle2D((xMax + xMin) / 2, (yMax + yMin) / 2, xMax - xMin, yMax - yMin);} }

Test13_MyRectangle2D.java

public class Test13_MyRectangle2D {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;}private double width, 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;}public Test13_MyRectangle2D(){x = 0;y = 0;width = 1;height = 1;}public Test13_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 width * height;}public double getPerimeter(){return 2 * (width + height);}public boolean contains(double x, double y){boolean bool1 = false, bool2 = false;if (x > this.x - width / 2 && x < this.x + width / 2)bool1 = true;if (y > this.y - height / 2 && y < this.y + height / 2)bool2 = true;return bool1 && bool2;}public boolean contains(Test13_MyRectangle2D r){return ((Math.abs(x - r.x)) < width / 2) && (Math.abs(y - r.y) < height / 2);}public boolean overlaps(Test13_MyRectangle2D r){return ((Math.abs(x - r.x)) < (width / 2 + r.width / 2)) && (Math.abs(y - r.y) < (height / 2 + r.height / 2));} }

運行結(jié)果

Enter five points:1.0 2.5 3 4 5 6 7 8 9 10 The bounding rectangle's center (5.0, 6.25), width 8.0, height 7.5

總結(jié)

以上是生活随笔為你收集整理的Java黑皮书课后题第10章:*10.15(几何:边框)边框是指包围一个二维平面上点集的最小矩形,编写一个方法,为二维平面上一系列点返回一个边框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。