Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Primer

Learning IntelliJ, Java programming, and unit testing.

  1. Setup
    1. Install Git
    2. Install IntelliJ
    3. Obtain and run project code
  2. Implement
    1. From Python to Java
    2. An ancient algorithm
    3. Common prefixes
  3. Test
    1. Unit testing
    2. Systematic testing

By the end of this project, students will be able to:

Can I work with someone else on this project?

Although this project requires an individual submission, we welcome collaboration and teamwork in this class. There are very few limits on collaboration in this course; our primary rule is that we ask that you do not claim to be responsible for work that is not yours. If you get a lot of help from someone else or from an online resource, cite it. I believe that there is a lot of value in learning from others, and even in reading others’ solutions, so long as you do not deprive yourself (or others) of the opportunity to learn.

We are comfortable doing this because each submission in this class comes in the form of a video that you record. Your video is a demonstration of everything that you learned throughout the process of working on an assignment. Our goal is for students to support each other and find community through this course. The real advantage of taking a course on-campus at a university is to be able to connect with others who share common interests in learning.

What am I submitting at the end of this project?

Satisfactory completion of the project requires a video-recorded individual presentation that addresses all the green callouts and your implementations.

The project instructions contain a lot of details to provide context, clarify common confusions, and help students get started. Your video explanation only needs to address tasks that are described in green callouts like this one.

Your video presentation should meet the following requirements:

  • Your presentation should not be much longer than 6 minutes and should include your voiceover. (Your video is appreciated but not necessary.)
  • Your presentation should include some kind of visually-organizing structure, such as slides or a document.
  • After submitting to Canvas, add a submission comment linking to your slides or document.

Submit your program code to the corresponding assignment in Ed Lessons by uploading all of your implementations.

Setup

Before we proceed, you’ll need to install and setup two pieces of software. If you run into any issues with the setup procedure, let the instructor know by posting on the discussion board. The installation process can be quite sensitive to the specific configuration of your computer, so it’s best to let the instructor know when something doesn’t go as expected.

Install Git

Git is a version control system (VCS), a tool for distributing, managing, and sharing code with other people. Install Git by following the instructions according to your computer’s operating system.

Windows
Download and install Git. All the default settings will work.
macOS
From Finder, navigate to Application | Utilities | Terminal. Type git --version into the terminal and press Enter. If you see a download prompt, follow the instructions.
ChromeOS
At the bottom right, select the time. Then, select Settings | Advanced | Developers and Turn On the Linux development environment. Finally, follow the Linux instructions for all remaining setup.
Linux
Install git using your package manager. On Debian, Ubuntu, or other apt-managed systems, type sudo apt install git into the terminal and press Enter.

The sudo command may require a password prompt. When you type into the password prompt, nothing will appear. This is a security feature: your typing is intentionally hidden!

Install IntelliJ

IntelliJ is an integrated development environment that is the most recommended software for writing Java programs. Follow the Standalone installation guide for your computer’s operating system: Windows, macOS, or Linux.

When downloading IntelliJ, we only need the completely free IntelliJ IDEA Community Edition. The download site promotes IntelliJ IDEA Ultimate; scroll down to find IntelliJ IDEA Community Edition. If you already have IntelliJ installed, be sure to update it to the latest version before proceeding.

Once IntelliJ is installed, run IntelliJ IDEA and select Skip Remaining and Set Defaults. Once you’ve finished the setup, you should see the Welcome to IntelliJ IDEA screen.

Welcome screen

Next, we have a few more setup steps before we can proceed with obtaining the project code. From the Welcome to IntelliJ IDEA screen:

  1. In the banner at the bottom, choose Enable New UI and restart IntelliJ.
  2. In the left sidebar, select Plugins, search for “jGRASP” in the marketplace, and then install the jGRASP plugin.
  3. In the left sidebar, select Project. In the space at the bottom, choose Start Tour and complete the onboarding tour.

Obtain and run project code

