Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have a Java assignment on Collinear Points and am struggling greatly. Please help. And here is the starter code... Point.Java: import java.util.Comparator; /**

Hi, I have a Java assignment on Collinear Points and am struggling greatly. Please help.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

And here is the starter code...

Point.Java:

import java.util.Comparator;

/** * Point.java. * Models a two dimensional point as a Cartesian coordinate (x, y) * in Quadrant I (x >= 0 and y >= 0). This class is designed to be * immutable. * */ public final class Point implements Comparable {

/** Compare two points with respect to the slope each makes with this point. */ public final Comparator slopeOrder = new ComparePointsBySlope();

/** x,y coordinates of this point. */ private final int x; private final int y;

/** * Create a point from the given x and y coordinates. If either x or y is * negative, throw an IllegalArgumentException. */ public Point(int x, int y) { this.x = 0; this.y = 0; }

/** * Return a string representation of this point. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ @Override public String toString() { return "(" + x + ", " + y + ")"; }

/** * Indicates whether some object is equal to this Point. A Point (x1, y1) is * equal to this Point (x0, y0) if and only if x0 == x1 and y0 == y1. All * six properties of the equals method specified in the Object class are * met. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (!(obj instanceof Point)) { return false; } Point that = (Point) obj; return (this.x == that.x) && (this.y == that.y); }

/** * Compares this point with the specified point for order. Returns a * negative integer, zero, or a positive integer if this point is less * than, equal to, or greater than the specified point. Points are ordered * first by y value and then by x value. All three properties of compareTo * as specified in the Comparable interface are met, and this * implementation is consistent with equals. */ @Override public int compareTo(Point that) { return -99; }

/** * Computes the slope of the line segment between this point (x0, y0) and * the specified point (x1, y1). Slope is computed as (y1 - y0) / (x1 - x0), * so the direction of the slope is from this point to the specified point. * The slope of a horizontal line segment is positive zero; the slope of a * vertical line segment is positive infinity; the slope of a degenerate * line segment (where this point and the specified point are the same) is * negative infinity. */ public double slopeTo(Point that) { return ((double) 1) / ((double) 2); } /** * Defines a total order for Points based on the slope that two specified points * make with this point. */ private class ComparePointsBySlope implements Comparator {

/** * Compares two specified points p1 and p2 for order. Returns a negative * integer, zero, or a positive integer if p1 is less than, equal to, or * greater than p2. All three properties of the compare method as * specified in the Comparator interface are met. */ @Override public int compare(Point p1, Point p2) { return -99; } } }

Line.Java:

import java.util.Collection; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;

/** * Line.java * Models a line segment as a sorted set of points. * */ public class Line implements Comparable, Iterable { SortedSet line; /** * Creates a new line containing no points. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ public Line() { line = new TreeSet(); } /** * Creates a new line with containing all distinct collinear points in the * Collection c. */ public Line(Collection c) {

} /** * Adds the point p to this line if p is collinear with all points already * in the line and p itself is not already in the line. Returns true if this * line is changed as a result, false otherwise. */ public boolean add(Point p) { return false; } /** * Returns the first (minimum) point in this line or null if this line * contains no points. */ public Point first() { return null; } /** * Returns the last (maximum) point in this line or null if this line * contains no points. */ public Point last() { return null; } /** * Returns the number of points in this line. */ public int length() { return -99; }

/** * Compares this line with the specified line for order. Returns a negative * integer, zero, or a positive integer if this line is less than, equal to, * or greater than the specified line. Lines are ordered first by their * first point then by their last point. An empty line is less than any * non-empty line, and all empty lines are equal. All three properties of * compareTo as specified in the Comparable interface are met, and this * implementation is consistent with equals. */ @Override public int compareTo(Line that) { return -99; }

/** * Provide an iterator over all the points in this line. The order in which * points are returned must be ascending natural order. */ @Override public Iterator iterator() { return null; } /** * Return true if this line's first and last points are equal to the * parameter's first and last points. Empty lines are equal to each other * but are not equal to any non-empty line. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (!(obj instanceof Line)) { return false; } Line that = (Line) obj; if ((this.length() == 0) && (that.length() == 0)) { return true; } if ((this.length() == 0) && (that.length() != 0)) { return false; } if ((this.length() != 0) && (that.length() == 0)) { return false; } return (this.first().equals(that.first())) && (this.last().equals(that.last())); } /** * Return a string representation of this line. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ @Override public String toString() { if (length() == 0) { return ""; } StringBuilder s = new StringBuilder(); for (Point p : line) { s.append(p + " -> "); } s = s.delete(s.length() - 4, s.length()); return s.toString(); } }

Extractor.Java:

import java.io.File; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet;

/** * Extractor.java. Implements feature extraction for collinear points in * two dimensional data. * */ public class Extractor { /** raw data: all (x,y) points from source data. */ private Point[] points; /** lines identified from raw data. */ private SortedSet lines; /** * Builds an extractor based on the points in the file named by filename. */ public Extractor(String filename) {

} /** * Builds an extractor based on the points in the Collection named by pcoll. * * THIS METHOD IS PROVIDED FOR YOU AND MUST NOT BE CHANGED. */ public Extractor(Collection pcoll) { points = pcoll.toArray(new Point[]{}); } /** * Returns a sorted set of all line segments of exactly four collinear * points. Uses a brute-force combinatorial strategy. Returns an empty set * if there are no qualifying line segments. */ public SortedSet getLinesBrute() { lines = new TreeSet(); return lines; } /** * Returns a sorted set of all line segments of at least four collinear * points. The line segments are maximal; that is, no sub-segments are * identified separately. A sort-and-scan strategy is used. Returns an empty * set if there are no qualifying line segments. */ public SortedSet getLinesFast() { lines = new TreeSet(); return lines; } }

Any help would be very much appreciated. Seriously thank you for your time.

Problem Overview This assignment will explore an example feature ertraction problem. Feature extraction is a subproblem of pattern recognition and is also used in areas such as statistical analysis, computer vision, and image processing. For example, an image processing problem may use a feature extraction algorithm to identify particular shapes or regions in a digitized image. In this assignment, we're going to focus on a very simple feature extraction problem: Given a set of points in two-dimensional space, identify every subset of four or more points that are collinear. For example, given the set of points depicted in Figure 1, your program would detect the three groups of collinear points as depicted by the line segments in Figure 2. 6 6 3 1 23 4 5 6 7 8 1 2 3 4 5 6 7 8 Figure 1: A set of 13 points. Figure 2: Three collinear groups identified. As always, we want our solution to be useful at scale. For example, Figure 3 plots ~100,000 points and Figure 4 shows the 34 collinear groups identified by blue line segments. Each collinear group in Figure 4 is composed of far more than four points; four is just the minimum number of points to qualify for the collinear pattern that we're looking for. In the general problem statement we will refer to line segments instead of segment must contain at least four points. groups, where each line Problem Statement: Given a set of N distinct points in Quadrant I of the Cartesian plane, identify every line segment that connects a subset of four or more of the points. Each point will be specified as an (x, y) pair where z and y are non-negative int values. For example, the thirteen points in Figures 1 and 2 are: (1, 7), (2, 2), (2, 5), (3, 1), (4, 4), (5, 3), (5, 6), (6, 6), 7, 1), 7, 3), (7, 4), (7, 9), (8, 8) You must solve this problem in terms of the classes and methods described in the following sections. 60 Figure 3: A set of~100,000 points. Figure 4: 34 collinear groups identified The Point class You must create an imbl data type Point that represents a point in Quadrant I of the Cartesian plane. A shell of the Point class is provided for you, and you must meet the requirements specified in this document and the provided source code comments Some fields and methods have been completed for you and must not be changed. Some methods have incomplete bodies and you must complete them yourself. You may add any number of private methods that you like, but you may public method or constructor. not add any public method or constructor, nor may you change the signature of any You may not add any fields to this class. A few aspects of the Point class are described in more detail below The compareTo method. This method must compare points by y-coordinates and then, if needed, by x-coordinates. Thus, the invoking point (zo- yo) is less than the parameter point (,y) if and only if either yo

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions