Skip to content
Snippets Groups Projects
Commit 22ebdf45 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
Branches master
No related tags found
No related merge requests found
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.unl.cse.soft160.sort_four_pairs</groupId>
<artifactId>sort_four_pairs</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sort_four_pairs</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package edu.unl.cse.soft160.sort_four_pairs;
public class SortFourPairs {
public static double readArgument(int position, String... arguments) {
return Double.parseDouble(arguments[position]);
}
public static String formatPair(double x, double y) {
return "(" + x + ", " + y + ")";
}
public static int getPositionOfMinimum(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3) {
int result = 0;
double xmin = x0;
double ymin = y0;
if (x1 < xmin || x1 == xmin && y1 < ymin) {
++result;
xmin = x1;
ymin = y1;
}
if (x2 < xmin || x2 == xmin && y2 < ymin) {
++result;
xmin = x2;
ymin = y2;
}
if (x3 < xmin || x3 == xmin && y3 < ymin) {
++result;
xmin = x3;
ymin = y3;
}
return result;
}
public static void main(String... arguments) {
if (arguments.length != 8) {
System.err.println("Sorts four points, by their x coordinates, breaking ties using y coordinates.");
System.err.println("Usage: [Program] [X0] [Y0] [X1] [Y1] [X2] [Y2] [X3] [Y3]");
System.exit(1);
}
double x0 = readArgument(0, arguments);
double y0 = readArgument(1, arguments);
double x1 = readArgument(2, arguments);
double y1 = readArgument(3, arguments);
double x2 = readArgument(4, arguments);
double y2 = readArgument(5, arguments);
double x3 = readArgument(6, arguments);
double y3 = readArgument(7, arguments);
double x4 = x0;
double y4 = y0;
switch (getPositionOfMinimum(x0, y0, x1, y1, x2, y2, x3, y3)) {
case 3:
x4 = x2;
x2 = x3;
x3 = x4;
y4 = y2;
y2 = y3;
y3 = y4;
case 2:
x4 = x1;
x1 = x2;
x2 = x4;
y4 = y1;
y1 = y2;
y2 = y4;
case 1:
x4 = x0;
x0 = x1;
x1 = x4;
y4 = y0;
y0 = y1;
y1 = y4;
}
switch (getPositionOfMinimum(Double.MAX_VALUE, Double.MAX_VALUE, x1, y1, x2, y2, x3, y3)) {
case 3:
x4 = x2;
x2 = x3;
x3 = x4;
y4 = y2;
y2 = y3;
y3 = y4;
case 2:
x4 = x1;
x1 = x2;
x2 = x4;
y4 = y1;
y1 = y2;
y2 = y4;
}
switch (getPositionOfMinimum(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE, x2, y2, x3, y3)) {
case 3:
x4 = x2;
x2 = x3;
x3 = x4;
y4 = y2;
y2 = y3;
y3 = y4;
}
System.out.println(formatPair(x1, y1));
System.out.println(formatPair(x2, y2));
System.out.println(formatPair(x3, y3));
System.out.println(formatPair(x4, y4));
}
}
package edu.unl.cse.soft160.sort_four_pairs;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.io.ByteArrayOutputStream;
public class SortFourPairsTest extends TestCase {
public SortFourPairsTest(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(SortFourPairsTest.class);
}
protected static String assemble(String... lines) {
return String.join("\n", lines) + "\n";
}
protected static String runMain(String... arguments) {
InputStream in = System.in;
PrintStream out = System.out;
System.setIn(new ByteArrayInputStream("".getBytes()));
ByteArrayOutputStream collector = new ByteArrayOutputStream();
System.setOut(new PrintStream(collector));
SortFourPairs.main(arguments);
System.setIn(in);
System.setOut(out);
return collector.toString();
}
public void testZeros() {
assertEquals(assemble("(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)"), runMain("0", "0", "0", "0", "0", "0", "0", "0"));
}
public void testAlreadySorted() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "1", "1", "2", "2", "3", "3"));
}
public void testReversed() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "2", "2", "1", "1", "0", "0"));
}
public void testJumbled() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "2", "2", "0", "0", "3", "3"));
}
public void testTieBreaking() {
assertEquals(assemble("(0.0, 0.0)", "(0.0, 1.0)", "(1.0, 2.0)", "(1.0, 3.0)"), runMain("1", "3", "0", "1", "0", "0", "1", "2"));
}
public void testMultipleTieBreaking() {
assertEquals(assemble("(0.0, 0.0)", "(0.0, 1.0)", "(0.0, 3.0)", "(1.0, 2.0)"), runMain("0", "1", "1", "2", "0", "0", "0", "3"));
}
public void testDuplicates() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(1.0, 1.0)", "(1.0, 2.0)"), runMain("1", "1", "1", "2", "1", "1", "0", "0"));
}
public void testPermutation0() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "1", "1", "2", "2", "3", "3"));
}
public void testPermutation1() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "1", "1", "3", "3", "2", "2"));
}
public void testPermutation2() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "3", "3", "1", "1", "2", "2"));
}
public void testPermutation3() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "0", "0", "1", "1", "2", "2"));
}
public void testPermutation4() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "0", "0", "2", "2", "1", "1"));
}
public void testPermutation5() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "3", "3", "2", "2", "1", "1"));
}
public void testPermutation6() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "2", "2", "3", "3", "1", "1"));
}
public void testPermutation7() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("0", "0", "2", "2", "1", "1", "3", "3"));
}
public void testPermutation8() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "0", "0", "1", "1", "3", "3"));
}
public void testPermutation9() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "0", "0", "3", "3", "1", "1"));
}
public void testPermutation10() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "3", "3", "0", "0", "1", "1"));
}
public void testPermutation11() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "2", "2", "0", "0", "1", "1"));
}
public void testPermutation12() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "2", "2", "1", "1", "0", "0"));
}
public void testPermutation13() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "3", "3", "1", "1", "0", "0"));
}
public void testPermutation14() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "1", "1", "3", "3", "0", "0"));
}
public void testPermutation15() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("2", "2", "1", "1", "0", "0", "3", "3"));
}
public void testPermutation16() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "2", "2", "0", "0", "3", "3"));
}
public void testPermutation17() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "2", "2", "3", "3", "0", "0"));
}
public void testPermutation18() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "3", "3", "2", "2", "0", "0"));
}
public void testPermutation19() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "1", "1", "2", "2", "0", "0"));
}
public void testPermutation20() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("3", "3", "1", "1", "0", "0", "2", "2"));
}
public void testPermutation21() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "3", "3", "0", "0", "2", "2"));
}
public void testPermutation22() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "0", "0", "3", "3", "2", "2"));
}
public void testPermutation23() {
assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)", "(3.0, 3.0)"), runMain("1", "1", "0", "0", "2", "2", "3", "3"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment