COMP2511 Week 3
Courtesy to Jayden Leung
Agenda
- Admin Stuff
- Code Review
- Domain Modeling (UML)
- Code Demo
Admin Stuff
- Get started on Assignment 1
- Due Week 5 Friday, 5pm (17th March)
- Go to help sessions if you need help
- Don't auto-generate UML diagrams
- Choose your partner for Assignment 2 and let me know before the end of the lab. Otherwise, I will let fate take its course ;(
Code Review
Code Review
public class A {
public static void f() {
C c = new C();
c.speak();
B b = c;
b.speak();
b = new B();
b.speak();
c.speak();
}
}
public class B {
public void speak() {
System.out.println("moo");
}
}
public class C extends B {
public void speak() {
System.out.println("quack");
}
}
Can you override a static method?
Code Review
public class A {
public static void f() {
C c = new C();
c.speak();
B b = c;
b.speak();
b = new B();
b.speak();
c.speak();
}
}
public class B {
public void speak() {
System.out.println("moo");
}
}
public class C extends B {
public void speak() {
System.out.println("quack");
}
}
Can you override a static method?
No, because a static method is attached to the class and not a particular instance
Code Review
public class A {
public static void f() {
C c = new C();
c.speak();
B b = c;
b.speak();
b = new B();
b.speak();
c.speak();
}
}
public class B {
public void speak() {
System.out.println("moo");
}
}
public class C extends B {
public void speak() {
System.out.println("quack");
}
}
What is the output of A.f() in the following?
Code Review
public class A {
public static void f() {
C c = new C();
c.speak();
B b = c;
b.speak();
b = new B();
b.speak();
c.speak();
}
}
public class B {
public void speak() {
System.out.println("moo");
}
}
public class C extends B {
public void speak() {
System.out.println("quack");
}
}
What is the output of A.f() in the following?
Code Review
public class A {
public static void f() {
C c = new C();
c.speak();
B b = c;
b.speak();
b = new B();
b.speak();
c.speak();
}
}
public class B {
public void speak() {
System.out.println("moo");
}
}
public class C extends B {
public void speak() {
System.out.println("quack");
}
}
What is the output of A.f() in the following? quack quack moo quack
Domain Modeling using UML
UML Classes


UML Relationships
Inheritance: "is-a" relationship
Realisation: "implements" relationship
Aggregation: "part of" relationship
- if A aggregates B and A is destroyed.
B can still exist.
Composition: "has-a" relationship
- if A composes B and A is destroyed.
B can no longer exist.

UML Cardinality

Types of cardinality
- one to one
- one to many
- many to meny
Domain Modeling
Create an OO domain model for a system with the following requirements.
A Car has one or more engines and a producer. The producer is a manufacturing company who has a brand name. Engines are produced by a manufacturer and have a speed. There are only two types of engines within UNSW's cars:
- Thermal Engines, which have a default max speed of 114, although they can be produced with a different max speed, and the max speed can change to any value between 100 and 250.
-
Electrical Engines, which have a default max speed of 180. This is the speed at which they are produced, and the max speed can change to any value that is divisible by 6.
Cars are able to drive to a particular location x, y.
Since UNSW is a world-leader in technology innovation, they want you to be able to model the behaviour of Time Travelling for any vehicle, and to model a time travelling car. A vehicle that travels in time stays in the same location but travels to a LocalDateTime.
Domain Modeling

Domain Modeling
Code Demo
Wonderous.java
Code Demo
The Wondrous Sequence is generated by the simple rule:
- If the current term is even, the next term is half the current term.
- If the current term is odd, the next term is three times the current term, plus 1.
For example, the sequence generated by starting with 3 is:
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
If the starting term is 1, then an empty list is returned.
Inside src/wondrous/Wondrous.java there is an implementation of this algorithm.
Inside src/wondrous/test/WondrousTest.java there is a single test for the function. The test currently fails.
Part 1 - Debugging
Explore the IDE tools built into VSCode by:
- Put a breakpoint on line 13 and run the tests in Debug Mode.
- Different features of Debug Mode:
- The variables section
- The debug console
- The 'watch' section
- The call stack Debug control
- Use the debug tools and the given algorithm to determine why the test is failing, and fix the bug.
Part 2 - Writing JUnit Tests
There is a further bug in the function not caught by the given unit test. Find the other bug, and write a corresponding unit test inside WondrousTest.
Part 2 - Writing JUnit Tests
public class WondrousTest {
@Test
public void testBasic() {
Wondrous w = new Wondrous();
List<Integer> expected = new ArrayList<Integer>(Arrays.asList(3, 10, 5, 16, 8, 4, 2, 1));
assertEquals(expected, w.wondrous(3));
}
@Test
public void testOne() {
Wondrous w = new Wondrous();
List<Integer> expected = new ArrayList<Integer>();
assertEquals(expected, w.wondrous(1));
}
@Test
public void testInvalid() {
Wondrous w = new Wondrous();
assertThrows(IllegalArgumentException.class, () -> {
w.wondrous(0);
});
}
}
Part 3 - Exceptional Conditions
- What are exceptions?
Part 3 - Exceptional Conditions
-
What are exceptions?
- An exception is an event, which occurs during the execution of a problem, that disrupts the normal flow of the program's instructions
- What are the 2 types of exceptions?
Part 3 - Exceptional Conditions
-
What are exceptions?
- An exception is an event, which occurs during the execution of a problem, that disrupts the normal flow of the program's instructions
-
What are the 2 types of exceptions?
- Checked: Must be checked, will result in a compilation error if not handled Any class that inherits from `Exception` is a checked exception. E.g., IOException, SQLException
- Unchecked: Genuine errors that occur at runtime Any class that inherits from `RuntimeException` is unchecked E.g., ArrayIndexOutOfBoundsExceptions, ArithmeticException
Part 3 - Exceptional Conditions
Modify the method such that if a start is less than 1, an IllegalArgumentException is thrown. Write a corresponding test for this inside WondrousTest.
In many cases when we throw an exception we need to update the method signature and existing tests but here we don't - why is this?
COMP2511 Tutorial 3
By Matthew Liu
COMP2511 Tutorial 3
- 125