890 views
owned this note
<style>
h3 {
border-bottom: 1px solid #ccc;
}
section {
margin-bottom: 2rem;
padding: 0em 1em;
padding-bottom: 1em;
border-radius: 4px;
background-color: #f7f7f7;
border: 1px solid #ccc;
}
summary {
font-weight: bolder;
}
summary:hover {
text-decoration: underline;
}
.todo {
color: #ff00ff;
border: 2px dashed #ff00ff;
padding: 0em 1em;
border-radius: 5px;
//display: none; // UNCOMMENT TO HIDE TODOs
}
</style>
# Lab 1 (CS15): Working with lists (Spring 2025)
**The goals of this lab are to:**
- help you design lightweight data using records in Java
- practice using map and filter in Java
- get more practice with writing recursive examples
## Getting Starter Files with GitHub
You can clone the starter files for this lab at this **[GitHub Classroom link](https://classroom.github.com/a/qwradR8Z)**.
**[Look here for super-important instructions](https://docs.cs200.io/s/java-stencil-setup-guide#Accepting-an-Assignment-on-GitHub-Classroom)** on how to accept the assignment and open the code in IntelliJ.
<!-- Refer to the [GitHub guide](https://docs.cs200.io/s/github-guide) if you are unfamiliar with it. Make sure you push your code regularly to back it up! -->
## Starting notes
- As you read through the code, you may notice that there are several Javadoc comments (starting with `/**`) marked with `TODO` that ask you to fill stuff in. These are for the end of the lab, when we'll learn about how Javadoc can help document our code--**please ignore these TODOs until the tasks direct you to fill them in**.
- Toward the end of this lab is a [special tutorial on using **git**](#Task-11-Pushing-your-code-to-git), which is an essential software tool we'll use to submit assignments (and which will be useful for any real-life CS work). **When you have ~15min left in the lab, skip ahead and do this part if you haven't reached it already**--it's important you get practice with git, and we want you to do this in lab while TAs are around to answer questions!
## Files!
It’s the start of a new semester and you’ve sworn to get your computer files organized.
### Task 1:
Create files in the `sol` directory called `File.java` and `Folder.java` and write `Record` definitions that can help you represent a filesystem (How did we do Records in Lecture 2?)
- Folders in the filesystem should have a name and can hold an arbitrary number of sub-folders, and an arbitrary number of files (each subfolder behaves like a folder).
::: info
Hint: what data structures can hold an arbitrary number of elements?
:::
- Files in the filesystem should have a name (without the extension), a size (in MB), and an extension (such as .txt).
### Task 2:
You will need to create a new class called `Lab01.java` and create the main method. In the `main` method, use your record definition(s) from above to instantiate data that represents the following file structure (📁 represents a folder, 📄represents a file). _Hint: it might help to make intermediate variables that you can use when constructing higher-level directories. You can also use_ `toFuncList` _like we did in class to quickly initialize a new List._
📁 user
📁 courses
📁 AFRI 100
📁 CS 200
📁 Homework
📄 HW1a.java (0.2 MB)
📄 HW1aTest.java (0.1 MB)
📁 Lecture notes
📄 boa.java (0.1 MB)
📁 fun
📄 minecraft.exe (238 MB)
📄 TODO.txt (0.05 MB)
📄 pals.jpg (2.3 MB)
:::success
**Checkpoint:** Call over a TA to check your work.
:::
### Task 3:
Now, in the main method of Lab01.java, create a list using `FuncList` (_just_ a list, not a folder) that contains the following files:
- HW2.java (0.3 MB)
- mercury.png (1.9 MB)
- venus.png (1.7 MB)
- rocketman.mp3 (3.1 MB)
### Task 4:
In Lab01.java, use `filter` or `map` to write a static method called `getImages` that takes in a list of files and returns a list of the files that are images (have extension png, gif, or jpg).
### Task 5:
In Lab01.java, write a static method called `addFileSize` that takes in a list of files and returns a double that represents the sum total of the sizes of the files in the list.
:::info
**_Useful built-ins:_** `[List].sum()` : a way of getting the sum of a numeric list using `FuncList`s. This summing method can only be used on FuncLists of numbers (ints or doubles) and can be found in the `FuncList` class in the `src` folder of your stencil code.
:::
### Task 6:
In Lab01.java, use `filter` or `map` to write a static method called `getLargeNames` that takes in a list of files and returns a list of just the _names_ (without the extension) of files that are at least 1MB in size.
### Task 7:
in Lab01.java, use `filter` or `map` to write a static method called `convertMusic` that takes in a list of files and returns a list of the same files, but with any .mp3 extensions changed to .wav.
_Hint: it will be simplest to create a helper method and call it in a predicate._
### Task 8:
In the main method of Lab01.java, print out the result of calling each of the methods in tasks 4-7 on the list you created in Task 3.
:::success
**Checkpoint:** Call over a TA to check your work.
:::
## Recursion Practice
In lecture, we learned the practice of writing a sequence of related examples. This can be an extremely useful practice for figuring out how to write a program that builds up an answer while traversing a data structure (whether by iteration or recursion). Let’s practice that skill.
Example from class if you need a reminder:
```java
@Test
public void testDoubling() {
// assume we have a static method in Lec03.java called "double" which takes
// in a FuncList of integers and returns a new FuncList with each
// item in the input list doubled
Assert.assertEquals(toFuncList(0, -6, 8, 16), Lec03.double(toFuncList(0, -3, 4, 8)));
// toFuncList(-3, 4, 8).link(0)
// toFuncList(-6, 8, 16).link(0) = Lec03.double(toFuncList(-3, 4, 8))).link(0)
// Lec03.double(input.rest()).link(input.first() * 2)
Assert.assertEquals(toFuncList( -6, 8, 16), Lec03.double( toFuncList(-3, 4, 8)));
// toFuncList(4, 8).link(-3)
// toFuncList(8, 16).link(-6) = Lec03.double(toFuncList(4, 8).link(-6)
Assert.assertEquals(toFuncList( 8, 16), Lec03.double( toFuncList(4, 8)));
// toFuncList(8).link(4)
// toFuncList(16).link(8) = Lec03.double(toFuncList(8).link(4 * 2)
Assert.assertEquals(toFuncList( 16), Lec03.double( toFuncList(8)));
// toFuncList().link(8)
// toFuncList().link(16) = Lec03.double(toFuncList()).link(8 * 2)
Assert.assertEquals(toFuncList( ), Lec03.double(toFuncList()));
}
```
:::info
**_NOTE:_** For tasks 9-10, you are only writing JUnit tests, you are NOT writing the methods. Work in a file called Lab01Test.java, and create a new test method for each task. Remember to put the @Test annotation above each test method.
:::
### Task 9:
Develop a sequence of related examples for a static method called `concatAll` in the `Lab01` class. You are not implementing this method just yet.
This method should take in a `FuncList` (this is the class in the src package) of strings and return the string resulting from concatenating (gluing) all of those strings together (e.g., `Lab01.concatAll(toFuncList("a", "b"))` should return "ab").
### Task 10:
Develop a sequence of related examples for a static method called `below5` that takes a `FuncList` of Integers and returns a list of those numbers that are smaller than 5.
:::success
**Checkpoint:** Call over a TA to check your work.
:::
### Task 11: Pushing your code to git
:::warning
**Note**: This task teaches you how to push your code to git, an important skill that you'll need for **every future assignment in this class, and every other CS course you may take**.
Even if you don't finish the rest of the lab, try this task before the end so that you can practice with git while a TA is present.
:::
When you started this lab, you cloned your code from a git repository: a git repo is a place to store your code and record a *history* of your changes. This *history* is important when you're working on a software project for a long period of time (so you can revert to old versions), or collaborating with others (for syncing updates between people). Git is a near-*universal* tool for software development (and we'll use it a lot in this class!), so in this lab we'll demonstrate some git operations to give you some practice.
To try using git, we're going to *commit* your current changes to the git history, then *push* them to your repository on GitHub so they're stored online. To do this:
#### Step 1: Your terminal
1. In IntelliJ, **open a terminal** by clicking on the terminal icon in the lower-left corner (see the figure for a reference)

2. Take a look at your terminal's *prompt*: it should give the name of a directory (ie, a folder on your computer), like the figure below. **This is the terminal's *working directory***: which acts like a "reference point" when you run terminal commands. Before continuing, **check that your terminal's working directory points to the same directory as your stencil** (eg. something ending with `lab01-15-<yourusername>`). You'll see why this is important in the next step.

Now we're ready to start using git!
### Step 2: Seeing what changed
Before you commit your code (and potentially send it to others), it's a good idea to review your work to see what will go into the history. To do this:
3. In your terminal (which we'd now say is "located in your stencil directory"), run the following command:
```shell
git status
```
`git status` asks git to show you what files changed since the last *commit* (or, the latest version git knew about, which was when you cloned the repository). After working on the lab, you should see one or more files show up as "`Changes not staged for commit`" (for files that were in the stencil, but you changed) or "`Untracked files`" (for new files you created), like this:

:::info
**Note**: If you can't see all the output in your terminal, try making IntelliJ's terminal window a bit larger.
:::
A few things to note here:
- The exact number of files will depend on how far you got in the lab--as long as one file shows as changed, you can continue.
- Look at the list of files: the list actually shows *paths* to files, relative to your working directory (ie, your stencil folder). For example `sol/File.java` refers to `<stencil dir>/sol/File.java`. This is why the working directory was important! You'll see this idea of *relative* file paths come up a lot in later assignments.
4. Because software projects can have a lot of files, we need to tell git which files you need to commit. This is called **staging your changes**. You can stage your changes using the command `git add`, like this (fill in each file that was changed):
```shell
## Idea: git add <file1> <file2> ...
git add sol/Lab01.java sol/File.java <... other new/changed files ...>
```
5. After you run `git add`, you shouldn't see any output in your terminal (with many terminal commands, "no news is good news"). **To see what changed, run `git status` again**, which should look like this:

`git status` should now show your changes as **staged**: this means they're ready to be committed. If you continue changing your files after this, you'll need to stage/commit them again.
6. Now it's time to commit! (You got this!) To commit your changes, do the following:
```
## Idea: git commit -m "<some message about what you did>"
git commit -m "finished lab 1"
```
Committing your work **takes your staged changes and adds them to the history**: a git repository is actually a list of these changes! After committing, you should see a message summarizing the changes, like this:

7. **Almost there**: now that you've committed your changes, you need to send them to your repository on Github: this is called **pushing your changes**. To push your code, run the following:
```
git push origin main
```
This tells git to send your changes to the location where you cloned the repository (called `origin` by default) on the `main` branch. The details of these aren't important now, but if all goes well you should see output like the following, denoting that the push was successful:

Yay, your changes are now on Github!
8. To see your changes online, **open up your stencil repository on Github** (if you can't remember the link, go here and look in the "Repositories" view). You should see the viewer show your latest commit message (like the figure), and if you browse around you should see your latest files!

Now that you can see your files on Github, you're done! Congrats on learning your first steps with git! :sunglasses:
:::success
**Git tips for the future**
Now that you've had some practice with git's core features, there's some shortcuts you should know to make your life easier:
- To quickly add all files at once without typing the names, run: `git add -A`
- The output of `git status` is a good quick reference on how to add/remove files from the set of staged changes--if you need help on this, start there!
For more info on git, see our [Github Guide](https://docs.cs200.io/s/github-guide)!
:::
## Additional tasks
Work on the following tasks as time permits in lab (you may not get this far--**that's okay**)
### Task 11:
Implement `concatAll` (for which you developed examples) as a static method in Lab01.java . Run your examples.
### Task 12:
Implement `below5` (for which you developed examples) as a static method in Lab01.java . Run your examples.
### Task 13a:
Develop a sequence of related examples for a static method called `countFiles` (which will live in the Lab01 class) that takes a folder (like in tasks 1-2) and returns the number of files within the entire filesystem. If you have time, work on implementing this function. Think about how to implement it both with plain recursion and by using `map` and `sum` as helpers.
### Task 13b: (Challenge)
Develop a sequence of related examples for a static method called `allFilenames` (which will live in the Lab01 class) that takes a folder (like in tasks 1/2) and returns the list of names of all files in the entire filesystem (duplicates are fine). If you have time, work on implementing this method. You should use a combination of map and recursion for this task.
Hint: think about writing a recursive helper function that concatenates a list of lists together into one list. You can concatenate just two lists together using
`lst1.append(lst2)`.
***
<!-- TODO: update link -->
_Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS0200 document by filling out the_ [_anonymous feedback form_](https://docs.google.com/forms/d/e/1FAIpQLSfe9cDwpzv7xCbbevbrAZacuB4MB8yxer8jiK0SK-CQH3RXuQ/viewform?usp=sharing)_._