This week you will implement an algorithm to reduce the number of dots in the picture so that more reasonable dot to dot challenges can be created.
Your program must:
Note: If the user loads a picture with 2,000 dots, and enters 100 for the desired number of dots, and subsequently enters 1,000 for the desired number of dots, your program must show a picture with 1,000 dots, not 100.
You must add the following two methods to the
private static double distance(Dot a, Dot b)that returns the distance between the two dots.
criticalValue(Dot previous, Dot next)that returns the critical value of the dot as defined below.
You must remove all constructors from the
Picture class and implement the following constructors:
Picture(List<Dot> emptyList)— Constructor that uses the list emptyList passed to it to store the dots for this picture.
Picture(Picture original, List<Dot> emptyList)— Constructor that copies the dots from original into emptyList and uses it to store the dots for this picture.
These constructors provide flexibility in our
Picture class because we can now specify the specific implementation of the
List used (e.g.,
LinkedList, or even our own
List implementation) by passing the appropriate list to the constructor. In fact, the
Picture class is no longer dependent on any concrete list implementation.
You must also implement the following methods in the
void save(Path path)— saves the picture to .dot path that is compatible with the format described in lab 1.
void removeDots(int numberDesired)— removes all but the numberDesired most critical dots.
numberDesireddots, the method returns without changing the picture.
numberDesired < 3, an
Your implementation of the
removeDots() method must be consistent with this flowchart:
The critical value of a dot depends on the relative proximity of it and its immediate neighbors. Consider three dots, labeled 1, 2, and 3, on a straight line. If dot 2 is removed, the connect the dots version of the line from 1 to 3 will look exactly the same. Therefore, we can conclude that the dot is not critical. If dot 2 is significantly far from the line connecting dot 1 and 3, then error would be introduced if dot 2 is eliminated, as shown in Figure 3.
The critical value of dot 2 is calculated as the sum of the distances from dot 2 to dot 1 and from dot 2 to dot 3 minus the distance from dot 1 to dot 3, i.e., ��2=�12+�23−�13
where ��� is the critical value for dot � and ��� are the distances illustrated below.
Note that in the case of a straight line between dots 1 and 3, ��2=0
Your program should assume that the first and the last dots are connected so they should be treated as neighbors when calculating their critical values.
There are a number of situations that could cause your program to throw an exception. For example, if the file is not found, cannot be opened, or contains incorrectly formatted data, it is likely that an exception will be thrown. In these cases, the program should display an useful message in an
Ambitious students may wish to: