Ejemplo n.º 1
0
  @Test
  public void test() {
    test = new PairTest<Object, Object>();

    Pair<String, String> twoStrings = new Pair<>("Ha", "Te");
    Pair<String, Double> stringDouble = new Pair<>("Ha", 3.4);
    Pair<Double, Integer> doubleInt = new Pair<>(3.0, 5);
    Pair<Integer, Integer> intInt = new Pair<>(6, 66);
    String result = twoStrings.toString();
    assertEquals("1:Ha\n2:Te", result); // tests 2 strings

    String resultTwo = stringDouble.toString();
    assertEquals("1:Ha\n2:3.4", resultTwo); // test string and a double

    String resultThree = doubleInt.toString();
    assertEquals("1:3.0\n2:5", resultThree);

    String resultFour = intInt.toString();
    assertEquals("1:6\n2:66", resultFour);

    /*sting string
    string double
    double int
    int string*/
  }
Ejemplo n.º 2
0
 public static void main(String[] args) {
   Pair<String> mm = new Pair<String>("1st", "2nd");
   System.out.println(mm.getFirst() + "," + mm.getSecond());
   Pair<Integer> i = new Pair<Integer>(3, 3);
   System.out.println(i.getFirst() + "," + i.getSecond());
   System.out.println(i.toString());
   System.out.println(mm.toString());
 }
Ejemplo n.º 3
0
  @Test
  public void testPair() {
    Pair<String, String> o1 = new Pair<String, String>();

    o1.setKey("key");
    o1.setValue("value");

    Pair<String, String> o2 = new Pair<String, String>("key", "value");

    Assert.assertEquals(o1, o2);
    Assert.assertEquals(o1.hashCode(), o2.hashCode());
    Assert.assertEquals(o1.toString(), o2.toString());
  }
Ejemplo n.º 4
0
 public static void main(String[] args) {
   int[] test = {9, 3, 6, 5, 5, 7, -1, 13, 14, -2, 12, 0};
   ArrayList<Pair> pairs = printPairSums(test, 12);
   for (Pair p : pairs) {
     System.out.println(p.toString());
   }
 }
Ejemplo n.º 5
0
 public int compare(Pair string1, Pair string2) {
   int ans = string1.toString().compareTo(string2.toString());
   if (ans < 0) {
     return -1;
   } else if (ans == 0) {
     if (string1.idFile < string2.idFile) {
       return -1;
     } else if (string1.idFile > string2.idFile) {
       return 1;
     } else {
       if (string1.idWord < string2.idWord) {
         return -1;
       } else if (string1.idWord > string2.idWord) {
         return 1;
       } else {
         return 0;
       }
     }
   } else {
     return 1;
   }
 }
Ejemplo n.º 6
0
  @Test
  public void testTwo() {
    testTwo = new PairTest<Object, Object>();

    // make two arraylists
    ArrayList<String> first = new ArrayList<String>();
    ArrayList<String> second = new ArrayList<String>();
    ArrayList<Integer> third = new ArrayList<Integer>();

    // initialize both with my values
    first.add("Hi");
    first.add("My name");
    second.add("is");
    second.add("Neil");
    third.add(66);
    third.add(6);

    // "Hi,My name,is"
    String al1 = first.toString();
    String al2 = second.toString();
    String al3 = third.toString();
    // pair them
    Pair<ArrayList, ArrayList> twoStrings = new Pair<>(first, second);
    Pair<ArrayList, ArrayList> stringAndInt = new Pair<>(second, third);
    Pair<ArrayList, ArrayList> intAndInt = new Pair<>(third, third);
    Pair<ArrayList, Double> arrayListAndDouble = new Pair<>(first, 7.9);
    // to string the

    String resultFirst = twoStrings.toString();
    String resultSecond = stringAndInt.toString();
    String resultThird = intAndInt.toString();
    String resultFourth = arrayListAndDouble.toString();

    assertEquals("1:" + al1 + "\n2:" + al2, resultFirst);
    assertEquals("1:" + al2 + "\n2:" + al3, resultSecond);
    assertEquals("1:" + al3 + "\n2:" + al3, resultThird);
    assertEquals("1:" + al1 + "\n2:" + 7.9, resultFourth);
  }
