Skip to content

Commit

Permalink
initial instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcal13 committed Sep 17, 2023
0 parents commit 0c57855
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Lab 2: Branching and Merging

The main topic of this first lab activity is branching and merging in git.

## Resources

https://www.atlassian.com/git/tutorials/using-branches contains an excellent, detailed
walkthrough of the steps we will be going through in the lab. We encourage you to use
it as a reference as needed. The short video (about 4 minutes) is well worth a quick view
in the link above.

The first coding part in this activity is (very loosely) based on a common problem encountered in
problems posted on https://projecteuler.net/archives which is another interesting source of challenging
programming problems. You might also recognize that the multiples of 3 and 5 problem from
last week came from there. If you are looking for challenges, definitely check it out.

# TASK 0: Fork this repo

Note: If you are in lab, your TA will share a different URL for you to fork from, so that you
can make pull requests to that repo during the Task 3 activity during lab.

During lab, you should not fork directly from
TODO REPO URL. If you miss the lab and work on this after, please fork TODO REPO URL.

- [ ] Make a fork of this repo and clone a local copy (as you did in Lab 1).
- Important: make sure to uncheck the option to only fork the main branch, as the repo
contains some branches you will use in this lab.

# TASK 1: Your first branch

- [ ] Create and checkout a new branch called `task_1` using either IntelliJ or the Terminal:
- IntelliJ: `Git -> New branch...`
- Terminal: `git checkout -b task_1`
- After, you can check `git status` or the Log tab of the Git tool window in IntelliJ to see
that you are now on the `task_1` branch.
- [ ] Open the TODO tool window (`View -> Tool Windows -> TODO`) and click on the TASK 1 TODO listed.
- [ ] Complete the TASK 1 TODO and commit your changes to this file (checking off the
completed items so far) and `DataTypes.java` (remove the word TODO and your bug fix).
- talk to those around you or your TA, then see the hints at the bottom of the readme if you get stuck.
- [ ] Now, we'll merge the `task_1` branch back into `main`. When merging,
you need to be currently on the branch you are trying to merge into, so we'll first checkout the main
branch:
- IntelliJ: `Git -> branches... -> main -> Checkout`
- Terminal: `git checkout main`
Note: everything we've done has been local to our repository and have not pushed anything yet.
- [ ] We are back on `main`, so we can now do the merge and complete our work!
- IntelliJ: `Git -> Merge... -> task_1 -> Merge`
- Terminal: `git merge task_1`

You should now see the changes you had made are also in the `main` branch.

- [ ] Now, we'll want to clean up since we are done with our `task_1` branch.
- IntelliJ: `Git -> branches... -> task_1 -> Delete`
- Terminal: `git branch -d task_1`

- [ ] Last step, we'll push our changes to the remote repository to share our work! (As we did in Lab 1.)
- we suggest you check off this last item, commit that change (just right on main is fine;
no need to branch for this little step), then push your code. Check GitHub to ensure you can
see your changes.

And that's it for Task 1! You might be wondering about how we are supposed to get someone else
to review and approve our changes before we commit and push our changes to the main branch of
our remote repository, since everything we just did was local. We'll explore how to do precisely
that by pushing our branch to our remote repository and making a pull request shortly, but first,
we'll talk about merge conflicts and how to resolve them.

Tip: In the Git tool window, you can open the `Console` tab to see the underlying git commands it
is performing when you ask IntelliJ to perform various git operations for you.

# TASK 2: Let's bake a cake!

Alice and Bob are planning to bake a cake, but can't agree on which recipe to use. They had
started working on the recipe in `recipe.md` together, and then each filled in the details of
what they felt would make the most delicious cake!
You'll notice that your repository already has two branches called `alice` and `bob`.

- [ ] Checkout the `alice` branch.
- [ ] Attempt to merge the `bob` branch into the `alice` branch using either IntelliJ or the Terminal.
- You will be prompted to resolve a merge conflict. To do this, you will need to pick and choose which
parts of each recipe to keep.
- Read what either `git` or `IntelliJ` tells you in order to complete the merge process.
- If you do the merge through the Terminal, you will need to edit `recipe.md` to remove all of the
merge conflict symbols which `git` has added to your file. Once done, you will need to `git add` the
`recipe.md` file and `git commit` to finish the merge.
- [ ] Once the merge is complete, delete the `bob` branch.

- [ ] Finally, checkout the `main` branch and merge the `alice` branch in (as we did previously).

Now, you are almost ready to share your recipe with the remote!

# TASK 3

- [ ] Checkout a new branch called `task_3`.
- [ ] Commit any changes that you want to `recipe.md` to improve the recipe.
- [ ] While still on the `task_3` branch, push your code to your remote repository on GitHub.
- [ ] Go to GitHub and you will see an option to make a pull request to the original repo. Make
a pull request and see that it shows up in the original repository that you forked.

Since others will also be making pull requests, we won't *actually* accept any of them for now, but
you'll get lots of practice with pull requests in the next task and throughout the rest of the course!

We're now ready to dive into a collaborative coding challenge.

Proceed to the second lab activity!

## Hints and Tips

### TASK 1 TODO: Hints
- Hint 1: you only have to change one line of code
- Hint 2: unlike Python, an int has a maximum value that it can store (see Integer.MAX_VALUE).
also see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

### TASK 2 (and possibly other places): Note about Unsaved local changes

If you have any local changes which you haven't committed, git will refuse to do certain operations
which would result in the loss of your changes. It will advise you on what you should do in order to
proceed. If you have any changes you want to keep, you will typically want to commit those changes.

Note: you can use `git status` or `Git -> commit...` to see the status of your files to check which
ones have changes not yet committed.

### TASK 3 Additional Note

When making a pull request, there may potentially be merge conflicts to resolve, as we had seen in the
previous task. GitHub as additional information about how its interface helps facilitate resolving such
conflicts:

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-on-github
12 changes: 12 additions & 0 deletions recipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Delicious Chocolate Cake Recipe

## Ingredients:
- 2 cups all-purpose flour
- 1 3/4 cups granulated sugar
- 3/4 cup unsweetened cocoa powder
- ...

## Instructions:
1. Preheat the oven to 350°F (175°C).
2. In a large bowl, whisk together the flour, sugar, and cocoa powder.
3. ...
14 changes: 14 additions & 0 deletions src/DataTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.List;

public class DataTypes {
// TODO TASK 1: fix this code so that it passes the test in DataTypesTest.java
public static long sum(List<Integer> numbers) {

int s = 0;
// below is a "foreach" loop which iterates through numbers
for (int x : numbers) {
s += x;
}
return s;
}
}
29 changes: 29 additions & 0 deletions test/DataTypesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class DataTypesTest {

@Test(timeout = 1000)
/**
* Test that DataTypes.sum returns the correct value for
* the sum from 1 to 1 million.
*/
public void largeSumTest() {

// You put an L at the end to indicate it is a long.
// try removing the L and you'll see that you get an error
// saying "Integer number too large".
// Note: You can use _ to help make it easier to
// read large numbers, as is done below.
long x = 500_000_500_000L;
List<Integer> lst = new ArrayList<>();
for (int i = 1; i <= 1_000_000; i++) {
lst.add(i);
}
assertEquals("sum form 1 to 1 million should be " + x, x, DataTypes.sum(lst));
}
}

0 comments on commit 0c57855

Please sign in to comment.