Courtesy to Jayden Leung
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?
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
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?
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?
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
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.
Types of cardinality
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:
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.
The Wondrous Sequence is generated by the simple rule:
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.
Explore the IDE tools built into VSCode by:
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.
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);
});
}
}
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?