CS 112
Spring 2019

Old version

This is the CS 112 site as it appeared on May 8, 2019.

Lab 1: Getting started

Task 0.0: Review lab policies

Note

You cannot get credit for completing the lab tasks at home and coming to lab only to sign the attendance sheet. Part of participation is attending the lab to interact and ask questions.

Task 1: Eclipse basics

Using folders

We strongly encourage you to create a separate folder for each lab and each problem set, so that you can more easily keep track of your work. For example, you could create a folder called lab1 for your work on this lab, and put all of the files for this lab in that folder. If you are working on a lab machine, you will need to create the folder on the Z: drive, so that it won’t be lost when you log out.

Task 1.1: Creating a new Java project (HelloWorld)

In Lab 0, you should have installed Eclipse on your computer. If you have not, you can use one of the lab computers.

To see how to use it, let’s take the following steps together:

  1. Start up Eclipse, either on your lab computer or on your own machine. If you have trouble finding the program, please let us know.

  2. Let’s create a new project by clicking on “Create a new Java project”

  3. Name the project “lab1”, keep the execution environment JRE at JavaSE-1.8

  4. On the package explorer on the left, expand out “lab1”, then expand out “src”. Under “New” click on “Class”.

  5. Name the new Java Class “HelloWorld”, select the default package and check the first of the three boxes below. Then Click “Finish”.

  6. Add the statement to print the literal string “Hello world!”. Save and execute the program.

Note

The main method (of your HelloWorld class) was executed automatically when you ran your program!

Task 1.2: Simple Debugging with Eclipse

Let’s start with a simple exercise to get used to the Eclipse environment. Using a GUI based editor gives us multiple advantages by highlighting and marking a multitude of errors while coding. Download the following file on your computer and try finding all the intentional bugs in the program.

Debugging.java