Ejemplo n.º 7
0
  public static String solve() {
    Pair min = new Pair(pentagonNumbers[0], pentagonNumbers[9999]);

    for (int i = 0; i < pentagonNumbers.length; i++) {
      for (int j = i + 1; j < pentagonNumbers.length; j++) {
        int diff = pentagonNumbers[j] - pentagonNumbers[i];
        if (!isPentagonNumber(diff)) continue;

        int sum = pentagonNumbers[i] + pentagonNumbers[j];
        if (!isPentagonNumber(sum)) continue;

        if (diff < min.b - min.a) {
          min.a = pentagonNumbers[i];
          min.b = pentagonNumbers[j];
        }
      }
    }

    return min.toString();
  }
Ejemplo n.º 8
0
 public int compareTo(Pair<A, B> o) {
   return toString().compareTo(o.toString());
 }
 public String pair(Pair element) {
   return this.toString() + element.toString();
 }
Ejemplo n.º 10
0
  public void simulateBall() {
    ballPreviousX = ballX;
    ballPreviousY = ballY;
    leftEdgeActive = rightEdgeActive = topEdgeActive = bottomEdgeActive = true;

    if (ballY >= MaxY) {
      ballStarted = false;
    }

    if (!ballStarted) {
      ballX = padx - ballDiameter / 2;
      ballY = padTop - ballDiameter;
    } else {
      ballX = ballX + (int) (Math.cos((angle * pi) / 180.0) * unit);
      ballY = ballY - (int) (Math.sin((angle * pi) / 180.0) * unit); // Y increases downward
      if (Math.sin((angle * pi) / 180.0) * unit >= 0) {
        top = true;
      } else top = false;

      if (Math.cos((angle * pi) / 180.0) * unit >= 0) {
        right = true;
      } else right = false;

      if (right) leftEdgeActive = false;
      else rightEdgeActive = false;

      if (top) bottomEdgeActive = false;
      else topEdgeActive = false;

      Pair temp;

      if (leftEdgeActive) {
        int xBlockLeft = ((ballX - blockStartX) / blockLength) * blockLength;
        int yBlockLeft = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;

        // Left Boundary Edge
        temp = new Pair(xBlockLeft, yBlockLeft);
        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || ballX <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {

            /*				if(fireBlocks.containsKey(temp.toString())){
            				fireEnabled = true;
            			}
            */
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }

      if (rightEdgeActive) {
        int xBlockRight = ((ballX - blockStartX + ballDiameter) / blockLength) * blockLength;
        int yBlockRight = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;
        // Right Boundary Edge
        temp = new Pair(xBlockRight, yBlockRight);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter > 0)
                && (ballY - blockStartY + ballDiameter / 2) >= 0)
            || (ballX + ballDiameter) >= MaxX) {
          // System.out.println("Right edge");
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*		if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (topEdgeActive) {
        int xBlockTop = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockTop = ((ballY - blockStartY) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockTop, yBlockTop);

        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || (ballY - ballDiameter) <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            		fireEnabled = true;
            }*/

            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            // System.out.println("temp " + temp.toString());

            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (bottomEdgeActive) {
        int xBlockBottom = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockBottom = ((ballY - blockStartY + ballDiameter) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockBottom, yBlockBottom);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter / 2 >= 0)
                && (ballY - blockStartY + ballDiameter >= 0))
            || (((((ballY + ballDiameter) >= padTop && (ballY + ballDiameter) <= padBottom))
                    || (ballY >= padTop && ballY <= padBottom))
                && (ballX) >= (padx - padLength / 2)
                && ballX <= (padx + padLength / 2))) {

          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
    }
  }