/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pointtest; import java.util.Scanner; /** * class Point represents a point inside a robot playing field * with the origin in the center and limits of 10 in all x, y * axis directions * @author jcmtiernan */ public class Point { private int x = 0; private int y = 0; private double distanceFromOrigin; private Integer quadrant = new Integer(1); private Scanner input = new Scanner(System.in); public Point(int x, int y) { setX(x); setY(y); //setDistanceFromOrigin(); } public Point(int x) { this.setX(x); this.y = 0; this.setDistanceFromOrigin(); } public Point(int x, int y, Integer quadrant) { if (!this.setX(x)) { System.out.println("In constructing a point,x value is not in 1st quadrant"); } this.setY(y); if (!this.setQuadrant(quadrant)) { System.out.println("In constructing a point, quadrant "+quadrant+" value is not valid"); } setDistanceFromOrigin(); } public Point() { this.x = 0; this.y = 0; setDistanceFromOrigin(); } public boolean setX(int x) { // x can have any value from -10 to 10 inclusive //System.out.println("In setX method, input x value is "+x); if ((x >= -10) && (x <= 10)) { this.x = x; } else { //this.x = 0; //do { System.out.println("The given x value "+x+" is not within bounds so x will not change."); /* System.out.println("Please enter a valid integer value for the x: "); x = input.nextInt(); */ //} //while (!setX(x)); } setDistanceFromOrigin(); return true; } public boolean setY(int y) { if ((y >= -10) && (y <= 10)) { this.y = y; } else { System.out.println("The given y value "+y+" is not valid so y will not change"); } setDistanceFromOrigin(); return true; } public boolean setQuadrant(Integer quadrant) { //this method should do real quadrant checking and determination if (((quadrant.compareTo(0) ) > 0) && ((quadrant.compareTo(5)) < 0)) { this.quadrant = quadrant; return true; } return false; } public int getX() { return x; } public int getY() { return y; } public Integer getQuadrant() { return quadrant; } public double getDistanceFromOrigin() { return distanceFromOrigin; } public void setDistanceFromOrigin() { this.distanceFromOrigin = calculateDistanceFromOrigin(); } private double calculateDistanceFromOrigin() { //sqrt of x*x + y*y distanceFromOrigin = Math.sqrt(x*x + y*y); //System.out.println(" distance is calc as "+distanceFromOrigin); return distanceFromOrigin; } @Override public String toString() { return String.format("("+x+","+y+")"); } }