Question
I need help with writing tests for class Point according to these specifications below Test cases for Constructors # Test Cases Expected Result 1 Point
I need help with writing tests for class Point according to these specifications below
Test cases for Constructors
# | Test Cases | Expected Result |
1 | Point p = new Point(); | getX() should return 0 getY() should return 0 |
2 | Point p = new Point(5,10); | getX() should return 5 getY() should return 10 |
3 | Point p = new Point (5,10); | getX() should return 5 getY() should return 10 |
4 | new Point( ); | should throw IllegalArgumentException |
5 | Point p = new Point (5,10); Point q = new Point(p); | getX() for q should return 5 getY() for q should return 10 |
6 | Point p; Point q = new Point(p); | should throw NullPointerException |
Test cases for toString()
# | Test Cases | Expected Result |
1 | Point p = new Point(5,10) p.toString() | (5,10) |
2 | Strng str = 3,7; Point p = new Point(str) p.toString() | (3,7) |
3 | Point p = new Point(5,10); Point q = new Point(p); q.toString() | (5,10) |
Test cases for equals()
# | Test Cases | Expected Result |
1 | Point p = new Point(5,10) p.equals(null) | false |
2 | Point p = new Point(5,10) Point q = new Point(5,10) p.equals(q) | true |
3 | Point p = new Point(5,10) Point q = new Point(10,5) p.equals(q) | false |
Test cases for manhattanDistance()
# | Test Cases | Expected Result |
1 | Point p = new Point(10,15) p.manhattanDistance(p) | 0 |
2 | Point p = new Point(5,7) Point q = new Point(10,15) p.manhattanDistance(q) | 13 |
3 | Point p = new Point(5,7) Point q = new Point(0,0) p.manhattanDistance(q) | 12 |
4 | Point p = new Point(5,7) Point q = new Point(0,-5) p.manhattanDistance(q) | 17 |
----
This is my code;
---
public class Point {
private int x; private int y;
/** * Initializes a newly created Point object with x and y * coordinates set to 0. */ public Point() {
}
/** * Initializes a newly created Point object with the given * values. * * @param x the x coordinate of this point * @param y the y coordinate of this point */ public Point(int x, int y) { this.x = x; this.y = y; }
/** * Initializes a newly created Point object with the values * from the input string. Throws an IllegalArgumentException * if parameter str is a null reference or contains more * than two values; sets both instance variables to zero if * str contains nothing but space characters. * * @param str string containing values of coordinates, such * as "10,20". */ public Point(String str) { String arr[] = str.split(","); this.x = Integer.parseInt(arr[0]); this.y = Integer.parseInt(arr[1]);
}
/** * Initializes a newly created Point object with the values * from the input Point object. * * @param other a Point object used to initialize this Point * object */ public Point(Point other) { this.x = other.x; this.y = other.y; }
/** * Returns the x coordinate of this Point object. * * @return the x coordinate of this object. */ public int getX() { return x; }
/** * Returns the y coordinate of this Point object. * * @return the y coordinate of this object. */ public int getY() { return y; }
/** * Returns a String object that represents this Point as, * for example, (5, 3) if x is 5 and y is 3. * * @return a string representation of this Point's value. */ public String toString() {
return "(" + x + "," + y + ")"; }
/** * Compares this object to the other object. The result is * true if and only if the argument is not null and is a * Point object that contains the same values as this Point * object. * * @param obj the object to compare with. * * @return true if the objects are the same; false * otherwise. */
public boolean equals(Object other) { if (this == other) return true; if (other == null) return false; if (getClass() != other.getClass()) return false; Point pt = (Point) other; if (x != pt.x) return false; if (y != pt.y) return false; return true; }
/** * Returns the Manhattan distance between this Point object * and the other Point object. * * Manhattan distance is the distance between two points if * you walk only in a horizontal or vertical direction. * * @param other the other Point object * * @return the Manhattan distance between this and other * Point objects. */ public int manhattanDistance(Point other) { int x = Math.abs(this.x - other.x);
int y = Math.abs(this.y - other.y);
return (x + y); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started