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:
• Initializing variables, e.g. int x = 5;
• Comparisons and logical operators
• Conditionals (if statements)
• Iteration (using for and while)

You won't be graded on whether or not the code works as it should, but with that said we suggest that you do give them a serious try in order to give yourself a chance to get comfortable with Java and dust off your programming skills from last semester.

In total, the homework assignment is worth 2 points.

For each of the coding problems,

• Minimal starter code is provided.
• You can use whatever tool you'd like to write and run your code. This could be the javac/java on your own computer or the lab machines, etc., or IntelliJ, or an online execution tool.
• For each problem, use the main method to perform an ad-hoc test (by ad-hoc, I just mean a crude test that isn't generalizable like the fancier tests that you'll be developing in HW1). For example, you can write in your main method a few print statements calling your method to ensure it correctly prints the right thing.

## 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:

• 40% of female students and 45% of non-binary students feel as though they need to prove themselves before being taken seriously in academic settings, compared to 25% of male students
• 49% of students with disabilities have considered leaving computer science, compared to 28% of students without disabilities
• 12% of low-income students have been encouraged to drop out by a faculty member, compared to 3% of higher income students.
• 51% of Black students and 73% of Hispanic students have been told their racial identity unfairly advantaged them getting job opportunities, compared to 7% of Asian students and 10% of White students
• 22% of non-heterosexual students believe all students have equal opportunities to succeed, compared to 37% of heterosexual students

### 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:

• Asking inappropriate questions Inappropriate questions come in many forms, including asking a student to explain an aspect of their identity that is not relevant to the course material. This includes things like asking probing questions about race, ethnicity, gender or disability.
• Making assumptions about people’s experiences or backgrounds Assuming aspects of people’s backgrounds - both positive and negative - serves to reinforce stereotypes. Do not make assumptions about others’ identities or academic experiences.
• Using a condescending or patronizing tone Talking down towards others is unacceptable. If you cannot work respectfully with another student, you should remove yourself from the situation rather than treating them poorly. Everyone deserves to be treated with respect, with no exceptions.
• Avoiding partnerships on the basis of belonging to an underrepresented group This is an unfortunately common situation, faced most significantly by members of underrepresented racial or ethnic groups. We expect all students in CS 61B to be welcoming towards others in lab, discussion, and office hours. This includes both responding with kindness when someone asks to be included in a group, but also actively reaching out if someone is sitting alone.
• Asking about grades or scores on assignments Grades and scores are a form of personal information that each student is entitled to keep private. Due to the competitive nature of computer science classes, we discourage students from sharing or asking about grades or scores to foster a kinder environment.

### 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:

• Make it about them, not you Usually, your first instinct is to explain what you meant, and how you aren’t a horrible person. However, in the moment, what is more important is to make the person who was hurt feel better. After all, if you accidentally step on someone’s foot, you should not expect them to make you feel better for stepping on their foot.
• Apologize Verbally apologize immediately and sincerely, without drawing back to anyone else’s behavior. Verbalizing your transgression goes a long way both in making you self-reflect and letting the other person know you understand what you did wrong. Spending too much time on your apology shifts focus to you, and shifts the burden to the other person to validate you. Do not expect the person you’ve hurt to accept immediately - it places an unfair expectation on them to glaze over your behavior.
• Amend your behavior In the future, make an active effort to not repeat your mistakes.

#### 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:

• Make Yourself Feel Comfortable It is okay if you prefer not to take action against inappropriate behavior - the priority here is ensuring that you feel comfortable. If that involves removing yourself from the situation or simply moving past it in the moment, that is up to your discretion.
• Establish boundaries Verbally express your feelings surrounding what was said to you and set expectations for future interactions.
• Ask a TA to step in Pull a TA aside and mention the behavior of your classmate. Ask the TA to step in on your behalf.
• Report the incident Submit the confidential incident form found on the website. Don’t hesitate even if you feel like the incident was “too small” and you’re worried about another student getting harshly punished because of your report. Action will only be taken through this if there is either an egregious act of inappropriate behavior, or if there is a repeated pattern of behavior by one student. This form will only be viewable to Professor Hilfinger, the head TAs (Henry, Linda, Anjali, Ethan), and our course climate TA (Jedi).

#### 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:

• Shut down the offender Quickly and respectfully shut down the student who has made a harmful comment without putting the hurt student on the spot and escalating the situation. The offender may not have known they made a mistake, and in that case, they will respond best if you attempt to call in, rather than call out.
• Pull the hurt person aside After the conversation ends, ensure everyone is feeling okay if you feel comfortable doing so. A small check-in makes sure the person who was hurt sees that others have seen the disrespectful behavior and agree that it is not okay.
• Make it about them, not you Remember that this is about making sure they feel safe and comfortable, and is not an opportunity for you to show off how much you are aware of these situations. There’s no need to press the issue if they say they are okay (you don’t want to create a situation if none existed in the first place). Lastly, it’s entirely possible that the interaction makes you more uncomfortable than the victim. In this case, you should deal with the situation, separately, without involving them further.

### 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.

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

