Homework 0: Being a Good Classmate and A Few Java Exercises

A. Preliminaries

If you fetched the skeleton code before January 19th at 11:14 PM, please refetch! We noticed a bug in the testing skeleton. You can do this with the following steps:

  git commit -a -m "initial work on hw0 before skeleton fix" // not necessary if you haven't already written code"
  git fetch shared
  git merge -m "merging in updates to skeleton"

If a merge conflict results because you edited parts of the skeleton that have been changed, don't panic! You can post on Piazza for help, or bring your computer to a lab or office hours once they are available and a TA will help you. Do NOT try to delete everything and start over.

There is a walkthrough video for this assignment. We recommend reading the whole spec before watching it.

Goals

There are two subparts to this homework: a Gradescope assignment and a few coding exercises. This homework cannot be dropped. Respectful communication is not optional and we want to make sure everyone does this assignment.

  1. The Gradescope assignment will prime you for the rest of this class, and help establish one of the key principles of being a software engineer: how to communicate. The Gradescope assignment is available and titled "Homework 0", and it repeats all of the content in the spec under section B "Respectful Communication" for your convenience. This assignment is worth 2 points and will be graded based on your honest effort and if you have demonstrated understanding of the material.
  2. The coding exercises here are ungraded and aim to introduce you to a few basic points of Java syntax:

In total, the homework assignment is worth 2 points.

For each of the coding problems,

B. Respectful Communication

Motivation

In CS 61B and throughout your time at Berkeley, you will learn many skills that will make you a good programmer. CS 61B is often regarded as the first class at Berkeley that really teaches about software engineering. When you hear this, you probably think this is entirely referring to the fact that this class is known for its large projects with little scaffolding. However, software engineering is a lot more than just churning out code like a machine–a real software engineer is typically someone who works with others, faces deadlines and stress, and has to manage it while maintaining respectful communication. Thus, in this class we consider it our duty to also teach effective communication, so that when you approach real world projects you can convey your ideas to colleagues. Even if you have excellent ideas and can design complex algorithms, a lack of ability to relay them to others will hinder your professional development. As such, this portion of the homework exists to get you thinking on how to interact with a wide array of individuals.

Respectful collaboration is especially vital when working with those generally underrepresented in computer science. For example, only 20-30% of Berkeley EECS/CS students use she/her/hers pronouns. Fewer than 1% of EECS/CS students identify as Black. As a result, it can feel daunting to members of these and other underrepresented communities to attend our classes. Often, when we think of discrimmination towards these groups we think of extreme acts such as sexual harassment or explicit hate speech. However, the small, frequent instances of negative remarks make an equally big impact. While the perpetrators may only participate in these situations rarely, the imbalance between the raw number of potential offenders and minority students means minority students face these situations constantly.

This assignment was created for an important reason - underrepresented groups continue to face discrimination actively throughout computing, including when taking CS 61B. Examples from the TAs’ experiences include being excluded from groups during lab, being sexually harassed by other students during class, and being told they only got opportunities due to their identity. It is ok to make mistakes; in reality, most people have misstepped in a social situation at some point or another. What matters is how you handle these uncomfortable situations once they occur. We ask you to genuinely read this homework and consider your actions so that CS 61B can be as welcoming and inclusive as possible. We understand that all students will have varying levels of familiarity with this material. If you are lost, we encourage you to do more research (we have more links in the FAQ). If you feel that the assignment is too elementary, we apologize but hope you can appreciate that our goal is to make sure everyone has the same baseline understanding.

Some statistics that help paint a picture of the impact of disrespectful communication:

What Does Disrespectful Communication Look Like?

Discrimination comes in many forms, both big and small. Certain statements may disproportionately affect some people more than others. Everyone has aspects of their history that they are sensitive about and in order to make sure everyone is comfortable, students should strive to avoid these sensitive topics. Because of this, we encourage students to stay professional in the classroom setting and never assume someone else’s comfort level with inappropriate humor.

