Note that in today's lab, you may be exposed to some new words that you may not understand; don't feel overwhelmed! Most of the information that you see today is meant to give you a feel for the computing environment that you'll be working in throughout the semester. You should be able to pick up on these things as you go along, and understand it by the end of the semester. It is more important that you carefully read the instructions to properly set up your account! At the end of the lab, there is a short exercise in Java to review some basic constructs that were covered in class this week.
As you can probably tell, the computer you're currently on isn't running Windows; it runs an operating system called Solaris instead. Solaris is a UNIX-like operating system (UNIX is really a family of operating systems). We'll be using it throughout this semester during lab.
Before you begin reading this section, make sure you have a terminal open. To open one, right click on your desktop area and click "Open Terminal."
The Terminal application is one that provides access to a command line interpreter, which is also known as a shell. Rather than using your mouse to point and click, a shell simply accepts commands that the user types in. There are are a variety of shells, but the one that we'll be using for this course is called bash. Unfortunately, bash is not the default shell for new CS accounts, so we'll have to change it. To do that, type "chsh" into the terminal and press Enter.
Note: For the rest of the course, the "$" in sample input/output represents the shell prompt, which is what the shell prints out every time it's waiting for a command. This means you should never type it! Also note that your prompt may not be the same; it may look something like "b146-00%" instead. The bolded text is the input, which is what you want to type in to the shell.
$ chsh Changing YP shell for <yourlogin> New shell: /usr/local/bin/bash yellow pages passwd changed on lore.cs.purdue.edu
The next time you login, the Terminal application will use the bash shell. However, for now, you're still using the C shell (where did we get a C shell 1,000 miles away from the ocean?). To start using bash, type:
$ /usr/local/bin/bash
Here's what your terminal should look like when you're done (the last line may look different; the red boxes are what you type in):

The concept behind shells are fairly simple; you enter a command, and it executes it. The result is printed to your screen. There are several basic commands that you should know:
The pwd command (short for "print working directory") tells you the directory you're currently in. When you login, you'll always be in the same directory, /home/yourlogin.
$ pwd /home/yourlogin
The ls command (short for "list") shows the contents of your working directory:
$ ls
Notice that nothing was printed out except another prompt. This is because your current directory is empty (not entirely true, there are lots of hidden files)! We can create a directory called cs190m, which we'll put all of our labs in during the semester. We do this by using the mkdir command (short for "make directory"):
$ mkdir cs190m $ ls cs190m
Now that we have our new directory, we can change our current directory to it with the cd (change directory) command:
$ cd cs190m $ pwd /home/yourlogin/cs190m
To remove files, use the rm command. Simply type in the name (or names, separated by spaces) of the file or directory you want to remove.
Note: If you want to remove a directory, you must also specify the -r flag. For example, to remove a directory called "cs190m" you would type rm -r cs190m.
We've shown you how to use cd to go further into directories, but what if we want to go back up? There's a special "directory" named .. that represents the directory "above" the directory you're in. Since we're in the cs190m directory, if we want to get back into our home directory, we simply type:
$ cd .. $ pwd /home/yourlogin
You need to know how to change your password. Since the one you got when you logged in is random, you may have a hard time remembering it. You can change it to something more familiar by using the passwd command (note that you won't see your password typed when you type it—this is intentional!):
$ passwd Old password: New password: Retype new password: yellow pages passwd changed on lore.cs.purdue.edu
There are certain programs that you may need in CS190M. To access these programs, type in the following:
$ export PATH=$PATH:/homes/cs190m/student_bin $ echo 'PATH=$PATH:/homes/cs190m/student_bin' >> ~/.bashrc
Note: It is vital that you type the previous two lines in properly (remember not to type the $, just the boldex text). If you don't, chances are that you won't be able to load DrJava, the official editor of this class.
If you ever need to look up how to use a certain command in your shell, use the "man" (short for manual) command. Simply type in "man <command>" where <command> is simply the name of the command you're looking up. You can hit Enter to scroll down through the article. For example, here's what the man page on ls looks like:

In CS, you'll be spending a lot of time writing code (and as such, knowing how to type well is very helpful!). There are various editors that you can use. However, the official editor of CS190M is going to be DrJava (you are not required to use it, but this is the only editor that will be supported). DrJava is a lightweight IDE, or Integrated Development Environment, for the Java programming language.
Once you have opened DrJava, you should get a window that looks like this:

Let's get started writing your first CS190M Java program. In the editing pane, type (or copy) the following in:
import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner s = new Scanner(System.in);
System.out.print("What is your name? ");
String name = s.nextLine();
System.out.println("Hello, " + name);
} }
Let's go through the meaning of each line:
import java.util.Scanner;
This line imports a class from the standard Java library called Scanner. Scanners are helpful for reading input from various sources.
public class Hello
This line declares a new class called Hello; for this lab, we use it simply for our main method.
public static void main(String[] args)
This starts the main method declaration; the main method is what contains the primary logic for starting a program.
Scanner s = new Scanner(System.in);
This declares a new Scanner object which, as you should remember from lectures, uses the new syntax. This will allow us to read input from the user.
System.out.print("What is your name? ");
This is a simple print statement prompting for the users age.
String name = s.nextLine();
Here we declare a new variable of type String called name. We call one of the Scanner methods, called nextLine, which lets a user type in something (their name) and assign that value to the name variable..
System.out.println("Hello, " + name);
Lastly, we print "Hello, " followed by what the user's name (for example, "Hello, Bob").
As you should know, braces ("{" and "}") are used to group lines of code into blocks, so they are not discussed here.
Save the file as Hello.java inside a lab01 folder that you should create inside the cs190m folder (if you don't know how to do this, review the "Using the Shell" section).
To compile Java code, we can simply hit the F5 key or click the
button to compile all open windows. In the bottom pane under the "Compiler Output" tab, you should see something that looks like this:

To see the fruits of our "labor," simply hit F2 or
. F2 runs the main method of the current class. Since Hello.java is the only file open, it will simply run what we've written. If you look at the Console tab on the lower pane (you may have to click it), you should see this:

The green box means the program is waiting for user input. If you type in a name, it will print out the expected response.
Modify this program to ask the user for his/her age. You can use a the Scanner method nextInt to get it (ie., s.nextInt() will give you an integer that the user types in; you just need to assign it to a variable as is similarly done in one of the lines you already have).
Given the user's age, you should find the lucky numbers. The "lucky numbers" are defined by the factors (not just prime!) of the age, excluding 1 and the age itself. For example, a 7-year-old would have no lucky numbers, since 7 is a prime number; a 21-year-old, on the other hand, would have 3 and 7 as lucky numbers. You should print these lucky numbers to the screen; if the user has no lucky numbers, then you should simply say so.
For essentially all of your classes that have programming assignments, you'll be using the turnin command to submit your code for grading. This is one command you'll want to know how to use (there's always the man pages!), since if you don't turn in your code properly, you will likely end up getting a zero on all of your hard-earned work. Typically, to turn in a lab, you'll go into the lab directory and then turnin all of your .java files, like so:
$ cd ~/cs190m/lab01 $ turnin -v -c cs190m -p lab01 *.java
Of course, the number of the lab changes after each week. For example, next week's lab will be lab02, rather than lab01. You can try these commands now to see what happens. If it succeeds, you should see something that contains a list of files you turned in—in this case, just Hello.java.
Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:
These computers probably wouldn't be very useful if you couldn't check e-mail on them. Fortunately, you can, in several ways. First, you can simply use webmail to check your e-mail. There is also the guide to setting up Thunderbird.
It's very important that you log out after you're done working. If you don't, one of two things happens. The first possibility is that the computer eventually locks itself, preventing anyone but yourself from logging in. This is very discourteous, especially if you're leaving for the day, so don't do it unless you're going to the bathroom or getting a drink of water! The second is that it does nothing, and someone logs in and potentially uses your code to cheat, getting you both into trouble. Neither of these possibilities are very good, so to logout, click on the System menu and then click Logout.
Lab created by: Daniel Tang