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
-
Structure. Labs will typically begin with a brief lecture and/or example to reinforce the material covered in lecture. The class will then have chance to ask questions. After that, there will be a series of exercises for you to complete. You are welcome to work on the exercises on your own or pair up with a classmate.
-
Attendance:
-
It is important that you attend the lab for which you are registered on StudentLink. If you need to attend another lab section on any given week, please notify your instructor in advance.
-
For each lab, you must be checked off by either a teaching fellow or course assistant in order to recieve credit for the lab.
-
-
Participation:
-
To get full credit for participation in lab, you must work productively and submit the work that you complete as specified by the TF. ***You will not be penalized if you cannot finish all of the lab exercies, but we strongly encourage you to complete the exercises outside of lab, and check your answers with the solutions when posted.
-
Don’t hesitate to ask for help! The course staff is more than happy to help you with any questions that you may have, and we will be coming around to assist you during lab.
-
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.
-
Collaboration.
- You are encouraged to work with your classmates on the exercises in the lab, but please ensure to check out with the TF individually.
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:
-
Start up Eclipse, either on your lab computer or on your own machine. If you have trouble finding the program, please let us know.
-
Let’s create a new project by clicking on “Create a new Java project”
-
Name the project “lab1”, keep the execution environment JRE at JavaSE-1.8
-
On the package explorer on the left, expand out “lab1”, then expand out “src”. Under “New” click on “Class”.
-
Name the new Java Class “HelloWorld”, select the default package and check the first of the three boxes below. Then Click “Finish”.
-
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.
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.
-
Once our code compiles, we should test our methods. We have two ways we can do so.
-
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
-
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.
-
-
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.
-
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 computeb/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.
-
-
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).
-
For Linux and macOS users, SSH is a built-in function within the terminal. To connect, simple open the terminal and enter “ssh your-bu-id@csa2.bu.edu”. You will be asked to input your password.
-
For Windows users, please install Putty. To connect, open Putty and simply enter “csa2.bu.edu” in the Host Name. Click “Open” and you will be asked to input your username and passwoard.
-
Follow the instructions of the teaching fellow to learn some of the basic Unix commands (e.g.
pwd
,ls
,mkdir
,cd
) that will allow you to navigate in terminal mode.
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:
- FileZilla (an open source software, available for Windows, Linus and macOS)
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:
- Quick tutorial video on gsubmit by Professor Attarwalla.
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.
-
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.)
-
Upload your HelloWorld.java to the above directory via FileZilla.
-
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 thecat
ormore
Unix command to visually see the contents of the file on the terminal.) -
(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.
-
Navigate up one directory level by doing “cd ..”
-
Compress our lab1 directory in preparation for gsubmit. Do “tar -zcvf lab1.tar.gz lab1”
-
Verify that our compressed file is available by doing “ls”. You should see a file named “lab1.tar.gz”
-
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.
-
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) |
|
|
concatenation |
|
|
finding the number of characters |
|
|
indexing |
|
|
slicing |
|
|
converting all letters to uppercase |
|
|
converting all letters to lowercase |
|
|
determining if a substring is in a string |
|
|
finding the index of the first occurence of a character or substring |
|
|
testing if two strings are equivalent |
|
|
Additional notes:
-
Java has a separate type called
char
for values that consist of a single character. Literals of this type are surrounded by single quotes (e.g., the'o'
in the calls1.indexOf('o'))
, whereas string literals must be surrounded by double quotes. (In Python, there is no separatechar
type, and string literals can be surrounded by either single or double quotes.) -
The
charAt()
method that we use for indexing in Java returns a value of typechar
. For example, the calls1.charAt(0)
in the table above would return thechar
value'B'
, since'B'
is the first character in the string"Boston"
.
Task 3.2: Strings and their methods (continued)
Given the information above, here are the tasks you should perform:
-
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.
-
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
-
We’re now going to solve a series of puzzles in which we construct an expression involving operations on
s1
and/ors2
to produce a specified target value.For example, let’s say that we want to construct an expression involving
s1
ors2
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"
-
Now solve the puzzles below by writing in your main function for each problem in your
StringTest.java
file.-
Construct an expression involving
s1
ors2
that produces the following value:"Uni"
-
Construct an expression involving
s1
ors2
that produces the following value:"UNI"
Hint: Chain two method calls together. For example:
System.out.println( s1.toLowerCase().substring(0, 3) ); "bos"
-
Construct an expression involving
s1
ors2
that produces the following value:'v'
Note that the single quotes mean that you must produce a
char
, not aString
. See the notes under the table above that discuss thechar
type. -
Construct an expression involving
s1
ors2
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.
-
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. -
a static method called
printNTimes
that takes an integern
and a string (in that order) as its parameters and prints the stringn
times. For example:> System.out.println( MethodTest.printNTimes(5, "hello") ); hello hello hello hello hello > System.out.println( MethodTest.printNTimes(2, "hello") ); hello hello
-
a static method called
replaceStart
that takes two stringss1
ands2
, and does the following:-
If the length of
s1
(call its1Len
) is less than the length ofs2
, the method returns a string in which the firsts1Len
characters ofs2
are replaced bys1
. 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 ofs2
, the method just returnss1
.> 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.
-
Go to Practice-It.
-
Create an account.
-
Select the group of problems entitled Building Java Programs, 3rd edition.
-
Select BJP3 Chapter 3: Parameters and Objects.
-
Try any of the following:
- BJP3 Self-Check 3.18-3.21
- BJP3 Exercise 3.17–3.22
The system will test your solution and tell you whether it is correct.