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
What is the output of A.f() in the following?
public class A {
public static void f() {
B b1 = new B();
B b2 = new B();
b1.incX();
b2.incY();
System.out.println(b1.getX() + " " + b1.getY());
System.out.println(b2.getX() + " " + b2.getY());
}
}
public class B {
private int x;
private static int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public void incX() {
x++;
}
public void incY() {
y++;
}
}
What is the output of A.f() in the following?
1 1
0 1
public class A {
public static void f() {
B b1 = new B();
B b2 = new B();
b1.incX();
b2.incY();
System.out.println(b1.getX() + " " + b1.getY());
System.out.println(b2.getX() + " " + b2.getY());
}
}
public class B {
private int x;
private static int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public void incX() {
x++;
}
public void incY() {
y++;
}
}
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?