Topics

Important Points

SWE Testing

Developer testing

Developer testing is the testing done by the developers themselves as opposed to dedicated testers or end-users.

Testing Automation

A test driver is the code that ‘drives’ the SUT for the purpose of testing e.g., invoking the SUT with test inputs and verifying if the behavior is as expected.

Usually, we have a tool to do this test automation. In Java, we use JUnit.

Java JUnit

When writing JUnit tests for a class Foo, the common practice is to create a FooTest class, which will contain various test methods for testing methods of the Foo class.

Suppose we want to write tests for the IntPair class below.

public class IntPair {
    int first;
    int second;

    public IntPair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    /**
     * Returns The result of applying integer division first/second.
     * @throws Exception if second is 0.
     */
    public int intDivision() throws Exception {
        if (second == 0){
            throw new Exception("Divisor is zero");
        }
        return first/second;
    }

    @Override
    public String toString() {
        return first + "," + second;
    }
}

What to test for when writing test cases? This should be another module at NUS LOL. But for now, we can take it as the goal of these tests is to catch bugs in the code.

SWE Documentation

Developer-to-developer documentation can be in one of two forms:

  1. Documentation for developer-as-user: This is to document how software componenets are to be used. Some examples are

    1. API documentation

    2. Tutorial-style instructional documentation

  2. Documentation for developer-as-maintainer: This is to document how a system or a component is designed so that other developers can maintain and evolve that part of code.

SWE Design Models

Design is the creative process of transforming the problem into a solution; the solution is also called design. While modelling represents something else.

For example, a class diagram shown as follows is a model that represents a software design.

An example class diagram

A model provides a simpler view of a complex entity because a model captures only a selected aspect.

For example, a class diagram captures the structure of the software design but not the behavior.

Multiple models of the same entity may be needed to capture it fully.

For example, in addition to a class diagram (or even multiple class diagrams), a number of other diagrams may be needed to capture various interesting aspects of the software.

SWE Class Diagrams Basics

UML Object Diagrams model object structures. UML Class Diagrams model class structures. Those rules that object structures need to follow can be illustrated as a class structure.

UML class diagrams describe the structure (but not the behavior) of an OOP solution. (You have already seen a class diagram from above). In this section, we will mainly touch the following:

1

Basic Structure

The basic UML notations used to represent a class is shown as follows:

The methods compartment and/or the attributes compartment may be omitted if such details are not important for the task at hand.

2

Visibility

+ for public, - for private, # for protected and ~ for package private

3

Generic Class Notation

Generic classes can be shown as given below.

4

Class Level members and methods notation

In UML class diagrams, underlines denote class-level attributes and methods. For example, we have the following Student class with class-level member totalStudents and class-level method getTotalStudents().

Association

Associations are the main connections among the classes in a class diagram.

  1. Objects in an OO solution need to be connected to each other to form a network so that they can interact with each other. Such connections between objects are called associations.

  2. Associations in an object structure can change over time.

  3. Associations among objects can be generalized as associations between the corresponding classes too.

To implement associations, we use instance level variables. In other words, An association is a relationship resulting from one object keeping a reference to another object (e.g., storing an object in an instance variable). For example, the Course class can have a students variable to keep track of students associated with a particular course.

1

Association notation in UML

You should use a solid line to show an association between two classes.

2

Associations as attributes

An association can be shown as an attribute instead of a line, and vice versa! The notation by default is as follows,

For example, the association that a Board has 100 Squares can be shown in either of these two ways:

Both is optional, which means we can use either one of them, but not both together!

In some cases, associations are better than attributes because they can show additional decorations such as association labels, association roles, multiplicity and navigability to add more information to a class diagram.

1

Association Labels

Association labels describe the meaning of the association. The arrow head indicates the direction in which the label is to be read.

For example, the same association is described using two different labels.

Here, an Admin uses a Student while a Student is used by an Admin.

2

Association Roles

Association Roles are used to indicate the role played by the classes in the association. We have two classes involved in an association, thus there are two roles.

3

Association Class

To know what an association class in Java is, please read this part in advance. In a class diagram, association classes are denoted as a connection to an association link using a dashed line as shown below.

4

UML Notes

UML notes can augment UML diagrams with additional information. These notes can be shown connected to a particular element in the diagram or can be shown without a connection. The diagram below shows examples of both.

