mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-14 04:37:28 -05:00
3.8 KiB
3.8 KiB
title | date | draft | tags | math |
---|---|---|---|---|
Recitation 11: Exam 2 Review | 2022-04-04T15:40:12-04:00 | false | false |
Conceptual Questions
-
When must a representation invariant hold?
-
What's the difference between a representation invariant and an abstract function?
-
Which type of testing only considers the specifications and not the implementation?
-
What is the difference between overriding and overloading a method?
-
Provide an example of representation exposure.
-
Are Java subtypes true subtypes? Why or why not?
Overloading vs Overriding: Spaghetti Code Edition
Consider the following Java code
class A {
void m(A a) { System.out.println("AA"); }
void m(B a) { System.out.println("AB"); }
void m(C a) { System.out.println("AC"); }
}
class B extends A {
void m(A a) { System.out.println("BA"); }
void m(B a) { System.out.println("BB"); }
void m(C a) { System.out.println("BC"); }
}
class C extends B {
void m(A a) { System.out.println("CA"); }
void m(B a) { System.out.println("CB"); }
void m(C a) { System.out.println("CC"); }
}
A a1 = new A();
A a2 = new B();
A a3 = new C();
B b1 = new B();
B b2 = new C();
C c1 = new C();
- Fill in the following table with the output of
row.m(col)
a1 | a2 | a3 | b1 | b2 | c1 | |
---|---|---|---|---|---|---|
a1 | ||||||
a2 | ||||||
a3 | ||||||
b1 | ||||||
b2 | ||||||
c1 |
- How would the grid change if I print it like below?
A[] all = {a1, a2, a3, b1, b2, c1};
for (A some_a : all) {
for (A some_a2 : all) {
some_a.m(some_a2); System.out.print(" ");
}
System.out.println("");
}
a1 | a2 | a3 | b1 | b2 | c1 | |
---|---|---|---|---|---|---|
a1 | ||||||
a2 | ||||||
a3 | ||||||
b1 | ||||||
b2 | ||||||
c1 |
Generics and Advanced Typing
Consider the following Java code:
class Mammal {}
class Cow extends Mammal {}
class Horse extends Mammal {}
class ToyHorse extends Horse {}
Mammal m = new Mammal();
Object o = new Object();
Horse h = new Horse();
ToyHorse t = new ToyHorse();
List<? extends Mammal> lem = new ArrayList<>();
List<? extends Horse> leh = new ArrayList<>();
List<? super Horse> lsh = new ArrayList<>();
For each of the following, note whether or not it will compile.
lem.add(h);
lsh.add(t);
lsh.add(o);
lem.add(null);
Assume that the ArrayList have elements in them.
h = lsh.get(0);
m = leh.get(0);
o = lsh.get(0);
Equivalence Relations
-
What are the three properties that you need to show in order to prove that the method is an equivalence relation.
-
Is the following an equivalence relation? If so, prove. Otherwise provide counterexample.
class String {
public boolean equals(String other) {
for (int i = 0; i < this.length; i++) {
if (this.contents[i] != other.contents[i]) {
return false;
}
}
return true;
}
}
- Is the following an equivalence relation? If so, prove. Otherwise provide counterexample.
class FuzzyNumber {
public boolean equals(FuzzyNumber other) {
// Check: this - 0.5 <= other <= this + 0.5
return this.number - 0.5 <= other.number && other.number <= this.number + 0.5;
}
}
- Is the following an equivalence relation? If so, prove. Otherwise provide a counterexample.
class FuzzyNumberEnhanced {
public boolean equals(FuzzyNumberEnhanced other) {
return this.number - other.number == 0
}
}