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.
The comparison of formal parameters and type parameters here is awesome!
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 asArrayList<E>orLinkedList<E>.
Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
Example: the
ArrayList<E>class implements theList<E>interface while theHashMap<K, V>class implements theMap<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 theList<E>interface.
The algorithms in Collections are class methods (like Collections.sort, Collections.binarySearch, Collections.reverse, etc.), not in the collection interfaces.
Below is a table summarizing the interfaces used in Java Collections.
–
Root interface for all collections of objects. Defines basic operations (add, remove, size, etc.).
Depends on subtype
Depends
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
–
Key-value pairs, unique keys. Each key maps to at most one value.
Keys: ❌ No
Values: ✅ Yes
✅ Depends (e.g., HashMap unordered, TreeMap sorted)
Notes
One example of the collections framework mentioned above is C++ STL.
CollectionandCollectionsare different!Collectionis an interfaceCollectionsis a helper class with only class/static methods (cannot be instantiated). It provides algorithms mentioned above.
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
The example above illustrates how to use a HashMap<String, Point> to maintain a list of coordinates and their identifiers e.g., the identifier x1 is used to identify the point 0,0 where x1 is the key and 0,0 is the value.
Java File Access
In this part, we will learn how to read from and write to files in Java.
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.
Write to a File
You can use a java.io.FileWriter object to write to a file. For example,
Note that you need to call the close() method of the FileWriter object for the writing operation to be completed.
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 -jarcommand e.g.,java -jar foo.jarlaunches thefoo.jarfile.The IDE or build tools such as Gradle can help you to package your application as a JAR file.
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:
Functional requirements specify what the system should do.
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 ...
User story is function requirement, not NFR!
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
Classic Questions
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