Additionally, we often joke around with friends in a way that is not appropriate for professional settings because we know our friends and can give them the benefit of the doubt. While someone may be comfortable with their friends making insensitive jokes or comments, they may find it uncomfortable when told by someone they aren’t familiar with.

Think this information isn't really relevant? One of our TAs can provide an example of a time someone else's humor made her uncomfortable: this TA was the only non-cisgender male in a group chat meant for classwork, but another student sent a crass sexual video as a joke. While this TA may or may not be comfortable with crass humor in other contexts, this scenario was uncomfortable because she couldn’t easily leave the assigned group, meaning she had to either ignore her discomfort or face an awkward conversation with people she wasn’t close to.

Examples of disrespectful communication include:

How Do I Respond if I’m in an Uncomfortable Situation?

When dealing with an uncomfortable situation, you may be in one of three roles: the person who has just been hurt, the person who has hurt someone, or a bystander. Most people have been in all of these roles at one point or another - what is important once you find yourself in an uncomfortable situation you react in an appropriate and timely manner.

What to do if you’ve hurt someone:

Sometimes, things you say might come out wrong or cause harm even if you have good intentions. If you find yourself in a situation where you’ve negatively impacted someone with your words or actions, there are a few steps you can take to step back and reduce the harm caused by your misstep:

What to do if you’ve just been hurt:

If you find yourself on the receiving end of disrespectful behavior, whether intentional or not, there are a few steps you can take if you wish, depending on the situation and your level of comfort with confrontation:

What to do when you're a bystander:

You may find yourself in a conversation where one person acts disrespectfully towards another. Oftentimes, someone who has just been hurt may not speak up due to social pressure or fear of further judgement. As such, there are steps you can take to stand up for others:

Respectful Communication: Conclusion

Computer science is for everyone, regardless of background or identity. We believe that students generally have positive intentions, but there will always be situations where people say something inappropriate or make each other uncomfortable. Our goal is to minimize these inappropriate interactions and the harm they cause so that all students feel welcome while taking CS 61B and it is everyone's responsibility to ensure the class stays this way. Now that you have completed this assignment, it is the expectation that we will all uphold these standards and treat each other with kindness and respect.

Coding Assignment

Setup for Coding

Make sure you have completed lab1 before doing this coding assignment. As with lab1, start this homework by using the following commands in your local repo directory (assuming that you are using our normal setup--working on the default master branch--and that you have committed any files you you already have there from other assignments.)

git fetch shared
git merge shared/hw0 -m "Get HW0 skeleton"
git push

We have provided a mostly empty skeleton for your Solutions, and a skeleton Tester.java file for writing simple tests of your solutions.

To run the tests in Tester.java, you can do the following:

javac Tester.java Solutions.java
java Tester

java is a compiled language, meaning code first has to be compiled, or reformatted in a way that a computer can understand it. The first line above, with javac, does the work to compile the code. Then, the compiled code can be run, using the java command. This is different from how we run code in a non-compiled language like python, which you make be familiar with. Note that we have to compile Tester.java and Solutions.java together, since Tester.java references Solutions.java.
To submit, you will tag a specific commit. Details can be found in the submission section.

Do the Reading

Make sure you've done the first reading assignment: AJR 1.1 through 1.9 (found here) and Chapter 1 of Head First Java. If you don't have Head First Java, that's fine, AJR provides all the information you need (but might be a harder read).

While you're reading, you may find it helpful to be able to run Java code so that you can experiment. You can compile and execute as you have done in lab, of course. There is also a convenient online Java compilation tool. To run code, simply add the code you'd like to execute to the main method and then press the Compile and Execute buttons.

Head First Java provides you with exercises that you can do as you read. Feel free to skip doing these if you'd like.

C. Java 101

The following is a quick reference/crash course on getting started with Java. It is not a replacement for a textbook, but provided for your convenience.

Basic syntax/rules

Classes

Methods

Conditionals and loops

Arrays

D. isEven

Write a function isEven(int x) that returns true if the input x is an even number. You can assume 0 is even.

