Purpose : In this homework you will write two classes meant to be used to instan
ID: 3765796 • Letter: P
Question
Purpose: In this homework you will write two classes meant to be used to instantiate objects of those types, but where the instantiated objects are themselves unique. This homework will continue your practice of creating classes and objects, as well as provide valuable exercises using arrays. You will also write a test class containing a main method in order to test your objects and their methods.
Point
Part a: Write an immutable class called Point to represent a point on the two dimensional Cartesian plane, with the following variables and methods:
1. Private data fields: x of type double and y of type double;
2. Public constructor : only one constructor that takes a double x_coord and a double Y_coord as arguments;
3. Public methods:
• boolean equals(Point anotherPoint) : returns true if the invoking Point is equal to the argument Point. Two points are equal if they have the same x coordinate and the same y coordinate;
• boolean lessThan(Point anotherPoint) : returns true if a Point is less than another Point by the following rule: first compare points by their y-coordinates, breaking ties by their x-coordinates. Formally, the invoking Point (x0, y0) is less than the argument Point (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1;
• double slopeTo(Point anotherPoint) : returns the slope between the invoking Point and the argument Point. The slope between two points is given by the formula: (y1 ? y0)/(x1 ? x0). The slope of a vertical line segment should be positive infinity, while the slope between a point and itself should be negative infinity.
• int compareSlopes(Point anotherPoint) : returns ?1 if the invoking Point has slope less than the argument Point from the origin, 0 if both points have equal slopes from the origin, and +1 if the invoking Point has slope greater than the argument Point from the origin. Note: the origin is the point (0, 0);
• String toString() : returns the string representation of a Point objects as follows: (x, y).
PointArray
Part b: Write a class called PointArray to represent an array of Point objects containing the following variables and methods:
1. Private data field: points an array of type Point;
2. Public constructors:
• a constructor that takes an array of doubles containing the x and y values of each point consecutively. That is, if we have the following array of doubles:
{2.4, 3.5, 4.7, 1.0, 6.4, 11.01}
then we can construct the array of three points:
{new Point(2.4, 3.5), new Point(4.7, 1.0), new Point(6.4, 11.01)}
Note: this constructor should only accept arrays of even length.
• a constructor that takes an argument of type FileInputStream, and constructs the points array by reading a text file containing the points. For example, if the file has the following content:
3
2 4
3.5 -1
0 17
then the first number is the number of points, and below it, each line is a point with x being the first number and y being the second. So we can read the first number in the file to construct a points array of that size, then loop through the file to construct each new Point and store it into the points array.
3. Public methods:
• boolean contains3collinear() : returns true if a PointArray contains three points which are collinear. For example, the points P0 : (x0, y0), P1 : (x1, y1), and P2 : (x2, y2) are collinear if the slope between P0 and P1 is the same as the slope between P1 and P2;
• void sort() : sorts the invoking PointArray object using selection sort: the sort is by the ascending order specified by the lessThan method from the Point class above;
• boolean equals(PointArray anotherPointArray) : returns true if the invoking PointArray is equal to the argument PointArray. Two arrays of points are equal if all the points are the same. Note: order does not matter for equality;
• String toString() : returns the string representation of a PointArray.
For example, a PointArray with 3 points P0 : (x0, y0), P1 : (x1, y1), and P2 : (x2, y2) would be printed as follows: {(x0, y0), (x1, y1), (x2,y2)}.
Testing
Part c: In a class called PointArrayTester, write a main method to test each of your constructors and methods from the PointArray class. Make at least three distinct PointArray objects and one non-distinct (to test the equals method). To test the constructor that takes a FileInputStream argument, download some of my test files from the Canvas assignment page. Note: if you wish you may pass in the test files to main as command line arguments, however this is not required.
Important: Please follow the API provided above. That is, use the method names and signatures as provided. I will be testing your code automatically with my own test files and classes.
Explanation / Answer
Below is your code.
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
Point.java
public class Point {
// data fields
private double x;
private double y;
// constructor
public Point(double x_coord, double y_coord) {
x = x_coord;
y = y_coord;
}
// equals method
public boolean equals(Point anotherPoint) {
if (x == anotherPoint.x && y == anotherPoint.y)
return true;
else
return false;
}
// lessThan method
public boolean lessThan(Point anotherPoint) {
if ((y < anotherPoint.y) || (y == anotherPoint.y && x < anotherPoint.x))
return true;
else
return false;
}
// slopeTo method
public double slopeTo(Point anotherPoint) {
double slope = 0;
if (anotherPoint.x - x != 0)
slope = (anotherPoint.y - y) / (anotherPoint.x - x);
return slope;
}
// compareSlopes method
int compareSlopes(Point anotherPoint) {
Point origin = new Point(0, 0);
if (slopeTo(origin) < anotherPoint.slopeTo(origin))
return -1;
else if (slopeTo(origin) == anotherPoint.slopeTo(origin))
return 0;
else // if(slopeTo(origin) > anotherPoint.slopeTo(origin))
return 1;
}
// toString method
public String toString() {
return "(" + x + ", " + y + ")";
}
}
PointArray.java
public class PointArray {
// data field
private Point points[];
// constructor 1
public PointArray(double arr[]) {
points = new Point[arr.length / 2];
int i = 0;
int j = 0;
while (i < arr.length) {
points[j] = new Point(arr[i], arr[i + 1]);
i += 2;
j++;
}
}
// constructor 2
public PointArray(FileInputStream fis) throws IOException {
byte b1[] = new byte[fis.available()];
fis.read(b1, 0, b1.length);
char ch = (char) b1[0];
int size = Integer.parseInt("" + ch);
String lines[] = new String[size];
for (int i = 0; i < lines.length; i++)
lines[i] = "";
int k = 0;
int count = 0;
for (int i = 3; i < b1.length; i++) {
if (b1[i] == ' ') {
count++;
if (count == 1)
k++;
} else {
lines[k] += (char) b1[i];
count = 0;
}
}
points = new Point[size];
int j = 0;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String tokens[] = line.split(" ");
double x_coord = Double.parseDouble(tokens[0]);
double y_coord = Double.parseDouble(tokens[1]);
points[j] = new Point(x_coord, y_coord);
j++;
}
fis.close();
}
// contains3collinear method
public boolean contains3collinear() {
if (points[0].slopeTo(points[1]) == points[1].slopeTo(points[2]))
return true;
else
return false;
}
// sort method
public void sort() {
for (int i = 0; i < points.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < points.length; j++) {
if (points[j].lessThan(points[minIndex]))
minIndex = j;
}
if (minIndex != i) {
Point temp = points[i];
points[i] = points[minIndex];
points[minIndex] = temp;
}
}
}
// equals method
public boolean equals(PointArray anotherPointArray) {
if (points.length != anotherPointArray.points.length)
return false;
boolean founds[] = new boolean[points.length];
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < anotherPointArray.points.length; j++) {
if (points[i].equals(anotherPointArray.points[j]) && founds[j] == false) {
founds[j] = true;
break;
}
}
}
for (int i = 0; i < founds.length; i++) {
if (founds[i] == false)
return false;
}
return true;
}
// toString method
public String toString() {
String result = "{";
for (int i = 0; i < points.length; i++) {
if (i != points.length - 1)
result += points[i] + ", ";
else
result += points[i];
}
result += "}";
return result;
}
}
PointArrayTester.java
public class PointArrayTester {
// main method
public static void main(String[] args) throws IOException {
FileInputStream file = new FileInputStream(new File("pointsFile.txt"));
double arr1[] = { 2.4, 3.5, 4.7, 1.0, 6.4, 11.01 };
double arr2[] = { 4.2, 5.3, 7.4, 0.1, 4.6, 1.11 };
double arr3[] = { 2.4, 3.5, 4.7, 1.0, 6.4, 11.01 };
PointArray pa1 = new PointArray(file);
PointArray pa2 = new PointArray(arr1);
PointArray pa3 = new PointArray(arr2);
PointArray pa4 = new PointArray(arr3);
System.out.println("Points Array #1: " + pa1);
System.out.println("Points Array #2: " + pa2);
System.out.println("Points Array #3: " + pa3);
System.out.println("Points Array #4: " + pa4);
System.out.println(" Points Array #1 is collinear.(T/F): " + pa1.contains3collinear());
System.out.println("Points Array #2 is collinear.(T/F): " + pa2.contains3collinear());
System.out.println("Points Array #3 is collinear.(T/F): " + pa3.contains3collinear());
System.out.println("Points Array #4 is collinear.(T/F): " + pa4.contains3collinear());
System.out.println(" Points Array #1 is equal to Points Array #3.(T/F): " + pa1.equals(pa3));
System.out.println("Points Array #2 is equal to Points Array #4.(T/F): " + pa2.equals(pa4));
pa1.sort();
pa2.sort();
pa3.sort();
pa4.sort();
System.out.println(" Sorted Points Array #1: " + pa1);
System.out.println("Sorted Points Array #2: " + pa2);
System.out.println("Sorted Points Array #3: " + pa3);
System.out.println("Sorted Points Array #4: " + pa4);
file.close();
}
}
pointsFile.txt
5
2 7
3 5
11 17
23 19
150 1
Output
Points Array #1: {(2.0, 7.0), (3.0, 5.0), (11.0, 17.0), (23.0, 19.0), (150.0, 1.0)}
Points Array #2: {(2.4, 3.5), (4.7, 1.0), (6.4, 11.01)}
Points Array #3: {(4.2, 5.3), (7.4, 0.1), (4.6, 1.11)}
Points Array #4: {(2.4, 3.5), (4.7, 1.0), (6.4, 11.01)}
Points Array #1 is collinear.(T/F): false
Points Array #2 is collinear.(T/F): false
Points Array #3 is collinear.(T/F): false
Points Array #4 is collinear.(T/F): false
Points Array #1 is equal to Points Array #3.(T/F): false
Points Array #2 is equal to Points Array #4.(T/F): true
Sorted Points Array #1: {(150.0, 1.0), (3.0, 5.0), (2.0, 7.0), (11.0, 17.0), (23.0, 19.0)}
Sorted Points Array #2: {(4.7, 1.0), (2.4, 3.5), (6.4, 11.01)}
Sorted Points Array #3: {(7.4, 0.1), (4.6, 1.11), (4.2, 5.3)}
Sorted Points Array #4: {(4.7, 1.0), (2.4, 3.5), (6.4, 11.01)}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.