Before You Begin
If you’d like to use your own computer, follow the setup procedure.
If you’d like to use the lab computers or the lab printers, request an EECS Instructional Account. Most students prefer to use their own laptops though, so instructional accounts will not be required for this course.
- Familiarize yourself with our course policies.
- Create a repository in our GitHub Classroom. This will require creating a GitHub account if you do not already have one. Once you have your
su18
repository, complete the repository registration form. Don’t follow the prompts on GitHub to make aREADME
file or clone the repository; we’ll do it our own way later in the lab. - Make sure you’re enrolled on Gradescope. If you’re not enrolled, ask a lab assistant and they can add you to the roster.
Be aware that there are a large number of setup steps this first week. In this class, we require students to use real-world tools, and that means that you’ll often run into real-world problems with configuration and setup. Don’t be discouraged, and make sure to ask for help if you’re stuck! The best place to ask for help is in the actual lab.
If ever something isn’t working, or a screen that should show up isn’t showing up, ask for help from another student or lab assistant sooner rather than later!
Find A Partner
Introduce yourself to a partner—someone you don’t already know—to collaborate with you to work on today’s lab activities. If you are unfamiliar with using a terminal, try to find a partner that has had some experience with one.
Try to find someone who uses the same operating system as you so it’s easier to work through the setup portion of the lab.
Your lab section leader will have a few suggested activities.
Learning by Doing
In this class, you will get to know the names of at least 80% of the students in your lab. You’ll be doing a lot of collaboration this semester, some online and some offline. Labs will generally follow this format:
- Quiz, covering the last lab’s concepts. Quizzes provide feedback on your mastery of the material thusfar.
- Text, interactive demos, and videos, to introduce the day’s new material. Discussions in small groups will also be interwoven into the introduction of the new material. Today’s lab also includes a lab checkoff, as well as an online assessment but not all labs will have these activities.
- Coding exercises, which ask you to complete a certain task using Java.
All of the activities we do in lab are meant to be constructive as reflected in the course policies. Your grade for the lab will usually be determined online through Gradescope: today’s activities, for example, consist of one part code submission, and one part online assessment.
The lab staff will also award Effort, Participation, and Altruism points based on your engagement in lab and other class contexts.
Programming in Java
Java 10 is currently installed on the instructional machines. But if you’re using your own computer, you’ll need to follow the setup instructions.
Hello, world!
Read Chapter 1.1 of the H61B textbook and complete Exercise 1.1.1 and Exercise 1.1.2 which are repeated below for your convenience.
Let’s look at our first Java program!
For this lab, you should use the command line, not an IDE like IntelliJ. Don’t worry about submitting your work yet, we’ll cover how to do that later in this lab.
Exercise 1.1.1
You might have already done this as part of setting up your own computer.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Using a text editor like Atom, Sublime Text, vim, or whatever you like, create a file on your computer called HelloWorld.java
and copy and paste the exact program from above.
Try out the javac HelloWorld.java
command. It’ll look like nothing happened. However, if you look in the directory, you’ll see that a new file named HelloWorld.class
was created. We’ll discuss what this is in a moment.
Now try entering the command java HelloWorld
. You should see “Hello World!” printed in your terminal.
Note that you do not type
java HelloWorld.class
. That is a common mistake that will cause an error message like this:Error: Could not find or load main class
Just for fun: Try opening up HelloWorld.class
using a text editor like Atom, Sublime, vim, or whatever you like. You’ll see lots of crazy garbage that only a Java interpreter could love. This is Java bytecode, which we won’t discuss in our course.
Exercise 1.1.2
The program below will print out the integers from 0 through 9.
Copy and paste the above program into a file called HelloNumbers.java
. Modify HelloNumbers
so that it prints out the cumulative sum of the integers from 0 to 9. For example, your output should start with 0 1 3 6 10… and should end with 45.
Also, if you’ve got an aesthetic itch, modify the program so that it prints out a new line at the end.
As you modify the program, write down the bugs you encountered during the programming process, and the bugs narrowly avoided. We’ll be revisiting this exercise in a later part of the lab.
Git
In this course, you’ll be required to use the git version control system, which is wildly popular out in the real world. Unfortunately, the abstractions behind it are fairly tricky to understand, so it is likely that you will encounter significant frustration at some point as you learn to use git.
Towards the middle of the semester, we’ll be learning the inner workings of Git in much greater detail but, for now, let’s just get a working knowledge of how to use git.
Before you proceed, read sections up to the Remote Repositories section of the Using Git Guide.
Do not proceed until you have read sections up to the Remote Repositories section of the Using Git Guide. You do not need to read past that.
Git Exercise
Now that you’ve read the first 3 sections of the Using Git Guide, you’re now ready to start using git! As part of your lab checkoff, you will be working through a small git workflow by setting up a git repository and making a couple commits to the repository. A lab assistant will look at your git repository during checkoff to ensure that it is in a good state.
If you need help with creating directories, creating files, changing directories, etc., first Learn to Use the Terminal in the setup lab.
- Create a directory called
lab01-checkoff
. You can put this directory anywhere on your computer. - Move into the
lab01-checkoff
directory, and initialize a git repository in this directory. - Create a file called
61b.txt
in any way you’d like. In this text file, add the text “61b version 1” into it. - Create another file called
61bl.txt
in any way you’d like. In this text file, add the text “61bl version 1” into it. -
Begin tracking only
61b.txt
, and create a new commit containing just this file, with the following commit message: “Add 61b.txt”.If Git prompts you to update your username and email, now is a good time to change your git name and email.
- Make a modification in
61b.txt
by changing the text in the file to: “61b changed to version 2”. - Make another commit, this time containing both
61b.txt
and61bl.txt
. The commit message should be: “Update 61b.txt and add 61bl.txt”. - Finally, make one more modification to
61b.txt
by changing the text in the file to: “61b changed to version 3”. Don’t commit this version.
At this point, if you were to type in git status
, something like this should show:
Also, if you were to run git log
, something like this should show:
Be sure to save this repository and directory until you get checked-off by a lab assistant.
Along with other short conceptual questions involving git, you will be asked to revert 61b.txt
back to the version in the most recent commit, as well as back to the earliest version of the file, so make sure you know how to do this!
Hint: Look into the
checkout
command. But be careful when using thecheckout
command, as your repo might end up in an unexpected state. Make sure to always specify a file (or directory) when you use checkout.Specifically, if you see something about your repository being in a detached HEAD state as a result of a checkout command, that is something we don’t want. Read the git-WTFS guide for more on what it is and how to fix it.
Git & Remote Repos
First, read the Remote Repositories section of the Using Git Guide.
In this course, you’ll be required to submit your code to your course GitHub repository. This is for several reasons:
- To spare you the incredible agony of losing your files.
- To submit your work for grading and to get results back from the autograder.
- To save you from the tremendous anguish of making unknown changes to your files that break everything.
- To ensure that we have easy access to your code so that we can help if you’re stuck.
- To dissuade you from posting your solutions on the web in a public GitHub repository. This is a major violation of course policy!
- To expose you to a realistic workflow that is common on every major project you’ll ever work on again.
- To enable safer, more equitable partner collaborations.
If you didn’t already create your GitHub account and GitHub Classroom repository as part of Before You Begin, do so now. Don’t follow the prompts on GitHub to make a README
file or clone the repository; we’ll do it our own way later in the lab.
Before beginning this section ensure that the name of your GitHub Classroom repository in the organization matches your GitHub username. If this is not true, please let a lab assistant know.
Setting up your Git Repository
Clone your GitHub Classroom git repository.
Navigate to the spot in your folders on your computer that you’d like to start your repository. In the example below, we’re assuming you want all your stuff in a folder named cs61bl
, but you can pick a different name if you’d like.
Your terminal might use a different prompt than
$
. Type out or paste only the part after the prompt.
$ cd cs61bl
Enter the following command to clone your GitHub repo. Make sure to replace the **
with your GitHub username.
$ git clone https://github.com/Berkeley-CS61B-Student/su18-**.git
If you’d like to setup SSH access so you don’t need to type in your GitHub password every time you use your repository, feel free to follow the relevant instructions on GitHub instead. If you don’t know what any of this means, just use the command above.
If you use macOS and you’ve previously logged into GitHub, your computer may have remembered your username and password from before. If it’s not the same login information as you’re using now, you may need to update keychain credentials.
Move into your newly created repo! (Make sure you do this part, or the rest of the steps below will not work correctly.)
$ cd su18-**
Add the skeleton
remote repository. You will pull from this remote repository to get starter code for assignments. (Make sure that you are within the newly created repository folder when the continue with these commands.)
Enter the following command to add the skeleton
remote.
$ git remote add skeleton https://github.com/cs61bl/skeleton-su18.git
Listing the remotes should now show both the origin
and skeleton
remotes.
$ git remote -v
If you get an error that says “Not a git repository”, make sure you’re in the su18-**
directory.
Change your Git name and email
Everytime a commit is made, your name and email address is recorded as part of the commit. Change your Git name and email by running the following commands, substituting your own name and email address where appropriate.
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
If you were prompted to do this after making a commit (as part of the Git Exercise), make sure to amend the commit to update the commit’s username and email.
$ git commit --amend --reset-author
Change your Git text editor
We’ll also change the text editor associated with Git. Sometimes, Git needs your help when inputting things like commit messages, so it will open a text editor for you. We recommend using Atom or Sublime Text, though you’re welcome to use your preferred text editor. Instructions differ based on your platform.
Working on the Skeleton
You must now pull from the skeleton
remote in order to get the starter code for lab 1. You will also do this when new projects and assignments are released. To do this, use the spookiest command in the whole git toolbox.
$ git pull skeleton master
What this does is grab all remote files from the repo named skeleton
(which is located at https://github.com/cs61bl/skeleton-su18.git
) and copies them into your current folder.
If you get an error similar to “fatal: refusing to merge unrelated histories”, you probably ran GitHub’s suggested commands when you created your repository. To fix this, for this time only, run:
$ git pull --rebase --allow-unrelated-histories skeleton master
If you list the files in your current directory, you’ll see that there are now two folders: lab01
and library-su18
. Look in the lab01
folder and you’ll see files called LeapYear.java
and magic_word.txt
that you’ll work with in later parts of this lab.
Move the HelloWorld.java
and HelloNumbers.java
that you previously created into the lab01
directory. If you didn’t create HelloNumbers.java
, go back and complete Exercise 1.1.2.
Stage and commit HelloWorld.java
and HelloNumbers.java
.
$ git add lab01/HelloWorld.java lab01/HelloNumbers.java
$ git commit -m "Completed first part of lab01"
Push these changes to the master
branch on the origin
remote repo.
$ git push origin master
You can verify that this was successful by checking your GitHub Classroom repository online.
Checkoff: Git Exercise
Once you’ve verified that your code was pushed to GitHub, raise your hand to get your git exercise checked off.
The lab assistant, upon completion of the lab checkoff, will tell you what to put into the magic word file in order to pass the autograder. If there’s a wait, feel free to move on until your name is called.
Leap Year
In the lab01
folder, you should see a file called LeapYear.java
. This program is supposed to test whether or not a given year is a leap year. The user will give a year as a command line parameter (examples given below), and then print out whether or not that year is a leap year, e.g.
$ java LeapYear 2000
2000 is a leap year.
$ java LeapYear 1999
1999 is not a leap year.
$ java LeapYear 2004
2004 is a leap year.
$ java LeapYear 2100
2100 is not a leap year.
A leap year is either:
- divisible by 400 or
- divisible by 4 and not by 100.
For example, 2000 and 2004 are leap years. 1900, 2003, and 2100 are not leap years.
Your code must declare a method as follows:
/**
* Update this comment to describe what this method does.
* @source CS 61BL Lab 1
*/
public static boolean isLeapYear(int year)
As with the previous coding exercise, make sure to write down any bugs you encounter while writing this program! We’ll be revisiting this shortly.
This method will be tested by the Gradescope autograder.
Once you have a working implementation, remember to add, commit, and push your changes to GitHub.
git add LeapYear.java
git commit -m "lab01: Completed LeapYear.java"
git push origin master
Style
In this course, we’ll work hard to try to keep our code readable. Some of the most important features of good coding style are:
- Consistent style
- Spacing, variable naming, brace style, etc.
- Size
- Lines that are not too wide, source files that are not too long.
- Descriptive naming
- As applied to variables, functions, classes. We prefer meaningful names like
year
orgetUserName
instead ofx
orf
. - Avoidance of repetitive code
- Strive to never have two significant blocks of code that are nearly identical except for a few changes. We’ll teach you a number of large-scale design and small-scale style techniques for working around these situations.
- Comments where appropriate
- Line comments in Java use the
//
delimiter. Block (or multi-line comments) comments use/*
and*/
. Make sure to update the Javadoc comment (“Update this comment…”) and provide a short description of the method’s behavior.
In addition, if you used any external resources, please cite it in the Javadoc comment using the @source
tag like in the example above. It’s not necessary to cite anything from the course staff, but you should cite any other resources like StackOverflow by pasting in a link.
We have a more formal style guide available for you to read at your own leisure, but the golden rule is this: Write your code so that it is easy for a stranger to understand.
Java Tips
- The
&&
operator computes the booleanand
of two expressions, while||
computes booleanor
. - The
%
operator implements remainder. Thus, the value ofyear % 4
will be0
,1
,2
, or3
. - The
!=
operator compares two values for inequality. The code fragmentif (year % 4 != 0)
reads as “if the remainder when dividing year by 4 is not equal to 0.” - When one of the arguments of the
+
operator is a String, the arguments are concatenated as Strings. For example,2 + "horse"
+"battery"
would return"2horsebattery"
. This is different from Python which doesn’t perform this automatic conversion.
Discussion: Compilation Errors
Throughout this lab, you may have run into some errors like the one below after running javac LeapYear.java
.
LeapYear.java:3: error: incompatible types: int cannot be converted to boolean
if (year) {
1 error
This is a compilation error because the compiler, javac
, is able to identify (‘catch’) the problem before we even run the code with java
!
Refer to your notes on Exercise 1.1.2 and Leap Year and discuss these questions with your table group.
- What kinds of bugs is the compiler able to catch? What do you suspect is going on behind the scenes?
- What kinds of bugs is it unable to catch?
- And how is this similar or different to other languages you may have used before, like Python, Scheme or MATLAB?
Submission
The last step is to submit your work with Gradescope. Gradescope is the site that you’ll use to submit lab and project assignments.
We added everyone’s CalCentral email to Gradescope on the first day of labs. Make sure to login using the email address listed on CalCentral.
If you’re having trouble accessing your Gradescope account or would like to use a different email address, ask a lab assistant! You might need to create a new Gradescope account under your CalCentral email, reset the password, or refresh the page a couple times before it will work.
Select the Lab 1: Java, Git assignment on Gradescope. Choose your su18-**
repository and use the master
branch.
Please report any issues you may have to Piazza. Entire error messages and/or screenshots are welcome.
We strongly encourage you to make frequent commits! Lack of proper version control will not be considered an excuse for lost work, particularly after the first few weeks.
Partner Submission
Most assignments will let you submit as a partnership. For each assignment, you can add group members to the submission.
Online Assessment
In addition, this lab also requires submission of an online assessment which can be found on your Gradescope dashboard. All the parts required for lab submission can either be found on the Gradescope dashboard, or clearly noted at the bottom of each lab.
You can submit the graded exercises and online assessment as many times as you want before the deadline so you don’t need to get the answers right on the first try. The assessment is meant to be a learning tool to check your understanding of the lab material. Collaborate with your lab partner, table group, and the lab staff: they’re all here to help!
Successful completion of all parts for this lab should result in a sum of 3 points. Congratulations on finishing your first CS 61BL lab!
Recap
Java is a compiled language. You can use javac
and java
to compile and run your code. Our first programs reveal several important syntax features of Java:
- All code lives inside a class.
- The code that is executed is inside a function, a.k.a. method, called
main
. - Curly braces are used to denote the beginning and end of a section of code, e.g. a class or method declaration.
- Statements end with semi-colons.
- Variables have declared types, also called their “static type”.
- Variables must be declared before use.
- Functions must have a return type. If a function does not return anything, we use void,
- The compiler ensures type consistency. If types are inconsistent, the program will not compile.
Java is also statically-typed. Static typing gives us a number of important advantages over languages without static typing:
- Types are checked before the program is even run, allowing developers to catch type errors with ease.
- If you write a program and distribute the compiled version, it is (mostly) guaranteed to be free of any type errors. This makes your code more reliable.
- Every variable, parameter, and function has a declared type, making it easier for a programmer to understand and reason about code.
There are downside of static typing, to be discussed later.
We also discussed briefly how to comment code and coding style. Coding style is very important in this course and in the real world. Code should be appropriately commented as described in the textbook and lectures. We haven’t learned what good style is yet, nor when to use comments.
Git is a version control system that tracks the history of a set of files in the form of commits. Commit often and use informative commit messages so it’s easy for the future-you to find what the change you’re looking for. Pull from the skeleton
remote repository to get or update starter code for assignments and use Gradescope to submit labs and projects.
If you haven’t had a chance to work on the optional Java introduction yet, now’s a good time!
Deliverables
In addition to the online assessment (“Lab 1: Assessment” on Gradescope), there are two required coding exercises.
HelloNumbers.java
LeapYear.java