Hint: use the "modulo" operator %, which returns the division remainder. For example, 5 % 3 = 2.

E. Max

Write a function max(int[] a) that takes in a non-empty array (that is, a.length > 0) and returns the maximum value of the array. Try writing this function using both a while loop and a for loop.

To test, modify your main function to perform an ad-hoc test of your method. In this context, this means a non-generalizable test in which you feed in specific test cases as input to the function, compile, and check that the output is what is expected.

F. WordBank

Write a function wordBank(String word, String[] bank) that returns whether the given word is contained in the array bank.

To check whether two String objects are equal, you should use a.equals(b) and NOT a == b. The former checks that the two Strings have the same sequence of characters (i.e. what you'd expect). However, the latter will check that the two Strings are the exact same object. For example:

String a = "hello";
String b = "hello";
a.equals(b);         // Evaluates to true
a == b;              // Evaluates to false

G. 3SUM (More Challenging)

Suppose we have a non-empty array of integers int[] a. The 3SUM problem asks if there are three integers (not neccesarily distinct) in a whose sum is zero. You may assume that the array is not empty.

For this problem, write a function threeSum(int[] a) that returns true if there exist three integers in a that sum to zero and false otherwise. Integers may be used more than once. You can uncomment the test in Tester.java, and then compile and run the tests to see if your code works.

For loops will look a lot more compact than while loops for this problem.

Examples:

This might seem daunting at first, but it's relatively straightforward. As a hint, consider an alternative way of stating the problem: Do there exist three indices (not necessarily distinct) f, g, and h such that a[f] + a[g] + a[h] == 0?

H. FAQ

Respectful Communication FAQs:

1. If I’m dealing with these issues in my life right now, where can I go to get help or report others?

2. If I want to learn more, where can I go to read about these issues?

3. Why do we have to learn about this in a computer science class?

This is a serious issue that exists at all levels of computer science. We do not want to send you all out into the workforce post-Berkeley if you will contribute to these harmful environments. If you want to read about a real world scenario of discrimination in computer science, you can read this article. Trigger warning: Extreme sexual harassment, self harm, death.

Coding FAQs:

1. I am really lost on 3SUM? How do I start?

Consider first how you would handle 1SUM. How would you figure out if there is a specific index in the given array which is equal to 0? From there, think about how you would implement 2SUM. How would you find out if there exists two indices in the array j,k (not necessarily distinct, that is j can equal k) such that array[j] + array[k] = 0? From here, try to generalize for the number of terms you are summing over! You'll see that 3SUM, 4SUM, 5SUM, etc are all very similar and you could easily make any of them!

2. How many test cases do I have to write?

You should think through the basic cases and as many of the corner cases as you can think of. Though you are not being graded on the correctness of your code for this assignment, later assignments will be graded on correctness and require you to test your own code! It is a good idea to start practicing now.

3. I am getting a weird compiler error!! Help!

Before you post on Piazza, try googling the error you are getting. This is how you learn, Google is one of the most important tools for any person who writes software. For example, if your terminal says that you are getting an "IndexOutOfBoundsError", try typing that into Google and following the advice of a reputable website (for example, stackOverflow).

4. My main method can't find my classes!

Can't access method from main

5. I am getting issues with multiple methods of the same name!

Check out this documentation!

I. Submission

We will only be grading the Gradescope assignment. The multiple choice questions are graded automatically (you should be able to check your score on those before submission), and the free response will be graded based on honest effort. There is no autograder for the coding exercises, though you may test your code locally by writing some tests.

J. Getting Help

If you're stuck, watch the walkthrough video. I recommend only watching it after giving the homework your best effort–which is why we are not releasing the video immediately.

For future assignments we are using an infrastructure called GitBugs for interfacing with staff for debugging help. More info to come on that in the next homework. We won't be using GitBugs on this assignment. If you need help on this homework, you should ask your lab TA or visit office hours for in person help. You're also welcome to use Piazza, but please read our Piazza guidelines before that.