-
June 13th, 2014, 06:40 PM
#1
Point Class Help....Testing for invalid input
My program should allow the user to enter in the x-coordinate and y-coordinate to set a point. I created a method to test for invalid input using the hasNextInt() method. I know i should be using a looping structure of some sort to repeatedly ask the user for an integer if they type something in that is invalid such as a character. How can I implement this method so that it stores the users x value that they enter? Am i on the right track?
// TEST DRIVER
import java.io.*;
import java.util.*;
public class Lab4 {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
Point p1 = new Point();
Point p2 = new Point(7,13);
Point p3 = new Point(7,15);
//declare variables
int x, y, z;
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(p3.toString());
if (p2.isVertical(p3) == true){
System.out.println("Second point " + p2 + " Lines up vertically with third point " + p3);
}else {
System.out.println("The points do not line up vertically ");
}
if (p2.isHorizontal(p3) == true){
System.out.println("Second point " + p2 + "Lines up horizontally with third point " + p3);
}else {
System.out.println("The points do not line up horizontally ");
}
System.out.println();
System.out.println("Enter the x-coordinate for the first point: "); // this is where I'm stuck
}// end main
}
//point class
import java.util.*;
public class Point {
Scanner input = new Scanner(System.in);
private int x; // to store variables for x & y
private int y;
private int z;
//default constructor
public Point () {
x = 0;
y = 0;
z = 0;
}
//alternate constructor
public Point (int x1, int y1) {
x = x1;
y = y1;
z = 0;
}
//alternate constructor
public Point (int x1, int y1, int z1) {
x = x1;
y = y1;
z = z1;
}
//set coordinates
public void setCoord (int x1, int y1){
x = x1;
y = y1;
}
//set coordinates
public void setCoord (int x1, int y1, int z1){
x = x1;
y = y1;
z = z1;
}
public String toString() {
return "x = " + x + ", y = " + y + ", z = " + z ;
}
//print method
public void print (int x, int y) {
System.out.println(x + "," + y);
}
//toString Method
public String toString1() {
return "First Point is " + x + ", " + y + ")";
}
public String toString2() {
return "Second Point is " + x + ", " + y + ")";
}
public String toString3() {
return "Third Point is " + x + ", " + y + ")";
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public void equals (Point p2) { // calles like if (p1.equals(p2))
if ((x==p2.getX()) && (y==p2.getY()) && (z==p2.getZ()))
System.out.println(" Coordinates are the same ");
else {
System.out.println(" Coordinates are different ");
}
}
public void copy(Point temp) {
x=temp.x;
y=temp.y;
}
public Point getCopy() {
Point temp = new Point();
temp.x = x;
temp.y = y;
return temp;
}
public boolean invalidInput(Point p){ //checks for integer input
if(!input.hasNextInt()){
System.out.println("Not an integer! Try again! Enter the x- coordinate for first point: ");
return true;
}else{
return false;
}
}
public void distanceFromOrigin(int x1, int y1) {
double dx = x-x1;
double dy = y-y1;
}
public void distance (int x1, int y1) {
double distance = Math.sqrt((x * x1) + (y * y1));
}
public void transform (int dx, int dy) {
x += dx;
y += dy;
}
public boolean isHorizontal (Point otherPoint) {
if (y == otherPoint.y) {
return true;
} else {
return false;
}
}
public boolean isVertical (Point otherPoint ) {
if (x == otherPoint.x) {
return true;
} else {
return false;
}
}
public double slope(int dx, int dy) {
int slope;
slope = (dy/dx);
return slope;
}
}
-
June 13th, 2014, 07:07 PM
#2
Re: Point Class Help....Testing for invalid input
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|