COMP2511 Week 3

Agenda

  • Admin Stuff
  • Code Review
  • Domain Modeling (UML)
  • Code Demo

Admin Stuff

  • Get started on Assignment 1asap 
    • Due Week 4 Friday, 5pm (7th October)
  • Go to help sessions if you need help
  • Don't auto-generate UML diagrams
  • Forming pairs for Assignment 2 

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");
    }
}

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

Code Review

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++;
    }
}

Code Review

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++;
    }
}

Code Review

  • Can you override a static method?

Code Review

  • Can you override a static method?
    • No, since the static method is attached to the class, it cannot be overridden

Domain Modeling

UML

UML

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:

  1. Put a breakpoint on line 13 and run the tests in Debug Mode.
  2. Briefly discuss different features of Debug Mode:
    1. The variables section
    2. The debug console
    3. The 'watch' section
    4. The call stack Debug control
    5. 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 22T3 Week 3

By Jayden Leung

COMP2511 22T3 Week 3

  • 116