/* * 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 in the 1st quadrant of the * Cartesian plane * @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) { System.out.println("In setX method, input x value is "+x); if (x >= 0) { this.x = x; } else { //this.x = 0; do { System.out.println("The given x value "+x+" is not greater than or equal to 0."); 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 >= 0) { this.y = y; } else { do { System.out.println("The given y value "+y+" is not greater than or equal to 0."); System.out.println("Please enter a valid integer value for the y: "); y = input.nextInt(); } while (!setY(y)); } setDistanceFromOrigin(); return true; } public boolean setQuadrant(Integer quadrant) { 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+")"); } }