Discussion 1: Coding in IntelliJ
The goal of today’s discussion is to get you set up with the tools that you’ll need for programming in this class. This includes setting up your IntelliJ environment, opening a project in IntelliJ, writing code, and running unit tests. You’ll also get practice with Java syntax for some foundational control structures such as loops and conditional statements.
Learning Outcomes
- Set up and author Java code in the IntelliJ IDEA IDE.
- Develop methods in Java from their specification that incorporate one or more (possibly nested) control structures such as
if-elsestatements,forloops, andwhileloops.
Before Discussion
- Go through our IntelliJ guide to install IntelliJ and get it configured on your computer. Don’t worry if you run into issues, the course staff can assist during class.
- If you have not programmed in Java before (or if you’d like a refresher), it may be useful to look through our transitioning to Java page, which describes the Java syntax that you’ll need for this discussion and the first assignment. It may also be helpful to have that page open in a separate tab during discussion to use as a reference.
Reminder: Discussion Guidelines
The work that you complete in discussion serves as a formative assessment tool; it offers the opportunity to assess your understanding of the material and for our course staff to get a “pulse” on how things are going, so we can make adjustments in future classes. It’s also a great place to practice technical communication and collaborative problem solving. You should complete this activity in a group comprising 2-3 students. At the end of class, your discussion TA will check off your progress on the activity and report your score for the day out of 4 points, with 2 points allocated to your presence and engagement during class and 2 points allocated for your completion of that day’s activity. More information about the grading and expectations can be found in the syllabus.
This discussion is meant to help get you comfortable with the IntelliJ IDE and basic Java syntax that we will use throughout the course, so you should complete it using your computer. We advise you to be pragmatic about your use of any web or AI resources during this activity, as this may hide foundational or logistical issues that our course staff can quickly address to put you on a good footing for the rest of the semester. Discussion activities are intended to serve as “strength training” for programming tasks we will expect on assignments and exams (and that you will encounter in future courses and careers), and their main benefit comes from thinking critically to “puzzle” them out.
Open the dis01 Project in IntelliJ
Your TAs will demonstrate how to download a zipped project directory from the course website, extract its contents, open the project in IntelliJ, and navigate its files. Carry out these steps on your computer, and let the TAs know if you run into any issues. Confirm that:
Run all of the test cases by right clicking the “tests” directory and selecting “Run ‘All Tests’”. You should see that all of the test cases fail (you haven’t written any code yet). Now, you’ll complete the definitions of the methods in “Practice.java” to get all of the test cases to pass.
Coding Practice
For the rest of the discussion, you’ll work on six method definitions that aim to get you comfortable with some of the operations and control structures in Java. You can check the correctness of these methods by running the provided test cases. Don’t worry too much about trying to understand all of the code in the test classes; it uses many concepts that we’ll be discussing soon.
med3() method, which returns the median of its three int parameters (a, b, and c). Use if-else statements (possibly nested) in your definition.
for loop to do a brute force calculation in the method gcdLoop(). We know that the gcd of \(m\) and \(n\) is at least 1 (since 1 is a divisor of any positive integer) and it is at most \(m\) (since no number greater than \(m\) can be a divisor of \(m\)). Therefore, we can check every value in this range and return the largest that is a common divisor of \(m\) and \(n\).
while loop in the method gcdEuclideanIterative(). Here, a while loop is a natural choice since we don't know for how many iterations this procedure will run. Rather, we know a condition when it should stop.
gcdEuclideanRecursive(), that computes the gcd using the Euclidean algorithm. The solution that we have in mind is a single line of code (that incorporates Java's conditional operator).
Now that you've written multiple different versions of the same computation, we can step back and think about which one is best. Which is the easiest to understand? Which do you think will run the fastest? Which will use the smallest amount of memory in your computer? Soon, we'll see how we can formally answer these questions.
fizzBuzz() that takes in a parameter \(n\) and prints the outputs for turns 1 through \(n\) in a FizzBuzz game, one per line. The start of the output for \(n \geq 6\) will look like:
12Fizz4BuzzFizz
fizzBuzzHard() that takes in a parameter \(n\) and prints the outputs for turns 1 through \(n\) in a hard FizzBuzz game, one per line.