• Every statement must end with a semicolon;
• Every variable has a strict type, and you must declare the type of the variable first.
• Think of "declaring" the variable as telling Java to reserve a space for it. For example, the line int x; tells Java, "Hey bestie! Can you reserve enough space for an int? And we'll refer to that int as x".
• Instantiating the variable actually puts a value there. For example, after declaring int x;, you can write x = 7;. This is like saying "Yo Java, remember how we reserved space for an int named x? Can we put the number 7 there now?"
• You can declare and instantiate variables separately, or on the same line. Example:

int variable; // Declaring... variable = 11; // Then instantiating! String otherVariable = "Hello World!"; // Or do it all at once!

#### Classes

• All code in Java is inside of a class.
• Classes can have variables, or fields, which are declared in the main body of the class.
• Classes have methods. If you're familiar with functions, "method" is just another way of saying "function".
• Example:

public class Dog { // This line is the "class signature" public int age; // Declaring an instance variable public String name; // And another instance variable

public String Bark() { // The Dog class has a "Bark" method

return "Bark, I'm " + name;

} }

#### Methods

• A method is a chunk of code that is run when it is called.
• A method signature is the identifying line that tells you all the basic information you need to know about the method:

3. The type that it returns
4. The name of the method
5. The type(s) of variable(s) it accepts as arguments, and the names of those arguments.
• A method that returns nothing will have the return type as "void".
• A method with no arguments will have nothing in the parenthesis.
• Example:

public class Math {

public int add(int x, int y) {

return x + y;

}

public int square(int x) {

return x * x;

}

public int pythagorean(int a, int b) {

int c;
int aSquared = square(a);
int bSquared = square(b);
return c;

}

public void encourage() {

System.out.println("You can do math!!!")
return;       // This return statement is technically optional.

}

}

#### Conditionals and loops

• Some basic logical tools we can use are the if statement, for loop, and while loop.
• The if statement and while loop are similar to what you might be familiar with from python–thanks 61A!
• The standard java "for loop" has three statements in the loop header: a statement that is executed only at the start of the loop, the stopping condition, and a statement that is executed at the end of every single iteration.
• Example:

public Class WeirdClass {

public boolean luckyNumber(int x) {

if (x == 7) {
return true;
} else {
return false;
}

}

public void printNumbers(int start, int stop) {

for (int curr = start; curr < stop; curr += 1) {
System.out.println(curr);
}

}

public int log2estimate(int x) {

int result = 0;
while (x >= 1) {
x = x / 2;
result += 1;
}
return result;

}

}

#### Arrays

• An array is like a simple list of items.
• Java arrays are fixed length, meaning they don't change size after you've made them.
• Like all Java variables, we need to specify the type for our arrays! So String[] arr1; declares an array that contains Strings, and boolean[] arr2 declares an array that contains booleans.
• We can access an array arr's length by doing arr.length.
• You can access an item of an array arr at a given index i using the syntax arr[i].
• Arrays are 0-indexed. This means given an array arr containing ['cat', 'dog', 'bunny'], arr[0] corresponds to "cat", and arr[3] is out of bounds!
• You can create an empty array of a given length like this: String[] arr = new String[4];. This creates a new array of length 4. Note that the contents are empty.
• You can instantiate an array with it's contents using curly braces. For example: int[] a = new int[]{1, 2, 3, 4} would create an array containing 1, 2, 3, and 4 and then give it the name a.

## 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:
• threeSum(new int[]{-6, 2, 4}) is true, can select -6, 2, and 4.
• threeSum(new int[]{-6, 2, 5}) is false.
• threeSum(new int[]{-6, 3, 10, 200}) is true, can select -6, 3, and 3.
• threeSum(new int[]{8, 2, -1, 15}) is true, can select 2, -1, and -1.
• threeSum(new int[]{8, 2, -1, -1, 15}) is true, can select 2, -1, -1.
• threeSum(new int[]{5, 1, 0, 3, 6}) is true, can select 0, 0, and 0.

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?

• Microaggressions

• https://www.npr.org/2020/06/08/872371063/microaggressions-are-a-big-deal-how-to-talk-them-out-and-when-to-walk-away
• Examples of racial microaggressions: https://sph.umn.edu/site/docs/hewg/microaggressions.pdf
• What to do when you make a mistake: https://www.apa.org/monitor/2017/01/microaggressions and https://hbr.org/2020/07/youve-been-called-out-for-a-microaggression-what-do-you-do
• Cultural competency and respect:

• https://extensionpublications.unl.edu/assets/html/g1375/build/g1375.htm
• https://www.cs.utexas.edu/users/downing/papers/When-Twice-as-Good-Isnt-Enough-2020.pdf
• https://www.idra.org/resource-center/religion-equity-in-schools-protecting-students-and-their-civil-rights/
• Disability

• Etiquitte: https://www.bestcolleges.com/resources/disability-etiquette/
• Current status: https://cra.org/crn/2020/11/expanding-the-pipeline-the-status-of-persons-with-disabilities-in-the-computer-science-pipeline/
• Harassment

• https://svsh.berkeley.edu/community-prevention-response-efforts
• https://svsh.berkeley.edu/support-resources
• https://www.newamerica.org/political-reform/reports/misogynist-incels-and-male-supremacism/recommendations/
• http://oro.open.ac.uk/61128/1/WebScience139.pdf
• https://xyonline.net/sites/xyonline.net/files/2018-05/Ging,%20Alphas,%20Betas,%20and%20Incels%202017.pdf
• Queer and trans

• Student parents

• https://studentparents.berkeley.edu/

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.