简体   繁体   中英

Printing and comparing array data in java

I recently started learning Java and I'm having some trouble understanding how to make arrays to work the way I want them to. Here I have an assignment of creating a polygon class with different methods inside. Basically the class represents a convex polygon in a plane. The array receives user input that consists of x and y coordinates and places them inside. (max number of vertices is 10). There are some functions that I have no idea how to do and I would really appreciate some help with. Point Class - which is used to get coordinates

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 + ")";
  }
}

Polygon Class - main class im working on

/**
 * 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() {
 
    }
}

I have no idea how to do these functions: Line 44: public Point highestVertex() {} - returns a copy of the highest point in the polygon. If there is more than one vertice at the same Y - the method will return the first one it encountered (with the said Y) If there are no vertices aka the array is empty it will return null. Line 52: public String toString() {} - method that returns a string of points representing the polygon. The string should be in the following format: The polygon has 5 vertices: ((2.0,1.0),(5.0,0.0),(7.0,5.0),(4.0,6.0),(1.0,4,0)) If there are no vertices the method will return a string in the following format: The polygon has 0 vertices.

English is not my first language so I apologize for any grammar mistakes in advance.

First, it's not really the best to ask homework questions here, this is a concept that you should learn.

In highestVertex(), they outline the 3 cases for you: 1st case: If point-y is equal to another point-y, return the first vertex that has point-y. 2nd case: if arr has no elements return null. 3rd case: in loop, check each element's y value in the array and compare it to the biggest y so far. Use this line before your loop:

int max = Integer.MIN_VALUE;

Inside loop:

if (arr[i] > max) max = arr[i]

For toString(), again loop throughout the array, and add each point to a tracker string that you will return.

String str = "";

loop

str += arr[i].toString() + ",";

This works except you need to loop until arr's second to last element, as you will have an extraneous comma after the last point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM