CSC 212: Programming with Data Structures

Lab 1: Intro to Java

Due: Thursday, Jan. 28, 11:59pm

Credit: based on Lab 1 by Nick Howe.

The goal of this lab is to become familiar with writing Java code, including class structure, primitive types (mainly int), loops, and our first data structure, arrays. Before starting this lab, make sure you have finished Lab 0 and submitted your work on the server.

Part 1: Variables and Methods

  1. The first step is to create a new folder for Lab 1 called lab1, and create the file 

    either using the Eclipse procedure described in Lab 0, or using another editor.

  2. Consider the following code in Python:
    # Sample program to demonstrate variable types
    def main():
        # ask user for a number
        n = int(input("Please enter an integer: "))
        # double the number
        ndoubled = n+n
        print(n, "doubled is", ndoubled)
        # square of number
        nsquared = n*n
        print(n, "squared is", nsquared)
        # square root of number
        nsqrt = n**(0.5)
        print("The square root of", n, "is", nsqrt)
    if __name__ == "__main__":
    We are going to convert this code into Java, step by step the way we built up during the first class. In Eclipse, the class name will be set up for you. To set up the Javadocs and main, you can use this template:
     *  TODO: Add a description of the program here.
     *  @author TODO: Your Name Here
     *  @version TODO: Today's Data Here
    class NumberFacts {
         *  TODO: Description of what main does here.
         *  @param args  Command line arguments
        public static void main(String[] args) {
            // TODO: convert Python code above into Java here

  3. To convert the Python code above, we need a way of obtaining input from the user. In, we saw one way of doing this. For this lab, we'll be using the Java Scanner class, which is a better way to obtain user input. We'll also be using this class for Homework 1. Here is an example of how to use the Scanner class:
    // ask user for a number
    System.out.print("Please enter an integer: ");
    Scanner input = new Scanner(;
    int n = input.nextInt();
    This answers a question from lecture about ways of obtaining input that automatically convert the String entered by the user. Scanner has a variety of such options.

    Q: If we only add the above code, what error do you get? Where is the actual code for the Scanner class? How can we import this code to use for our program?

    After adding the code above, test it! It's good practice to test after every change. Then convert the remaining Python code into Java, thinking carefully about the type of each variable you declare. Test out your program with a variety of integers to make sure you get the results you were expecting.

    Notes: you might want to use the builtin Math class for the square root function. Also, System.out.println takes only 1 argument, a String, which you can build up using the "+" operator.

  4. Now we're going to add the first method to, which will determine whether or not your integer is a prime number. The return type of this method should be a a boolean (true or false). Here is a template for this function:
         *  Tests whether an integer is prime
         *  @param n  The number to test
        public static boolean prime(int n) {
            // TODO complete this method

    Finish implementing this method, using a for loop. To test whether one number is divisible by another, you can use the mod operator, which in Java is the symbol: %. After you finish implementing this method, add a call to prime in main, the print the result. One way to print the result is:

    System.out.println(n + " is prime? " + isprime);

Part 2: Arrays

  1. The primitive types in Java (int, float, double, boolean, char, byte, short, and long) have fixed sizes, so memory for arrays of these types can be allocated upfront. Here is an example of how to declare and allocate an array of 6 floats:
     float[] numbers = new float[6];
    Create an array with space to hold ten int values. Then, using a loop, fill in the array as the multiples of n (number entered by the user), from 0 times n to 9 times n.

    You can put this loop in main. There is actually an existing function to print arrays, called Arrays.toString. An example of how to use this code is shown below:

    After you've finished the loop, test your code by printing the multiples (add some text to the printed line to describe what you are doing).

  2. Now you are ready to move your code to the server, then compile, run, and submit. You may want to refer back to Lab 0. There is one final part, which is to submit a typescript for your code. A typescript is a record of what your code produced on the commandline. Below is a basic workflow, with specifics for each operating system.

    Copy over your code and log into aurora, for each operating system:

    • Windows (personal): Copy over your Java file using Putty secure copy (pscp.exe). Then log on to the server using Putty.

    • Mac or Linux (personal):
    • Linux (lab): nothing!

    Below is the basic workflow once you are on aurora, with Java style comments on the right. The part in red will create a typescript.

    cd public_html/                    // change directory
    mkdir lab1                         // make new directory for Lab 1
    mv lab1/          // move code into new folder
    cd lab1                            // move into Lab 1 folder
    javac             // compile code
    java NumberFacts                   // test code 
    script                              // start recording the output
    java NumberFacts                    // run your program as usual
    exit                                // finish recording output   
    less typescript                     // view the script file, q to quit
    rsubmit lab1       // submit the code
    rsubmit lab1 typescript             // submit the output
    Make sure you've submitted these two files, then you're done! If you finish early, you can start on Homework 1. If you are not finished by the end of lab period, you have until midnight to submit.