Topics

Important Points

Java Generics

I believe CS2030S has a very good explanation about generics! Feel free to go back and review it.

Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.

Java Collections

A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.

The collections framework is a unified architecture for representing and manipulating collections. It contains the following:

  • Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation.

    • Example: the List<E> interface can be used to manipulate list-like collections which may be implemented in different ways such as ArrayList<E> or LinkedList<E>.

  • Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

    • Example: the ArrayList<E> class implements the List<E> interface while the HashMap<K, V> class implements the Map<K, V> interface.

  • Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.

    • Example: the sort(List<E>) method can sort a collection that implements the List<E> interface.

Below is a table summarizing the interfaces used in Java Collections.

Interface
Extends
Characteristics
Allows Duplicates?
Ordered?

Root interface for all collections of objects. Defines basic operations (add, remove, size, etc.).

Depends on subtype

Depends

Collection

Ordered sequence, index-based access, insertion at specific positions.

✅ Yes

✅ Yes

Collection

Models a mathematical set; no duplicate elements allowed.

❌ No

❌ Not necessarily

Set

Maintains elements in ascending order according to natural order or comparator.

❌ No

✅ Yes

Collection

Holds elements prior to processing (FIFO typically, but can vary).

✅ Yes (depends on implementation)

✅ Often

Queue

Double-ended queue; supports insertion/removal at both ends.

✅ Yes

✅ Yes

Key-value pairs, unique keys. Each key maps to at most one value.

Keys: ❌ No

Values: ✅ Yes

✅ Depends (e.g., HashMap unordered, TreeMap sorted)

Map

Maintains mappings in ascending key order.

Keys: ❌ No

✅ Yes

Notes

HashMap Example

This content actually can serve as a good reference for CS2040S!

HashMap is an implementation of the Map interface. It allows you to store a collection of key-value pairs.

Code Explanation

Java File Access

In this part, we will learn how to read from and write to files in Java.

1

Represent a File in Java

You can use the java.io.File class to represent a file object. It can be used to access properties of the file object.

Code Explanation

2

Read File as a source of data in Scanner

You can read from a file using a Scanner object that uses a File object as the source of data. For example,

3

Write to a File

You can use a java.io.FileWriter object to write to a file. For example,

4

Append to a File

You can create a FileWriter object that appends to the file (instead of overwriting the current content) by specifying an additional boolean parameter to the constructor. For example,

Java JAR Files

Java applications are typically delivered as JAR (short for Java Archive) files. A JAR contains Java classes and other resources (icons, media files, etc.).

  • An executable JAR file can be launched using the java -jar command e.g., java -jar foo.jar launches the foo.jar file.

  • The IDE or build tools such as Gradle can help you to package your application as a JAR file.

For this course, you just need to know how to follow these steps to create a JAR file for your ip and tp.

SWE Requirements

A software requirement specifies a need to be fulfilled by the software product. And a software project may be,

  • a brownfield project e.g., develop a product to replace/update an existing software product

  • a greenfield project e.g., develop a totally new system from scratch

In either case, requirements need to be gathered, analyzed, specified, and managed. And requirements come from stakeholders.

Stakeholder is an individual or an organization that is involved or potentially affected by the software project. e.g., users, sponsors, developers, interest groups, government agencies, etc.

Requirements can be divided into two in the following way:

  1. Functional requirements specify what the system should do.

  2. Non-functional requirements (NFR) specify the constraints under which the system is developed and operated.

Non-functional Requirement

Below are some examples of non-functional requirement categories:

  • Data requirements e.g. size, volatility, persistency etc.,

  • Environment requirements e.g. technical environment in which the system would operate in or needs to be compatible with.

  • Accessibility, Capacity, Compliance with regulations, Documentation, Disaster recovery, Efficiency, Extensibility, Fault tolerance, Interoperability, Maintainability, Privacy, Portability, Quality, Reliability, Response time, Robustness, Scalability, Security, Stability, Testability, and more ...

Functional Requirement

As the name suggests and the definition above, these are just the technical stuff, like user story.

SWE Use Case

Don't confuse use case with user story.

Use case is a description of a set of sequences of actions, including variants, that a system performs to yield an observable result of value to an actor. Thus,

  • A use case describes an interaction between the user and the system for a specific functionality of the system, and

  • it captures the functional requirements of a system.

This part is totally the same as last week's content, maybe should remind the course team to update 😂

SWE Code Quality: Unsafe Practices

It is safer to use language constructs in the way they are meant to be used, even if the language allows shortcuts. Such coding practices are common sources of bugs. Know them and avoid them. These practice includes

1

case statement

  1. Always include a default branch in case statements.

  2. Furthermore, use the default branch for the intended default action and not just to execute the last option.

2

Don't recycle variables or parameters

  • Use one variable for one purpose.

  • Do not reuse formal parameters as local variables inside the method.

For example,

Classic Questions

1

Non-functional Requirements

Given below are some requirements of TEAMMATES (an online peer evaluation system for education). Which one of these are non-functional requirements?


Explanation: (b) are (e) are functions available for a specific user types. Therefore, they are functional requirements. (a), (c), (d), (f) and (g) are either constraints on functionality or constraints on how the project is done, both of which are considered non-functional requirements.

Last updated