CSCI 120: Lab 1

Objectives

The Linux System

The following is a series of steps that will familiarize you with a few commands in the Linux environment. All text in italics is what you must type at the command prompt.
  1. Log in to your Linux account. Your username should be the first initial of your first name followed by your last name. Your initial password will be given in the lab. If you signed in to the class late, we'll have to create an account during the lab.
  2. We will be interacting with the computer via a command line tool. To open it, go to the "Applications" menu in the upper left-hand corner of the screen. From it, in the "System Tools" menu, pick "Terminal". Now you can type Linux commands.
  3. The first to learn is how to change your password, and you should do that immediately. At the prompt, enter yppasswd (which stands for "yellow pages password"). You'll need to enter your old password and your new password twice. Remember your new password!
  4. Find the home page for this course on the web. A good web browser is Firefox, that you can find in the Applications/Internet submenu. Open Firefox and go to the cs120 home page http://babbage.clarku.edu/~fgreen/courses/cs120. Bookmark this page for future reference. (Do the same on your own computer when you get back to it.) Information will be added to this page as the term progresses.
  5. Find the page for this lab on the web: Click on "Labs" at the top of the page, or manually go down to the section near the bottom of the page entitled "Labs," then click on "Lab 1" to get here. In the future you will find your lab assignments posted on this page, so you can get started as soon as you are ready.
  6. Create a subdirectory (i.e., folder) in your home directory for your csci120 course this semester. Use the Linux commands: mkdir cs120 and cd cs120. The first one creates the the directory (mkdir stands for make directory), and the second one changes your current directory to it. (TIP: If you get lost and forget what directory you're in, read item 9 below!)
  7. Create subdirectories in your cs120 directory to store and organize your files for labs and assignments, etc.. After finishing step 6, use the Linux command mkdir labs to create a subdirectory called labs.
  8. Use the Linux command ls -la to see what's in your current directory.  The "ls" part just asks Linux to list the contents of the current directory. The "-la" part says to give more details about each subdirectory and file, including the date, time and permissions. You don't have to understand what all of this means right now. Just observe the file names listed in the right column. (You can leave out the -la, in which case you just get a list of the file names and no other information.)
  9. It's useful to go up to higher level directories as well as descend into lower ones. You know how to descend (cd directoryname). To go up one level, cd ../ will do the trick. If you want to jump up two levels in one fell swoop, use cd ../../. (And guess how you go up three levels?). If you've lost track of where you are and want to go back to your home directory, just type cd with nothing after it. Practice going up and down a little at the command line, typing ls to see what's in your current directory as you navigate.
  10. Create a subdirectory assignments in your directory cs120, and afterwards, check what is in your cs120 directory. Should be labs and assignments if you've followed all the preceding steps carefully.
  11.  Use the Linux command cd labs to change to your labs directory. Stay there for the next step...

BlueJ

Now to create a program. We will be using BlueJ, which is an example of an Integrated Development Environment, or IDE. Such an environment is software that makes it easy to create, edit, compile, and maintain programs or collections of programs that work together, called "projects". BlueJ was especially designed for Java, and, what's more for learning Java, and I think you will find it to be very convenient and handy.
  1. Begin by starting up BlueJ. At the command prompt, simply type,
    bluej &
  2. Although in the beginning you will be writing programs that go in a single file, in BlueJ it is always necessary to create such a program in the context of a "project." Very soon you will find several program files in the project, so it's a good thing to get used to.
  3. From the Project menu, select "New Project." When it asks you for the project name, navigate to the "labs" directory you created in the first part of the lab and name the project "Lab1". Note that this has the effect of creating a new folder; you can check this by using the Linux commands to get into your "labs" folder to see the presence of the "Lab1" folder. You (and BlueJ, on your behalf) will be adding files to this folder. When I say "Lab1 folder", this is the folder I mean.
  4. With the BlueJ project window in the foreground, press the "New Class" button. You will be asked for the name of a class; in Java, each file contains only one class, which must have the same name as the file. Give your class the name "Count" (the uppercase "C" is important!). A box representing the class should appear in the project window. Note that you can move the box around inside the project window, which may not appear very useful at this point. It will be later on. (This is a very simple example of what is called a UML diagram; we will discuss such diagrams when we get to Chapter 4.)
  5. Double-click on the "Count" box. Notice that BlueJ fills the file with a code "template". We'll hardly ever use this feature. In fact we'll proceed to ignore even now! Use the edit menu to select all the text in the file and delete it.
  6. Enter the following into the "Count" file. You can just copy it manually, but a more sure-fire way to do it would be to copy it from the browser and paste it into the file electronically:
    /******************************************************************
     *
     *   Count.java: for lab 1
     *
     *   Name:     
    
     *   Login:    
     *   Course:   CSCI 120
     *   Date:     
     *
     *   Description:
     *     This program counts from 1 to 5 in English, French or Spanish.
     *
     *******************************************************************/
    import java.util.Scanner;
    
    public class Count
    {
    
        public static void main (String[] args)
        {
    
          Scanner keyboard = new Scanner(System.in);
    
          System.out.println("What language would you like to hear?");
          System.out.print("Type 1 for English, 2 for French, and 3 for Spanish: ");
    
          int answer = keyboard.nextInt();
     
          if (answer == 1)
            System.out.println ("one two three four five");
          else if (answer == 2)
            System.out.println ("un deux trois quatre cinq");
          else
            System.out.println ("uno dos tres cuatro cinco");
    
        }
    
    }
    Of course, just to make at least a little use of the BlueJ editor, enter your name, login, and the date as indicated above.
  7. I expect that you will find that the box representing the Count class is cross-hatched; that's because it hasn't been compiled yet. Compiling is simplicity itself: If you haven't guessed already, click on the "Compile" button in the project window. This compiles all java files in the Lab1 folder (only one in this case). Alternatively, you can open the Count file and click its own individual "Compile" button.
  8. Running the file is almost as simple. Position the cursor over the "Count" box. Right click on it; you will get a menu. Select the cryptic-loooking choice "void main (String [] args)". What this does is run the main method, a.k.a. "THE algorithm in a Java program". You will get another "Method Call" window asking for the "arguments" of the main method. Just click the "Okay" button of that window. The program will then run in a window that pops up automatically. Type in one of the choices (1, 2, or 3) when the running program prompts you for it.
  9. Congratulations! You have just created and run a Java program with BlueJ.

Submission

Now you will learn how to submit a project electronically.
  1. First get back to the command line, and using cd, cd ../, and/or cd directoryname, find your way to the "labs" directory. If all has gone well, and you type ls, you'll see one directory, namely the one entitled Lab1.
  2. Type,

    submit 1202 Lab1

    This will copy all of the contents of lab1 to a directory that can be read by the instructor and TAs. This is exactly how you will submit electronic copies of assignments (using appropriate subdirectories of your cs120/assignments directory). Also print out a hard copy of your program.
    TIP: As the semester progresses, keep your cs120 folder well-organized. Create individual directories for each assignment and lab, using the "mkdir" command.
  3. Show the instructor the copy of the program in your "cs120" folder before you log out and leave the lab, and also turn in the hard copy. Don't forget to log out!