Appearance
23T3
Exam Format
Please read
This past paper was done with internet available. So don't worry about certain answers that seem really hard. The main thing as you revise is the concepts of the questions.
I've done for as most as I can, a question and answer format. For example
- Q: How do you determine what pattern a problem is?
- If you're not sure which pattern it is, go through each one and eliminate them
I've also added timestamps to the ones that were covered in the revision lecture.
Don't worry if these questions are not doable, we're aiming to
- Fill gaps on our current understanding (so just look at the answer straight away)
- See what kind of questions is expected (after looking at the answer, try to write it out)
Format
Part 1: Short Answer (25 marks)
- Answer all of Q1-Q6 (19 marks)
- Answer two out of six choice question Q7-Q12 (6 marks)
Part 2: Extended Answer (10 marks)
- Answer question 13 (13 marks)
Part 3: Design & Programming (65 marks)
- Question 14
- Question 15
Notes
- Important concepts are strategy, state and composite patterns (Pass/Credit)
Question 1 (4 marks)
In each of the following code snippets
- Identify the error logic present (1 mark)
- Write a failing assertion that would catch the logic error as part of a unit test (e.g “assetEquals(4, f(2))” (1 mark)
Write your answer in q1.txt.
Part A (2 marks)
java
@Override
public boolean isEqual(Object obj) {
If (this == obj) return true;
If (obj == null) return false;
If (!(obj instanceof Article)) return false;
Article other = (Article) obj;
return this.title.equals(other.title) && this.view.equals(other.view);
}Part B (2 Marks)
java
public List<Integer> withoutOddNumbers(List<Integer> numbers) {
for (Integer number : numbers) {
if (number % 2 != 0) {
numbers.remove(number);
}
}
return new ArrayList<Integer>(numbers);
}Answer
Part A
Q: What's the different between instanceof and getClass()? A: instanceof handles subclasses but getClass() doesn't
java
@Override
public boolean isEqual(Object obj) {
If (this == obj) return true;
If (obj == null) return false;
If (!(obj instanceof Article)) return false;
If (obj.getClass() != this.getClass()) return false;
// instanceof will return true for subclasses, we don't want that
// we want to `getClass()` instead since that will strictly compare
Article other = (Article) obj;
return this.title.equals(other.title) && this.view.equals(other.view);
}
// Test case
News news = new News(); // class New extends Article
assertFalse(article.equals(news));Part B
- Q: Can you modify a list as you iterate through it?
- A: No, you're not allowed to (Java will throw)
java
// Test case
assertDoesNotThrow(ConcurrentModificationException.class, withoutOddNumbers(new List(1,2,3)));Question 2 (3 marks)
Part A (1 mark)
Select one or more of the following correct answers:
The dependency inversion principle encourages programming to _______
- An implementation, not an interface
- An interface, nor an implementation
- Abstraction, not concretions
- Concretions, not abstractions
- A black box
- A white box
Write your answers in "q2.txt"
Part B (2 marks)
Consider the "Angle" class versus the "MathsHelper" class from the "Blackout" assignment, provided in "Angle.java" and "MathsHelper.java"
Which class do you think has better cohesion? Justify your answer.
Write your answer in "q2.txt"
Answer
Part A
- Q: What is common between an interface, abstraction and "black box"?
- They all minimise coupling (or decoupling), i.e the methods can change their implementation and you don't need to worry about anything
Part B
(Skipped in revision lecture)
Question 3 (4 Marks)
In 22T2 COMP2511 is contemplating moving to Jira for students to manage their projects. There are three different types of Jira boards that can be used: Scrum, Kanban and Bug Tracking. All boards have the same flow of work, and allow for features including support for issue tracking and customisation of project-specific features. However, some elements may vary for each type of board — Scrum board takes two week sprits whereas Kanban has a series of epics that are progressed through, and bug tracking projects only have a single backlog. once a project is created as Scrum, Kanban or Bug Tracking it cannot change it type.
What design pattern could be used to model this? Justify your choice by describing how the above scenario relates to the key characteristics of your chosen Design Pattern.
Write your answer insider q3.txt
Answer
Q: How do you determine what pattern a problem is?
- A: If you're not sure which pattern it is, go through each one and eliminate them
Q: What is the template pattern?
- A: https://refactoring.guru/design-patterns/template-method (Basically an abstract class that lets child class inherits it)
Q: What are the key characteristic of question 3?
- A: Key characteristics
- Some parts are the same, but some parts are different
- Cannot change it type so it can't be strategy or state pattern
- A: Key characteristics
Question 4 (2 marks)
Webster is waiting for tickets to be released to the next Sydney Swans. He has setup notification on his phone that will alert him when the tickets open, and he is also checking is phone regularly since there are a limited number of tickets and he needs to be the one of the first ones to book to save a place.
Which implementation of the Observer pattern is present in the above scenario, push or pull? Justify your answer
Write your answer inside “q4.txt”
Question 5 (3 marks)
Identify the code smells present in the following pieces of code, and explain whether you think this is indicative of an underlying design problem, if so what the problem is, or alternative if you think you don’t have enough information to tell.
Write your answers insider “q5.txt”
Part A (2 marks)
Scenario: A satellite simulation where various satellites are orbiting the Jovian ring.
java
private List<Satellite> standardSatellites;
private List<Satellite> quantumSatelilites;
/*
* Run the simluation for a single minute
*/
public void simulate() {
// Update Position of Satellites
for (Satellite s : standardSatellites) {
s.setPosition(s.getPosition() + 3000 / s.getHeight());
}
for (Satellite s : quantumSatelilites) {
s.setPosition(s.getPosition() + 2000 / s.getHeight());
}
// ... so on
}Part B (1 mark)
java
public class ConsumeService {
// ...
public void startConsumerService(Config config, Consumer consumer, Logger logger,
Tracer tracer, Metrics metrics, String[] topics,
boolean willTimeout, int timeout) {
}
}Answer
Part A
- Q: What are the code smell?
- Feature Envy, Law of Delimiter, Should use subclass to implement the differences in the same methods
Question 6 (3 marks)
During the project, a student on the forum asked the following question
Are we allowed to modify the “Position” class given to us!”
Tina answered the following
Yes, as long as you don’t ban the precondition and postcondition of that class - we will rely
With reference to the Liskov Substituion Principle, explain what is meant by "breaking the precondition and postconditions". Provide one example (either breaking the presconditions or postcondition) of this with reference to the Position class, provided for you in Position.java and explain how it would affect the automarking in this scenario.
Write your answer inside q6.txt
java
public static final Position calculatePositionBetween(Position a, Position b) {
return new Position(b.x - a.x, b.y - a.y);
}:::
Answer
Part A
- Q: What is precondition and postcondition?
- How do you weaken/strengthen a precondition?
- How do you weaken/strengthen a postcondition?
- What is contractual design?
Precondition:
Definition:
- Example:
- Preconditions: COMP2511 - COMP2521, COMP1531
- Weakening precondition: COMP2511-v2 - COMP2521
- Strengthening precondition: COMP2511-v2 - COMP2521, COMP1531, COMP999
- Notes: Can only weaken preconditions, not strengthen them
LSP: All subtypes must be substitutable for their base types;
Post condition
- v1: postcondition - gets you a destination, free meal
- v2(weaken): postcondition - free meal
- v2(strengthen): postcondition - gets you a destination, free meal, umbrella
Contractual design