Classes, Encapsulation and Composition · Write code · 3 marks
Write the class Color so that two colors with the same name are always the same object.
The constructor is private; clients call the static factory Color.of(String name).
Names are compared ignoring case and surrounding whitespace: of(" Red ") == of("red"). A name that is blank throws IllegalArgumentException.
String getName() returns the trimmed, lower-case name.
public final class Color {
private static final Map<String, Color> CACHE = new HashMap<>();
private final String name;
private Color(String name) { this.name = name; }
public static Color of(String name) {
String key = name.trim().toLowerCase();
if (key.isEmpty()) {
throw new IllegalArgumentException("blank name");
}
return CACHE.computeIfAbsent(key, Color::new);
}
public String getName() { return name; }
}The static map is shared by all calls, so the first call for a name creates the object and later calls return it. The key is normalized before the lookup.
Unit Testing with JUnit · Write code · 3 marks
A test helper generates the boundary values for a method that accepts the integers lo to hi.
/**
* Return the values lo - 1, lo, lo + 1, hi - 1, hi and hi + 1,
* in increasing order and without duplicates.
*
* boundaryValues(1, 10) returns [0, 1, 2, 9, 10, 11]
* boundaryValues(3, 4) returns [2, 3, 4, 5]
* boundaryValues(5, 5) returns [4, 5, 6]
*
* Precondition: lo <= hi
*/
public static List<Integer> boundaryValues(int lo, int hi)public static List<Integer> boundaryValues(int lo, int hi) {
Set<Integer> values = new TreeSet<>();
for (int d = -1; d <= 1; d++) {
values.add(lo + d);
values.add(hi + d);
}
return new ArrayList<>(values);
}A TreeSet removes duplicates (when the range is short the values overlap) and keeps the values sorted.
Design Patterns · Short answer · 2 marks
Subclasses of Report must be able to change what is printed, but never the order of the steps.
public abstract class Report {
public void print() {
System.out.println(header());
System.out.println(body());
}
protected String header() { return "REPORT"; }
protected abstract String body();
}Which method should be declared final, and why? [1]
Why is body abstract while header has an implementation? [1]
print: it is the template method that fixes the sequence of steps. If a subclass could override it, it could reorder or skip the steps, defeating the point of the pattern.
body has no sensible default, so every subclass must supply it (the compiler enforces that); header is a hook with a reasonable default that a subclass may override but does not have to.
UML Class and Sequence Diagrams · Testing · 2 marks
A diagram gives Employee a parking space with multiplicity 0..1: an employee has at most one.
public class Employee {
private Parking parking;
public Parking getParking() { return parking; }
/** @throws IllegalStateException if a space is already assigned */
public void assign(Parking p) {
if (parking != null) throw new IllegalStateException();
parking = p;
}
}Write one JUnit 5 test method that checks the upper bound of the multiplicity: a second space is refused, and the first space stays assigned.
@Test
void secondSpaceIsRefused() {
Employee e = new Employee();
Parking first = new Parking();
e.assign(first);
assertThrows(IllegalStateException.class,
() -> e.assign(new Parking()));
assertSame(first, e.getParking());
}The test assigns one space (the bound), checks that a second assignment throws, and then checks the state: the first space must still be the one stored. assertSame compares identity, which is what is wanted for an object reference.
Version Control with Git · Multiple choice · 1 mark
A project contains these files, and the compiled .class files must never be committed.
Main.java
notes.txt
build/Main.class
src/Cart.java
src/Cart.classWhich single line in .gitignore ignores both .class files and none of the others?
build/
*.class
/*.class
.class
Answer: (B)
(B) A pattern without a slash matches at any depth, so *.class matches build/Main.class and src/Cart.class. build/ ignores only that directory and leaves src/Cart.class. A leading slash anchors the pattern to the repository root (C), where there are no .class files. .class names a file called exactly that (D).
Software Design · University of Toronto (UofT, U of T, UToronto) · also written CSC207, CSC 207
Make a free, printable CSC207H1 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 360 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CSC207H1 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of Toronto. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.