用java打印和比较数组数据
我最近开始学习Java,在理解如何使数组按我希望的方式工作时遇到了一些困难。 这里我有一个任务,创建一个多边形类,里面有不同的方法。 基本上,该类表示平面中的凸多边形。 该阵列接收由x和y坐标组成的用户输入,并将其放置在内部。(最大顶点数为10)。 有一些功能,我不知道如何做,我真的很感谢一些帮助。 点类-用于获取坐标用java打印和比较数组数据,java,Java,我最近开始学习Java,在理解如何使数组按我希望的方式工作时遇到了一些困难。 这里我有一个任务,创建一个多边形类,里面有不同的方法。 基本上,该类表示平面中的凸多边形。 该阵列接收由x和y坐标组成的用户输入,并将其放置在内部。(最大顶点数为10)。 有一些功能,我不知道如何做,我真的很感谢一些帮助。 点类-用于获取坐标 public class Point { private double _x; private double _y; public Point() {
public class Point {
private double _x;
private double _y;
public Point() {
this._x = 0.0D;
this._y = 0.0D;
}
public Point(double x, double y) {
this._x = x;
this._y = y;
}
public Point(Point other) {
this._x = other._x;
this._y = other._y;
}
public double getX() {
return this._x;
}
public double getY() {
return this._y;
}
public void setX(double x) {
if (x >= 0.0D)
this._x = x;
}
public void setY(double y) {
if (y >= 0.0D)
this._y = y;
}
public boolean isAbove(Point other) {
return (this._y > other._y);
}
public boolean isUnder(Point other) {
return other.isAbove(this);
}
public boolean isLeft(Point other) {
return (this._x < other._x);
}
public boolean isRight(Point other) {
return other.isLeft(this);
}
public double distance(Point other) {
double distance = Math.sqrt(Math.pow(this._x - other._x, 2.0D) + Math.pow(this._y - other._y, 2.0D));
return distance;
}
public void move(double dx, double dy) {
double x = this._x + dx;
double y = this._y + dy;
if (x >= 0.0D && y >= 0.0D) {
this._x = x;
this._y = y;
}
}
public boolean equals(Point other) {
return (this._x == other._x && this._y == other._y);
}
public String toString() {
return "(" + this._x + "," + this._y + ")";
}
}
公共类点{
私人双x;
私人双人房;
公共点(){
这个._x=0.0D;
这个._y=0.0D;
}
公共点(双x,双y){
这个.x=x;
这个._y=y;
}
公共点(其他点){
这个。x=其他。x;
这个。_y=其他。_y;
}
公共双getX(){
把这个还给我;
}
公共双盖{
把这个还给我;
}
公共无效集x(双x){
如果(x>=0.0D)
这个.x=x;
}
公共空间设置(双y){
如果(y>=0.0D)
这个._y=y;
}
公共布尔值isAbove(点其他){
返回(此._y>其他._y);
}
公共布尔值低于(点其他){
返回其他。isAbove(本);
}
公共布尔isLeft(点其他){
返回(本.x<其他.x);
}
公共布尔值isRight(点其他){
返回other.isLeft(this);
}
公共双倍距离(点其他){
双距离=Math.sqrt(Math.pow(this.\ux-other.\ux,2.0D)+Math.pow(this.\uy-other.\uy,2.0D));
返回距离;
}
公共无效移动(双dx,双dy){
双x=这个;
双y=这个;
如果(x>=0.0D&&y>=0.0D){
这个.x=x;
这个._y=y;
}
}
公共布尔等于(点其他){
返回(this.\ux==other.\ux&&this.\uy==other.\uy);
}
公共字符串toString(){
返回“(“+this.\ux+”,“+this.\uy+”)”;
}
}
多边形类-我正在处理的主类
/**
* Write a description of class Polygon here.
*
* @author [REDACTED]
* @version (Ver 1.0)
*/
public class Polygon {
private Point[] _vertices;
private int _noOfVertices;
public Polygon() {
_vertices = (Point[]) new Point[10];
_noOfVertices = 0;
}
public Polygon(Point[] arr) {
_vertices = (Point[]) new Point[10];
_noOfVertices = 0;
if (arr.length > 10) {
return;
}
// for (Point P : arr)
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
_vertices[i] = arr[i];
_noOfVertices++;
}
}
}
public boolean addVertex(double x, double y) {
if (_noOfVertices >= 10)
return false;
Point p = new Point(x, y);
_vertices[_noOfVertices] = p;
_noOfVertices++;
return true;
}
public Point highestVertex() {
for (int i = 0; i < _noOfVertices; i++) {
}
}
public String toString() {
}
public double calcPerimeter() {
for (int i = 0; i < arr.length; i++) {
}
}
public double caclArea() {
Point ppp = _vertices[zzz]
}
public boolean isBigger(Polygon other) {
}
public int findVertex(Point p) {
for (int i = 0; i < _noOfVertices; i++) {
if (p.equals(_vertices[i])) {
return i;
}
}
return -1;
}
public Point getNextVertex(Point p) {
for (int i = 0; i < _noOfVertices; i++) {
if (p.equals(_vertices[i])) {
if (i == _noOfVertices - 1) {
return new Point(_vertices[0]);
}
return new Point(_vertices[i + 1]);
}
}
return null;
}
public Polygon getBoundingBox() {
}
}
/**
*在这里写一个类多边形的描述。
*
*@作者[修订]
*@version(1.0版)
*/
公共类多边形{
私有点[]_顶点;
私人国际公寓;
公共多边形(){
_顶点=(点[])新点[10];
_无弹性=0;
}
公共多边形(点[]arr){
_顶点=(点[])新点[10];
_无弹性=0;
如果(arr.length>10){
返回;
}
//对于(P点:arr)
对于(int i=0;i=10)
返回false;
点p=新点(x,y);
_顶点[_noOfVertices]=p;
_noOfVertices++;
返回true;
}
公共点最高顶点(){
对于(int i=0;i<\u无自由度;i++){
}
}
公共字符串toString(){
}
公用双测径仪(){
对于(int i=0;i
我不知道如何实现这些功能:
第44行:公共点highestVertex(){}-返回多边形中最高点的副本。如果在同一个Y上有多个顶点-该方法将返回它遇到的第一个顶点(使用所述Y),如果没有顶点(即数组为空),则将返回null。
第52行:公共字符串toString(){}-返回表示多边形的点字符串的方法。字符串应采用以下格式:
多边形有5个顶点:
((2.0,1.0),(5.0,0.0),(7.0,5.0),(4.0,6.0),(1.0,4,0))
如果没有顶点,该方法将返回以下格式的字符串:
多边形有0个顶点
英语不是我的第一语言,所以我为任何语法错误提前道歉。首先,在这里问家庭作业问题并不是最好的,这是一个你应该学习的概念 在highestVertex()中,它们为您概述了3种情况: 第一种情况:如果点y等于另一个点y,则返回具有点y的第一个顶点。 第二种情况:如果arr没有元素,则返回null。 第三种情况:在循环中,检查数组中每个元素的y值,并将其与迄今为止最大的y值进行比较。 在循环之前使用此行:
int max = Integer.MIN_VALUE;
内部循环:
if (arr[i] > max) max = arr[i]
对于toString(),再次在整个数组中循环,并将每个点添加到将返回的跟踪器字符串中
String str = "";
环路
除了需要循环直到arr的倒数第二个元素外,这是有效的,因为在最后一个点之后会有一个多余的逗号。首先,在这里问家庭作业问题并不是最好的,这是一个你应该学习的概念 在highestVertex()中,它们为您概述了3种情况: 第一种情况:如果点y等于另一个点y,则返回具有点y的第一个顶点。 第二种情况:如果arr没有元素,则返回null。 第三种情况:在循环中,检查数组中每个元素的y值,并将其与迄今为止最大的y值进行比较。 在循环之前使用此行:
int max = Integer.MIN_VALUE;
内部循环:
if (arr[i] > max) max = arr[i]
对于toString(),再次循环