When two classes are linked by an association, it does not necessarily mean both objects taking part in an instance of the association knows about (e.g., has a reference to) each other. The concept of navigability tells us if an object taking part in association knows about the other. In other words, it tells us if we can 'navigate' from the one object to the other in a given direction — because if the object 'knows' about the other, it has a reference to the other object, and we can use that reference to 'navigate to' (e.g., access) that other object.

Navigability can be unidirectional or bidirectional:

  • Unidirectional: If the navigability is from A to B, we say a will have a reference to b but b will not have a reference to a.

  • Bidirectional: b will have a reference to a and a will have a reference to b.

For example, the following code has two unidirectional associations between the Person class and the Cat class (in opposite directions). Because the breeder is not necessarily the same person keeping the cat as a pet, they are two separate associations, not a bidirectional association.

We use arrowheads to indicate the navigability of an association.

Multiplicity

Multiplicity is the aspect of an OOP solution that dictates how many objects take part in each association.

To implement the multiplicty, we have the following four types

1

Optional associations

A normal instance-level variable gives us a 0..1 multiplicity because a variable can hold a reference to a single object or null.

For example, in the code below, the Logic class has a variable that can hold 0..1 i.e., zero or one Minefield objects.

2

Compulsory associations

When we call the instantiate method new(), the multiplicity will be 1.

We use the following notation to draw multiplicity in the class diagram.

Below are some commonly used multiplicities:

  • 0..1 : optional, can be linked to 0 or 1 objects.

  • 1 : compulsory, must be linked to one object at all times.

  • * : can be linked to 0 or more objects.

  • n..m : the number of linked objects must be within n to m inclusive e.g., 2..5, 1..* (one or more), *..5 (up to five)

SWE Object Diagrams Basics

An object diagram shows an object structure at a given point of time and it can be used to complement class diagrams. For example, we can use object diagrams to model different object structures that can result from a design represented by a given class diagram.

1

Object Notation

The following is an example notation to represent an object.

Notes

  1. Unlike classes, there is no compartment for methods.

  2. Attributes compartment can be omitted if it is not relevant to the purpose of the diagram.

  3. Object name can be omitted too e.g., :Car which is meant to say 'an unnamed instance of a Car object'.

The following are examples of some acceptable object notations also.

2

Association

In object diagrams, we also have associations. A solid line indicates an association between two objects, which is similar to the convention used in the class diagram.

Object diagrams vs. Class diagrams

Compared to the notation for class diagrams, object diagrams differ in the following ways:

  • Show objects instead of classes:

    • Instance name may be shown

    • There is a : before the class name

    • Instance and class names are underlined

  • Methods are omitted

  • Multiplicities are omitted. Reason: an association line in an object diagram represents a connection to exactly one object (e.g., the multiplicity is always 1).

  • Furthermore, multiple object diagrams can correspond to a single class diagram.

  • When the class diagram has an inheritance relationship, the object diagram should show either an object of the parent class or the child class, but not both.

  • Association labels/roles can be omitted unless they add value.

Java Association Class

An association class represents additional information about an association. It is a normal class but plays a special role from a design point of view.

For example, a Man class and a Woman class are linked with a ‘married to’ association and there is a need to store the date of marriage. However, that data is related to the association rather than specifically owned by either the Man object or the Woman object. In such situations, an additional association class can be introduced, e.g., a Marriage class, to store such information.

Implement an association class

There is no special way to implement an association class. It can be implemented as a normal class that has variables to represent the endpoint of the association it represents.

Classic Questions

1

API Documentation

Choose correct statements about API documentation.


The answer is All.

2

Models

Choose the correct statements about models.


Answer is a, b, c, and e.

3

UML Class Diagram

Which of these follow the correct UML notation?


3, 4 and 5 are correct.

  • 4: Both Attributes and Methods compartments can be omitted.

  • 5: The Attributes and Methods compartments can be empty.

4

Association Class

Which of these are suitable as an Association Class? Note: the orange color class in the middle is the one being proposed as an association class.


(a)(b)(d)

Explanation: Mileage is a property of the car, and not specifically about the association between the Driver and the Car. If Mileage was defined as the total number of miles that car was driven by that driver, then it would be suitable as an association class.

5

Project Management: Team Structure

Which team structure is the most suitable for a school project?


(a): Given that students are all peers and beginners, the egoless team structure seems most suitable for a school project.

Last updated