Skip to content

Commit

Permalink
reformat notes
Browse files Browse the repository at this point in the history
  • Loading branch information
bertie-wheen committed Jan 31, 2025
1 parent ce875ee commit 4e433d3
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 137 deletions.
61 changes: 34 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
# Data Structures and Algorithms, 2024-5: Labs


## Docs


[JavaDoc for the labs is hosted here.](https://bertie-wheen.github.io/dsa-2024-5)


## Notes


1. [Getting Started](https://github.com/bertie-wheen/dsa-2024-5/tree/trunk/src/main/java/dsa/lab01)
2. [Static Arrays & Linked Lists](https://github.com/bertie-wheen/dsa-2024-5/tree/trunk/src/main/java/dsa/lab02)


## Setup


What to do at the start of a lab session (if using lab computers):

* Open the Software Hub and launch:
* Git for Windows
* IntelliJ Community Edition
* Git for Windows
* IntelliJ Community Edition
* Open PowerShell and run:
* If lab 1:
* `N:`
* `git clone https://github.com/bertie-wheen/dsa-2024-5 dsa`
* Else (labs 2 onwards):
* `cd N:/dsa`
* `git pull`
* If you get an error "fatal: detected dubious ownership in
repository ..." (which you likely will):
* `git config --global --add safe.directory '%(prefix)///smbhome.uscs.susx.ac.uk/<username>/dsa'`
(where `<username>` is your username)
* `git pull`
* If lab 1:
* `N:`
* `git clone https://github.com/bertie-wheen/dsa-2024-5 dsa`
* Else (labs 2 onwards):
* `cd N:/dsa`
* `git pull`
* If you get an error "fatal: detected dubious ownership in
repository ..." (which you likely will):
* `git config --global --add safe.directory '%(prefix)///smbhome.uscs.susx.ac.uk/<username>/dsa'`
(where `<username>` is your username)
* `git pull`
* Open IntelliJ IDEA and:
* Open `N:/dsa` as a project
* In the menu, select `File -> Project Structure`
(or press `Ctrl+Alt+Shift+S`)
* Select `Project` in the left sidebar (if it isn't already selected)
* If there is no SDK:
* Click on the dropdown, and select `Download JDK`
* Choose `Amazon Corretto` as the vendor, and click `Download`
* Click `Apply`, then `OK` to close the project structure dialog
* Open `N:/dsa` as a project
* In the menu, select `File -> Project Structure` (or press
`Ctrl+Alt+Shift+S`)
* Select `Project` in the left sidebar (if it isn't already selected)
* If there is no SDK:
* Click on the dropdown, and select `Download JDK`
* Choose `Amazon Corretto` as the vendor, and click `Download`
* Click `Apply`, then `OK` to close the project structure dialog


## Structure


These labs are structured into
[packages](https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html),
one package per lab (e.g. `dsa.lab01` for lab 1). All are subpackages of the
[packages](https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html), one
package per lab (e.g. `dsa.lab01` for lab 1). All are subpackages of the
top-level `dsa` package, and each is further structured into subpackages.

Each lab contains an `exercises` subpackage (e.g. `dsa.lab01.exercises`)
that contains one or more exercises. Each exercise is a partially-implemented
[class](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html)
with one or more `TODO`s that you are expected to fill in in order to complete
the implementation.
[class](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html) with one
or more `TODO`s that you are expected to fill in in order to complete the
implementation.

Model solutions for each of the exercises are provided in the `solutions`
subpackage (e.g. `dsa.lab01.solutions`).
Expand Down
95 changes: 51 additions & 44 deletions src/main/java/dsa/lab01/README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,82 @@
# Data Structures and Algorithms: Lab 1


A very warm welcome to the Data Structures and Algorithms labs!

In this first lab we'll learn how to set up your machine and a few basic
programming concepts in Java that we'll need for this module.


## Setup


What to do at the start of EVERY lab session (if using lab computers):

* Open the Software Hub and launch:
* Git for Windows
* IntelliJ Community Edition
* Git for Windows
* IntelliJ Community Edition
* Open PowerShell and run:
* If lab 1:
* `N:`
* `git clone https://github.com/bertie-wheen/dsa-2024-5 dsa`
* Else (labs 2 onwards):
* `cd N:/dsa`
* `git pull`
* If you get an error "fatal: detected dubious ownership in
repository ..." (which you likely will):
* `git config --global --add safe.directory '%(prefix)///smbhome.uscs.susx.ac.uk/<username>/dsa'`
(where `<username>` is your username)
* `git pull`
* If lab 1:
* `N:`
* `git clone https://github.com/bertie-wheen/dsa-2024-5 dsa`
* Else (labs 2 onwards):
* `cd N:/dsa`
* `git pull`
* If you get an error "fatal: detected dubious ownership in
repository ..." (which you likely will):
* `git config --global --add safe.directory '%(prefix)///smbhome.uscs.susx.ac.uk/<username>/dsa'`
(where `<username>` is your username)
* `git pull`
* Open IntelliJ IDEA and:
* Open `N:/dsa` as a project
* In the menu, select `File -> Project Structure`
(or press `Ctrl+Alt+Shift+S`)
* Select `Project` in the left sidebar (if it isn't already selected)
* If there is no SDK:
* Click on the dropdown, and select `Download JDK`
* Choose `Amazon Corretto` as the vendor, and click `Download`
* Click `Apply`, then `OK` to close the project structure dialog
* Open `N:/dsa` as a project
* In the menu, select `File -> Project Structure` (or press
`Ctrl+Alt+Shift+S`)
* Select `Project` in the left sidebar (if it isn't already selected)
* If there is no SDK:
* Click on the dropdown, and select `Download JDK`
* Choose `Amazon Corretto` as the vendor, and click `Download`
* Click `Apply`, then `OK` to close the project structure dialog


## Interfaces


What are they? A list of method declarations. A "contract": a bunch of methods
usually coming together with an informal explanation, in plain English, of what
they are supposed to do.
they are supposed to do.

Example: have a look at the `lab01\base\StringContainer.java` file, containing
the `StringContainer` interface.

Interfaces can have some methods already implemented "by default". Indeed, the
method `isEmpty()` in the `StringContainer` interface is fully implemented.


## Classes implementing interfaces


A class "implementing" an interface is a class that supports ALL the methods of
the given interface, that is, that has all the methods listed in the interface
together with code that actually implements them. (Methods that have a default
implementation in the interface may be overridden, but they don't have to.)
Although not required from a purely syntactic point of view, we also want these
methods to implement the methods *correctly*, by which we mean that we want them
to behave as instructed in the informal explanations in the interface.
to behave as instructed in the informal explanations in the interface.

In other words: a class implementing an interface is a class that "fulfills the
contract" specified by the interface. It can also do *more* stuff, have its own
fields and extra methods.
fields and extra methods.

Example: have a look at the `lab01\exercises\StringArray.java` file containing
the `StringArray` class. It's got a Java array as a private field "holding" the
strings. If you're using IntelliJ (as you should), you can click on the
collapsed link "Constructors" to see that we also have two constructors methods:
you can safely ignore them. There's also an implementation of the
`contains(string)` method. You'll notice that the `size()` method contains just

```java
// TODO: Implement StringArray.size()
throw new TODO();
```

This is your very first exercise of this class! In these labs, whenever you see
methods with the lines above in them, you can tell that that method is for *you*
to implement. Simply delete these lines and replace them with the correct code
Expand All @@ -85,22 +90,24 @@ querying how many strings it contains, by asking if it's empty or not, or by
checking whether a given string does or does not appear in the container. The
user cannot see or access the underlying array of strings.

If you select the `Run Lab 1 tests` run configuration in the top-right, you can
run all the tests for the exercises in this lab (`StringArray` and `Array`) and
check which are passing. If you implemented `StringArray.size()` correctly, you
should see a green tick next to `StringArray`, but still a red exclamation mark
symbol next to `Array` - these are for the next exercise, which we turn to now.

(If you don't see `Run Lab 1 tests`, expand `All Configurations`.)
If you select the `Run tests` run configuration in the top-right, you can run
all the tests for the exercises that have been released thus far (assuming
you're reading this in the first week, currently two: `StringArray` and `Array`)
and check which are passing. If you implemented `StringArray.size()` correctly,
you should see a green tick next to `StringArrayTests`, but still a red
exclamation mark symbol next to `ArrayTests` - these are for the next exercise,
which we turn to now.

(One more thing before we do - getting all green ticks on the tests is great,
but they aren't exhaustive, so there could still be issues with your solution.
We highly recommend looking at the model solutions provided and comparing your
code with it, method by method, to check that you considered all cases and/or
approached it in the right way.)


## Generic types


What if I want to talk about a container of, say, integers? Or characters? Or
Booleans? It would be impractical to define several different interfaces for
each of them. Instead, we use *generic types* as part of the interface's
Expand All @@ -125,17 +132,17 @@ type: `String`. All we would need to do is to modify its declaration from
`public class StringArray implements StringContainer` to
`public class StringArray implements Container<String>`. We can, however, do
better: we can define a new class, `Array<Item>`, which *also* features a
generic, unspecified type.
generic, unspecified type.

Example: have a look at the `lab01\exercises\Array.java` file containing the
`Array<Item>` interface, which implements `Container<Item>` with methods updated
to accommodate the generic type. Can *you* implement the `size()` method? It
will only require one line of code.

(If you managed `StringArray.size()`, this should be pretty straightforward.
The point of this week's exercises aren't to be hard, but to introduce you to
the mechanics of working on and testing your solutions. Try 'Run tests' again,
and check whether you now have all green ticks!)
(If you managed `StringArray.size()`, this should be pretty straightforward. The
point of this week's exercises aren't to be hard, but to introduce you to the
mechanics of working on and testing your solutions. Try 'Run tests' again, and
check whether you now have all green ticks!)

But we can't go on forever with unspecified types: when we want to create an
actual *object*, **then** we must specify what actual type we want for our items
Expand All @@ -150,9 +157,8 @@ specified type. Eventually we *must* specify a type. For example,

Try to play with our classes. For instance, try to run the following code in the
main method, and see what happens:

```java
Array<String> myArray = new Array<>("Hello","world");
Array<String> myArray = new Array<>("Hello", "world");

System.out.println("size = " + myArray.size());
System.out.println("Is it empty? " + myArray.isEmpty());
Expand All @@ -163,8 +169,10 @@ System.out.println("Does it contain the word `bye`? " + myArray.contains("bye"))
(Remember to add `import dsa.lab01.exercises.Array;` at the top of the
`Main.java` file first.)


## Interfaces extending other interfaces


Our `Container<Item>` interface is pretty basic. As we saw, an object belonging
to a class implementing it does not have to do much. Let's make things a bit
more exciting by defining a new interface `BadStaticSequence<Item>` with the
Expand All @@ -175,7 +183,7 @@ in the sequence.

Example: have a look at the `lab01\base\BadStaticSequence.java` file, containing
the `BadStaticSequence<Item>` interface with the methods from `Container<Item>`
copied and pasted plus `get(index)` and `set(index,string)`.
copied and pasted plus `get(index)` and `set(index, string)`.

(Don't worry about the details of what a sequence is or what the methods should
do, we'll see this better in the lectures.) As the name tries to suggest, this
Expand All @@ -187,11 +195,10 @@ and to make Java actually aware of the close relationship between

Example: have a look at the `lab01\base\StaticSequence.java` file, and notice
the syntax

```java
public interface StaticSequence<Item> extends Container<Item>
public interface StaticSequence<Item>
extends Container<Item>
```

Now any class implementing `StaticSequence<Item>` *must* contain implementations
of all the (not already implemented by default) methods in
`StaticSequence<Item>` **and** in `Container<Item>`. The Java compiler will
Expand Down
Loading

0 comments on commit 4e433d3

Please sign in to comment.