====== homework1 ====== ==== TestHW.java ==== package homework1; import org.junit.*; import static org.junit.Assert.*; public class TestHW { @Test public void test01() { Point p1 = new Point(2, 5); Point p2 = new Point(5, 2); assertEquals("(2,5) is to be equal to (2,5).", p1, new Point(2, 5)); assertFalse("(2,5) is not to be equal to (5,2).", p1.equals(p2)); assertFalse("(5,2) is not to be equal to (2,5).", p2.equals(p1)); Canvas c = new HashCanvas("CC"); c.put(new Point(2, 5), 1); c.put(new Point(5, 2), 2); c.put(new Point(8, 2), 5); c.put(new Point(1, 3), 9); c.put(new Point(1, 4), 2); c.put(new Point(2, 8), 5); c.put(new Point(3, 5), 3); c.put(new Point(2, 2), 2); assertEquals("Name of canvas is to be CC.", "CC", c.getName()); assertTrue("Canvas should contain (2, 8).", c.contains(new Point(2, 8))); assertFalse("Canvas should not contain (2, 7).", c.contains(new Point(2, 7))); assertEquals("Colour of (3,5) is to be 3.", 3, c.get(new Point(3,5))); } @Test public void test02() { MyCanvas c = new MyHashCanvas("BB", 10); c.put(new Point(2, 5), 1); c.put(new Point(5, 2), 2); c.put(new Point(8, 2), 5); c.put(new Point(1, 3), 9); c.put(new Point(1, 4), 2); c.put(new Point(2, 8), 5); c.put(new Point(3, 5), 3); c.put(new Point(2, 2), 2); assertEquals("Name of canvas is to be BB.", "BB", c.getName()); assertEquals("Number of colour 2 is to be 3.", 3, c.numOfColour(2)); c.remove(new Point(1, 4)); assertEquals("Number of colour 2 is to be 2.", 2, c.numOfColour(2)); } } ===== solution ===== ==== MyHashCanvas.java ==== package homework1.solution; public class MyHashCanvas extends ija.HashCanvas implements ija.MyCanvas { int pole[]; public MyHashCanvas(String ObjectName, int MaxPocetBarev) { super(ObjectName); this.pole = new int[MaxPocetBarev]; } public void put(Object o1, int o2) { super.put(o1, o2); pole[o2]++; } public void remove(Object o) { pole[super.get(o)]--; super.remove(o); } public int numOfColour(int c) { return pole[c]; } } ==== Point.java ==== package homework1.solution; public class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (o instanceof Point) { Point p = (Point)o; if (x == p.x && y == p.y) { return true; } } return false; } public int hashCode() { return this.x * this.y + this.x; } }