CSCI-142: Computer Science 2
Lab 1 - From Python to Java

Introduction

In this lab you will develop programs with the Java programming language. While it is up to you to choose the Integrated Development Environment (IDE) that you would prefer to use, it is highly recommended that you use IntelliJ IDEA for your development. Not only is IntelliJ similar in look, feel, and functionality to PyCharm, but all of the instructions provided to you throughout the course will use IntelliJ as the example IDE.

If you have not yet configured your own machine for Java and IntelliJ IDEA, please refer to the set up instructions.

Reading User Input

In Java, the primary mechanism for reading user input from the command line is the java.util.Scanner class. Once it has been created to read from a specific input source (such as the command line or a file), a new Scanner provides several methods for reading user input as various types including integers, booleans, floating point values, individual words, or an entire line. For example:

    // create a new scanner to read from the command line
    java.util.Scanner scanner = new java.util.Scanner(System.in);

    // prompt the user to enter a word
    System.out.println("Please enter a word: ");
    // read only the next word and return it as a string
    String word = scanner.next();

    // prompt the user to enter a number
    System.out.print("Please enter a number: ");
    // read the next word (up to the first space) and convert it into an int
    int number = scanner.nextInt();

    // prompt the user to enter a string
    System.out.println("Please enter a string of text: ");
    // read everything that the user types up to the end of the line
    String line = scanner.nextLine();

    // ALWAYS close your scanner
    scanner.close();

As you can probably imagine, many of the programs that you write this semester will require reading input from the user. You should become familiar and comfortable with using Scanner to do so. You can read more about the java.util.Scanner class in Java's online API documentation.

A Note About Packages

Java includes a mechanism for organizing classes such that related classes can be grouped together into a logical namespace called a package. Every Java class exists in a package; the package is either explicitly declared at the top of the class file using a package statement, e.g. package my.package;, or the class is placed into default package (the package with no name).

For example, the core Java language classes like java.lang.String and java.lang.System are grouped together in the package called java.lang. As may be apparent from this example, the full name of every class in the package begins with the name of the package.

A class in one package cannot directly access a class in another package. The notable exception is classes in the java.lang package; all such classes are implicitly and transparently imported into any Java program, and so an explicit import statement is not required. There are several ways to access a class in one package from a class in another package:

You will need to use classes from other Java packages for virtually every assignment this semester, and so you should get comfortable with using the import statement.

You can also place your own classes into packages to better organize them, and you will be encouraged/required to do so for many assignments. To create a package in IntelliJ IDEA, follow these instructions:

  1. Right-click on the src folder in your project and select New -> Package from the popup menu.
    Create a New Package

  2. In the dialog that appears, type the name of the package, e.g. lab01.student and then click the OK button.
    Name the New Package

A class is declared to be inside a package by using the package keyword. This will happen automatically if you use IntelliJ IDEA to create a class inside a package. For example, if you create a new class in the lab01.student package, the following will be added to the top of the .java file:

	package lab01.student;

Implementation

Procedural Programming in Java

For this lab you will be creating a single IntelliJ project and completing four main programming tasks, each in a separate .java file:

  1. Implement the "good hash function" based on your team's work during problem solving.
  2. Implement your problem solving team's primality test algorithm.
  3. Implement a second primality test based on the Sieve of Eratosthenes
  4. Finally, have a little fun with a version of Turtle that has been implemented in Java.

Lab 01 Project Structure

Create a new project for this lab just as you did when going through the Java & IntelliJ set up instructions.

Lab 01 Project Structure

Before you get started with the activities, you should get create a project with the structure depicted above. To do so, follow these steps:

  1. Create a new package named lab01.student
  2. Create the following new classes to in the lab01.student package (right click on the package and select New -> Java Class)...:
  3. Create a new package named turtle and copy the provided Turtle.java file into it.
  4. Download and copy the Lab01Main.java file directly into the src folder in your project (i.e. not into one of the packages).
  5. It may be convenient for you to also download and save the HTree_py.txt file in your project for your later reference while completing Activity 4.

Provided Files

You have been provided with a few files to help you implement the lab.


Activity 1: A Good Hash Function

Recall the definition of the good hash function: given a string s of length n, the hashcode h is computed using the following formula:
h = s[0]*31(n-1) + s[1]*31(n-2) + ... + s[n-1]*310

