summaryrefslogtreecommitdiff
path: root/labs/lab01_pair
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
commit89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch)
treecdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab01_pair
downloadcoe0445-master.tar.gz
coe0445-master.tar.bz2
coe0445-master.zip
Inital commitHEADmaster
Diffstat (limited to 'labs/lab01_pair')
-rw-r--r--labs/lab01_pair/.Pair.java.swpbin0 -> 12288 bytes
-rw-r--r--labs/lab01_pair/GraphComponent.java139
-rw-r--r--labs/lab01_pair/Pair.java83
-rw-r--r--labs/lab01_pair/PairInterface.java55
-rw-r--r--labs/lab01_pair/PairTester.java286
-rw-r--r--labs/lab01_pair/ParabolaFrame.java36
-rw-r--r--labs/lab01_pair/lab01_pair.pdfbin0 -> 133803 bytes
7 files changed, 599 insertions, 0 deletions
diff --git a/labs/lab01_pair/.Pair.java.swp b/labs/lab01_pair/.Pair.java.swp
new file mode 100644
index 0000000..6cd3b15
--- /dev/null
+++ b/labs/lab01_pair/.Pair.java.swp
Binary files differ
diff --git a/labs/lab01_pair/GraphComponent.java b/labs/lab01_pair/GraphComponent.java
new file mode 100644
index 0000000..089b76a
--- /dev/null
+++ b/labs/lab01_pair/GraphComponent.java
@@ -0,0 +1,139 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.geom.Line2D;
+import java.util.ArrayList;
+import javax.swing.JComponent;
+
+@SuppressWarnings("serial")
+public class GraphComponent extends JComponent
+{
+ private int leftMargin = 10;
+ private int rightMargin = 10;
+ private int topMargin = 10;
+ private int bottomMargin = 10;
+ private double largest;
+ private double largestX;
+ private double smallestX;
+ private double largestY;
+ private double smallestY;
+ private int numData;
+ private int width;
+ private int height;
+ private ArrayList<PairInterface<Double,Double>> data;
+
+ public GraphComponent(ArrayList<PairInterface<Double,Double>> aData)
+ {
+ data = aData;
+ numData = data.size();
+ PairInterface<Double,Double> tempData = data.get(0);
+
+ largestX = tempData.fst();
+ smallestX = largestX;
+ largestY = tempData.snd();
+ smallestY = largestY;
+
+ for(int i = 1; i < numData; i++)
+ {
+ tempData = data.get(i);
+
+ if(largestX < tempData.fst())
+ {
+ largestX = tempData.fst();
+ }
+
+ if(smallestX > tempData.fst())
+ {
+ smallestX = tempData.fst();
+ }
+
+ if(largestY < tempData.snd())
+ {
+ largestY = tempData.snd();
+ }
+
+ if(smallestY > tempData.snd())
+ {
+ smallestY = tempData.snd();
+ }
+ }
+
+ if(Math.abs(largestX) < Math.abs(smallestX))
+ {
+ largestX = Math.abs(smallestX);
+ }
+ else
+ {
+ largestX = Math.abs(largestX);
+ }
+
+ if(Math.abs(largestY) < Math.abs(smallestY))
+ {
+ largestY = Math.abs(smallestY);
+ }
+ else
+ {
+ largestY = Math.abs(largestY);
+ }
+
+ if(largestX > largestY)
+ {
+ largest = largestX;
+ }
+ else
+ {
+ largest = largestY;
+ }
+ }
+
+ public void paintComponent(Graphics g)
+ {
+ Graphics2D g2 = (Graphics2D) g;
+
+ width = this.getWidth();
+ height = this.getHeight();
+
+
+
+ Line2D.Double line = new Line2D.Double(0,0,0,0);
+
+ // Draw Axis;
+
+ g2.setColor(Color.BLACK);
+
+ line.setLine(getX(0),getY(0),getX(-largest),getY(0));
+ g2.draw(line);
+ line.setLine(getX(0),getY(0),getX(0),getY(largest));
+ g2.draw(line);
+ line.setLine(getX(0),getY(0),getX(largest),getY(0));
+ g2.draw(line);
+ line.setLine(getX(0),getY(0),getX(0),getY(-largest));
+ g2.draw(line);
+
+ // Draw frequency the graph
+
+ g2.setColor(Color.GREEN);
+
+ for(int i = 0; i < numData - 1; i++)
+ {
+ line.setLine(getX(data.get(i).fst()), getY(data.get(i).snd()), getX(data.get(i + 1).fst()), getY(data.get(i + 1).snd()));
+ g2.draw(line);
+ }
+ }
+
+ public int getX(double aValue)
+ {
+ int numberOfPixels = width - (leftMargin + rightMargin);
+ double range = largest + largest;
+ double deltaWidth = numberOfPixels / range;
+ return (int) ((aValue + largest) * deltaWidth) + leftMargin;
+ }
+
+ public int getY(double aValue)
+ {
+ int numberOfPixels = height - (topMargin + bottomMargin);
+ double range = largest + largest;
+ double deltaWidth = numberOfPixels / range;
+ return (int) ((-aValue + largest) * deltaWidth) + topMargin;
+ }
+}
diff --git a/labs/lab01_pair/Pair.java b/labs/lab01_pair/Pair.java
new file mode 100644
index 0000000..ef49c76
--- /dev/null
+++ b/labs/lab01_pair/Pair.java
@@ -0,0 +1,83 @@
+
+public class Pair<T1,T2> implements PairInterface<T1,T2>
+{
+ // TO DO: Instance Variables
+ T1 one;
+ T2 two;
+
+ public Pair(T1 aFirst, T2 aSecond)
+ {
+ one = aFirst;
+ two = aSecond;
+ }
+
+ /**
+ * Gets the first element of this pair.
+ * @return the first element of this pair.
+ */
+ public T1 fst()
+ {
+ return one;
+ }
+
+ /**
+ * Gets the second element of this pair.
+ * @return the second element of this pair.
+ */
+ public T2 snd()
+ {
+ return two;
+ }
+
+ /**
+ * Sets the first element to aFirst.
+ * @param aFirst the new first element
+ */
+ public void setFst(T1 aFirst)
+ {
+ one = aFirst;
+ }
+
+ /**
+ * Sets the second element to aSecond.
+ * @param aSecond the new second element
+ */
+ public void setSnd(T2 aSecond)
+ {
+ two = aSecond;
+ }
+
+ /**
+ * Checks whether two pairs are equal. Note that the pair
+ * (a,b) is equal to the pair (x,y) if and only if a is
+ * equal to x and b is equal to y.
+ * @return true if this pair is equal to aPair. Otherwise
+ * return false.
+ */
+ public boolean equals(Object otherObject)
+ {
+ if(otherObject == null)
+ {
+ return false;
+ }
+
+ if(getClass() != otherObject.getClass())
+ {
+ return false;
+ }
+ Pair pairObject = (Pair) otherObject;
+ return one.equals(pairObject.fst()) && two.equals(pairObject.snd());
+ }
+
+ /**
+ * Generates a string representing this pair. Note that
+ * the String representing the pair (x,y) is "(x,y)". There
+ * is no whitespace unless x or y or both contain whitespace
+ * themselves.
+ * @return a string representing this pair.
+ */
+ public String toString()
+ {
+ return "(" + one.toString() + "," + two.toString() + ")";
+ }
+}
diff --git a/labs/lab01_pair/PairInterface.java b/labs/lab01_pair/PairInterface.java
new file mode 100644
index 0000000..b3d78fa
--- /dev/null
+++ b/labs/lab01_pair/PairInterface.java
@@ -0,0 +1,55 @@
+
+public interface PairInterface<T1,T2>
+{
+ /**
+ * Gets the first element of this pair.
+ * @return the first element of this pair.
+ */
+ public T1 fst();
+
+ /**
+ * Gets the second element of this pair.
+ * @return the second element of this pair.
+ */
+ public T2 snd();
+
+ /**
+ * Sets the first element to aFirst.
+ * @param aFirst the new first element
+ */
+ public void setFst(T1 aFirst);
+
+ /**
+ * Sets the second element to aSecond.
+ * @param aSecond the new second element
+ */
+ public void setSnd(T2 aSecond);
+
+ /**
+ * Checks whether two pairs are equal. Note that the pair
+ * (a,b) is equal to the pair (x,y) if and only if a is
+ * equal to x and b is equal to y.
+ *
+ * Note that if you forget to implement this method, your
+ * compiler will not complain since your class inherits this
+ * method from the class Object.
+ *
+ * @return true if this pair is equal to aPair. Otherwise
+ * return false.
+ */
+ public boolean equals(Object otherObject);
+
+ /**
+ * Generates a string representing this pair. Note that
+ * the String representing the pair (x,y) is "(x,y)". There
+ * is no whitespace unless x or y or both contain whitespace
+ * themselves.
+ *
+ * Note that if you forget to implement this method, your
+ * compiler will not complain since your class inherits this
+ * method from the class Object.
+ *
+ * @return a string representing this pair.
+ */
+ public String toString();
+}
diff --git a/labs/lab01_pair/PairTester.java b/labs/lab01_pair/PairTester.java
new file mode 100644
index 0000000..a1d3d50
--- /dev/null
+++ b/labs/lab01_pair/PairTester.java
@@ -0,0 +1,286 @@
+import java.util.Random;
+
+public class PairTester
+{
+ public static void main(String[] args)
+ {
+ int point = 0;
+ Random rand = new Random();
+
+ // Test a pair of string and integer
+
+ System.out.print("Testing Pair<String,Integer>: ");
+
+ int l1 = rand.nextInt(10) + 10;
+ String s1 = randomString(l1);
+ PairInterface<String,Integer> p1 = new Pair<String,Integer>(s1,l1);
+
+ if(checkFromToString("(" + s1 + "," + l1 + ")", p1.toString()))
+ {
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Test a pair of integer and string
+
+ System.out.print("Testing Pair<Integer,String>: ");
+ int l2 = rand.nextInt(10) + 10;
+ String s2 = randomString(l2);
+ PairInterface<Integer,String> p2 = new Pair<Integer,String>(l2,s2);
+ if(checkFromToString("(" + l2 + "," + s2 + ")", p2.toString()))
+ {
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Test a pair of String and pair
+
+ System.out.print("Testing Pair<String,Pair<Integer,String>>: ");
+ int l3_1 = rand.nextInt(10) + 10;
+ int l3_2 = rand.nextInt(10) + 10;
+ String s3_1 = randomString(l3_1);
+ String s3_2 = randomString(l3_2);
+ PairInterface<Integer,String> p3_1 = new Pair<Integer,String>(l3_1 + l3_2, s3_1);
+ PairInterface<String, PairInterface<Integer,String>> p3_2 = new Pair<String, PairInterface<Integer,String>>(s3_2, p3_1);
+ if(checkFromToString("(" + s3_2 + ",(" + (l3_1 + l3_2) + "," + s3_1 + "))", p3_2.toString()))
+ {
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Test a pair of pair and Integer
+
+ System.out.print("Testing Pair<String,Pair<Integer,String>>: ");
+ int l4_1 = rand.nextInt(10) + 10;
+ int l4_2 = rand.nextInt(10) + 10;
+ String s4 = randomString(l4_1 + l4_2);
+ PairInterface<String,Integer> p4_1 = new Pair<String,Integer>(s4,l4_1);
+ Pair<Integer, PairInterface<String,Integer>> p4_2 = new Pair<Integer, PairInterface<String,Integer>>(l4_2, p4_1);
+ if(checkFromToString("(" + l4_2 + ",(" + s4 + "," + l4_1 + "))", p4_2.toString()))
+ {
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Test 5-tuple
+
+ System.out.print("Testing 5-tuple (e.g., (1,(2,(3,(4,5))))): ");
+ int[] l5 = new int[5];
+ for(int i = 0; i < 5; i++)
+ {
+ l5[i] = rand.nextInt(10) + 10;
+ }
+ String s5 = "(" + l5[0] + ",(" + l5[1] + ",(" + l5[2] + ",(" + l5[3] + "," + l5[4] + "))))";
+ PairInterface<Integer,Integer> p5_1 = new Pair<Integer,Integer>(l5[3],l5[4]);
+ PairInterface<Integer,PairInterface<Integer,Integer>> p5_2 = new Pair<Integer,PairInterface<Integer,Integer>>(l5[2],p5_1);
+ PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>> p5_3 =
+ new Pair<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>(l5[1],p5_2);
+ PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>> p5_4 =
+ new Pair<Integer,PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>>(l5[0],p5_3);
+ if(checkFromToString(s5, p5_4.toString()))
+ {
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ if(point != 5)
+ {
+ System.out.println("This test class will use variables generated in");
+ System.out.println("previous tests. Your class Pair fails one or more");
+ System.out.println("test(s). This test class will terminated. Fix");
+ System.out.println("your class Pair first and run the test class again.");
+ System.out.println("Your current point is " + point + ".\n");
+ return;
+ }
+
+ // Testing the method equals()
+
+ System.out.println("Testing the method equals()");
+
+ PairInterface<String,Integer> p6 = new Pair<String,Integer>(s1,l1);
+
+ System.out.print("Test whether " + p1 + " is equals to " + p6 + ": ");
+ if(!p1.equals(p6))
+ {
+ System.out.println("FAIL");
+ System.out.println("The pair " + p1 + " and the pair " + p6 + " are equal.");
+ System.out.println("But your method equals() returned false.");
+ System.out.println("Fix your method equals() first and run the test class again.");
+ System.out.println("Your current point is " + point + ".\n");
+ return;
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ PairInterface<String,Integer> p6_2 = new Pair<String,Integer>(s1.substring(1,s1.length()), l1);
+ System.out.print("Test whether " + p1 + " is equals to " + p6_2 + ": ");
+ if(p1.equals(p6_2))
+ {
+ System.out.println("FAIL");
+ System.out.println("The pair " + p1 + " and the pair " + p6_2 + " are not equal.");
+ System.out.println("But your method equals() returned true.");
+ System.out.println("Fix your method equals() first and run the test class again.");
+ System.out.println("Your current point is " + point + ".\n");
+ return;
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ PairInterface<String,Integer> p6_3 = new Pair<String,Integer>(s1, l1 + 1);
+ System.out.print("Test whether " + p1 + " is equals to " + p6_3 + ": ");
+ if(p1.equals(p6_3))
+ {
+ System.out.println("FAIL");
+ System.out.println("The pair " + p1 + " and the pair " + p6_3 + " are not equal.");
+ System.out.println("But your method equals() returned true.");
+ System.out.println("Fix your method equals() first and run the test class again.");
+ System.out.println("Your current point is " + point + ".\n");
+ return;
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ point++;
+ System.out.println("Your current point is " + point + ".\n");
+
+
+
+ System.out.print("Testing the method equals() (x,(y,z)) = (x,(y,z)): ");
+ PairInterface<Integer,String> p7_1 = new Pair<Integer,String>(l3_1 + l3_2, s3_1);
+ Pair<String, PairInterface<Integer,String>> p7_2 = new Pair<String, PairInterface<Integer,String>>(s3_2, p7_1);
+ if(!p3_2.equals(p7_2))
+ {
+ System.out.println("FAIL");
+ System.out.println("The pair " + p3_2 + " and the pair " + p7_2 + " are equal.");
+ System.out.println("But your method equals() returned false.");
+ System.out.println("Fix your method equals() first and run the test class again.");
+ System.out.println("Your current point is " + point + ".\n");
+ return;
+ }
+ else
+ {
+ System.out.println("PASS");
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Testing the method fst()
+
+ System.out.print("Testing the method fst(): ");
+ if(!s1.equals(p1.fst()))
+ {
+ System.out.println("FAIL");
+ System.out.println("First element of the pair " + p1 + " is " + s1 + ".");
+ System.out.println("But the method fst() of your class returned " + p1.fst() + ".\n");
+ }
+ else
+ {
+ System.out.println("PASS");
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Testing the method snd()
+
+ System.out.print("Testing the method snd(): ");
+ if(!s2.equals(p2.snd()))
+ {
+ System.out.println("FAIL");
+ System.out.println("Second element of the pair " + p2 + " is " + s2 + ".");
+ System.out.println("But the method snd() of your class returned " + p2.fst() + ".\n");
+ }
+ else
+ {
+ System.out.println("PASS");
+ point++;
+ }
+ System.out.println("Your current point is " + point + ".\n");
+
+ // Test setFst() and setSnd()
+
+ System.out.print("Testing the methods setFst() and setSnd(): ");
+ PairInterface<String,Integer> p10_1 = new Pair<String,Integer>("Hello",123);
+ PairInterface<String,Integer> p10_2 = new Pair<String,Integer>("How are you?",54321);
+
+ p10_1.setFst("How are you?");
+ p10_1.setSnd(54321);
+
+ if(!p10_1.equals(p10_2))
+ {
+ System.out.println("FAIL");
+ System.out.println("The pair p10_1 was constructed using the following statement:");
+ System.out.println(" PairInterface<String,Integer> p10_1 = new Pair<String,Integer>(\"Hello\",123);");
+ System.out.println("Then the folloiwng statements are executed:");
+ System.out.println(" p10_1.setFst(\"How are you?\");");
+ System.out.println(" p10_1.setSnd(54321);");
+ System.out.println("After above statements, the pair p10_1 should be " + p10_2 + ".");
+ System.out.println("But the method toString() of our pair p10_1 is returns " + p10_1 + ".\n");
+ }
+ else
+ {
+ System.out.println("PASS");
+ point++;
+ }
+ System.out.println("Your final point is " + point + ".\n");
+
+ if(point == 10)
+ {
+ System.out.println("Contratulation! Your class Pair works perfectly (I guess).");
+ System.out.println("You can run ParabolaFrame to see how Pair can be used in a program.");
+ }
+ else
+ {
+ System.out.println("There is one or more errors in your class.");
+ System.out.println("Fix your bugs to get more points.");
+ }
+ }
+
+ public static boolean checkFromToString(String s1, String s2)
+ {
+ if(s1.equals(s2))
+ {
+ System.out.println("PASS");
+ return true;
+ }
+ else
+ {
+ System.out.println("FAILED");
+ System.out.println("The method toString() should to return: " + s1);
+ System.out.println("But your method toString() returns : " + s2);
+ return false;
+ }
+ }
+
+ public static String randomString(int length)
+ {
+ String result = "";
+
+ for(int i = 0; i < length; i++)
+ {
+ result = result + randomChar();
+ }
+
+ return result;
+ }
+
+ public static char randomChar()
+ {
+ Random rand = new Random();
+ int modifier = rand.nextInt(26);
+
+ if(rand.nextBoolean())
+ {
+ return (char) ('a' + modifier);
+ }
+ else
+ {
+ return (char) ('A' + modifier);
+ }
+ }
+}
diff --git a/labs/lab01_pair/ParabolaFrame.java b/labs/lab01_pair/ParabolaFrame.java
new file mode 100644
index 0000000..7d77c72
--- /dev/null
+++ b/labs/lab01_pair/ParabolaFrame.java
@@ -0,0 +1,36 @@
+import java.awt.BorderLayout;
+import java.util.ArrayList;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.SwingConstants;
+
+public class ParabolaFrame
+{
+ public static void main(String[] args)
+ {
+ // Create an empty list of points (x,y) using Pair<Double,Double>
+
+ ArrayList<PairInterface<Double,Double>> data = new ArrayList<PairInterface<Double,Double>>();
+
+ // Generate (x,y) points: y = (8/25)x^2 - 3 from -5.0 to 5.0 at
+ // at every 0.01.
+
+ for(double i = -5.0; i <= 5.0; i = i + 0.01)
+ {
+ data.add(new Pair<Double,Double>(i,(((i*i)*8)/25)-3));
+ }
+
+ // Show the Parabola graph
+
+ GraphComponent gc = new GraphComponent(data);
+ JLabel label = new JLabel("Good Job!!!");
+ label.setHorizontalAlignment(SwingConstants.CENTER);
+ JFrame frame = new JFrame();
+ frame.setTitle("Example of Using Pair");
+ frame.setSize(500,500);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.add(gc);
+ frame.add(label, BorderLayout.SOUTH);
+ frame.setVisible(true);
+ }
+}
diff --git a/labs/lab01_pair/lab01_pair.pdf b/labs/lab01_pair/lab01_pair.pdf
new file mode 100644
index 0000000..8cdc686
--- /dev/null
+++ b/labs/lab01_pair/lab01_pair.pdf
Binary files differ