563 views
owned this note
<style>h3{border-bottom: 3px solid black}</style>
# Lab 1 (CS15): Working with lists
**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
<!-- TODO: change links -->
You can clone the starter files for this lab at this **[GitHub Classroom link](https://classroom.github.com/a/Hskh1VhY)**. **[Look here for 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!
## How labs work: Pair Programming
You should work on the labs using *pair programming*. This is a style of collaboration where both participants are always working on the same piece of code: one of you should be typing at the keyboard while the other should be helping to "navigate" by giving direction on what to do and watching for mistakes. You should alternate betweek roles at roughly 15 minute intervals (you can choose when to switch).
Mostly, just make sure everyone gets a chance to both type at the keyboard and be "navigator" pretty equally. For details, see our [Pair Programming Guide](https://docs.google.com/document/d/1qJBZcPxNC6X1k95YH2w8K4ZC-K1IEy9eVbk4YuzeRXc/edit?usp=sharing).
## 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.
- Folders in the filesystem should have a name and can hold an arbitrary number of sub-folders and files (each subfolder behaves like a folder).
- Files in the filesystem should have a name (without the extension), a size (in MB), and an extension (such as .txt).
### Task 2:
In the main method of `Lab01.java`, 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 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.
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.
:::
## 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://forms.gle/Kpk1Do76jF2eVSUP8)_._