After you’ve completed the onboarding tour, select the Main menu (hamburger menu in the top left corner), choose New | Project | Project from Version Control. Paste the following URL, choose a place to save the project, and clone the project code repository.

https://github.com/kevinlin1/primer.git

The first time you open the project, IntelliJ will ask you whether to trust the project. Choose Trust Project so that IntelliJ has your permission to run code.

After a few seconds, IntelliJ will open the main editor screen. Let’s create and run a class in IntelliJ:

  1. Look at the left side of the screen for the Project tool window that shows all the files in the project.
  2. In the project tool window, right-click the folder named src and select New | Java class from the menu.
  3. Name the class “Demo” and click OK. In the new file, paste in the contents of Demo.java.
  4. In the editor, find the gutter containing all the line numbers and click on the Green play button.
  5. Finally, select Run ‘Demo’ from the menu.

If you see the following output in the run tool window, you’re all set!

[3, 4]
My computer doesn't display the expected output!

If you’re instead seeing a “Cannot start compiler” notification with a suggestion to configure the Project SDK. Follow the link in the notification, or from the main menu select File | Project Structure. In the Project Structure window, open the SDK dropdown.

Change the project SDK

If IntelliJ detected an existing Java SDK, it will be listed under Detected SDKs.

  • If an SDK version 17 or greater is available, select it.
  • Otherwise, select Add SDK | Download JDK and choose the latest from any vendor.

Click OK and try running BrowserHistory again. It should work now!

Implement

From Python to Java

Consider the following Python code which performs a task that is intentionally obfuscated. In the Equivalent class, convert the Python code verbatim into Java. Write your Java code as similar to the Python code as possible, but apply the Java naming conventions for variables and functions.

import math

def foo(start, end):
    if start <= 1:
        start = 2
    output_list = []
    for n in range(start, end):
        if is_foo(n):
            output_list.append(n)
    return output_list

def is_foo(n):
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

Assume the arguments start and end will be integers.

Explain how you converted the Python range function into Java for loops.

An ancient algorithm

In mathematics, the Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.

It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them that is equal to that prime.

Animation of the Sieve of Eratosthenes 1

In the Sieve class, write a Java implementation for sieve that implements the Sieve of Eratosthenes. The sieve method takes as input 2 integer arguments: start and end. The method should return an integer array containing the primes in the interval [start, end). In other words, start is included in the range but end is not.

Return an empty array if either start or end is negative or end is less than or equal to start.

Explain the part of the Sieve class that you’re most proud of programming.

Common prefixes

Suppose you’re designing a file organization system that automatically groups files under a certain label. The common prefix among a group of filenames can be used to create a hierarchy or grouping of the files, making it easier for users to navigate and find desired files. For example, if a group of files have the names accounting_jan.xlsx, accounting_feb.xlsx, and accounting_mar.xlsx, the common prefix among all three of these names is accounting_, so these files could be grouped together under a folder named accounting.

In the CommonPrefixes class, implement the findCommonPrefixes method that takes an array of strings and returns the longest common prefix among all the given strings. If there is no common prefix among all the strings, return an empty string.

Explain the part of the CommonPrefixes class that you’re most proud of programming.

Test

To create a test function:

  1. In the editor, move your cursor to the class that you’d like to test. Then, press Alt+Enter and select Create Test.
  2. In the Create Test dialog, select the library JUnit5 and fix the problem by using the latest stable version of JUnit (5.9.3 at the time of writing).
  3. Select the methods that you’d like to test and click OK.

Unit testing

Write at least three test cases for findCommonPrefixes in a single test function.

Explain your test cases for findCommonPrefixes. How do your test cases provide confidence that the program works on different potential inputs?

Systematic testing

Write a systematic test for the sieve algorithm that iterates over all valid inputs for start and end between 0 and 100 and compares the result to your foo function, which also computes all the prime numbers within the same interval.

Explain your systematic test for the sieve algorithm. How does your test code systematically check the combinations of start and end?

  1. SKopp, CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/, via Wikimedia Commons