Q1
(note inside of the Employee constructor, the exception is unchecked so we dont have to add throws )
-
The super constructor of employee doesnt take a name
-
Comparable requires that
compareTois implemented but it never was -
@override on a method that can’t be overriden (i.e. the method overrided does not exist in the abstract class)
-
Cannot instance person in main
-
Oh main doesn’t have the right signature
-
Doesn’t implement
compareTo -
Super constructor DNE
-
@Override is bad
-
Person is abstract
is that it?
Q2
Alpha: B07 âś…no message âś…~~~~~~todo this one is CRAZY
spoilers
answer is
B07
Alpha âś…Alpha: CSC âś…CSCB07 âś…Alpha: B07 âś…
Q3
a)
- No symmetry, cuz d.equals(b) is true (b is a document and has the same content) but b.equals(d) is NOT
- Reflexive yes because d.equals(d) is true since the type and content is the same. Same for b.equals(b)
- Transitive is true becuz assuming the content and titles of the books and docs are the same…
- todo Engrain this sorta logic! It’s really intuitive with practice though. a67 for the win.
- Suppose a1.equals(a2) and a2.equals(a3) and assume they all have the same content; if they’re a book, they should have the same title.
- If a1 is a document, then a1.equals(a3) because:
- if a2 is a book, then a1.equals(a2) holds. a2.equals(a3) can only be true if a3 is a book, so a1.equals(a3) since a1 is a doc and a3 is a book
- if a2 is a document, then a3 can either be a book or document. either way, a1.equals(a3) holds
- If a1 is a book, then a1.equals(a3) cuzzz
- a2 must be a book for a1.equals(a2) to hold. Similarly a3 must be a book since a2.equals(a3) must hold. Therefore a1.equals(a3) since a1 and a3 must both be books.
- If a1 is a document, then a1.equals(a3) because:
- Consistent
- Because the function doesn’t change the variables, the state of the class when called again is the same, so the function will return the same output as the content and value of variables have not changed
b)
public int hashCode() {
return document.length(); // Equal objects have equal hash codes is TRUE!
}c) With composition (I got spoiled for the answer lol) but it’s simple: have a document instance within the book and reference it, instead of implementing it and dealing with bloated boilerplate. Composition my beloved
Q4
- todo I need to remember all the kinds of jtest. There was
assertEquals,assertTrue,assertNotNullbut I haven’t used the last two enough (and used them wrong, too) - Also remember to use the annocation
@Testto show the method is a test
Basically good but use assert false as well as assert true. Use assert equals when comparing if two values are equal as well. Yeah just gotta remember thattodo
@Test
public void testEquals1() {
Point p1 = new Point(0, 0);
Circle c1 = new Circle(p1, 4.0);
Circle c2 = null;
assertNotEquals(c1, c2);
}
@Test
public void testEquals2() {
Point p1 = new Point(0, 0);
Circle c1 = new Circle(p1, 4.0);
Circle c2 = c1;
assertTrue(c1.equals(c2) == true);
}
@Test
public void testEquals3() {
Point p1 = new Point(0, 0);
Circle c1 = new Circle(p1, 4.0);
assertTrue(c1.equals(p1) == false);
}
@Test
public void testEquals4() {
Point p1 = new Point(0, 0);
Circle c1 = new Circle(p1, 4.0);
Circle c2 = new Circle(p1, 4.0);
assertTrue(c1.equals(c2) == true);
}
@Test
public void testEquals5() {
Point p1 = new Point(0, 0);
Circle c1 = new Circle(p1, 4.0);
Circle c2 = new Circle(p1, 3.0);
assertFalse(c1.equals(c2));
}
@Test
public void testEquals6() {
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 1);
Circle c1 = new Circle(p1, 4.0);
Circle c2 = new Circle(p2, 4.0);
assertFalse(c1.equals(c2));
}Q5
todo Remember, by default things are “public”-ish (package private)
- x is defaulted to 0 as it’s an instance field (unlike local variables, who you should explicitly set a default value to) so the output should intuitively be
2(C) - d (will not compile cuz you can still access static things that illegal-looking way)
- Better explanation: you cannot define the same function signature twice, even if the static modifier differentiates the two
nullpointerexception(java is too dumb to know m is null)- Okay…
- should be ok but
abstractis implicit - B is good
int xshould be constant. otherwise it’s good Oh nvm java doesnt do that for you?- Nope it has to be marked
final
- Nope it has to be marked
- nuh uh uh. No implementations
- should be ok but
- Alright…
- A is chill
- B cannot have abstract members in non-abstract class
- C is chill
- D is chill
sis an array = a reference type. so it’s by defaultnull. accessinglengthas a member should throw aNullPointerException(C)- The empty-arged constructor is private, so the fraction cannot be instanced. It will not compile
- okee
- false
- false
- true
- false
- âś… Good work
- aaa
- A is good cuz equal objects have equal hashcodes
- B is good
- Two classes can have the same id but different department so the hashcodes would be different for the same class (as defined by equals)
- D is false cuz id is what determines equivalence
- note that string’s hashcode function is overwritten
- D: none of them (should be gibberish)
- 4
- okay..
- Duration is equal when the mins and secs are equal
- Specific duration is true also if millis are equal
- Reflexive is true
- Not symmetry (dur.equals(spec) can be true when spec.equals(dur) is false)
- consider s1.equals(d1) and d1.equals(s2) where s1’s mili = 10 and s2’s mili = 20
- Because d1 is not a specific duration, s1.equals(d1) uses the duration implementation, which compares minutes and seconds. Thus, s1 and d1 only have equivalent secs/mins.
- Because d1.equals(s2) casts s2 to a duration, it again doesn’t consider miliseconds.
- So s2 and s1 may have different miliiseconds, meaning they are not equivalent.
- Consistency is true
- todo I dont know exceptions well enough…!
- If you want to catch errors in errors, you have to nest. But catch statements chained are to select between the many kinds of errors you might get (i.e. runtime exception, any old exception, etc.). The
finallyis run every time no matter what though.
- If you want to catch errors in errors, you have to nest. But catch statements chained are to select between the many kinds of errors you might get (i.e. runtime exception, any old exception, etc.). The
Q6
Basically
Q7
a)
R ⇒ True: it’s reachable because 1 < 3 ⇒ b < c is true. Also 2 > 1 ⇒ !(a < b) so the m = b fault is hit with this test.
I ⇒ True. m = b is a fault. The expected m at this point is m = a
P ⇒ True. The incorrect m propagates down to the return of the function
R ⇒ True. The return of median(2,1,3) is 1 and 1 != 2 so the assertTrue test will fail when it was expected to have passed
b) let A = { 1 }, 1
then left = 0, right = 0. 0 < 0 is false. skips to -1
c) A = { 2, 3 }, x = 7
-
assertEquals(binSearch(new int[]{2, 3}, 7), -1) -
When item isn’t found… hmmm smart!
-
down-casting means going from parent to child (usually destructive; requires explicit cast)
-
upcasting means going from child to parent (not destructive. Can be done implicitly)