In order to add the Debugging class to our project we can simply drag the Debugging.java file into our src. Now you can open up Debugging.java and fix the errors you see.

  1. Once our code compiles, we should test our methods. We have two ways we can do so.

    1. We can add a main method to our Debugging.java file and include calls to test our method. Example:

      System.out.println(triArea(10, 3));
      > 15.0
      
    2. We can write a separate simple test class containing a main method that includes calls to test our method.

      System.out.println((Debugging**.triArea(10, 3));
      > 15.0
      

      Note the difference! Because this call is now being made in a method which is written in a separate class to which the method triArea belongs, we need to prepend the class name when we call the method.

    In both cases, this result is correct, because the area of a triangle with base 10 and height 3 is indeed 15.0.

  2. One test is almost never good enough! Try adding this line to (one of) your main functions and rerunning. What is the result? Is it correct?

    System.out.println(Debugging.triArea(9, 3));
    

    You should see that this call returns 12.0. However, the actual area of a triangle with base 9 and height 3 is 13.5.

  3. To fix this logic error, we need to realize the following:

    • Java’s / operator is used for both integer division and floating-point division.

    • If both of the operators are integers, / performs integer division, which truncates the digits after the decimal. This is what is happening when we compute b/2.

    • In order to get floating-point division — which preserves the digits after the decimal — we need to make sure that at least one of the operands is a floating-point number.

    Go ahead and make the change needed to get floating-point division.

  4. Save your code and rerun your program to ensure it works out

Task 2: CSA Machines Basics

Task 2.0: Make sure you have your CS account ready!

Follow the instructions in Task 0 from Lab 0. You will need your cs account to access the csa machine, which will be where you submit all your assignments.

Task 2.1: Basic Bash and Accessing CSA Machines via SSH

You will need to learn the basics of how to navigate and perform simple actions within the Unix bash shell. First, we need to learn how to connect to the CSA machines via Secure Shell (SSH).

Task 2.2: Programming Java with Unix-machines via SSH (optional)

We will show you how to edit, compile and execute Java codes using bash text editors such as NANO, EMACS or VIM.

Task 2.3: Transferring files

There are a few ways to transfer file between your local machine (aka your laptop) and remote machines (aka the csa machines).

Today we will introduce one of them:

We will be using a method called Secure File Transfer Protocal (SFTP) to upload our files to the CSA Machines.

Task 2.4: Using GSubmit

Follow the instructions as presented by the teaching fellow. You can also refer to the following tutorial on how to use the gsubmit.

If you are still confused about gsubmit after the lab, you can watch this video below provided by one of the CS112 instructors:

Task 2.5: Let’s do a dummy submission!

Assuming we understood the basics of how to use the csa machine. Now I would like you to submit the “HelloWorld.java” via gsubmit.

  1. If you haven’t already done it, make a new directory called “lab1” on the csa machine. (It doesn’t matter where it is located, but I suggest to create it under your home directory.)

  2. Upload your HelloWorld.java to the above directory via FileZilla.

  3. In the bash shell, navigate to the lab1 directory and make sure that the HelloWorld.java file is there. (You can verify by doing ls to list all the files in the directory. You can also use the cat or more Unix command to visually see the contents of the file on the terminal.)

  4. (optional) You can use an editor to look at the file, and even modify it in some way. But if you change the source file, you need to compile it to make sure no errors were introduced.

  5. Navigate up one directory level by doing “cd ..”

  6. Compress our lab1 directory in preparation for gsubmit. Do “tar -zcvf lab1.tar.gz lab1”

  7. Verify that our compressed file is available by doing “ls”. You should see a file named “lab1.tar.gz”

  8. Now submit your lab via gsubmit. Do “gsubmit cs112 lab1.tar.gz”. You will be asked to input your name and your BU ID on the first time.

  9. You can verify that your lab is properly submitted by doing “gsubmit cs112 -ls”. You should see all the files you have submitted.

The attendence of this lab will be evaluated based on whether you have submited lab1.tar.gz via gsubmit properly. Please make sure you have named all files correctly (names are case-sensitive).

Task 3: Practising Java with Eclipse (Optional)

Task 3.1: Strings and their methods

In Problem Set 1, there are several problems in which you will need to work with String objects in Java.

It turns out that Java lacks many of Python’s built-in operators and functions for strings. Instead, most operations involving strings – including indexing and slicing – are done using methods that are inside the string object.

The table below compares the ways in which some common string operations would be accomplished in Python and Java:

operation

in Python

in Java

assigning to a variable (and, in Java, declaring the variable)

s1 = 'Boston'
s2 = "University"

String s1 = "Boston";
String s2 = "University";

concatenation

s1 + s2

s1 + s2

finding the number of characters

len(s1)

s1.length()

indexing
(note: Java does not have negative indices)

s1[0]
s2[-1]

s1.charAt(0)
s2.charAt(s2.length()-1)
(see the notes below the table for a key fact about this method)

slicing

s1[1:4]
s2[3: ]
s1[ :5]

s1.substring(1, 4)
s2.substring(3)
s1.substring(0, 5)

converting all letters to uppercase

s1.upper()

s1.toUpperCase()

converting all letters to lowercase

s2.lower()

s2.toLowerCase()

determining if a substring is in a string

s2 in s1
'sit' in s2

s1.contains(s2)
s2.contains("sit")

finding the index of the first occurence of a character or substring

s1.find("o")
s2.find('ver')

s1.indexOf('o')
s2.indexOf("ver")

testing if two strings are equivalent

s1 == s2
s1 == 'Boston'

s1.equals(s2)
s1.equals("Boston")

Additional notes:

Task 3.2: Strings and their methods (continued)

Given the information above, here are the tasks you should perform:

  1. To ensure that you understand how the above string operations work in Java, write a simple test program which you can use to play around with the different string operations.

  2. Begin by initializing two string variables in your main method (which can be used in the later tasks). Example:

    String s1 = "Boston";
    String s2 = "University";
    

    Test that these variables still have their values

    > System.out.println(s1);
    "Boston"
    > System.out.println(s2);
    "University"
    

    If for some reason the variables don’t have these values, redeclare the variables

  3. We’re now going to solve a series of puzzles in which we construct an expression involving operations on s1 and/or s2 to produce a specified target value.

    For example, let’s say that we want to construct an expression involving s1 or s2 that produces the following value:

    "ton"
    

    One possible expression that works for this puzzle is s1.substring(3, 6).

    > System.out.println( s1.substring(3, 6) );
    "ton"
    
  4. Now solve the puzzles below by writing in your main function for each problem in your StringTest.java file.

    1. Construct an expression involving s1 or s2 that produces the following value:

      "Uni"
      
    2. Construct an expression involving s1 or s2 that produces the following value:

      "UNI"
      

      Hint: Chain two method calls together. For example:

      System.out.println( s1.toLowerCase().substring(0, 3) );
      "bos"
      
    3. Construct an expression involving s1 or s2 that produces the following value:

      'v'
      

      Note that the single quotes mean that you must produce a char, not a String. See the notes under the table above that discuss the char type.

    4. Construct an expression involving s1 or s2 that produces the following value:

      "STONE"
      

Task 3.3: Writing static methods

This task asks you to write a series of static methods.

Begin by downloading this file: MethodTest.java. Open it in Eclipse, and add your methods to the class that we have given you in that file.

You should also add a main method or write a simple test program to test out each of the methods you write. We show the sample calls below as though they are being made from a separate test program.

  1. a static method called print3Times that takes a string as its parameter and prints it three times. For example:

    > System.out.println( MethodTest.print3Times("hello") );
    hello
    hello
    hello
    

    We have given you this example method in the starter file.

    One thing worth noting: the header of the method has the word void in it. Make sure you understand what that means, and ask us if you’re not sure.

  2. a static method called printNTimes that takes an integer n and a string (in that order) as its parameters and prints the string n times. For example:

    > System.out.println( MethodTest.printNTimes(5, "hello") );
    hello
    hello
    hello
    hello
    hello
    > System.out.println( MethodTest.printNTimes(2, "hello") );
    hello
    hello
    
  3. a static method called replaceStart that takes two strings s1 and s2, and does the following:

    • If the length of s1 (call it s1Len) is less than the length of s2, the method returns a string in which the first s1Len characters of s2 are replaced by s1. For example:

      > System.out.println( MethodTest.replaceStart("abc", "xxxxxxx") );
      "abcxxxx"
      > System.out.println( MethodTest.replaceStart("hello", "xxxxxxx") );
      "helloxx"
      
    • If the length of s1 is greater than or equal to the length of s2, the method just returns s1.

      > System.out.println( MethodTest.replaceStart("hello", "bye") );
      "hello"
      

Extra Practice!

If you get through the exercises above, congratulations!

For extra practice, you can try some exercises from an excellent site called Practice-It.