For the first acitvity, you will write a class that implements the "good hash function" that your problem solving team wrote together during lab. The class must meet the following requirements:

Standalone Output

When executed as a standalone program, GoodHashFunc should produce output similar to the example below. Please note that you should not be overly concerned if your hash function does not produce the exact output displayed. It is more important that you pass the tests in Lab01Main.

Please enter a string: this is a string
The computed hash for the specified string is: -409156356

Lab01Main Output

Once your primality test is functioning, executing Lab01Main should produce output similar to the example below.

Testing GoodHashFunc...
  all hash function tests passed!
...

Javadocs


Activity 2: Primality Test

A prime number is any integer that is greater than 1 and is evenly divisible by only two numbers: 1 and itself. The first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

For the second exercise, you must write a class that implements the primality test algorithm that your problem solving team wrote together during lab. The class must meet the following requirements:

Standalone Output

When executed as a standalone program, PrimalityTest should produce output similar to the example below.

    Enter a number (0 to quit): 1
    1 is not prime.
    Enter a number (0 to quit): 2
    2 is prime!
    Enter a number (0 to quit): 3
    3 is prime!
    Enter a number (0 to quit): 4
    4 is not prime.
    Enter a number (0 to quit): 5
    5 is prime!
    Enter a number (0 to quit): 6
    6 is not prime.
    Enter a number (0 to quit): 7
    7 is prime!
    Enter a number (0 to quit): 8
    8 is not prime.
    Enter a number (0 to quit): 9
    9 is not prime.
    Enter a number (0 to quit): 0
    Goodbye!

Lab01Main Output

Once your primality test is functioning, executing Lab01Main should produce output similar to the example below.

...
Testing PrimalityTest...
  all primality tests passed!
...

Javadocs


Activity 3: The Sieve of Eratosthenes

Animation courtesy of wikimedia.org

For this assignment you will implement an alternative way to determine whether or not a number is prime: The Sieve of Eratosthenes. You should read the algorithm described in the link and verify that you understand it before continuing. You will write a class that implements the Sieve of Eratosthenes algorithm as described and meets the following additional requirements:

Standalone Output

When executed as a standalone program, SieveOfEratosthenes should produce output similar to the example below.

Please enter an upper bound: 10000
Please enter a positive number (0 to quit): 1
1 is not prime.
Please enter a positive number (0 to quit): 2
2 is prime!
Please enter a positive number (0 to quit): 3
3 is prime!
Please enter a positive number (0 to quit): 4
4 is not prime.
Please enter a positive number (0 to quit): 5
5 is prime!
Please enter a positive number (0 to quit): 131
131 is prime!
Please enter a positive number (0 to quit): 157
157 is prime!
Please enter a positive number (0 to quit): 0
Goodbye!

Lab01Main Output

Once your primality test is functioning, executing Lab01Main should produce output similar to the example below.

...
Testing SieveOfEratosthenes...
  all Sieve of Eratosthenes tests passed!

Javadocs


Activity 4: Turtle!

For this exercise, you are given a complete Python prgram: HTree.py. This program includes an algorithm that draws H shapes recursively. The program accepts a single command line argument that indicates the depth of recursion. Below you can see the images produced when the program is executed with a depth of 0, 1, 2, and 3 respectively.

H-Tree, Depth=0 H-Tree, Depth=1
H-Tree, Depth=2 H-Tree, Depth=3

Your task is to "port" (a shorthand term for "translate") the original Python program to Java. You will create a new program in the lab01.student package, HTree. Like the supplied program, your HTree implementation should expect the recursion depth to be provided as a command line argument. You will need to create and edit an IntelliJ IDEA run configuration to supply a default depth (e.g. 3). When executed, your program should produce images very similar to the above examples.

Note that you will need to create a new package in your IntelliJ IDEA project named turtle, into which you will copy the provided files (see below).

Provided Files

Javadocs

You may refer to the Javadocs provided for the classes below.

Take note:


Submission

You need to submit all of your source code to the MyCourses dropbox before the due date.

Right click on your source directory and make a zip file for it (not .7z or .rar!), named lab1.zip. Go to the MyCourses dropbox and submit it. Make sure that you upload and submit - if you forget the second step your code will not be submitted. You can double check you submitted by going to the dropbox. Also, you should receive an email when you successfully submit.

Grading

The grade breakdown for this lab is as follows: